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

// Logical section of page
$clinic_patientObj =  new  clinic_patient();

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);
    
    if($_REQUEST['app_type'] == 'dependantdata' && $_REQUEST['dependants_id'] !=  '')
    {
        $where = " c.clinic_patient_depid  = '".$_REQUEST['dependants_id']."' ";
    }else{
        $where = " 1=1 ";
    }
    
    /*if(isset($_REQUEST['dependant_name']) && $_REQUEST['dependant_name'] != '')
    {
        $depentname = preg_replace('/\s\s+/', ' ', $_REQUEST['dependant_name']);
        $fullname = explode(" ", $depentname);
        if($fullname && count($fullname)>0)
        {
            $where ="";
            foreach ($fullname as $key => $f) 
            {
                $where .=" dependant_fname LIKE '%".($f)."%' OR dependant_fname LIKE '%".(ucfirst($f))."%'  OR dependant_lname LIKE '%".($f)."%' OR dependant_lname LIKE '%".(ucfirst($f))."%'";
                if( ($key+1) != count($fullname) )
                {
                        $where .=" OR ";
                }
            }
        }
        
    }*/

    if(isset($_REQUEST['firstname']) && $_REQUEST['firstname'] != '' && $_REQUEST['lastname'] != '')
    {
        $firstname = trim($_REQUEST['firstname']);
        $lastname = trim($_REQUEST['lastname']);

        $where .= " AND ((dependant_fname='".w::e($firstname)."' and   dependant_lname='".w::e($lastname)."') OR (dependant_fname='".w::e(ucfirst($firstname))."' and   dependant_lname='".w::e(ucfirst($lastname))."'))";
    }else if(isset($_REQUEST['firstname']) && $_REQUEST['firstname'] != '' && $_REQUEST['lastname'] == ''){

        $firstname = trim($_REQUEST['firstname']);

        $where .= " AND ((dependant_fname LIKE '%".w::e($firstname)."%') OR (dependant_fname LIKE '%".w::e(ucfirst($firstname))."%' ))";
    }else if(isset($_REQUEST['lastname']) && $_REQUEST['lastname'] != '' && $_REQUEST['firstname'] == ''){

        $lastname = trim($_REQUEST['lastname']);

        $where .= " AND ((dependant_lname LIKE '%".w::e($lastname)."%') OR ( dependant_lname LIKE '%".w::e(ucfirst($lastname))."%'))";
    }

    if(isset($_REQUEST['clinic_id']) && $_REQUEST['clinic_id'] != '')
    {
        
        $where .= " AND clinic_patient_cid = '".$_REQUEST['clinic_id']."'";
    }
    if(isset($_REQUEST['patient_id']) && $_REQUEST['patient_id'] != '')
    {
        
        $where .= " AND (dependant_id = '".$_REQUEST['patient_id']."' )";
    }

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

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

    
    if(isset($_REQUEST['dependant_email']) && $_REQUEST['dependant_email'] != '')
    {
        $where .= " AND dependant_email_address = '".w::e($_REQUEST['dependant_email'])."' ";
    }

    if(isset($_REQUEST['dependant_phone']) && $_REQUEST['dependant_phone'] != '')
    {
        $where .= " AND dependant_phone = '".w::e($_REQUEST['dependant_phone'])."' ";
    }

    if(isset($_REQUEST['campaign_status']) && $_REQUEST['campaign_status'] != '')
    {
        if($_REQUEST['campaign_status'] == '1')
        {
            $where .= " AND is_messagecenter_campaign = '".$_REQUEST['campaign_status']."'";    
        }else if($_REQUEST['campaign_status'] == '0')
        {
            $where .= " AND is_messagecenter_campaign IS NULL";    
        }else{
            $where .= " AND is_messagecenter_campaign = '".$_REQUEST['campaign_status']."'";
        }
        
    }

    if(isset($_REQUEST['notification_status']) && $_REQUEST['notification_status'] != '')
    {
        if($_REQUEST['notification_status'] == '1')
        {
            $where .= " AND is_messagecenter_notification = '".$_REQUEST['notification_status']."'";    
        }else if($_REQUEST['notification_status'] == '0')
        {
            $where .= " AND is_messagecenter_notification IS NULL";    
        }else{
            $where .= " AND is_messagecenter_notification = '".$_REQUEST['notification_status']."'";
        }
        
    }

    if(isset($_REQUEST['admin_notification_status']) && $_REQUEST['admin_notification_status'] != '')
    {
        if($_REQUEST['admin_notification_status'] == '1')
        {
            $where .= " AND is_m_administrative_notification = '".$_REQUEST['admin_notification_status']."'";    
        }else if($_REQUEST['admin_notification_status'] == '0')
        {
            $where .= " AND is_m_administrative_notification IS NULL";    
        }else{
            $where .= " AND is_m_administrative_notification = '".$_REQUEST['admin_notification_status']."'";
        }
        
    }

    if(isset($_REQUEST['chat_pin_status']) && $_REQUEST['chat_pin_status'] != '')
    {
        if($_REQUEST['chat_pin_status'] == '1')
        {
            $where .= " AND is_m_chat_pin_sendsms = '".$_REQUEST['chat_pin_status']."'";    
        }else if($_REQUEST['chat_pin_status'] == '0')
        {
            $where .= " AND is_m_chat_pin_sendsms IS NULL";    
        }else{
            $where .= " AND is_m_chat_pin_sendsms = '".$_REQUEST['chat_pin_status']."'";
        }
        
    }
    
    if(isset($_REQUEST['chat_status']) && $_REQUEST['chat_status'] != '')
    {
        if($_REQUEST['chat_status'] == '1')
        {
            $where .= " AND is_m_chat_sendsms = '".$_REQUEST['chat_status']."'";    
        }else if($_REQUEST['chat_status'] == '0')
        {
            $where .= " AND is_m_chat_sendsms IS NULL";    
        }else{
            $where .= " AND is_m_chat_sendsms = '".$_REQUEST['chat_status']."'";
        }
        
    }
    

    if (isset($_REQUEST['search']['value']) && $_REQUEST['search']['value']!=""){
        $where = "clinic_patient_cid LIKE '%".$_REQUEST['search']['value']."%' OR  clinic_unique_id LIKE '%".$_REQUEST['search']['value']."%' OR clinic_name LIKE '%".$_REQUEST['search']['value']."%' OR clinic_phone LIKE '%".$_REQUEST['search']['value']."%' OR clinic_email LIKE '%".$_REQUEST['search']['value']."%' OR dependant_id LIKE '%".$_REQUEST['search']['value']."%' OR dependant_unique_id LIKE '%".$_REQUEST['search']['value']."%'";
    }    
        
    
    $orderFields = array('clinic_patient_id');
    
    // 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_patient_id desc ");
    }

    $tables = array('tbl_dependant d','tbl_clinic cl');
    $jfields = array('d.dependant_id = c.clinic_patient_depid','cl.clinic_id = c.clinic_patient_cid');
    $jtype   = array("INNER","INNER");
    $data   = array('where' => $where,  'joinTables' =>$tables, 'joinFields' =>$jfields, 'joinTypes' => $jtype,'orderBy'=>$orderby,'limit' => $limit);

    $clinicpatientDetails = $clinic_patientObj->getAllRecords($data);
    //print_r($clinicpatientDetails);exit;
    $recordsTotal   =   $clinic_patientObj->getAllCount($data);
        
    if ($clinicpatientDetails && count($clinicpatientDetails)>0){
        foreach ($clinicpatientDetails as $key => $clinicpatient){

            

            if($clinicpatient->is_messagecenter_campaign == '1')
            {
                $campaign = 'On';
            }else{
                $campaign = 'Off';
            }

            if($clinicpatient->is_messagecenter_notification == '1')
            {
                $notification = 'On';
            }else{
                $notification = 'Off';
            }

            if($clinicpatient->is_m_administrative_notification == '1')
            {
                $administrative_notification = 'On';
            }else{
                $administrative_notification = 'Off';
            }

            if($clinicpatient->is_m_chat_pin_sendsms == '1')
            {
                $chat_pin_sendsms = 'On';
            }else{
                $chat_pin_sendsms = 'Off';
            }

            if($clinicpatient->is_m_chat_sendsms == '1')
            {
                $chat_sendsms = 'On';
            }else{
                $chat_sendsms = 'Off';
            }

            if($clinicpatient->is_remind_for_medicine == '1')
            {
                $remind_for_medicine = 'On';
            }else{
                $remind_for_medicine = 'Off';
            }

            if($clinicpatient->is_importedExist == '1')
            {
                $is_importedExist = 'On';
            }else{
                $is_importedExist = 'Off';
            }

            if($clinicpatient->is_imported == '1')
            {
                $is_imported = 'On';
            }else{
                $is_imported = 'Off';
            }

            if($clinicpatient->is_verified == '1')
            {
                $is_verified = 'On';
            }else{
                $is_verified = 'Off';
            }

            if($clinicpatient->ghlTags == '1')
            {
                $ghlTags = 'On';
            }else{
                $ghlTags = 'Off';
            }

            if($clinicpatient->ghl_past_recall == '1')
            {
                $ghl_past_recall = 'On';
            }else{
                $ghl_past_recall = 'Off';
            }

            if($clinicpatient->ghl_future_recall == '1')
            {
                $ghl_future_recall = 'On';
            }else{
                $ghl_future_recall = 'Off';
            }

            if($clinicpatient->ghl_overdue == '1')
            {
                $ghl_overdue = 'On';
            }else{
                $ghl_overdue = 'Off';
            }

            if($clinicpatient->ghl_pending_treatment == '1')
            {
                $ghl_pending_treatment = 'On';
            }else{
                $ghl_pending_treatment = 'Off';
            }

            if($clinicpatient->is_push_alert == '1')
            {
                $is_push_alert = 'On';
            }else{
                $is_push_alert = 'Off';
            }
            //"<b>Xcare Clinic Patient Id</b>:".$clinicpatient->clinic_patient_id,
            
            $response[] = array('<input type="checkbox" name="check[]" id="check[]" value="'.$clinicpatient->clinic_patient_id.'" class="check_class" />', 

                    
                    "<b>Clinic Id:
                     </b>".$clinicpatient->clinic_id."<br/><b>Clinic External Id:
                     </b>".$clinicpatient->clinic_unique_id."<br/><b>Clinic
                     Name: </b>".$clinicpatient->clinic_name."<br/><b>Clinic
                     Phone: </b>".$clinicpatient->clinic_phone."<br/><b>Clinic
                     Email: </b>".$clinicpatient->clinic_email."<br/><br/>",

                     "<b>Xcare Patient Id:
                     </b>".$clinicpatient->dependant_id."<br/><b>Patient External Id:
                     </b>".$clinicpatient->dependant_unique_id."<br/><b>
                     First Name: </b>".w::d($clinicpatient->dependant_fname)."<br><b>Last Name: </b>".w::d($clinicpatient->dependant_lname)."<br/><b>
                     Communication Phone: </b>".w::d($clinicpatient->dependant_phone)."<br/><b>
                     Communication Email: </b>".w::d($clinicpatient->dependant_email_address)."<br/><br/>",

                    "<b>Marketing: </b>".$campaign.'<br/><b>Reminders: </b>'.$notification.'<br/><b>Administrative Notification: </b>'.$administrative_notification."<br/><b>ChatPin SendSms: </b>".$chat_pin_sendsms.'<br/><b>Chat SendSms: </b>'.$chat_sendsms.'<br/><b>RemindForMedicine: </b>'.$remind_for_medicine.'<br/><b>ImportedExist: </b>'.$is_importedExist.'<br/><b>IsVerified: </b>'.$is_verified.'<br/><b>Tags: </b>'.$ghlTags.'<br/><b>PastRecall: </b>'.$ghl_past_recall.'<br/><b>FutureRecall: </b>'.$ghl_future_recall.'<br/><b>Overdue: </b>'.$ghl_overdue.'<br/><b>PendingTreatment: </b>'.$ghl_pending_treatment.'<br/><b>PushAlert: </b>'.$is_push_alert."",

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