<?php 
//=====================================================================================
//INICIO LA SESION---------------------------------------------------------------------
//=====================================================================================
session_start();
//=====================================================================================
//CONEXION-----------------------------------------------------------------------------
//=====================================================================================
require_once '../../../conn/conexion.php';
$bd = new connexion();
$bd->query("SET NAMES 'utf8'");
//=====================================================================================
//----------------VARIBLES-------------------------------------------------------------
//=====================================================================================
$user_gestor_v = $_SESSION["user_gestor_v"];
$html = "";
$url = 'http://api.gestor.dealerbest.com/api/stat';
$data = array (
    "cifdni" => $user_gestor_v,
    "user_id" => "1234",
    "colaborador_id" => "98765"
);
$headers = [];
$data = curlPostData($url,$data,$headers);
$counter = 1;
//=====================================================================================
//----------------ACCIONES-------------------------------------------------------------
//=====================================================================================
//=====================================================================================
$html .= '
<div class="container-fluid mt--6">
    <div class="row">
';
foreach ($data as $key => $value) {
    foreach ($value as $key2 => $value2) {
        if ($key2 == 'EN TRAFICO') {
            if($value2 == ''){$info_trafic = 0;}else{$info_trafic = $value2;}
        }elseif ($key2 == 'INICIAL') {
            if($value2 == ''){$info_inicial = 0;}else{$info_inicial = $value2;}
        }elseif ($key2 == 'TERMINADO') {
            if($value2 == ''){$info_terminado = 0;}else{$info_terminado = $value2;}
        }elseif ($key2 == 'RETENIDO') {
            if($value2 == ''){$info_retenido = 0;}else{$info_retenido = $value2;}
        }
    };
    include("eval_codigos_tramites.php");
    $info_total = $info_inicial + $info_terminado + $info_retenido + $info_trafic;
    if ($info_total == 1) {
        $desc_exp = "Expediente";
    }elseif ($info_total >= 2) {
        $desc_exp = 'Expedientes';
    }
    if ($info_total > 0) {
        $html .= '
        <div class="col p-2" align="center">
            <div style="width: 25rem;">   
                <div class="card card-stats mb-4 mb-lg-0 bg-teal opacity-9 shadow">
                    <div class="card-body">
                        <div class="row">
                            <div class="col" align="left">
                                <h5 class="card-title text-uppercase text-white mb-0"><span id="txt_desc_tramite_'.$counter.'">'.$desc_tramite.'</span></h5>
                                <a href="javascript:consultaExpedientesDashboard('.$counter.')"
                                    <span class="h2 font-weight-bold mb-0 text-success">
                                        <strong><span id="txt_total_exp_'.$counter.'">'.$info_total.'</span> '.$desc_exp.'</strong>
                                    </span>
                                </a>
                                <span id="txt_code_tramite_'.$counter.'" style="display:none;">'.$key.'</span>
                            </div>
                            <div class="col-auto">
                                <a href="javascript:consultaExpedientesDashboard('.$counter.')" data-toggle="tooltip" data-placement="top" title="ACCEDER">
                                    <div class="icon icon-shape bg-success text-white rounded-circle shadow">
                                        <i class="fas fa-search"></i>
                                    </div>
                                </a>
                            </div>
                        </div>
                        <p class="mt-3 mb-0 text-muted text-sm" align="center">
                            <span class="mr-2">
                                <i class="fas fa-file-download fa-lg text-white"> <span id="txt_inicial_exp_'.$counter.'">'.$info_inicial.'</span></i>
                            </span>
                            <span class="mr-2">
                                <i class="fas fa-file-import fa-lg text-white"> <span id="txt_traff_exp_'.$counter.'">'.$info_trafic.'</span></i>
                            </span>
                            <span class="mr-2">
                                <i class="fas fa-file-upload fa-lg text-white"> <span id="txt_terminado_exp_'.$counter.'">'.$info_terminado.'</span></i>
                            </span>
                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                            <span class="mr-2">
                                <i class="fas fa-exclamation-triangle fa-lg text-warning"> &nbsp;&nbsp;<span id="txt_retenido_exp_'.$counter.'">'.$info_retenido.'</span></i>
                            </span>
                        </p>
                    </div>
                </div>
            </div>
        </div>
        ';
    }
    $counter ++;
};
$html .= '
    </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
);
mysqli_close($bd);
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);
}
?>
