<?php 
// Include main file
include_once('../libs/main.php');
// Check speciality Login
include_once('checkLogin.php');
// Logical section of page
$specialityObj =  new  speciality();
$adminlogObj = new admin_log();
$modulepagesObj = new module_pages();

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

        $specialityDetail = $specialityObj->fromId($_REQUEST['speciality_id']);
        if($specialityDetail->speciality_status == '1')
        {
            $old_status = 'Active';
        }else{
            $old_status = 'Inactive';
        }

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

        $specialityObj->changeStatus($_REQUEST['speciality_id']);
        $speciality_detail = " <b>Speciality status</b>: "." ".$old_status." <b>To</b> ".$new_status." ";
        $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$speciality_detail);
        echo RECORD_STATUS;
    }
    return true;
}

if(isset($_REQUEST['act']) && $_REQUEST['act']=='change_enable_disable' && isset($_REQUEST['speciality_id'])){
    if(isset($_REQUEST['status'])){

        $specialityDetail = $specialityObj->fromId($_REQUEST['speciality_id']);
        if($specialityDetail->speciality_enable_disable == '1')
        {
            $old_status = 'Enable';
        }else{
            $old_status = 'Disable';
        }

        if($_REQUEST['status'] == '1')
        {
            $new_status = 'Enable';
            $enable_status = '1';
        }else{
            $new_status = 'Disable';
            $enable_status = '0';
        }

        $specialitiesData = array(
                        'speciality_enable_disable'=>$enable_status,
                    );
        $specalityupd = $specialityObj->updateRecords($specialitiesData,$_REQUEST['speciality_id']);

        $speciality_detail = " <b>Speciality status</b>: "." ".$old_status." <b>To</b> ".$new_status." ";
        $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$speciality_detail);
        echo RECORD_STATUS;
    }
    return true;
}

if(isset($_REQUEST['act']) && $_REQUEST['act']=='delete' && isset($_REQUEST['speciality_id'])){
    $specialityObj->deleteFromId($_REQUEST['speciality_id']);
    $successMsg = base64_encode(RECORD_DELETE);
}
if(isset($_REQUEST['action']) && $_REQUEST['action']!=''){
    if($_REQUEST['action']=="activate_all"){
        $data=array("speciality_status" => '1');
        $chk_data = $_POST['check'];
        foreach($chk_data as $akey => $val_actchk)
        {
            $old_data = $specialityObj->fromId($val_actchk);
            if($old_data->speciality_status == '1')
            {
                $old_status = 'Active';
            }else{
                $old_status = 'Inactive';
            }

            if($old_data->speciality_status != '1')
            {
                $new_status = 'Active';
                $specialitydata = " <b>Status</b>: "." ".$old_status." <b>To</b> ".$new_status." ";
                $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$specialitydata);
            }
        }
        $rs = $specialityObj->updateManyRecords($data, $_POST['check']);
        $successMsg = base64_encode(RECORD_ALL_ACTIVE);
    }else if($_REQUEST['action']=="inactivate_all"){
        $data=array("speciality_status" => '0');

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

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

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

        $chk_del = implode(",",$_POST['check']);
        
        $specialitydata = " <b>Delete Id</b>: "." ".$chk_del;
        $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$specialitydata);

        $rs = $specialityObj->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['speciality_name']) && $_REQUEST['speciality_name'] != '')
    {
        $where .= " AND speciality_name LIKE '%".$_REQUEST['speciality_name']."%'";
    }
    if(isset($_REQUEST['speciality_slug']) && $_REQUEST['speciality_slug'] != '')
    {
        $where .= " AND speciality_slug LIKE '%".$_REQUEST['speciality_slug']."%'";
    }
    if(isset($_REQUEST['speciality_status']) && $_REQUEST['speciality_status'] != '')
    {
        $where .= " AND speciality_status LIKE '%".$_REQUEST['speciality_status']."%'";
    }

    

    if (isset($_REQUEST['search']['value']) && $_REQUEST['search']['value']!=""){
        $where = " speciality_name LIKE '%".$_REQUEST['search']['value']."%' OR speciality_slug LIKE '%".$_REQUEST['search']['value']."%' ";
    }    
    $orderFields = array('', 'speciality_name', 'speciality_slug','speciality_status');
    // 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(" speciality_id desc ");
    }
    $data    = array('where' => $where,'limit' => $limit,'orderBy' => $orderby);
    $specialityDetails = $specialityObj->getAllRecords($data);
    $recordsTotal   =   $specialityObj->getAllCount($data);

    if ($specialityDetails && count($specialityDetails)>0){
        foreach ($specialityDetails as $key => $speciality){
            if($speciality->speciality_status =='1'){
              $status = '<a href="javascript:void(0)" onclick="change_status('.$speciality->speciality_id.',0)" title="Inactivate"><span class="badge badge-success">Active</span></a>';
            }else{
              $status = '<a href="javascript:void(0)" onclick="change_status('.$speciality->speciality_id.',1)" title="Activate"><span class="badge badge-danger">Inactive</span></a>';
            }

            if($speciality->speciality_enable_disable =='1'){
              $speciality_enable_disable = '<a href="javascript:void(0)" onclick="enable_disable_status('.$speciality->speciality_id.',0)" title="Disable"><span class="badge badge-success">Enable</span></a>';
            }else{
              $speciality_enable_disable = '<a href="javascript:void(0)" onclick="enable_disable_status('.$speciality->speciality_id.',1)" title="Enable"><span class="badge badge-danger">Disable</span></a>';
            }

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

            $action = '<a href="specialityadd.php?act=edit&speciality_id='.$speciality->speciality_id.'" class="btn btn-success white-btn"><i class="nav-icon i-Pen-2 "></i></a>';
                      
            $response[] = array('<input type="checkbox" name="check[]" id="check[]" value="'.$speciality->speciality_id.'" class="check_class" />', 
                    $speciality->speciality_name,
                    $speciality->speciality_slug,
                    "<div id='td_status_".$speciality->speciality_id."'>".$speciality_enable_disable."</div>",  
                    "<div id='td_status_".$speciality->speciality_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;
}
// Include Template 
include_once('../libs/routs.php');
?>