<?php 

// Include main file
include_once('../libs/main.php');

// Check admin Login
include_once('checkLogin.php');

/*$adminlogObj = new admin_log();
$modulepagesObj = new module_pages();

$filename=basename($_SERVER['SCRIPT_FILENAME']);

if($filename == 'adminlist.php')
{
    $module_id = $adminlogObj->getModuleId($filename);
    $page_id = $adminlogObj->getPageId($filename);
    $allpages = $modulepagesObj->fromId($page_id);
    $pagename = $allpages->page;
}*/
if (isset($_REQUEST['act']) && $_REQUEST['act']=='approveClaim' && isset($_REQUEST['id']) && $_REQUEST['id']!=""){
    $loyalty_actionObj = new loyalty_action();
    $actionDetails =$loyalty_actionObj->fromId($_REQUEST['id']);
    if($actionDetails && $actionDetails->loyalty_action_status=='Claimed'){
        $loyalty_actionData =array(
            "loyalty_action_status"=>"Earned",
            "loyalty_action_action"=>"Verified",
            "loyalty_action_verified_from"=>"Xcare Admin",
            "claim_verified_by"=>$_SESSION['admin_id'],
            "claim_verified_at"=>date("Y-m-d H:i:s")
        );
        $rs =$loyalty_actionObj->updateRecords($loyalty_actionData,$actionDetails->loyalty_action_id);
        if($rs){
            $loyalty_claimObj = new loyalty_claim();
            $loyalty_claimData =array(
                "loyalty_claim_status"=>"Approved",
                "loyalty_claim_verified_from"=>"Xcare Admin",
                "loyalty_claim_claimNumber"=>$actionDetails->loyalty_action_claimNumber,
                "loyalty_claim_claimAppointment"=>$actionDetails->loyalty_action_claimAppointment,
                "loyalty_claim_receipt"=>$actionDetails->claim_receipt1,
                "loyalty_claim_verified_by"=>$_SESSION['admin_id'],
                "loyalty_claim_verified_at"=>date("Y-m-d H:i:s"),
                "loyalty_claim_created_at"=>date("Y-m-d H:i:s"),
                "loyalty_claim_clinic_id"=>$actionDetails->loyalty_action_clinic_id,
                "loyalty_claim_action_id"=>$actionDetails->loyalty_action_id
                // "loyalty_claim_reason"=>$_REQUEST['reason'],
            );
            $rs =$loyalty_claimObj->addRecords($loyalty_claimData);

            $dependantObj = new dependant();
            $dependant =$dependantObj->fromId($actionDetails->loyalty_action_dependant_id);
            if($dependant->dependant_member_email!="" && !is_null($dependant->dependant_member_email)){
                $mailArray =array(
					"name" =>w::d($dependant->dependant_fname)." ".w::d($dependant->dependant_lname),
                    "claimNumber"=>$actionDetails->loyalty_action_claimNumber,
                    "points"=>$actionDetails->loyalty_action_rpoints
				);
				$subject = emailTemplates::subject('approveClaim');
				$mailText = emailTemplates::approveClaim($mailArray);
                sendMail("no-reply@xcarepro.com","XcarePro",$dependant->dependant_member_email,"",$subject,$mailText);
            }    
            echo json_encode(array('error'=>false,'msg'=>'Success.'));
            exit;
        }
    }
    exit;
}
if (isset($_REQUEST['act']) && $_REQUEST['act']=='declineClaim' && isset($_REQUEST['id']) && $_REQUEST['id']!=""){
    if($_REQUEST['reason']==""){
        echo json_encode(array('error'=>true,'msg'=>'Please enter the reason.'));
        exit;
    }
    $loyalty_actionObj = new loyalty_action();
    $actionDetails =$loyalty_actionObj->fromId($_REQUEST['id']);
    if($actionDetails && $actionDetails->loyalty_action_status=='Claimed'){
        $loyalty_actionData =array(
            "loyalty_action_status"=>"Declined",
            "loyalty_action_action"=>"Verified",
            "loyalty_action_verified_from"=>"Xcare Admin",
            "claim_verified_by"=>$_SESSION['admin_id'],
            "claim_verified_at"=>date("Y-m-d H:i:s"),
            "claim_reason"=>$_REQUEST['reason'],
        );
        $rs =$loyalty_actionObj->updateRecords($loyalty_actionData,$actionDetails->loyalty_action_id);
        if($rs){
            $loyalty_claimObj = new loyalty_claim();
            $loyalty_claimData =array(
                "loyalty_claim_status"=>"Declined",
                "loyalty_claim_verified_from"=>"Xcare Admin",
                "loyalty_claim_claimNumber"=>$actionDetails->loyalty_action_claimNumber,
                "loyalty_claim_claimAppointment"=>$actionDetails->loyalty_action_claimAppointment,
                "loyalty_claim_receipt"=>$actionDetails->claim_receipt1,
                "loyalty_claim_verified_by"=>$_SESSION['admin_id'],
                "loyalty_claim_verified_at"=>date("Y-m-d H:i:s"),
                "loyalty_claim_created_at"=>date("Y-m-d H:i:s"),
                "loyalty_claim_reason"=>$_REQUEST['reason'],
                "loyalty_claim_clinic_id"=>$actionDetails->loyalty_action_clinic_id,
                "loyalty_claim_action_id"=>$actionDetails->loyalty_action_id
            );
            $rs =$loyalty_claimObj->addRecords($loyalty_claimData);

            $dependantObj = new dependant();
            $dependant =$dependantObj->fromId($actionDetails->loyalty_action_dependant_id);
            
            if($dependant->dependant_member_email!="" && !is_null($dependant->dependant_member_email)){
                $mailArray =array(
					"name" =>w::d($dependant->dependant_fname)." ".w::d($dependant->dependant_lname),
                    "claimNumber"=>$actionDetails->loyalty_action_claimNumber,
                    "reason"=>$loyalty_claimData['loyalty_claim_reason']
				);
				$subject = emailTemplates::subject('declineClaim');
				$mailText = emailTemplates::declineClaim($mailArray);
                sendMail("no-reply@xcarepro.com","XcarePro",$dependant->dependant_member_email,"",$subject,$mailText);
            }   
            echo json_encode(array('error'=>false,'msg'=>'Success.'));
            exit;
        }
    }
    exit;
}
if (isset($_REQUEST['act']) && $_REQUEST['act']=='resubmitClaim' && isset($_REQUEST['id']) && $_REQUEST['id']!=""){
    if($_REQUEST['reason']==""){
        echo json_encode(array('error'=>true,'msg'=>'Please enter the reason.'));
        exit;
    }
    $loyalty_actionObj = new loyalty_action();
    $actionDetails =$loyalty_actionObj->fromId($_REQUEST['id']);
    if($actionDetails && $actionDetails->loyalty_action_status=='Claimed'){
        $loyalty_actionData =array(
            "loyalty_action_status"=>"Allocated",
            "loyalty_action_action"=>"Claim",
            "loyalty_action_verified_from"=>"Xcare Admin",
            "claim_verified_by"=>$_SESSION['admin_id'],
            "claim_verified_at"=>date("Y-m-d H:i:s"),
            "claim_reason"=>$_REQUEST['reason'],
            // "loyalty_action_claimAppointment"=>0,
        );
        $rs =$loyalty_actionObj->updateRecords($loyalty_actionData,$actionDetails->loyalty_action_id);
        if($rs){
            $loyalty_claimObj = new loyalty_claim();
            $loyalty_claimData =array(
                "loyalty_claim_status"=>"Resubmit",
                "loyalty_claim_verified_from"=>"Xcare Admin",
                "loyalty_claim_claimNumber"=>$actionDetails->loyalty_action_claimNumber,
                "loyalty_claim_claimAppointment"=>$actionDetails->loyalty_action_claimAppointment,
                "loyalty_claim_receipt"=>$actionDetails->claim_receipt1,
                "loyalty_claim_verified_by"=>$_SESSION['admin_id'],
                "loyalty_claim_verified_at"=>date("Y-m-d H:i:s"),
                "loyalty_claim_created_at"=>date("Y-m-d H:i:s"),
                "loyalty_claim_reason"=>$_REQUEST['reason'],
                "loyalty_claim_clinic_id"=>$actionDetails->loyalty_action_clinic_id,
                "loyalty_claim_action_id"=>$actionDetails->loyalty_action_id
            );
            $rs =$loyalty_claimObj->addRecords($loyalty_claimData);

            $dependantObj = new dependant();
            $dependant =$dependantObj->fromId($actionDetails->loyalty_action_dependant_id);
            if($dependant->dependant_member_email!="" && !is_null($dependant->dependant_member_email)){
                $mailArray =array(
					"name" =>w::d($dependant->dependant_fname)." ".w::d($dependant->dependant_lname),
                    "claimNumber"=>$actionDetails->loyalty_action_claimNumber,
                    "reason"=>$loyalty_claimData['loyalty_claim_reason']
				);
				$subject = emailTemplates::subject('resubmitClaim');
				$mailText = emailTemplates::resubmitClaim($mailArray);
                sendMail("no-reply@xcarepro.com","XcarePro",$dependant->dependant_member_email,"",$subject,$mailText);
            }   
            echo json_encode(array('error'=>false,'msg'=>'Success.'));
            exit;
        }
    }
    exit;
}
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['search_input']) && $_REQUEST['search_input']!=''){
    // $where = " ( clinic_name LIKE '".$_REQUEST['search_input']."%' OR clinic_name LIKE '".$_REQUEST['search_input']."%' )";
    // }
    
    // //$orderFields = array('','admin_fname','admin_email_address','admin_type','admin_created_date','admin_last_login_date');    
    
    // // 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_id asc ");
    // }
    // $pointmasterObj = new pointmaster();
    $loyalty_actionObj = new loyalty_action();
    $tables = array('tbl_clinic c','tbl_dependant d');
    $jfields = array('c.clinic_id = l.loyalty_action_clinic_id','d.dependant_id=l.loyalty_action_dependant_id');
    $jtype   = array("INNER","INNER"); 
    $where =" loyalty_action_status='Claimed' ";
    if($_REQUEST['clinic_id']!=""){
        $where .=" AND loyalty_action_clinic_id='".$_REQUEST['clinic_id']."' ";
    }else{
        $where .=" AND loyalty_action_clinic_id!='0' ";
    }
    
    if($_REQUEST['claimNumber']!=""){
        $where .=" AND loyalty_action_claimNumber='".$_REQUEST['claimNumber']."' ";
    }
    $arg =array('where'=>$where,'joinTables' =>$tables,'joinFields' =>$jfields,'joinTypes' => $jtype,'limit' => $limit);
    $allClaim =$loyalty_actionObj->getAllRecords($arg);

    // //print_r($orderby);exit;
    // $data    = array('where' => $where,'limit' => $limit, 'orderBy'=>$orderby);
    // $pointmasterDetails = $pointmasterObj->getAllRecords($data);
    // //print_r($adminDetails);exit;
    $recordsTotal   =   $loyalty_actionObj->getAllCount($arg);

    if ($allClaim && count($allClaim)>0){
        foreach ($allClaim as $key => $claim){
            $clinicName  =$claim->clinic_name;
            $services="";
            if($claim->loyalty_action_appointment_id!=0){
                $appointmentObj = new appointment();
                $requested =$appointmentObj->fromId($claim->loyalty_action_appointment_id);
                $services .="<b>Requested Appointment: </b>
                <br/>Date:".$requested->appointment_date."
                <br/>Time:".$requested->appointment_time;
            }
            if($claim->loyalty_action_claimAppointment!=0){
                $appointmentObj = new appointment();
                $claimAppointment =$appointmentObj->fromId($claim->loyalty_action_claimAppointment);
                $services .="<br/><b>Claimed Appointment: </b>
                <br/>Date:".$claimAppointment->appointment_date."
                <br/>Time:".$claimAppointment->appointment_time;    
            }
            if($claim->loyalty_action_type=="REVIEW"){
                $services ="<b>Service Date: </b>".date("Y-m-d", strtotime($claim->loyalty_action_created_at));
            }
            $view ="";
            if($claim->claim_receipt1!='' && is_array(json_decode($claim->claim_receipt1,1))){
                $img =json_decode($claim->claim_receipt1,1);
                 $view ="<a target='_blank' href='".SITE_URL."/uploads/receipt/".$img['name'].".".$img['type']."'>View</a>";
            }
            $comment=$claim->claim_reason;
            $status = '<a href="javascript:void(0)" title="Approve Claim" onclick="approveClaim('.$claim->loyalty_action_id.')"><span class="badge badge-success">Approve</span></a>';
            $status .= ' <a href="javascript:void(0)" title="Decline Claim"><span class="badge badge-danger" onclick="declineClaim('.$claim->loyalty_action_id.')">Decline</span></a>';
            $status .= ' <a href="javascript:void(0)" title="Resubmit Claim"><span class="badge badge-warning" onclick="resubmitClaim('.$claim->loyalty_action_id.')">Resubmit</span></a>';
            $response[] = array(
                    "<b>".$claim->loyalty_action_claimNumber."</b><br/>
                    <b>Patient :".w::d($claim->dependant_fname)." ".w::d($claim->dependant_lname),
                    $claim->claim_date,
                    $claim->loyalty_action_type." - ".$claim->loyalty_action_source,
                    $services,
                    $view,
                    $status
             );

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