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

$newLeadObj   = new newpatient();
$dependantObj = new dependant();
$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['firstname'])){
        $where .= " AND d.dependant_fname LIKE '%".w::e($_REQUEST['firstname'])."%' ";
    }
    if(!empty($_REQUEST['lastname'])){
        $where .= " AND d.dependant_lname LIKE '%".w::e($_REQUEST['lastname'])."%' ";
    }
    if(!empty($_REQUEST['clinic_id'])){
        $where .= " AND n.newpatient_clinic_id = '".($_REQUEST['clinic_id'])."' ";
    }
    if(!empty($_REQUEST['startdate']) && !empty($_REQUEST['enddate'])){
        $where .= " AND DATE(n.newpatient_created_at) BETWEEN '".($_REQUEST['startdate'])."' AND '".($_REQUEST['enddate'])."' ";
    }

    $tables  = array('tbl_dependant d','tbl_clinic c');
    $jfields = array(
        'n.newpatient_patient_id = d.dependant_id',
        'c.clinic_id = n.newpatient_clinic_id'
    );
    $jtype   = array('LEFT','LEFT');

    $orderby = array(" n.newpatient_created_at DESC ");
    //echo $where;exit;
    $data = array(
        'where'=>$where,'joinTables'=>$tables,'joinFields'=>$jfields,'joinTypes'=>$jtype,
        'orderBy'=>$orderby,'limit'=>$limit
    );
    //$data['debug']=true;
    $leadDetails  = $newLeadObj->getAllRecords($data);
    
    $recordsTotal = $newLeadObj->getAllCount($data);

    if ($leadDetails && count($leadDetails)>0){
        foreach ($leadDetails as $lead){
            $patientInfo = "<b>ID:</b> ".$lead->dependant_id."<br/>
                            <b>Name:</b> ".w::d($lead->dependant_fname)." ".w::d($lead->dependant_lname)."<br/>
                            <b>Phone:</b> ".w::d($lead->dependant_phone)."<br/>
                            <b>Email:</b> ".w::d($lead->dependant_email_address);

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

            $newPatientInfo = "<b>Type:</b> ".$lead->newpatient_type."<br/>
                           <b>Name:</b> ".($lead->newpatient_fname)." ".($lead->newpatient_lname)."<br/>
                           <b>Source:</b> ".$lead->newpatient_source;               

            $created = "<b>Created At:</b> ".$lead->newpatient_created_at;

            $response[] = array($patientInfo,$clinicInfo,$newPatientInfo,$created);
        }
        $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');
?>
