<?
include_once(_DIR_EXT.'/HTML/c_page_TXT_AJAX.php') ;
class c_page_ajax extends c_page_TXT_AJAX
{
    public function getMOInfo()
    {
/*        $result = execSQL_van("SELECT pers.obj_name as name, FROM_UNIXTIME(mo.r_data) as date, mo.dopusk as dopusk
            FROM obj_site_personals pers, obj_site_mo_session mo
            WHERE pers.pkey=mo.member_id
            AND mo.pkey=(SELECT MAX(pkey) FROM obj_site_mo_session WHERE member_id=pers.pkey )
            AND pers.propusk='" . $_POST["passNumber"] . "';");
        $result['name']=strtoupper($result['name']);
        $result['curDate']=date("Y-m-d H:i:s");

        $jsonResult = json_encode($result); */
           $time_mo=getdate(time()) ;
           $time_from=mktime($time_mo['hours']-3,$time_mo['minutes'],$time_mo['seconds'],$time_mo['mon'],$time_mo['mday'],$time_mo['year']);
           

        $result2=execSQL_van('select * from view_mo_session where propusk="'.$_POST['passNumber'].'" and c_data>'.$time_from.' order by pkey desc limit 1');
        $rec_person=PERSONAL()->get_personal_by_propusk($_POST['passNumber']) ;
        $result2=ESMO()->check_active_dopusk($rec_person['pkey']) ;
        if($result2['pkey'])$result2['date']=date('d.m.Y H:i',$result2['c_data']);
        $result2['name']=$result2['member_name'];
        $result2['curDate']=date("d.m.Y H:i");
        $jsonResult = json_encode($result2);
// echo $jsonResult;

        echo $jsonResult;
        //obj_site_personals


    }

    public function base64_to_jpeg($base64_string) {
        $output_file=date("Y-m-d-H-i-s").".jpg";
       /* $ifp = fopen($output_file, "wb");

        //$data = explode(',', $base64_string);

        fwrite($ifp, base64_decode($base64_string));
        fclose($ifp);*/

        return $output_file;
    }

    public function getRequestData()
    {

        $result = execSQL_van("
        SELECT
          max(R.pkey) as requestPkey, PDR.QR_code as QRCode, PD.firstName as firstName, PD.lastName as lastName,
          PD.middleName as middleName, PD.job as job, PD.position as position, R.permission as permission
        FROM
          obj_site_personal_data PD, obj_site_request R, obj_site_personal_data_request PDR
        WHERE
          PDR.id_request=R.pkey AND
          PDR.id_personal_data=PD.pkey AND
          R.date=CURRENT_DATE AND
          PD.passport_number='".$_POST["passportNumber"]."';
        ");//".$_GET["passportNumber"]."
        if ($result["requestPkey"]!="")
        {
            $photoFileName=$this->base64_to_jpeg($_POST["photo"]);
            execSQL_update("UPDATE obj_site_personal_data SET firstName='".$_POST['firstName']."', lastName='".$_POST['lastName']."', middleName='".$_POST['middleName']."', photo='".$photoFileName."' WHERE passport_number='".$_POST["passportNumber"]."';");
        }
        $result['firstName']=mb_strtoupper($_POST['firstName'],'UTF-8');
        $result['lastName']=mb_strtoupper($_POST['lastName'],'UTF-8');
        $result['middleName']=mb_strtoupper($_POST['middleName'],'UTF-8');
        $result['job']=mb_strtoupper($result['job'],'UTF-8');
        $result['position']=mb_strtoupper($result['position'],'UTF-8');



        $jsonResult = json_encode($result);

        echo $jsonResult;
        //obj_site_personals


    }

    public function getRequestDataGate()
    {

        $result = execSQL_van("
        SELECT
          max(R.pkey) as requestPkey, PD.firstName as firstName, PD.lastName as lastName,
          PD.middleName as middleName, PD.job as job, PD.position as position, R.permission as permission,
          PD.photo as photo
        FROM
          obj_site_personal_data PD, obj_site_request R, obj_site_personal_data_request PDR
        WHERE
          PDR.id_request=R.pkey AND
          PDR.id_personal_data=PD.pkey AND
          R.date=CURRENT_DATE AND
          PDR.QR_code='".$_POST["passNumber"]."';
        ");//".$_GET["passportNumber"]."

        $result['firstName']=mb_strtoupper($result['firstName'],'UTF-8');
        $result['lastName']=mb_strtoupper($result['lastName'],'UTF-8');
        $result['middleName']=mb_strtoupper($result['middleName'],'UTF-8');
        $result['job']=mb_strtoupper($result['job'],'UTF-8');
        $result['position']=mb_strtoupper($result['position'],'UTF-8');



        $jsonResult = json_encode($result);

        echo $jsonResult;
        //obj_site_personals


    }
}
?>