<?php
include_once('../libs/main.php');
include_once('checkLogin.php');

$callObj   = new call();
$clinicObj = new clinic();

if (isset($_REQUEST['act']) && $_REQUEST['act']=='load') {

    $records = $_REQUEST['length'];
    $start   = ($_GET['start'] ?? 0);
    $limit   = array('start'=>$start,'record'=>$records);

    $where = " 1=1 ";

    if(!empty($_REQUEST['clinic_id'])){
        $where .= " AND c.call_clinic_id = '".($_REQUEST['clinic_id'])."' ";
    }
    if(!empty($_REQUEST['startdate']) && !empty($_REQUEST['enddate'])){
        $where .= " AND DATE(c.call_date) BETWEEN '".($_REQUEST['startdate'])."' AND '".($_REQUEST['enddate'])."' ";
    }
    

    $tables  = array('tbl_clinic cl');
    $jfields = array('cl.clinic_id = c.call_clinic_id');
    $jtype   = array('LEFT');

    $orderby = array(" c.call_date DESC ");

    $data = array(
        'where'=>$where,'joinTables'=>$tables,'joinFields'=>$jfields,'joinTypes'=>$jtype,
        'orderBy'=>$orderby,'limit'=>$limit
    );

    $callDetails  = $callObj->getAllRecords($data);
    $recordsTotal = $callObj->getAllCount($data);

    if ($callDetails && count($callDetails)>0){
        foreach ($callDetails as $row){

            $clinicInfo = "<b>Clinic:</b> ".$row->clinic_name."<br/>
                           <b>Email:</b> ".$row->clinic_email."<br/>
                           <b>Phone:</b> ".$row->clinic_phone."<br/>
                           <b>Website:</b> ".$row->clinic_website;

            $callDetail = "<b>Source:</b> ".$row->call_source."<br/>
                           <b>Customer:</b> ".$row->call_customer."<br/>
                           <b>Tracking:</b> ".$row->call_tracking;

            $callDate = "<b>Call Date:</b> ".$row->call_date;

            $response[] = array($clinicInfo,$callDetail,$callDate);
        }
        $result = array('recordsFiltered'=>$recordsTotal,'recordsTotal'=>$recordsTotal,'data'=>$response);
    } else {
        $result = array('recordsFiltered'=>0,'recordsTotal'=>0,'data'=>array());
    }
    echo json_encode($result);
    exit;
}
include_once('../libs/routs.php');
?>
