<?php
session_start();
$usuarioSession = $_SESSION['USUARIO'];
$grupo = $_SESSION['GRUPO'];
$contacto = $_SESSION['CONTACTO'];
$awa_session_id = $_SESSION['AWA_SESION_ID'];
include('../includes/conex.php');
include('../includes/eat.php');
include('../includes/historial.php');
include('../includes/functions.php');
include('../includes/esquemas.php');
include('../class/Schema.php');
include('../class/MotherCar.php');
include('../class/Expediente.php');
include('../class/Bibliotecas.php');
include('../class/Static.php');
include('../class/WebServices.php');
include('../lg/config_vo.php');
if(!isset($usuarioSession) OR $awa_session_id != $awa_id) {
    eatCookies();
    header('Location:../index.php');
}
if(isset($_COOKIE['usuario']) AND $_COOKIE['usuario'] != $staticUsu ) {
    eatCookies();
    header('Loction:../index.php');
}
if (isset($_COOKIE['idioma'])) {
    include('../lg/LG_' . $_COOKIE['idioma'] . '.php');
} else {
    include('../lg/LG_SP.php');
}

$Schema = new Schema($con);
$Bibliotecas = new Bibliotecas($con);
$p = Statics::SafeInput($con, $_POST['portales']);
$activos = Statics::SafeInput($con, $_POST['activos']);

?>

<table class="table table-hover" id="table-multipost">
    <thead>
        <tr class="text-dark">
            <th scope="col" class="border-top-0 text-center">
                <div class="custom-control custom-checkbox">
                    <input type="checkbox" class="custom-control-input" id="checkAll">
                    <label class="custom-control-label" for="checkAll"></label>
                </div>
            </th>
            <th scope="col" class="border-top-0"><?php echo mb_strtoupper($vehiculos_lg, 'UTF-8'); ?></th>
            <th scope="col" class="border-top-0"><?php echo mb_strtoupper($publicacion_lg, 'UTF-8'); ?></th>
            <th scope="col" class="border-top-0"><?php echo mb_strtoupper($gestion_lg, 'UTF-8'); ?></th>
        </tr>
    </thead>
    <tbody class="bg-white border">
        <?php
            $html = '';
            $type = 'iiiii';
            $condition = '';
            $values = [$p, 1, 1, 0, 0];
            if(isset($_POST['stock'])) {
                if(!empty($_POST['stock'])) {
                    $type .= 'i';
                    $condition .= 'AND V.STOCK = ?';
                    array_push($values, Statics::SafeInput($con, $_POST['stock']));
                }
            }
            $inside_code = $Bibliotecas->select_campo_tabla_parametros('BBDD_ALIAS', 'VALOR');
            $res = $Schema->executeQuery('SELECT V.MOSTRAR, V.CODIGO AS COD_VEH, V.CODE_EXT, P.ACTIVO, P.EXPORTADO, P.VEHICULO, P.INICIO
            FROM VEHICULOS V
            LEFT JOIN VEHICULOS_PUBLICACIONES P ON V.CODIGO = P.VEHICULO AND P.PORTAL = ?
            AND P.ACTIVO = ? 
            WHERE V.ACTIVO = ?
            AND V.VENTA_TOTALSINDTO <> ?
            AND V.PAPELERA = ?
            ' . $condition . '
            ORDER BY V.MOSTRAR ASC', $type, $values);
            if($res->num_rows) {
                $nr = 0;
                while($row = $res->fetch_array()) {
                    $nr++;
                    $fecha_publicacion = empty($row['INICIO']) ? '<i class="far fa-calendar-minus"></i>' : date('d/m/Y', strtotime($row['INICIO']));
                    $app_code_ext = empty($row['CODE_EXT']) ? $inside_code : $row['CODE_EXT'];
                    $check = $row['VEHICULO'] != NULL ? 'checked' : FALSE;
                    $html .= '<tr class="' . $check . '">';
                        $html .= '<th class="text-center"><div class="custom-control custom-checkbox">';
                            $html .= '<input type="checkbox" class="custom-control-input checkMe" id="check-' . $row['COD_VEH'] . '" ' . $check . ' name="codigos[]" value="' . $row['COD_VEH'] . '">';
                            $html .= '<label class="custom-control-label" for="check-' . $row['COD_VEH'] . '"></label>';
                        $html .= '</div></th>';
                    
                        $html .= '<th class="text-dark font-weight-normal">' . $row['MOSTRAR'] . '</th>';
                        $html .= '<th class="text-muted font-weight-normal text-center">' . $fecha_publicacion . '</th>';
                        $html .= '<th class="text-dark text-center font-weight-normal"><i class="far fa-eye ml-2 mt-1  c-pointer ver" aria-label="' . $row['COD_VEH'] . '#' . $app_code_ext . '"></i></th>';
                    $html .= '</tr>';
                }
            } else {
                $html .= '<tr><th class="text-center" colspan="4">' . $sin_resultados_lg . '</th></tr>';
            }

            echo $html; 
        ?>
    </tbody>
</table>


<script>

/* $('#selectUnchecked').on('click', function() {
    alert(1)
})  */    


$('#checkAll').on('click', function() {
    if($(this).is(':checked')) {
        $('.checkMe').prop('checked', true)
        //$('.checkMe').addClass('is-valid');
        if($('#selectUnchecked').is(':checked')) {
            $('#activos-now').html(total - $('.checked').length)
            activos = total - $('.checked').length;
        } else {
            $('#activos-now').html(total)
            activos = total;
        }
    } else {
        $('.checkMe').prop('checked', false)
        $('.checkMe').removeClass('is-valid');
        $('#activos-now').html(0)
        activos = 0;
    }
})

$('.checkMe').on('click', function() {
    if($(this).is(':checked')) {
        activos += 1;
        $('#activos-now').html(activos)
        $(this).addClass('is-valid');
    } else { 
        activos -= 1;
        $('#activos-now').html(activos)
        $(this).removeClass('is-valid');
    }
})

$('#selectUnchecked').on('click', function() {
    if($(this).is(':checked')) {
        activos = $('.is-valid').length;
        $('.checked').fadeOut();
        $('#total-now').html(total - $('.checked').length);

        if($('#checkAll').is(':checked')) {
            console.log(1)
            $('#activos-now').html(total - $('.checked').length)

        } else {
            console.log(0)
            $('#activos-now').html(0)
        }
    } else {
        if($('#checkAll').is(':checked')) {
            activos = total;
        } else {
            activos = <?php echo $activos; ?> + $('.is-valid').length;
        }
        $('.checked').fadeIn();
        $('#total-now').html(total);
        $('#activos-now').html(activos)
    }
})
</script>

