<?php 
//=====================================================================================
//INICIO LA SESION---------------------------------------------------------------------
//=====================================================================================
session_start();
//=====================================================================================
//----------------VARIBLES-------------------------------------------------------------
//=====================================================================================
$user_gestor_v = $_SESSION["user_gestor_v"];
$code_tramite = $_SESSION["code_tramite"];
$html = "";
$row = '';
$estado = '';
switch ($_POST["id_estado"]) {
    case 1:
        $estado = "INICIAL";
        break;
    case 2:
        $estado = "EN TRAFICO";
        break;
    case 3:
        $estado = "TERMINADO";
        break;
    case 4:
        $estado = "RETENIDO";
        break;
}
$url = 'http://api.gestor.dealerbest.com/api/expedientes';
$data = array (
    "cifdni" => $user_gestor_v,
    "user_id" => "1234",
    "colaborador_id" => "98765",
    "tipo_gestion" => $code_tramite,
    "estado" => $estado
);
$headers = [];
$json_file_decode = curlPostData($url,$data,$headers);
//=====================================================================================
//----------------ACCIONES-------------------------------------------------------------
//=====================================================================================
//=====================================================================================
for($i=0;$i<count($json_file_decode->data);$i++){ 
    $fch_prev = date_create(str_replace("-", "/", $json_file_decode->data[$i]->fecha));
    $fch = date_format($fch_prev,"d-m-Y");
    $row .= '
    <tr>
        <td class="budget text-white">
            '.$json_file_decode->data[$i]->expediente.'
        </td>
        <td class="budget text-white">
            '.$fch.'
        </td>
        <td class="budget text-white">
            '.$json_file_decode->data[$i]->matricula.'
        </td>
        <td class="budget text-white">
            '.$json_file_decode->data[$i]->bastidor.'
        </td>
        <td class="budget text-white">
            '.$json_file_decode->data[$i]->estado.'
        </td>
        <td class="budget text-white">
            <button onClick="javascript:consultaExpedientesDetalle('.$json_file_decode->data[$i]->idExpediente.');" class="btn btn-icon btn-outline-primary" type="button">
                <span class="btn-inner--icon"><i class="fas fa-search"></i></span>
            </button>
        </td>
    </tr>
    ';
}
$html .= '
<div class="container-fluid">
    <div class="row">
        <div class="col-sm-12 col-md-12 col-lg-12">
            <div class="card bg-teal shadow">
                <div class="card-header bg-transparent border-0">
            </div>
            <div class="card-body">
                <div class="row">
                    <div class="col-lg-12">
                        <div class="table-responsive">
                            <table id="tabla_expedientes" class="table table-flush  align-items-center" id="datatable-basic">
                                <thead class="text-center">
                                    <tr>
                                        <th scope="col" class="sort text-white" data-sort="name">Expedientes</th>
                                        <th scope="col" class="sort text-white" data-sort="status">Fecha</th>
                                        <th scope="col" class="sort text-white" data-sort="status">Matrícula</th>
                                        <th scope="col" class="sort text-white" data-sort="budget">Bastidor</th>
                                        <th scope="col" class="sort text-white" data-sort="status">Estado</th>
                                        <th scope="col"></th>
                                    </tr>
                                </thead>
                                <tbody class="text-center">
                                    '.$row.'
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
';
$r_code = 400;
$r_msg = "success : request success";
$r_html = $html;
//RESPUESTA============================================================================
$response[] = array ( 
    "code" => $r_code,
    "message" => $r_msg,
    "html" => $r_html
);
header("Content-type: application/json; charset=utf-8");
echo json_encode($response);
//=====================================================================================
//----------------FUNCIONES------------------------------------------------------------
//=====================================================================================
function curlPostData($url,$data,$headers) {
    $ch = curl_init();
    $timeout = 30;
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    //curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $response = curl_exec ($ch);

    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
    }
    curl_close ($ch);
    return json_decode($response);
}
?>