<?php  
// Include main file
include_once('../libs/main.php');
// Check dependant Login
include_once('checkLogin.php');

// Logical section of page
//$reschedulerequestObj =  new reschedulerequest();
$reschedulerequestObj =  new rescheduleRequest();


if (isset($_REQUEST['act']) && $_REQUEST['act']=='load'){
    
    $records = $_REQUEST['length'];
    $start=0;
    if(isset($_GET['start']) && $_GET['start']!='0'){
        $page = $_GET['start'];
    }
    $start=($page-1)*$limit;
    $limit = array('start'=>$_GET['start'],'record'=>$records);
    
    $where = " 1=1 ";

    if(isset($_REQUEST['firstname']) && $_REQUEST['firstname'] != '' && $_REQUEST['lastname'] != '')
    {
        $firstname = trim($_REQUEST['firstname']);
        $lastname = trim($_REQUEST['lastname']);

        $where .= " AND ((dep.dependant_fname='".w::e($firstname)."' and   dep.dependant_lname='".w::e($lastname)."') OR (dep.dependant_fname='".w::e(ucfirst($firstname))."' and   dep.dependant_lname='".w::e(ucfirst($lastname))."'))";
    }else if(isset($_REQUEST['firstname']) && $_REQUEST['firstname'] != '' && $_REQUEST['lastname'] == ''){

        $firstname = trim($_REQUEST['firstname']);

        $where .= " AND ((dep.dependant_fname LIKE '%".w::e($firstname)."%') OR (dep.dependant_fname LIKE '%".w::e(ucfirst($firstname))."%' ))";
    }else if(isset($_REQUEST['lastname']) && $_REQUEST['lastname'] != '' && $_REQUEST['firstname'] == ''){

        $lastname = trim($_REQUEST['lastname']);

        $where .= " AND ((dep.dependant_lname LIKE '%".w::e($lastname)."%') OR ( dep.dependant_lname LIKE '%".w::e(ucfirst($lastname))."%'))";
    }

    if(isset($_REQUEST['appoinment_id']) && $_REQUEST['appoinment_id'] != '')
    {
        $where .= " AND ap.appointment_id = '".$_REQUEST['appoinment_id']."'";
    }

    if(isset($_REQUEST['appoinment_external_id']) && $_REQUEST['appoinment_external_id'] != '')
    {
        $where .= " AND ap.appointment_unique_id = '".$_REQUEST['appoinment_external_id']."'";
    }

    if(isset($_REQUEST['doctor_id']) && $_REQUEST['doctor_id'] != '')
    {
        $where .= " AND d.doctor_id = '".$_REQUEST['doctor_id']."'";
    }

    if(isset($_REQUEST['provider_external_id']) && $_REQUEST['provider_external_id'] != '')
    {
        $where .= " AND d.doctor_unique_id = '".$_REQUEST['provider_external_id']."'";
    }

    if (isset($_REQUEST['search']['value']) && $_REQUEST['search']['value']!="")
    {
        $where = " rescheduleRequest LIKE '%".$_REQUEST['search']['value']."%' ";
    }

    $orderFields = array('rescheduleRequest');
    
    // Array of order by 
    $orderby=array();
    if (isset($_REQUEST['order']['0']['column']) && $_REQUEST['order']['0']['column']!=""){
        $orderby = array(" ".$orderFields[$_REQUEST['order']['0']['column']]." ".$_REQUEST['order']['0']['dir']);
    }else{
        $orderby = array(" rescheduleRequest desc ");
    }

    /*$tables = array('tbl_appointment ap','tbl_dependant d','tbl_clinic c');
    $jfields = array('ap.appointment_id = l.log_appointment_id','ap.appointment_dependant_id = d.dependant_id','c.clinic_id=l.log_clinic_id');
    $jtype   = array("INNER","INNER","INNER");*/

    $tables = array('tbl_appointment ap','tbl_doctor d','tbl_dependant dep');
    $jfields = array('r.rescheduleRequest_appointment_id = ap.appointment_id','d.doctor_id=r.rescheduleRequest_doctor_id','dep.dependant_id=ap.appointment_dependant_id');
    $jtype   = array("INNER","INNER","LEFT");
    $data   = array('where' => $where,  'joinTables' =>$tables, 'joinFields' =>$jfields, 'joinTypes' => $jtype,'orderBy'=>$orderby,'limit' => $limit);

    $reschedulerequestDetails = $reschedulerequestObj->getAllRecords($data);
    //print_r($schedulerequestDetails);exit;
    $recordsTotal   =  $reschedulerequestObj->getAllCount($data);
     
    if ($reschedulerequestDetails && count($reschedulerequestDetails)>0){
        $alldoctorDetail = '';
        foreach ($reschedulerequestDetails as $key => $reschedulerequestdata)
        {
            
            $response[] = array( 
                    "<b>Xcare Id</b>: ".$reschedulerequestdata->rescheduleRequest,
                    "<b>Appointment Id: </b>".$reschedulerequestdata->appointment_id.'<br/><b>Appointment External Id: </b>'.$reschedulerequestdata->appointment_unique_id.'<br/><b>Date: </b>'.$reschedulerequestdata->appointment_date.'<br/><b>Time: </b>'.$reschedulerequestdata->appointment_time.'<br/><b>Status: </b>'.$reschedulerequestdata->appointment_status,
                    "<b>Id: </b>".$reschedulerequestdata->doctor_id.'<br/><b>External Id: </b>'.$reschedulerequestdata->doctor_unique_id.'<br/><b>Name: </b>'.$reschedulerequestdata->doctor_login_fullname.'<br/><b>Email: </b>'.$reschedulerequestdata->doctor_login_email.'<br/><b>Phone: </b>'.$reschedulerequestdata->doctor_login_phone.'<br/><b>Phone Verified: </b>'.$reschedulerequestdata->doctor_login_phone_verified.'<br/><b>Phone Vcode: </b>'.$reschedulerequestdata->doctor_login_pcode.'<br/><b>Email Verified: </b>'.$reschedulerequestdata->doctor_login_email_verified.'<br/><b>Email Vcode: </b>'.$reschedulerequestdata->doctor_login_ecode,
                    "<b>Xcare Patient ID : </b>".$reschedulerequestdata->dependant_id."<br/><b>External Id: </b>".$reschedulerequestdata->dependant_unique_id."<br/><b>First Name: </b>".w::d($reschedulerequestdata->dependant_fname)."<br/><b>Last Name: </b>".w::d($reschedulerequestdata->dependant_lname)."<br/><b>Email: </b>".w::d($reschedulerequestdata->dependant_email_address)."<br/><b>Phone: </b>".w::d($reschedulerequestdata->dependant_phone),
                    "<b>Date</b>: ".$reschedulerequestdata->rescheduleRequest_date."<br/><b>Start Time</b>: ".$reschedulerequestdata->rescheduleRequest_time."<br/><b>End Time</b>: ".$reschedulerequestdata->rescheduleRequest_etime."<br/><b>Notes</b>: ".$reschedulerequestdata->rescheduleRequest_notes
            );
        }
        $result=array('recordsFiltered'=>$recordsTotal,'recordsTotal'=>$recordsTotal,'data'=>$response);
    }else{
        $result=array('recordsFiltered'=>0,'recordsTotal'=>0,'data'=>array());
    }
    echo json_encode($result);
    exit;
}

/*$clinicObj = new clinic();
$where = "clinic_status = '1'";
$data = array('where'=>$where);
$allclinic = $clinicObj->getAllRecords($data);*/

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