<?php  
// Include main file
include_once('../libs/main.php');
// Check admin Login
include_once('checkLogin.php');
// Logical section of page
$clinicObj =  new  clinic();
$adminlogObj = new admin_log();
$modulepagesObj = new module_pages();
$filename=basename($_SERVER['SCRIPT_FILENAME']);

if($filename == 'appointment_statusupdate.php')
{
    $module_id = $adminlogObj->getModuleId($filename);
    $page_id = $adminlogObj->getPageId($filename);
    $allpages = $modulepagesObj->fromId($page_id);
    $pagename = $allpages->page;
}


if (isset($_REQUEST['act']) && $_REQUEST['act']=='getpmsstatus' && isset($_REQUEST['clinic_id']) && $_REQUEST['clinic_id']!='' )
{
    $pmsappointmentstatusObj = new pmsAppointmentStatus();
    $where = array("pmsAppointmentStatus_clinic_id="=>$_REQUEST['clinic_id']);
    $pmsappointmentstatusArg = array('where'=>$where);
    $pmsAppointmentStatusDetails = $pmsappointmentstatusObj->getAllRecords($pmsappointmentstatusArg);
    ?>
     ?>
    <!-- <option value="">Select Status</option> -->
    <?php
    foreach ($pmsAppointmentStatusDetails as $key => $pmsAppointmentStatusvalue) 
    { ?>
       <option value="<?php echo $pmsAppointmentStatusvalue->pmsAppointmentStatus_status; ?>" ><?php echo $pmsAppointmentStatusvalue->pmsAppointmentStatus_status; ?></option>
                             
    <?php }   
    exit;
}


if(isset($_REQUEST['act']) && $_REQUEST['act'] == 'updatestatus')
{   
    
    $clinic_id = $_REQUEST['clinic_id'];
    $pmsstatus_id = $_REQUEST['pmsstatus_id'];
    $xcare_status = $_REQUEST['xcare_status_id'];
    
    
    $pmsFloatingStatus = $pmsstatus_id;
    if($pmsFloatingStatus && count($pmsFloatingStatus)>0){
      foreach ($pmsFloatingStatus as $key => $value) {
        $data_cell = '%"appointmentStatus"'.":".'"'.$value.'"'."%".'';  
          if($key==0){
              $wherechanges =" AND ( ";
          }
          $wherechanges .=" appointment_metadata LIKE '".$data_cell."' ";
          if( ($key+1) == count($pmsFloatingStatus) ){
              $wherechanges .=" )";
          }
          if( ($key+1) != count($pmsFloatingStatus) ){
              $wherechanges .=" OR ";
          } 
      }
    }    
    //echo $wherechanges;exit;
    $appointmentObj = new appointment();
    $appointmentObj->resultAs = "array";
    $fields  = array('a.appointment_id as id','a.appointment_unique_id as uniqueid','a.appointment_status as currentStatus','a.appointment_clinic_id as clinicid','a.appointment_metadata as metadata','a.appointment_date as date','a.appointment_time as time');
    $where =" appointment_clinic_id='".$clinic_id."'".$wherechanges;
    $arg =array('fields'=>$fields,'where'=>$where);
    //$arg['debug']=true;
    $allAppointemnt  =$appointmentObj->getAllRecords($arg);
    /*print_r('<pre>');
    print_r($allAppointemnt);
    exit;*/

    if($allAppointemnt && count($allAppointemnt)>0){
      foreach ($allAppointemnt as $key => $a) {

        if($xcare_status == 'schedule'){
            $metadata =json_decode($a['metadata'],1);

            if(isset($metadata['appointment']['confirmation']) ){
                unset($metadata['appointment']['confirmation']);
            }
            $metadata['appointment']['floating']=false; 
            //print_r($metadata);exit;
            $appointmentData =array(
                "appointment_status"=>'scheduled',
                "appointment_metadata"=>json_encode($metadata),
              );
              $rs = $appointmentObj->updateRecords($appointmentData,$a['id']);
        }
        if($xcare_status == 'completed'){
            $appointmentData =array(
                "appointment_status"=>'completed'
              );
              $rs = $appointmentObj->updateRecords($appointmentData,$a['id']);
        }
        if($xcare_status == 'cancelled'){
            $appointmentData =array(
                "appointment_status"=>'cancelled'
              );
              $rs = $appointmentObj->updateRecords($appointmentData,$a['id']);
        }
        if($xcare_status == 'confirmed')
        {
            if(isset($a['metadata']) && is_array(json_decode($a['metadata'],1))){
              $metadata =json_decode($a['metadata'],1);
              $metadata['appointment']['floating']=false;
              $metadata['appointment']['confirmation']['attempt'] =true;
              $metadata['appointment']['confirmation']['by'] ="admin side";
              $metadata['appointment']['confirmation']['status'] ="confirmed";
              $appointmentData =array(
                "appointment_status"=>'scheduled',
                "appointment_metadata"=>json_encode($metadata)
              );
              $rs = $appointmentObj->updateRecords($appointmentData,$a['id']);
              /*print_r('<pre>');
              print_r(json_encode($metadata));
              exit;*/
            }
        }
        if($xcare_status == 'floating')
        {
            if(isset($a['metadata']) && is_array(json_decode($a['metadata'],1))){
              $metadata =json_decode($a['metadata'],1);
              $metadata['appointment']['floating']=true; 
              $appointmentData =array(
                "appointment_status"=>'scheduled',
                "appointment_metadata"=>json_encode($metadata)
              );
              $rs = $appointmentObj->updateRecords($appointmentData,$a['id']);
              /*print_r('<pre>');
              print_r(json_encode($metadata));
              exit;*/
            }
        }
               
      }

      echo "Success";

      
      if(isset($clinic_id) && $clinic_id != '')
      {
         $clinicObj = new clinic();
         $clinicDetails = $clinicObj->fromId($clinic_id);
         $clinicName = $clinicDetails->clinic_name;
      }else{
         $clinicName = "";
      }

      $AppoimentId = array_column($allAppointemnt, 'id');
      $appdataval = implode(",",$AppoimentId);

      $admin_detail.= " <b>Clinic Name: </b>.".$clinicName." and <b>PMS Status: </b> "." ".implode(",",$pmsstatus_id)." and <b>Xcare Status: </b> ".$xcare_status." and <b>Update Records: </b>".count($allAppointemnt)."<br><b>Appointment Id: </b>".$appdataval;

      $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$admin_detail);
    }
    
    exit;
}


// Include Template 
include_once('../libs/routs.php');
?>