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


$customerObj = new customer();
if(isset($_REQUEST['act']) && $_REQUEST['act']=='delete' && isset($_REQUEST['customer_id'])){
    $customerObj->deleteFromId($_REQUEST['customer_id']);
    $successMsg = base64_encode(RECORD_DELETE);
}

if(isset($_REQUEST['action']) && $_REQUEST['action']!=''){
    
    if($_REQUEST['action']=="delete_all"){
        $rs = $customerObj->deleteManyRecords($_POST['check']);
        $successMsg = base64_encode(RECORD_ALL_DELETE);
    }
}

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['customer_clinic']) && $_REQUEST['customer_clinic'] != ''){
        $where .= " AND  customer_clinic_id = '".$_REQUEST['customer_clinic']."' ";
    }

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

    if (isset($_REQUEST['search']['value']) && $_REQUEST['search']['value']!=""){
        $where .= " AND ( customer_fname LIKE '%".$_REQUEST['search']['value']."%' OR  customer_lname LIKE '%".$_REQUEST['search']['value']."%' OR customer_email_address LIKE '%".$_REQUEST['search']['value']."%' OR customer_phone LIKE '%".$_REQUEST['search']['value']."%' OR customer_homephone LIKE '%".$_REQUEST['search']['value']."%' OR customer_workphone LIKE '%".$_REQUEST['search']['value']."%' )";
    }    
    $orderFields = array('','customer_id','customer_fname','customer_lname','customer_email_address','customer_phone');
    
    // 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(" customer_id desc ");
    }

    //print_r($orderby);exit;
    //$data   = array('limit' => $limit);
    //,'tbl_speciality s'
    //,'cd.ccustomer_speciality = s.speciality_id'
    //,"LEFT"
    $tables = array('tbl_clinic cl');
    $jfields = array('cl.clinic_id = c.customer_clinic_id');
    $jtype   = array("INNER");
    $data   = array('where' => $where,  'joinTables' =>$tables, 'joinFields' =>$jfields, 'joinTypes' => $jtype,'orderBy'=>$orderby,'limit' => $limit);
    // $data['debug'] ="true";
    $customerDetails = $customerObj->getAllRecords($data);
    $recordsTotal   =   $customerObj->getAllCount($data);
        
    if ($customerDetails && count($customerDetails)>0){
        foreach ($customerDetails as $key => $customer){

            $response[] = array('<input type="checkbox" name="check[]" id="check[]" value="'.$customer->customer_id.'" class="check_class" />', 
                    "<b>Xcare Id: </b>".$customer->customer_id."<br/>
                    <b>Clinic Name: </b>".$customer->clinic_name."<br/>
                    <b>External Id: </b>".$customer->customer_externalId."<br/>
                    <b>Title: </b>".$customer->customer_title."<br/>
                    <b>FirstName: </b>".w::d($customer->customer_fname)."<br/>
                    <b>LastName: </b>".w::d($customer->customer_lname)."<br/>
                    <b>Gender: </b>".$customer->customer_gender,

                    "<b> Email: </b>".w::d($customer->customer_email_address)."</b></br>
                    <b>Phone: </b>".w::d($customer->customer_phone)."<br/>
                    <b>HomePhone: </b>".w::d($customer->customer_homephone)."<br/>
                    <b>WorkPhone: </b>".w::d($customer->customer_workphone)."<br/>
                    <b>Contact Method: </b>".$customer->customer_contact_method,

                    "<b>Address: </b>".$customer->customer_address."<br/>
                    <b>City: </b>".$customer->customer_city."<br/>
                    <b>State: </b>".$customer->customer_state."<br/>
                    <b>Zipcode: </b>".$customer->customer_zipcode."<br/>
                    <b>Location: </b>".$customer->customer_location,

                    "<b>Type: </b>".$customer->customer_type."<br/>
                    <b>Language: </b>".$customer->customer_language."<br/>
                    <b>Status: </b>".$customer->customer_status,

                    '<a id="delete-confirm" onclick="delete_entry('.$customer->customer_id.');" class="btn btn-danger white-btn"><i class="nav-icon i-Close-Window "></i></a>'

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