<?php 
// Include main file
include_once('../libs/main.php');
// Check admin Login
include_once('checkLogin.php');
// Logical section of page
$cconsultationObj =  new  cconsultation();

if(isset($_REQUEST['act']) && $_REQUEST['act']=='change_status' && isset($_REQUEST['cconsultation_id'])){
    if(isset($_REQUEST['status'])){
        $cconsultationObj->changeStatus($_REQUEST['cconsultation_id']);
        echo RECORD_STATUS;
    }
    return true;
}

if(isset($_REQUEST['act']) && $_REQUEST['act']=='delete' && isset($_REQUEST['cconsultation_id'])){
    //echo $_REQUEST['cconsultation_id'];exit;
    //$cconsultationObj->deleteFromId($_REQUEST['cconsultation_id']);

    $cconsultation_id = $_REQUEST['cconsultation_id'];

    $tables = array('tbl_clinicSchedule_detail sd','tbl_clinicSchedule sc');
    $jfields = array(
        'c.cconsultation_id = sd.clinicSchedule_detail_consultation_id',
        'sd.clinicSchedule_detail_schedule_id = sc.clinicSchedule_id'

    );
    $jtype   = array("INNER");

    $where = "sd.clinicSchedule_detail_consultation_id = '$cconsultation_id'";

    $data = array(
        'where' => $where,
        'joinTables' => $tables,
        'joinFields' => $jfields,
        'joinTypes' => $jtype
    );
    //$data['debug'] = true;
    $detailsBeforeDelete = $cconsultationObj->getAllRecords($data);
    //print_r($detailsBeforeDelete);exit;
    if($detailsBeforeDelete && count($detailsBeforeDelete)>0){
        foreach ($detailsBeforeDelete as $key => $consultationData){

            $clinicScheduleObj =  new  clinicSchedule();
            $clinicScheduleObj->deleteFromId($consultationData->clinicSchedule_detail_schedule_id);


            $clinicSchedule_detailObj =  new  clinicSchedule_detail();
            $clinicSchedule_detailObj->deleteFromId($consultationData->clinicSchedule_detail_id);
            
        }
    }
    
    $cconsultationObj->deleteFromId($_REQUEST['cconsultation_id']);

    $successMsg = base64_encode(RECORD_DELETE);
}
if(isset($_REQUEST['action']) && $_REQUEST['action']!=''){
    if($_REQUEST['action']=="activate_all"){
        $data=array("cconsultation_status" => '1');
        $chk_data = $_POST['check'];
        $rs = $cconsultationObj->updateManyRecords($data, $_POST['check']);
        $successMsg = base64_encode(RECORD_ALL_ACTIVE);
    }else if($_REQUEST['action']=="inactivate_all"){
        $data=array("cconsultation_status" => '0');
        $chk_data = $_POST['check'];
        $rs = $cconsultationObj->updateManyRecords($data, $_POST['check']);
        $successMsg = base64_encode(RECORD_ALL_INACTIVE);
    }else if($_REQUEST['action']=="delete_all"){
        $chk_del = implode(",",$_POST['check']);
        $rs = $cconsultationObj->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['cconsultation_speciality_id']) && $_REQUEST['cconsultation_speciality_id']!=""){
         $speciality_id = $_REQUEST['cconsultation_speciality_id'];
         $where .= " and cconsultation_speciality_id='".$speciality_id."' "; 
    }

    if(isset($_REQUEST['cconsultation_speciality_name']) && $_REQUEST['cconsultation_speciality_name']!=""){
         
         $where .= " and cconsultation_name='".$_REQUEST['cconsultation_speciality_name']."' "; 
    }

    if(isset($_REQUEST['cconsultation_price']) && $_REQUEST['cconsultation_price']!=""){
         
         $where .= " and cconsultation_price='".$_REQUEST['cconsultation_price']."' "; 
    }

    if(isset($_REQUEST['cconsultation_status']) && $_REQUEST['cconsultation_status']!=""){
         
         $where .= " and cconsultation_status='".$_REQUEST['cconsultation_status']."' "; 
    }

    if(isset($_REQUEST['clinic_id']) && $_REQUEST['clinic_id']!=""){
         
         $where .= " and cconsultation_clinic_id='".$_REQUEST['clinic_id']."' "; 
    }

    if (isset($_REQUEST['search']['value']) && $_REQUEST['search']['value']!=""){
        $where .= " AND (cconsultation_name LIKE '%".$_REQUEST['search']['value']."%' )";
    }    
        
    $orderFields = array('cconsultation_id','cconsultation_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(" cconsultation_id desc ");
    }

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

    $cconsultationDetails = $cconsultationObj->getAllRecords($data);
    $recordsTotal   =   $cconsultationObj->getAllCount($data);

        
    if ($cconsultationDetails && count($cconsultationDetails)>0){
        foreach ($cconsultationDetails as $key => $cconsultation){

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

            $action = '<a href="clinic_consultationadd.php?act=edit&cconsultation_id='.$cconsultation->cconsultation_id.'&clinic_id='.$cconsultation->cconsultation_clinic_id.'" class="btn btn-success white-btn"><i class="nav-icon i-Pen-2 "></i></a>
                <br><br><a href="clinic_consultationlist.php?act=delete&cconsultation_id='.$cconsultation->cconsultation_id.'&clinic_id='.$cconsultation->cconsultation_clinic_id.'" class="btn btn-danger white-btn"><i class="nav-icon i-Close-Window "></i></a>';
            $domainObj = new domain();
            $domainDetails =$domainObj->fromId($cconsultation->cconsultation_domain_id);

            $specialityObj = new speciality();
            $specialityObj->resultAs ="array";
            $fields=array('speciality_name');
            $where = "speciality_id IN (".$cconsultation->cconsultation_speciality_id.")";
            $arg = array('where'=>$where);
            $allspecialities  =$specialityObj->getAllRecords($arg);
            $specialities =array_column($allspecialities, "speciality_name");
             $response[] = array('<input type="checkbox" name="check[]" id="check[]" value="'.$cconsultation->cconsultation_id.'" class="check_class" />',  
                 $domainDetails->domain_title,
                 implode(",", $specialities),   
                 $cconsultation->cconsultation_name,
                 $cconsultation->cconsultation_desc,
                 $cconsultation->cconsultation_duration,
                 $cconsultation->cconsultation_price,
                 "<div id='td_status_".$cconsultation->cconsultation_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;
}


$clinicObj = new clinic();
$clinicDetails = $clinicObj->fromId($_REQUEST['clinic_id']);
// Include Template 
include_once('../libs/routs.php')
?>