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

// Logical section of page
$modulesObj =  new  modules();
$modulepagesObj = new  module_pages();

if(isset($_REQUEST['act']) && $_REQUEST['act']=='change_status' && isset($_REQUEST['user_id'])){
    if(isset($_REQUEST['status'])){
        $userObj->changeStatus($_REQUEST['user_id']);
        echo RECORD_STATUS;
    }
    return true;
}
if(isset($_REQUEST['act']) && $_REQUEST['act']=='delete' && isset($_REQUEST['id'])){
    $modulesObj->deleteFromId($_REQUEST['id']);
    echo RECORD_DELETE;

}
if(isset($_REQUEST['action']) && $_REQUEST['action']!=''){
    if($_REQUEST['action']=="activate_all"){
        $data=array("user_status" => '1');
        $rs = $userObj->updateManyRecords($data, $_POST['check']);
        $successMsg = base64_encode(RECORD_ALL_ACTIVE);
    }else if($_REQUEST['action']=="inactivate_all"){
        $data=array("user_status" => '0');
        $rs = $userObj->updateManyRecords($data, $_POST['check']);
        $successMsg = base64_encode(RECORD_ALL_INACTIVE);
    }else if($_REQUEST['action']=="delete_all"){
        $rs = $userObj->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['modulename']) && $_REQUEST['modulename'] != '')
    {
        $where .= " AND name LIKE  '%".$_REQUEST['modulename']."%'";
    }
    
    if (isset($_REQUEST['search']['value']) && $_REQUEST['search']['value']!=""){
        $where = " id LIKE '%".$_REQUEST['search']['value']."%' OR  name LIKE '%".$_REQUEST['search']['value']."%' ";
    }    
        
    $orderFields = array('id');
    
    // 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 ");
    }
   
    /*$fields = array('m.id','m.name','mp.module_id');
    $tables = array('tbl_module_pages mp');
    $jfields = array('mp.module_id = m.id');
    $jtype   = array("INNER");
    $groupBy = array("module_id");
    $data   = array('where' => $where,  'joinTables' =>$tables, 'joinFields' =>$jfields, 'joinTypes' => $jtype,"groupBy"=>$groupBy,'limit' => $limit);*/

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

    $moduleDetails = $modulesObj->getAllRecords($data);
    //print_r($moduleDetails);exit;
    $recordsTotal   =   $modulesObj->getAllCount($data);
     
    if ($moduleDetails && count($moduleDetails)>0){
        foreach ($moduleDetails as $key => $moduledata)
        {
            
            if(isset($moduledata->id) && $moduledata->id != '')
            {
                
                $where = "module_id='".$moduledata->id."'";
                $moduledataArg = array('where' => $where);
                $modulepage = $modulepagesObj->getAllRecords($moduledataArg);
                //print_r($modulepage);exit;
                $modulepageName = "";
                foreach ($modulepage as $mkey => $mpvalue) 
                {
                    $modulepageName .= $mpvalue->page."\n";
                }
            }else{
                $modulepageName = "";
            }
            
            $response[] = array( 
                    $moduledata->id,
                    $moduledata->name,
                    $modulepageName,
                    '<a href="moduleadd.php?act=edit&id='.$moduledata->id.'" class="btn btn-success white-btn"><i class="nav-icon i-Pen-2 "></i></a>',
                   // date('Y-m-d',strtotime($dependant->dependant_created_at))
            );
        }
        $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');
?>