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

// Object
$docObj = new documentation();

if (isset($_REQUEST['act']) && $_REQUEST['act']=='load') {
    
    $records = $_REQUEST['length'];
    $page = isset($_GET['start']) ? intval($_GET['start']) : 0;
    $limit = array('start'=>$page,'record'=>$records);
    
    $where = " 1=1 ";
    
    if(isset($_REQUEST['id']) && $_REQUEST['id'] != '') {
        $where .= " AND id = '".intval($_REQUEST['id'])."'";
    }
    
    if(isset($_REQUEST['title']) && $_REQUEST['title'] != '') {
        $where .= " AND title LIKE '%".$_REQUEST['title']."%' ";
    }
    
    if(isset($_REQUEST['status']) && $_REQUEST['status'] != '') {
        $where .= " AND status = '".intval($_REQUEST['status'])."' ";
    }
    
    if (isset($_REQUEST['search']['value']) && $_REQUEST['search']['value']!="") {
        $search = $_REQUEST['search']['value'];
        $where = " id LIKE '%$search%' OR title LIKE '%$search%' ";
    }    
    
    $orderFields = array('id','title');    
    
    // Array of order by 
    $orderby = array();
    if (isset($_REQUEST['order']['0']['column']) && $_REQUEST['order']['0']['column']!="") {
        $colIndex = $_REQUEST['order']['0']['column'];
        $orderby = array(" ".$orderFields[$colIndex]." ".$_REQUEST['order']['0']['dir']);
    } else {
        $orderby = array(" id desc ");
    }

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

    $docDetails = $docObj->getAllRecords($data);
    $recordsTotal = $docObj->getAllCount($data);

    if ($docDetails && count($docDetails)>0) {
        foreach ($docDetails as $key => $doc) {
            
            if($doc->status == '1'){
              $status = '<span class="badge badge-success">Active</span>';
            }else{
              $status = '<span class="badge badge-danger">Inactive</span>';
            }
            
            $action = '<a href="documentationadd.php?act=edit&id='.$doc->id.'" class="btn btn-success white-btn"><i class="nav-icon i-Pen-2 "></i></a>';

            //"<div id='td_status_".$doc->id."'>".$status."</div>",
            $response[] = array(
                '<input type="checkbox" name="check[]" value="'.$doc->id.'" class="check_class" />',
                $doc->id,
                $doc->title,
                $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');
?>
