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

// Logical section of page
$payClinicObj =  new  payClinic();

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

$filename=basename($_SERVER['SCRIPT_FILENAME']);
if($filename == 'payClinicList.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']=='change_status' && isset($_REQUEST['id'])){
    if(isset($_REQUEST['status'])){

        $clinicDetail = $payClinicObj->fromId($_REQUEST['id']);
        if($clinicDetail->status == '1')
        {
            $old_status = 'Active';
        }else{
            $old_status = 'Inactive';
        }

        if($_REQUEST['status'] == '1')
        {
            $new_status = 'Active';
        }else{
            $new_status = 'Inactive';
        }

        $payClinicObj->changeStatus($_REQUEST['id']);

        $clinic_detail = " <b>Pay Clinic Status</b>: "." ".$old_status." <b>To</b> ".$new_status." ";
        $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$clinic_detail);

        echo RECORD_STATUS;
    }
    return true;
}
if(isset($_REQUEST['act']) && $_REQUEST['act']=='delete' && isset($_REQUEST['id'])){
    $payClinicObj->deleteFromId($_REQUEST['id']);
    $successMsg = base64_encode(RECORD_DELETE);
}

if(isset($_REQUEST['action']) && $_REQUEST['action']!=''){
    
    if($_REQUEST['action']=="activate_all"){
        $data=array("status" => '1');

        $chk_data = $_POST['check'];
        foreach($chk_data as $akey => $val_actchk)
        {
            $old_data = $payClinicObj->fromId($val_actchk);
            if($old_data->status == '1')
            {
                $old_status = 'Active';
            }else{
                $old_status = 'Inactive';
            }

            if($old_data->status != '1')
            {
                $new_status = 'Active';
                $clinicdata = " <b>Status</b>: "." ".$old_status." <b>To</b> ".$new_status." ";
                $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$clinicdata);
            }
        }

        $rs = $payClinicObj->updateManyRecords($data, $_POST['check']);
        $successMsg = base64_encode(RECORD_ALL_ACTIVE);
    }else if($_REQUEST['action']=="inactivate_all"){
        $data=array("status" => '0');

        $chk_data = $_POST['check'];
        foreach($chk_data as $akey => $val_actchk)
        {
            $old_data = $payClinicObj->fromId($val_actchk);
            if($old_data->status == '1')
            {
                $old_status = 'Active';
            }else{
                $old_status = 'Inactive';
            }

            if($old_data->status != '0')
            {
                $new_status = 'Inactive';
                $clinicdata = " <b>Status</b>: "." ".$old_status." <b>To</b> ".$new_status." ";
                $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$clinicdata);
            }
        }

        $rs = $payClinicObj->updateManyRecords($data, $_POST['check']);
        $successMsg = base64_encode(RECORD_ALL_INACTIVE);
    }else if($_REQUEST['action']=="delete_all"){

        $chk_del = implode(",",$_POST['check']);
        
        $clinicdata = " <b>Delete Id</b>: "." ".$chk_del;
        $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$clinicdata);
        
        $rs = $payClinicObj->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['name']) && $_REQUEST['name'] != '')
    {
        $where .= " AND name LIKE '%".$_REQUEST['name']."%'";
    }
    if(isset($_REQUEST['email']) && $_REQUEST['email'] != '')
    {
        $where .= " AND email LIKE '%".$_REQUEST['email']."%'";
    }
    if(isset($_REQUEST['phone']) && $_REQUEST['phone'] != '')
    {
        $where .= " AND phone LIKE '%".$_REQUEST['phone']."%'";
    }
    if(isset($_REQUEST['status']) && $_REQUEST['status']!=""){
         $where .= " and status='".$_REQUEST['status']."' "; 
    }


    if (isset($_REQUEST['search']['value']) && $_REQUEST['search']['value']!=""){
        $where = " name LIKE '%".$_REQUEST['search']['value']."%' OR email LIKE '%".$_REQUEST['search']['value']."%' OR phone LIKE '%".$_REQUEST['search']['value']."%' ";
    }    
        
    $orderFields = array('','name','email', 'phone', 'formattedAddress');
    
    // 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(" id desc ");
    }
    $data    = array('where' => $where, 'orderBy' => $orderby, 'limit' => $limit);

    $payClinicDetails = $payClinicObj->getAllRecords($data);
    $recordsTotal   =   $payClinicObj->getAllCount($data);
        
    if ($payClinicDetails && count($payClinicDetails)>0){
        foreach ($payClinicDetails as $key => $clinic){

            if($clinic->status =='1'){
              $status = '<a href="javascript:void(0)" onclick="change_status('.$clinic->id.',0)" title="Inactivate"><span class="badge badge-success">Active</span></a>';
            }else{
              $status = '<a href="javascript:void(0)" onclick="change_status('.$clinic->id.',1)" title="Activate"><span class="badge badge-danger">Inactive</span></a>';
            }
           
            if($clinic->updatedAt != '' && $clinic->updatedAt != '0000-00-00 00:00:00'){
                $updatedAt =  date('Y-m-d',strtotime($clinic->updatedAt));
            }else{
                $updatedAt = '';
            } 
            $response[] = array('<input type="checkbox" name="check[]" id="check[]" value="'.$clinic->id.'" class="check_class" />', 
                    $clinic->name,
                    $clinic->email,
                    $clinic->phone,
                    $clinic->formattedAddress,
                    date('Y-m-d',strtotime($clinic->createdAt)),
                    $updatedAt,
                    "<div id='td_status_".$clinic->id."'>".$status."</div>",    
                    '<a href="payClinicAdd.php?act=edit&id='.$clinic->id.'" class="btn btn-success white-btn"><i class="nav-icon i-Pen-2 "></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');
?>
