<?php 
// Include main file
include_once('../libs/main.php');
// Check admin Login
include_once('checkLogin.php');
// Logical section of page
$appointmenttypeObj =  new  appointmenttype();
if(isset($_REQUEST['act']) && $_REQUEST['act']=='change_status' && isset($_REQUEST['appointmenttype_id'])){
    if(isset($_REQUEST['status'])){
            $appointmenttypeObj->changeStatus($_REQUEST['appointmenttype_id']);
            echo RECORD_STATUS;
    }
        return true;
}

if(isset($_REQUEST['act']) && $_REQUEST['act']=='delete' && isset($_REQUEST['appointmenttype_id'])){
    $appointmenttypeObj->deleteFromId($_REQUEST['appointmenttype_id']);
    $successMsg = base64_encode(RECORD_DELETE);
}
if(isset($_REQUEST['action']) && $_REQUEST['action']!=''){
    if($_REQUEST['action']=="activate_all"){
        $data=array("appointmenttype_status" => '1');
        $rs = $appointmenttypeObj->updateManyRecords($data, $_POST['check']);
        $successMsg = base64_encode(RECORD_ALL_ACTIVE);
    }else if($_REQUEST['action']=="inactivate_all"){
        $data=array("appointmenttype_status" => '0');
        $rs = $appointmenttypeObj->updateManyRecords($data, $_POST['check']);
        $successMsg = base64_encode(RECORD_ALL_INACTIVE);
    }else if($_REQUEST['action']=="delete_all"){
        $rs = $appointmenttypeObj->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['appointmenttype_speciality_id']) && $_REQUEST['appointmenttype_speciality_id']!=""){
         $speciality_id = $_REQUEST['appointmenttype_speciality_id'];
         $where = " appointmenttype_speciality_id='".$speciality_id."' "; 
    }
    if (isset($_REQUEST['search']['value']) && $_REQUEST['search']['value']!=""){
        $where .= " AND (appointmenttype_name LIKE '%".$_REQUEST['search']['value']."%' )";
    }    
        
    $orderFields = array('appointmenttype_id','appointmenttype_name');
    
    // 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(" appointmenttype_id desc ");
    }

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

    $appointmenttypeDetails = $appointmenttypeObj->getAllRecords($data);
    $recordsTotal   =   $appointmenttypeObj->getAllCount($data);

        
    if ($appointmenttypeDetails && count($appointmenttypeDetails)>0){
        foreach ($appointmenttypeDetails as $key => $appointmenttype){

            if($appointmenttype->appointmenttype_status =='1'){
              $status = '<a href="javascript:void(0)" onclick="change_status('.$appointmenttype->appointmenttype_id.',0)" title="Inactivate"><span class="badge badge-success">Active</span></a>';
            }else{
              $status = '<a href="javascript:void(0)" onclick="change_status('.$appointmenttype->appointmenttype_id.',1)" title="Activate"><span class="badge badge-danger">Inactive</span></a>';
            }

            $action = '<a href="appointmenttypeadd.php?act=edit&appointmenttype_id='.$appointmenttype->appointmenttype_id.'" class="btn btn-success white-btn"><i class="nav-icon i-Pen-2 "></i></a>
                 <a id="delete-confirm" onclick="delete_entry('.$appointmenttype->appointmenttype_id.');" class="btn btn-danger white-btn"><i class="nav-icon i-Close-Window "></i></a>';

             $response[] = array('<input type="checkbox" name="check[]" id="check[]" value="'.$appointmenttype->appointmenttype_id.'" class="check_class" />',  
                 $appointmenttype->appointmenttype_name,
                 $appointmenttype->appointmenttype_duration,
                 "<div id='td_status_".$appointmenttype->appointmenttype_id."'>".$status."</div>",
                 $action
                );
        }
        $result=array('recordsFiltered'=>$recordsTotal,'recordsTotal'=>$recordsTotal,'data'=>$response);
    }else{
        $result=array('recordsFiltered'=>0,'recordsTotal'=>0,'data'=>array());
    }
    echo json_encode($result);
    exit;
}
$specialityObj = new speciality();
$allspecialities = $specialityObj->getAllRecords();
// Include Template 
include_once('../libs/routs.php')
?>