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

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

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

            $hygieneDetail = $hygieneObj->fromId($_REQUEST['hygiene_id']);
            if($hygieneDetail->hygiene_status == '1')
            {
                $old_status = 'Active';
            }else{
                $old_status = 'Inactive';
            }

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

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

if(isset($_REQUEST['act']) && $_REQUEST['act']=="order" && $_REQUEST['hygiene_order']!="" && $_REQUEST['position']!=""){
    $select =array('hygiene_order as neworder','hygiene_id as newid');
    $id=$_REQUEST['hygiene_id'];
    $order=$_REQUEST['hygiene_order'];
    if ($_REQUEST['position']=="Up"){
        $orderby =array('hygiene_order','desc');
        $where['hygiene_order<']=$_REQUEST['hygiene_order'];
    }else{
        $orderby =array('hygiene_order','asc');
        $where['hygiene_order>']=$_REQUEST['hygiene_order'];
    }
    
    $data =array('fields'=>$select,'where'=>$where,'orderBy'=>$orderby,'id'=>$id,'order'=>$order);
    $hygieneObj->changeOrder($data);
    header('Location:hygienelist.php?hygiene_speciality_id='.$_REQUEST['speciality_id']);
    exit;
}
if(isset($_REQUEST['act']) && $_REQUEST['act']=='delete' && isset($_REQUEST['hygiene_id'])){
    $hygieneObj->deleteFromId($_REQUEST['hygiene_id']);
    $successMsg = base64_encode(RECORD_DELETE);
}
if(isset($_REQUEST['action']) && $_REQUEST['action']!=''){
    if($_REQUEST['action']=="activate_all"){
        $data=array("hygiene_status" => '1');

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

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

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

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

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

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

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

    if (isset($_REQUEST['search']['value']) && $_REQUEST['search']['value']!=""){
        $where .= " AND (hygiene_title LIKE '%".$_REQUEST['search']['value']."%' OR  hygiene_desc LIKE '%".$_REQUEST['search']['value']."%')";
    }    
        
    $orderFields = array('hygiene_id','hygiene_title');
    
    // 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(" hygiene_order asc ");
    }

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

    $hygieneDetails = $hygieneObj->getAllRecords($data);
    $recordsTotal   =   $hygieneObj->getAllCount($data);

        
    if ($hygieneDetails && count($hygieneDetails)>0){
        foreach ($hygieneDetails as $key => $hygiene){

            if($hygiene->hygiene_status =='1'){
              $status = '<a href="javascript:void(0)" onclick="change_status('.$hygiene->hygiene_id.',0)" title="Inactivate"><span class="badge badge-success">Active</span></a>';
            }else{
              $status = '<a href="javascript:void(0)" onclick="change_status('.$hygiene->hygiene_id.',1)" title="Activate"><span class="badge badge-danger">Inactive</span></a>';
            }
            if($key!=0){
            $order = '<a title="move up" href="hygienelist.php?act=order&hygiene_id='.$hygiene->hygiene_id.'&speciality_id='.$hygiene->hygiene_speciality_id.'&hygiene_order='.$hygiene->hygiene_order.'&position=Up"><i class="nav-icon i-Arrow-Up-3"></i></a>';
            }
            if(count($hygieneDetails)!=($key+1)){
                $order .= '&nbsp;&nbsp;<a title="move down" href="hygienelist.php?act=order&hygiene_id='.$hygiene->hygiene_id.'&hygiene_order='.$hygiene->hygiene_order.'&position=down"><i class="nav-icon i-Arrow-Down-3"></i></a>';
            }
            

            // $action = '<a href="hygieneadd.php?act=edit&hygiene_id='.$hygiene->hygiene_id.'" class="btn btn-success white-btn"><i class="nav-icon i-Pen-2 "></i></a>
            //      <a id="delete-confirm" onclick="delete_entry('.$hygiene->hygiene_id.');" class="btn btn-danger white-btn"><i class="nav-icon i-Close-Window "></i></a>';
            $action = '<a href="hygieneadd.php?act=edit&hygiene_id='.$hygiene->hygiene_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="'.$hygiene->hygiene_id.'" class="check_class" />', 
                    $order ,
                 $hygiene->hygiene_title,
                 $hygiene->hygiene_desc,
                 "<div id='td_status_".$hygiene->hygiene_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');
?>