<?php 
    //cms/api/ocupations/list

    header("Content-Type:application/json");
    include("../../../settings/dbconn.php");
    include("../../../settings/utils.php");
    
    // parametros obligatorios
    $parmsob = array("sessionid",'order','offset','numofrec'); 
    if (!parametrosValidos($_GET, $parmsob)){
        badEnd("400", array("msg"=>"Parametros obligatorios " . implode(", ", $parmsob)));
    }
    
    //Declaracion de variables
    $out = new stdClass();
    $sessionid = mysqli_real_escape_string($db, $_GET["sessionid"]);
    $order = determinateOrder(mysqli_real_escape_string($db, $_GET['order']));
    $offset = mysqli_real_escape_string($db, $_GET['offset']);
    $numofrec = mysqli_real_escape_string($db, $_GET['numofrec']);
    $filter = '';
    
    //Validamos la session
    isSessionValidCMS($db, $sessionid);
    
    //Validamos que el usuario tenga privilegios
    if(!tienePrivilegio($db, $sessionid, 65))
        badEnd("403", array("msg"=>"No tiene Privilegios"));
        
    // Si obtenemos el filter, armamos el query
    if(isset($_GET['filter']) && $_GET['filter']){
        // Iniciamos la cadena del query
        $filter = " WHERE ( ";
        
        //Separamos el filtro por el simbolo +
        $filterEl = separateFilter($_GET["filter"]);
        
        //Recorremos cada elemento
        foreach($filterEl as $index => $el){
            if($index == 0){
                $filter .= " dsc LIKE '%".$el."%' OR dsc_eng LIKE '%".$el."%'";
            }else{
                $filter .= " OR dsc LIKE '%".$el."%' OR dsc_eng LIKE '%".$el."%'";
            }
        }
        
        $filter .= " ) ";
    }
        
    // Consultamos la cantidad registros
    $sql = "SELECT COUNT(id) AS qty ".
    "       FROM ocupations ".
    "       ".$filter;
    if (!$rs=$db->query($sql))
        badEnd("500", array("msg"=>determinateDBError($db)));
        
    $row = $rs->fetch_assoc();
    //Retornamos el total
    $out->numofrecords = (int)$row["qty"];
    
    // Consultamos los detalles de los registros
    $sql = "SELECT * FROM ocupations ".
    "       ".$filter.
    "       ".$order.
    "       LIMIT ".$offset.", ".$numofrec;
    if (!$rs=$db->query($sql))
        badEnd("500", array("msg"=>determinateDBError($db)));
    
    while($row=$rs->fetch_assoc()){
        $record = new stdClass();
        
        $record->id = (int)$row['id'];
        $record->dsc = $row['dsc'];
        $record->dsc_eng = $row['dsc_eng'] == null ? '' : $row['dsc_eng'];
        
        $records [] = $record;
    }
    
    $out->records = $records;
        
    //Privilegios del usuario actual CMS
    $access = new stdClass;
    $access->read = tienePrivilegio($db, $sessionid, 65);
    $access->update = tienePrivilegio($db, $sessionid, 63);
    $access->delete = tienePrivilegio($db, $sessionid, 64);
    $access->create = tienePrivilegio($db, $sessionid, 62);
    $out->privileges = $access;
    
    header("HTTP/1.1 200");
    echo (json_encode($out));
    die();
    
    
    /*  Funcion para determinar el orden de los registros, donde si:
    *   1 = nombre ASC
    *   2 = ID ASC
    *   -1 = nombre DESC
    *   -2 = ID DESC
    */
    function determinateOrder($order){
        $str = '';
        
        switch($order){
            case 1:
                $str = ' ORDER BY dsc ASC ';
                break;
            case 2:
                $str = ' ORDER BY id ASC ';
                break;
            case -1:
                $str = ' ORDER BY dsc DESC ';
                break;
            case -2:
                $str = ' ORDER BY id DESC ';
                break;
        }
        
        return $str;
    }
?>