<?php  
// Include main file
include_once('../libs/main.php');
// Check admin Login
include_once('checkLogin.php');
// Logical section of page
$practiceObj =  new  practice();
$guarantorObj = new guarantor();
if(isset($_REQUEST['office_id']) && $_REQUEST['office_id']!=""){
    $practiceObj = new practice();
    $where =array('practice_office_id='=>$_REQUEST['office_id']);
    $arg = array('where'=>$where);
    $practiceDetails = $practiceObj->FromFields($arg);
    if($practiceDetails){

            // $guarantorObj = new guarantor();
            // $where =array('guarantor_practice_id='=>$practiceDetails->practice_office_id);
            // $data =array('where'=>$where);
            // $exist = $guarantorObj->FromFields($data);
            // if($exist){
            //    header("Location:guarantorlist.php?failedMsg=Guarantors already fetched from PMS for this practice.");
            //    exit;   
            // }

        $guarantorObj = new guarantor();
        $arg =array(
          "request_key"=>$practiceDetails->practice_request_key,
          "office_id"=>$practiceDetails->practice_office_id,
          "secret_key"=>$practiceDetails->practice_secret_key,
          "clinic_id"=>$practiceDetails->practice_xcare_id,
          "endpoint"=>$practiceDetails->practice_endpoint
        );
        $status  = $guarantorObj->pullGuarantors($arg);
        if($status){
            header("Location:guarantorlist.php?successMsg=Successfully pulled all guarantor for ".$practiceDetails->practice_office_id);
            exit;
        }else{
            header("Location:guarantorlist.php?successMsg=Successfully pulled all guarantor for ".$practiceDetails->practice_office_id);
            exit;
        }
    }else{
        header("Location:guarantorlist.php?failedMsg=Error Occur.");
        exit;
    }
}

if(isset($_REQUEST['action']) && $_REQUEST['action']!=''){
    
    if($_REQUEST['action']=="delete_all"){

        $chk_del = implode(",",$_POST['check']);
        
        /*$patientdata = " <b>Delete Id</b>: "." ".$chk_del;
        $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$patientdata);*/
        
        $rs = $guarantorObj->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['practice_id']) && $_REQUEST['practice_id'] != "")
    {
        $where .= " AND  guarantor_practice_id = '".$_REQUEST['practice_id']."' ";
    }
    if (isset($_REQUEST['sikka_id']) && $_REQUEST['sikka_id']!=""){
        $where .= " AND  guarantor_sikka_id = '".$_REQUEST['sikka_id']."' ";
    }
    if (isset($_REQUEST['email']) && $_REQUEST['email']!=""){
        $where .= " AND  guarantor_email = '".$_REQUEST['email']."' ";
    }
    /*if (isset($_REQUEST['fullname']) && $_REQUEST['fullname']!="")
    {
        $fullname = str_replace(' ', '', $_REQUEST['fullname']);

        //$where .=" AND CONCAT(guarantor_fname,'',guarantor_lname) LIKE '%".$fullname."%' ";
        $where .=" AND ( guarantor_fname LIKE '%".$_REQUEST['fullname']."%' OR CONCAT(guarantor_fname,'',guarantor_lname) LIKE '%".$fullname."%' OR guarantor_lname LIKE '%".$_REQUEST['fullname']."%') ";
        //$where .= " AND ('%".$fullname."%' LIKE Concat(Concat('%',guarantor_fname),'%') 
          //          OR '%".$fullname."%' LIKE  Concat(Concat('%',guarantor_lname),'%'))";
    }*/

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

        $where .= " AND ((guarantor_fname='".($firstname)."' and   guarantor_lname='".($lastname)."') OR (guarantor_fname='".(ucfirst($firstname))."' and   guarantor_lname='".(ucfirst($lastname))."'))";
    }else if(isset($_REQUEST['firstname']) && $_REQUEST['firstname'] != '' && $_REQUEST['lastname'] == ''){

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

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

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

        $where .= " AND ((guarantor_lname LIKE '%".($lastname)."%') OR ( guarantor_lname LIKE '%".(ucfirst($lastname))."%'))";
    }
    if (isset($_REQUEST['clinic_id']) && $_REQUEST['clinic_id']!=""){
        $where .= " AND  guarantor_clinic_id = '".$_REQUEST['clinic_id']."' ";
    }
    if(isset($_REQUEST['guarantor_xcare_id']) && $_REQUEST['guarantor_xcare_id']!="")
    {
        if(is_numeric($_REQUEST['guarantor_xcare_id']))
        {
            $where .= " AND  guarantor_xcare_id = '".$_REQUEST['guarantor_xcare_id']."' ";    
        }
        if($_REQUEST['guarantor_xcare_id'] == 'IS NULL')
        {    
            $where .= " AND  (`guarantor_xcare_id` IS NULL OR `guarantor_xcare_id` = '') ";
        }
    }
    if(isset($_REQUEST['guarantor_gender']) && $_REQUEST['guarantor_gender'] != '')
    {
        $where .= " AND guarantor_gender = '".$_REQUEST['guarantor_gender']."'";
    }
    if(isset($_REQUEST['guarantor_phone']) && $_REQUEST['guarantor_phone'] != '')
    {
        $where .= " AND (guarantor_phone = '".$_REQUEST['guarantor_phone']."' OR guarantor_workphone = '".$_REQUEST['guarantor_phone']."' OR guarantor_cell = '".$_REQUEST['guarantor_phone']."')";
    }
    if(isset($_REQUEST['guarantor_dob']) && $_REQUEST['guarantor_dob'] != '')
    {
        $where .= " AND guarantor_dob = '".$_REQUEST['guarantor_dob']."'";
    }
    if(isset($_REQUEST['guarantor_id']) && $_REQUEST['guarantor_id'] != '')
    {
        $where .= " AND guarantor_id = '".$_REQUEST['guarantor_id']."'";
    }



    if (isset($_REQUEST['search']['value']) && $_REQUEST['search']['value']!=""){
        $where = " guarantor_fname LIKE '%".$_REQUEST['search']['value']."%' OR guarantor_lname LIKE '%".$_REQUEST['search']['value']."%'  OR guarantor_email LIKE '%".$_REQUEST['search']['value']."%' OR guarantor_phone LIKE '%".$_REQUEST['search']['value']."%'   OR guarantor_workphone LIKE '%".$_REQUEST['search']['value']."%'  OR guarantor_cell LIKE '%".$_REQUEST['search']['value']."%' OR guarantor_sikka_id LIKE '%".$_REQUEST['search']['value']."%' OR guarantor_practice_id LIKE '%".$_REQUEST['search']['value']."%' OR guarantor_id LIKE '%".$_REQUEST['search']['value']."%'";
    }    
    $orderFields = array('guarantor_id','guarantor_fname','','','','guarantor_email');
    
    // 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(" guarantor_id desc ");
    }

    $data    = array('where' => $where,'limit' => $limit, 'orderBy'=>$orderby);

    $guarantorDetails = $guarantorObj->getAllRecords($data);
    $recordsTotal   =   $guarantorObj->getAllCount($data);

        
    if ($guarantorDetails && count($guarantorDetails)>0){
        foreach ($guarantorDetails as $key => $guarantor){

            $clinicObj = new clinic();
            $where =array('clinic_id='=>$guarantor->guarantor_clinic_id);
            $data1 =array('where'=>$where);
            $clinicexist = $clinicObj->FromFields($data1);
            $clinicDetail = '';
            if($clinicexist)
            {
                $clinicDetail .= "<b>ClinicId :</b>".$clinicexist->clinic_id."<br/><b>UniqueId : </b>".$clinicexist->clinic_unique_id."<br/><b>ClinicName :</b>".$clinicexist->clinic_name."<br/><b>Phone : </b>".$clinicexist->clinic_phone."<br/><b>Email : </b>".$clinicexist->clinic_email;
            }

            if(is_null($guarantor->guarantor_xcare_id) && $guarantor->guarantor_xcare_id == '')
            {
                $all_checkbox = '<input type="checkbox" name="check[]" id="check[]" value="'.$guarantor->guarantor_id.'" class="check_class" />';
            }else{
                $all_checkbox = '';
            } 

            $response[] = array(
                    $all_checkbox,
                    $guarantor->guarantor_id,
                    "<b>FirstName : </b>".$guarantor->guarantor_fname."<br/><b>LastName :</b>".$guarantor->guarantor_lname,
                    "<b>SikkaId : </b>".$guarantor->guarantor_sikka_id."<br/><b>OfficeId : </b>".$guarantor->guarantor_practice_id."<br/><b>XcareId: </b>".$guarantor->guarantor_xcare_id."<br/><b>Loadeddate: </b>".$guarantor->guarantor_loadeddate,
                    "<b>DOB :</b>".date("Y-m-d",strtotime($guarantor->guarantor_dob))."<br/><b>Gender : </b>".$guarantor->guarantor_gender,
                    $clinicDetail,
                    $guarantor->guarantor_email, 
                    "<b>Home :</b>".$guarantor->guarantor_phone."<br/><b>Work : </b>".$guarantor->guarantor_workphone."<br/><b>Cell : </b>".$guarantor->guarantor_cell
             );
        }
        $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');
?>