<?php 

// Include main file
include_once('../libs/main.php');

// Check admin Login
include_once('checkLogin.php');

/*$adminlogObj = new admin_log();
$modulepagesObj = new module_pages();

$filename=basename($_SERVER['SCRIPT_FILENAME']);

if($filename == 'adminlist.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']=='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['search_input']) && $_REQUEST['search_input']!=''){
    // $where = " ( clinic_name LIKE '".$_REQUEST['search_input']."%' OR clinic_name LIKE '".$_REQUEST['search_input']."%' )";
    // }
    
    // //$orderFields = array('','admin_fname','admin_email_address','admin_type','admin_created_date','admin_last_login_date');    
    
    // // 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(" clinic_id asc ");
    // }
    // $pointmasterObj = new pointmaster();
    $loyalty_claimObj = new loyalty_claim();
    $tables = array('tbl_loyalty_action la','tbl_clinic c','tbl_dependant d');
    $jfields = array('la.loyalty_action_id=l.loyalty_claim_action_id','c.clinic_id = la.loyalty_action_clinic_id','d.dependant_id=la.loyalty_action_dependant_id');
    $jtype   = array("INNER","INNER","INNER"); 
    if($_REQUEST['clinic_id']!=""){
        $where .=" AND loyalty_action_clinic_id='".$_REQUEST['clinic_id']."' ";    
    }else{
        $where .=" AND loyalty_action_clinic_id!='0' ";    
    }
    if($_REQUEST['claimStatus']!=""){
        $where .=" AND loyalty_claim_status='".$_REQUEST['claimStatus']."' ";    
    }
    if($_REQUEST['claimNumber']!=""){
        $where .=" AND loyalty_claim_claimNumber='".$_REQUEST['claimNumber']."' ";    
    }
    $orderBy = array(" loyalty_claim_id desc ");
    $arg =array('where'=>$where,'joinTables' =>$tables,'joinFields' =>$jfields,'joinTypes' => $jtype,'limit' => $limit,'orderBy'=>$orderBy);
    $allClaim =$loyalty_claimObj->getAllRecords($arg);

    // //print_r($orderby);exit;
    // $data    = array('where' => $where,'limit' => $limit, 'orderBy'=>$orderby);
    // $pointmasterDetails = $pointmasterObj->getAllRecords($data);
    // //print_r($adminDetails);exit;
    $recordsTotal   =   $loyalty_claimObj->getAllCount($arg);

    if ($allClaim && count($allClaim)>0){
        foreach ($allClaim as $key => $claim){
            $clinicName  =$claim->clinic_name;
            $services="";
            if($claim->loyalty_action_appointment_id!=0){
                $appointmentObj = new appointment();
                $requested =$appointmentObj->fromId($claim->loyalty_action_appointment_id);
                $services .="<b>Requested Appointment: </b>
                <br/>Date:".$requested->appointment_date."
                <br/>Time:".$requested->appointment_time;
            }
            if($claim->loyalty_claim_claimAppointment!=0){
                $appointmentObj = new appointment();
                $claimAppointment =$appointmentObj->fromId($claim->loyalty_action_claimAppointment);
                $services .="<br/><b>Claimed Appointment: </b>
                <br/>Date:".$claimAppointment->appointment_date."
                <br/>Time:".$claimAppointment->appointment_time;    
            }
            if($claim->loyalty_action_type=="REVIEW"){
                $services ="<b>Service Date: </b>".date("Y-m-d", strtotime($claim->loyalty_action_created_at));
            }
            $view ="";
            if($claim->loyalty_claim_receipt!='' && is_array(json_decode($claim->loyalty_claim_receipt,1))){
                $img =json_decode($claim->loyalty_claim_receipt,1);
                 $view ="<a target='_blank' href='".SITE_URL."/uploads/receipt/".$img['name'].".".$img['type']."'>View</a>";
            }
            $comment=$claim->loyalty_claim_reason;
            $claimVerifiedFrom =$claim->loyalty_claim_verified_from;
            if($claimVerifiedFrom=='Xcare Admin'){
                $adminObj = new admin();
                $adminDetails =$adminObj->fromId($claim->loyalty_claim_verified_by);
                $claimVerifiedBy =$adminDetails->admin_fname." ".$adminDetails->admin_lname;
            }
            if($claimVerifiedFrom=='Clinic Admin'){
                $userObj = new user();
                $userDetails =$userObj->fromId($claim->loyalty_claim_verified_by);
                $claimVerifiedBy =$userDetails->user_fname." ".$userDetails->user_lname;
            }
            $claimVerifiedAt =$claim->loyalty_claim_verified_at;
            $claimDate =$claim->loyalty_claim_date;
            $claimNumber =$claim->loyalty_claim_claimNumber;
            $claimBy =w::d($claim->dependant_fname)." ".w::d($claim->dependant_lname);
            $claimStatus =$claim->loyalty_claim_status;
            $response[] = array(
                    "<b>".$claim->loyalty_action_claimNumber."</b>",
                    "<b>".$claimStatus."</b>",
                    "<b>Clinic Name: </b>".$clinicName."<br/>
                    <b>Type: </b>".$claim->loyalty_action_type."<br/>
                    ".$services."<br/>
                    <b>Patient: </b>".$claimBy."<br/>
                    <b>Claim Date: </b>".$claimDate."<br/>",
                    "<b>Verified From: </b>".$claimVerifiedFrom."<br/>
                    <b>Verified By: </b>".$claimVerifiedBy."<br/>
                    <b>Verified At: </b>".$claimVerifiedAt."<br/>",
                    nl2br($comment),
                    $view
                    
             );

        }
        $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')
?>