<?php include('./templates/cabecera.php'); ?>
<?php include('./config/db.php'); ?>
<?php include('funciones.php'); ?>
<?php 
$html = "reportePlan.php";
if(!$_GET){
    echo "<script>location.href='$html?pagina=1';</script>";
}
$error = "";
$ok = "";
$txtFiltro=(isset($_POST['txtFiltro']))?$_POST['txtFiltro']:"";
$getPagina=(isset($_POST['pagina']))?$_POST['pagina']:"";
$totalPaginas = 10;
$paginadorSalto = 9;
$txtId=(isset($_POST['txtId']))?$_POST['txtId']:"";
$selPlan=(isset($_POST['selPlan']))?$_POST['selPlan']:"";

$action=(isset($_POST['action']))?$_POST['action']:"";
//echo($selPlan);
switch($action){
    case "Volver":
            echo "<script>location.href='index.php';</script>";
        break;
    case "pdf":
            echo "<script>location.href='actaPlan.php?selPlan=".$selPlan."&return=".$html."';</script>";
        break;
    case "XLS":
        echo "<script>window.open('excel_repo1.php?selPlan=".$selPlan."', '_blank');</script>";
    break;
}

$sqlFormulario="select fi.id, pp.nombrePlan, cli.nombre, act.detalle, fi.fechaRealizacion, fi.adjunto 
from formularioingreso fi inner join planprevencion pp on fi.planId=pp.id
inner join cliente cli on cli.id = fi.clienteId
inner join actividades act on act.id = fi.actividadId
where pp.id in (:planId)";  
//echo($sqlFormulario);
if($txtFiltro!="" ){
    $sqlFormulario = $sqlFormulario . " and fi.id LIKE :id or pp.nombrePlan LIKE :id or cli.nombre LIKE :id or act.detalle LIKE :id 
    or fi.fechaRealizacion LIKE :id or fi.adjunto LIKE :id ";
}
try{
    $sql=$conexion->prepare($sqlFormulario); 
    $sql->bindParam(":planId",$selPlan);
    if($txtFiltro!="" ){
        $txtFiltro2=limpiaHTML("%".$txtFiltro."%");
        $sql->bindParam(":id",$txtFiltro2);
    }

    $sql->execute();
    if($sql->rowCount() > 0){
        $totalDatos = $sql->rowCount();
        $paginas = $totalDatos/$totalPaginas;
        $paginas = ceil($paginas);
    }else{
        $paginas = 1;
    }

}catch (Exception $err) {
    echo ("Ha ocurrido un error en la solicitud. "); 
}
//echo("getPagina = ".$getPagina);
if(is_null($getPagina)){ //$getPagina
    $getPagina=0;
    //echo("getPagina = ".$getPagina);
}else{
    $getPagina = intval($getPagina);
    //echo("getPagina = ".$getPagina);
}

if($getPagina > 0){
    $getPagina = $getPagina - 1;
}
$iniciar = ($getPagina) * $totalPaginas;

$sqlFormulario=$sqlFormulario." ORDER BY fi.id desc LIMIT :iniciar,:totalPaginas;";

try{
    $sql=$conexion->prepare($sqlFormulario); 
    $sql->bindParam(":planId",$selPlan);
    if($txtFiltro!="" ){
        $txtFiltro2=limpiaHTML("%".$txtFiltro."%");
        $sql->bindParam(":id",$txtFiltro2);
    }
    $sql->bindParam(":iniciar",$iniciar, PDO::PARAM_INT);
    $sql->bindParam(":totalPaginas",$totalPaginas, PDO::PARAM_INT);
    $sql->execute();
}catch (Exception $err) {
    echo ("Ha ocurrido un error en la solicitud. ");    
    throw $err;
}
$listaFormulario = $sql->fetchAll(PDO::FETCH_ASSOC); //almacena todos los registros como arreglo
?>
<script type="text/javascript">
	$(document).ready(function(){
        //$('#selPlan').select2();
        //$('#selCliente').select2();
        //$('#selInstalacion').select2();
        $('#selPlan').change(function(){
            this.form.submit();
		});
	})

function recargaInstalacion(idForm){
    $.ajax({
        type:"POST",
        url:"actualizaInstalacion.php",
        data:"idForm=" + idForm,
        success:function(r){
            $('#select2lista'+idForm).html(r);
            //$('#selInstalacion').select2();
        }
    });
}
</script>
<div class="col-md-4">
    <div class="card">
        <div class="card-header">
            <h4>Formulario de ingreso<h4/>
        </div>
        <div class="card-body">
                <?php if(strlen($error) > 0) {?>
                    <div class='alert alert-danger' role="alert">
                        <?php echo $error; ?>
                    </div>
                <?php }?>
            <form method="POST" enctype="multipart/form-data">
            <input type="hidden" class="form-control" name="txtId" id="txtId" value="<?php echo $txtId; ?>" >
                <div class = "form-group">
                    <label for="exampleSelect1" class="form-label mt-1">Plan de prevenci&oacute;n*</label>
                    <select class="form-select" id="selPlan" name="selPlan" required  >
                    <option value="" >Seleccione</option>
                    <?php
                    try{
                    $sqlPlan=$conexion->prepare("SELECT id, clienteId, nombrePlan, fechaInicio, fechaTermino FROM planprevencion where estadoId=1;");  
                    $sqlPlan->execute();
                    $listaPlan = $sqlPlan->fetchAll(PDO::FETCH_ASSOC); //almacena todos los registros como arreglo
                        foreach($listaPlan as $plan) { 
                            $txtId=$plan['id'];
                            ?>
                            <option value="<?php echo $txtId ?>" <?php if($selPlan==$txtId) {echo ("selected"); } ?>><?php echo $plan['nombrePlan'] ?></option>
                        <?php 
                        } 
                    }catch (Exception $err) {
                        echo ("Ha ocurrido un error en la solicitud. ");    
                        throw $err;
                    }
                    ?>
                    </select>
                </div>
                
                <br/>
                <div class="btn-group" role="group" aria-label="">
                <?php
                    if($selPlan != ""){
                ?>
                    <button type="submit" name="action" value="XLS" class="btn btn-outline-success" >XLS</button> &nbsp;
                    <button type="submit" name="action" value="pdf" class="btn btn-outline-warning" >PDF</button>&nbsp;
                    <?php
                    }
                ?>
                    <button type="submit" name="action" formnovalidate="" value="Volver" class="btn btn-outline-info">Volver</button>
                </div>

            </form>
        </div>
    </div>
</div>
<div class="col-md-8">
        <form class="d-flex" method="POST">
            <input type="hidden" class="form-control" name="pagina" id="pagina" value="<?php echo $getPagina; ?>" >
            <input type="hidden" name="txtId" id="txtId" value="<?php echo $txtId; ?>" >
            <input type="hidden" name="selPlan" id="selPlan" value="<?php echo $selPlan; ?>" >
            <input class="form-control me-sm-2" type="text" placeholder="filtrar" name="txtFiltro" id="txtFiltro" value="<?php echo ($txtFiltro!="")?$txtFiltro:""; ?>">
            <button class="btn btn-outline-info" name="action" type="submit" value="Filtrar">Filtrar</button>
        </form>
    <table class="table table-hover">
        <thead>
            <tr>
                <th>ID</th>
                <th>Cliente</th>
                <th>Actividad</th>
                <!--th>Fecha</th>
                <th>Adjunto</th>
                <th>Acci&oacute;n</th-->
            </tr>
        </thead>
        <tbody>
            <?php 
            foreach($listaFormulario as $form) { ?>
            <tr>
                    <td>
                        <h2 class="accordion-header" id="headingOne">
                            <button class="btn btn-outline-dark" type="button" data-bs-toggle="collapse" data-bs-target="#collapse<?php echo $form['id'] ?>" aria-expanded="false" aria-controls="collapseOne" onclick="recargaInstalacion(<?php echo $form['id'] ?>);">
                                <?php echo $form['id'] ?>
                            </button>
                        </h2>
                    </td>
                <td><?php echo $form['nombre'] ?></td>
                <td><?php echo $form['detalle'] ?></td>
                <!--td>
                    <?php 
                        echo date("d-m-Y",strtotime($form['fechaRealizacion']));
                    ?>
                </td>
                <td><?php echo $form['adjunto'] ?></td-->
                
            </tr>
            <tr>
                    <td colspan="4">
                        <div class="accordion" id="accordionExample<?php echo $form['id'] ?>">
                            <div class="accordion-item">
                                <div id="collapse<?php echo $form['id'] ?>" class="accordion-collapse collapse" aria-labelledby="headingOne" data-bs-parent="#accordionExample<?php echo $form['id'] ?>" >
                                    <div class="accordion-body">
                                    <table class="table table-hover">
                                        <thead>
                                            <tr>
                                                <th>F. Realizaci&oacute;n</th>
                                                <th>Nombre adjunto</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <tr>
                                                <td><?php echo date("d-m-Y",strtotime($form['fechaRealizacion'])) ?></td>
                                                <td>
                                                    <?php echo $form['adjunto'] ?>
                                                </td>
                                            </tr>
                                        </tbody>
                                    </table>
                                        <table class="table table-hover">
                                            <thead>
                                                <tr>
                                                    <th colspan="3">Instalaciones</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                            </tbody>
                                    </table>
                                    <table class='table table-hover'>
                                        <tbody>
                                            <tr>
                                                <td>
                                                    <?php
                                                        try{
                                                            $sqlInstalacion=$conexion->prepare("SELECT inst.id, inst.nombre FROM formularioingreso ing inner JOIN 
                                                            formularioinstalacion ins on ins.formularioId = ing.id inner JOIN instalacion inst on inst.id = ins.instalacionId
                                                            where ing.id in (:id) order by ins.id asc;");  
                                                            $sqlInstalacion->bindParam(":id",$form['id']);
                                                            $sqlInstalacion->execute();
                                                            $listaInstalacion = $sqlInstalacion->fetchAll(PDO::FETCH_ASSOC); //almacena todos los registros como arreglo
                                                                foreach($listaInstalacion as $ins) { 
                                                                    ?>
                                                                        <i class='bi bi-arrow-right'>
                                                                            <?php echo $ins['nombre'] ?> <br>
                                                                        </i>
                                                                    <?php
                                                            } 
                                                            
                                                        }catch (Exception $err) {
                                                            echo ("Ha ocurrido un error en la solicitud. ");    
                                                            throw $err;
                                                        }
                                                    ?>
                                                </td>
                                            <tr >
                                        </tbody>
                                    </table>
                                        
                                    </div>
                                </div>
                            </div>
                        </div>
                    </td>
                </tr>
            <?php } ?>
        </tbody>
    </table>
    <div>
        <ul class="pagination">

            <li class="page-item ">
                    <form class="d-flex" method="POST">
                        <button class="page-link" type="submit" <?php echo ($getPagina<1)?"disabled":""; ?>>&laquo;</button>
                        <input type="hidden" name="pagina" id="pagina" value="<?php echo $getPagina; ?>" >
                        <input type="hidden" name="txtFiltro" id="txtFiltro" value="<?php echo $txtFiltro; ?>" >
                        <input type="hidden" name="selPlan" id="selPlan" value="<?php echo $selPlan; ?>" >
                    </form>
                <!--a class="page-link" href="<?php echo $html; ?>?pagina=<?php echo $getPagina-1 ?>&txtFiltro=<?php echo $txtFiltro; ?>">&laquo;</a-->
            </li>
                <?php for($i=0; $i< $paginas; $i++ ) { 
                    if((($i+1)%$paginadorSalto)==0){
                        echo("</ul><ul class='pagination'>");
                    } 
                ?>
            <li class="page-item ">
                    <form class="d-flex" method="POST">
                    <?php
                    $active = "";
                    if ($getPagina == $i){$active = "active";}else{$active = "";}
                    ?>
                        <button class="page-link <?php echo $active; ?>" type="submit" ><?php echo $i+1 ?></button>
                        <input type="hidden" name="pagina" id="pagina" value="<?php echo $i+1; ?>" >
                        <input type="hidden" name="txtFiltro" id="txtFiltro" value="<?php echo $txtFiltro; ?>" >
                        <input type="hidden" name="selPlan" id="selPlan" value="<?php echo $selPlan; ?>" >
                    </form>
                <!--a class="page-link" href="<?php echo $html; ?>?pagina=<?php echo $i+1 ?>&txtFiltro=<?php echo $txtFiltro; ?>"><?php echo $i+1 ?></a-->
            </li>
            <?php } ?>
            <li class="page-item ">
                    <form class="d-flex" method="POST">
                    <?php //echo $paginas; ?>
                        <button class="page-link" type="submit" <?php echo ($getPagina+1>=$paginas)?"disabled":""; ?>>&raquo;</button>
                        <input type="hidden" name="pagina" id="pagina" value="<?php echo $getPagina+2; ?>" >
                        <input type="hidden" name="txtFiltro" id="txtFiltro" value="<?php echo $txtFiltro; ?>" >
                        <input type="hidden" name="selPlan" id="selPlan" value="<?php echo $selPlan; ?>" >
                    </form>
                <!--a class="page-link" href="<?php echo $html; ?>?pagina=<?php echo $getPagina+1 ?>&txtFiltro=<?php echo $txtFiltro; ?>">&raquo;</a-->
            </li>
        </ul>
    </div>
</div>

<?php include('./templates/pie.php'); ?>