<?php   
// Include main file
include_once('../libs/main.php');
// Check admin Login
include_once('checkLogin.php');
// Logical section of page
$writebackStatusObj =  new  writebackStatus();


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 ";
    //serach parmeter

    if (isset($_REQUEST['clinic_id']) && $_REQUEST['clinic_id']!=""){
        $where .= " AND  writebackStatus_clinic_id = '".$_REQUEST['clinic_id']."' ";
    }
    if(isset($_REQUEST['writebackStatus_appointment_id']) && $_REQUEST['writebackStatus_appointment_id'] != ''){
        $appointmentObj = new appointment();
        $appointment = $appointmentObj->getByUniqueId($_REQUEST['writebackStatus_appointment_id']);
        $appointmentId = $appointment->appointment_id;
        $where .= " AND writebackStatus_appointment_id = '".$appointmentId."'";
    }

    if (isset($_REQUEST['search']['value']) && $_REQUEST['search']['value']!=""){
        $where = " writebackStatus_appointment_id LIKE '%".$_REQUEST['search']['value']."%' OR writebackStatus_clinic_id LIKE '%".$_REQUEST['search']['value']."%' ";
    }    
    $orderFields = array('writebackStatus_id','writebackStatus_type','writebackStatus_status','','','','');
    
    // 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(" writebackStatus_id desc ");
    }

    $tables = array('tbl_clinic c','tbl_appointment a');
    $jfields = array('c.clinic_id = w.writebackStatus_clinic_id','a.appointment_id=w.writebackStatus_appointment_id');
    $jtype   = array("LEFT","INNER"); 
    $data    = array('joinTables' =>$tables, 'joinFields' =>$jfields, 'joinTypes' => $jtype,'where' => $where,'limit' => $limit, 'orderBy'=>$orderby);

    $writebackStatusDetails = $writebackStatusObj->getAllRecords($data);
    $recordsTotal   =   $writebackStatusObj->getAllCount($data);

        
    if ($writebackStatusDetails && count($writebackStatusDetails)>0){
        foreach ($writebackStatusDetails as $key => $writebackStatus){
            $fResponse="";
            if(!is_null($writebackStatus->writebackStatus_response) && $writebackStatus->writebackStatus_response!="null" && is_array(json_decode($writebackStatus->writebackStatus_response,1))){
                $ffresponse = json_decode($writebackStatus->writebackStatus_response,1);
                if($ffresponse && count($ffresponse)>0){
                    foreach ($ffresponse as $key => $value) {
                      $fResponse .="<b>".$key." :</b>".$value."<br/>";  
                    }
                }
            }
            $faResponse="";
            if(!is_null($writebackStatus->writebackStatus_attemptResponse) && $writebackStatus->writebackStatus_attemptResponse!="null" && is_array(json_decode($writebackStatus->writebackStatus_attemptResponse,1))){
                $ffresponse = json_decode($writebackStatus->writebackStatus_attemptResponse,1);
                if($ffresponse && count($ffresponse)>0){
                    foreach ($ffresponse as $key => $value) {
                      $faResponse .="<b>".$key." :</b>".$value."<br/>";  
                    }
                }
            }
            $response[] = array(
                    $writebackStatus->clinic_name,
                    "<b>UniqueId:</b>".$writebackStatus->appointment_unique_id."<br/><b>Date Time :</b>".$writebackStatus->appointment_date." ".$writebackStatus->appointment_time, 
                    "<b>Log Id : </b>".$writebackStatus->writebackStatus_logId."<br/><b>Status : </b>".$writebackStatus->writebackStatus_status."<br/><b>Type :</b>".$writebackStatus->writebackStatus_type."<br/><b>Xcare Status :</b>".$writebackStatus->writebackStatus_xcareStatus."<br/><b>Attempt Counter : </b>".$writebackStatus->writebackStatus_attemptCounter,
                    $writebackStatus->writebackStatus_result,
                    "<b>Response : </b>".$fResponse, 
                    "<b>Attempt Response : </b>".$faResponse,
                    $writebackStatus->writebackStatus_created_at,
                    $writebackStatus->writebackStatus_updated_at
             );
        }
        $result=array('recordsFiltered'=>$recordsTotal,'recordsTotal'=>$recordsTotal,'data'=>$response);
    }else{
        $result=array('recordsFiltered'=>0,'recordsTotal'=>0,'data'=>array());
    }
    echo json_encode($result);
    exit;
}

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