<?php
require_once 'class/c_login.php';
require_once 'include/bd_pdo.php';

$user = Login::get_user();

$user->access();
if ($user->acceso == null) {
    // die($access);
    header('Location: main.php?error=1');
} else {
    $user->print_to_log('Carreras');
}
$facultad = $user->facultad['facultad_id'] ?? -1;
if ($user->admin) {
    $facultad = null;
}
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Carreras</title>
    <?php
    include 'import/html_css_files.php';
    ?>

</head>

<body>
    <?php
    include "import/html_header.php";
    html_header(
        "CARRERAS",
        "Gestión de Checador "
    );
    $user->access();
    $max_count = 30;

    /*$sql = "SELECT * FROM   (:nombre, :carrera, :facultad)";
    if (isset($_POST["desc"])) {
        $desc = $_POST["desc"];
        $filter_desc = htmlspecialchars($desc, ENT_QUOTES, 'UTF-8');
    } else {
        $desc = null;
    }

    if (isset($_POST["carrera"])) {
        $pcarrera = $_POST["carrera"];
        $filter_carrera = htmlspecialchars($pcarrera, ENT_QUOTES, 'UTF-8');
    } else {
        $pcarrera = null;
    }

    if ($pcarrera == "") {
        $sql .= "LIMIT 30";
    }
    $fs_materias = query(
        $sql,
        array(":nombre" => $desc, ":carrera" => $pcarrera, ":facultad" => $facultad),
        single: false
    );
    $fs_carreras = query(
        "SELECT * FROM fs_carreras(:facultad, null, null) ORDER BY unaccent(carrera_nombre)",
        array(":facultad" => $facultad),
        single: false
    );*/

    $stmt = $pdo->prepare('SELECT ca.carrera_id, ca.clave_carrera, ca.carrera_nombre, f.facultad_nombre 
                                    FROM carrera ca
                                    JOIN facultad f ON f.facultad_id = ca.facultad_id
                                    WHERE ca.carrera_id != 0
                                    ORDER BY f.facultad_nombre, ca.carrera_nombre');
    $stmt->execute();
    $carr = $stmt->fetchAll();

    $stmt = $pdo->prepare('SELECT * FROM nivel');
    $stmt->execute();
    $nivel = $stmt->fetchAll();



    $stmt = $pdo->prepare('SELECT * FROM facultad');
    $stmt->execute();
    $facultad = $stmt->fetchAll();


    //print_r($fs_materias);
    
    ?>
    <main class="content marco">
        <!-- Filtro -->
       

        <!-- Tabla -->
        <div class="row">
            <div class="col-12 table-responsive">
                <!--
                 
                     <button type="button" class="btn btn-outline-primary" data-toggle="modal"
                            data-target="#modal">
                            <span class="ing-editar ing-fw"></span>
                    </button>
                   
                    -->
                <div class="col-12 text-end pb-3">
                    <button type="button" class="btn btn-outline-primary" data-toggle="modal" data-target="#modalCrear">
                        <span class="ing-mas ing-fw"></span>
                        Crear
                    </button>
                </div>

                <table class="table table-sm table-striped table-white">
                    <thead class="thead-dark">
                        <tr>
                            <th>Clave Carrera</th>
                            <th>Nombre Facultad</th>
                            <th>Nombre Carrera</th>
                            <th>Editar</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        if ($carr) {
                            foreach ($carr as $materia) {

                                ?>
                                <tr data-id="<?= $materia["carrera_id"]; ?>" id="<?= $materia["carrera_id"]; ?>">
                                    <td class="text-primary">
                                        <?= $materia["clave_carrera"]; ?>
                                    </td>
                                    <td class="text-primary">
                                        <?= $materia["facultad_nombre"]; ?>
                                    </td>
                                    <td class="text-primary">
                                        <!-- <small>(<?= $materia["clave_materia"]; ?>)</small> -->
                                        <?= $materia["carrera_nombre"]; ?>
                                    </td>
                                    
                                    <td>
                                        <button type="button" class="btn btn-outline-primary" data-toggle="modal"
                                            data-target="#modal">
                                            <span class="ing-editar ing-fw"></span>
                                        </button>
                                    </td>
                                </tr>
                                <?php

                            }
                        }
                        ?>
                    </tbody>
                </table>
                <div id="message"></div>
            </div>
        </div>
    </main>
    <!-- Footer -->
    <?php
    include "import/html_footer.php";
    ?>

    <!-- Modal Crear-->
    <div class="modal fade" id="modalCrear" tabindex="-1" role="dialog" aria-labelledby="modalCrear" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="col-12 modal-title text-center">
                        <span id="modalLable">
                            Crear Carrera
                        </span>



                        <button type="button" class="close text-white" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>

                    </h4>
                </div>
                <div class="modal-body">
                    <form action="action\action_carreras_insert.php" method="post" id="formaModal"
                        onsubmit="return valida_campos_crear()">
                        <div class="form-box">

                            <div class="form-group row">
                                <label for="carreraCrear" class="col-4 col-form-label">Nombre Carrera*</label>
                                <div class="col-8">
                                    <input id="carreraCrear" name="carreraCrear" type="text" class="form-control" maxlength="100">
                                </div>
                            </div>


                            <div class="form-group row">
                                <label for="facultadCrear" class="col-4 col-form-label">Nombre Facultad*</label>
                                <div class="col-8">
                                    <?php
                                    echo '<select class="form-control area" id="facultadCrear" name="facultadCrear" required style="color: black;">';
                                    foreach ($facultad as $op) {
                                        echo '<option value="' . $op['facultad_id'] . '">' . $op['facultad_nombre'] . '</option>';
                                    }
                                    echo '</select>';
                                    ?>
                                </div>
                            </div>

                            <div class="form-group row">
                                <label for="nivelCrear" class="col-4 col-form-label">Nivel*</label>
                                <div class="col-8">
                                    <?php
                                    echo '<select class="form-control area" id="nivelCrear" name="nivelCrear" required style="color: black;">';
                                    foreach ($nivel as $op) {
                                        echo '<option value="' . $op['nivel_id'] . '">' . $op['nivel_nombre'] . '</option>';
                                    }
                                    echo '</select>';
                                    ?>
                                </div>
                            </div>

                            <div class="form-group row">
                                <label for="nombreCarreraCrear" class="col-4 col-form-label">Clave de la Carrera*</label>
                                <div class="col-8">
                                    <input id="nombreCarreraCrear" name="nombreCarreraCrear" type="text" class="form-control"
                                        maxlength="100">
                                </div>
                            </div>

                            <div class="form-group row">
                                <div class="offset-4 col-8">
                                    <button type="submit" class="btn btn-outline-primary" id="submitBtn" data-tipo="1">
                                        <span class="ing-aceptar ing-fw"></span> Guardar
                                    </button>
                                    <button type="reset" class="btn btn-outline-danger" data-dismiss="modal">
                                        <span class="ing-cancelar ing-fw"></span> Cancelar
                                    </button>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
    <!-- End Modal Crear-->

    <!-- Modal -->
    <div class="modal fade" id="modal" tabindex="-1" role="dialog" aria-labelledby="modal" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="col-12 modal-title text-center">
                        <span id="modalLable">
                            Editar Carrera
                        </span>
                        <button type="button" class="close text-white" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </h4>
                </div>
                <div class="modal-body">
                    <form action="./action/action_carreras_update.php" method="post" id="formaModal"
                        onsubmit="return valida_campos_editar()">
                        <input type="hidden" name="id" id="carrera_id">
                        <div class="form-box">

                            <div class="form-group row">
                                <label for="carreraEditar" class="col-4 col-form-label">Nombre Carrera*</label>
                                <div class="col-8">
                                    <input id="carreraEditar" name="carreraEditar" type="text" class="form-control" maxlength="100">
                                </div>
                            </div>


                            <div class="form-group row">
                                <label for="facultad" class="col-4 col-form-label">Nombre Facultad*</label>
                                <div class="col-8">
                                    <?php
                                    echo '<select class="form-control area" id="facultadEditar" name="facultadEditar" required style="color: black;">';
                                    foreach ($facultad as $op) {
                                        echo '<option value="' . $op['facultad_id'] . '">' . $op['facultad_nombre'] . '</option>';
                                    }
                                    echo '</select>';
                                    ?>
                                </div>
                            </div>

                            <div class="form-group row">
                                <label for="nivel" class="col-4 col-form-label">Nivel*</label>
                                <div class="col-8">
                                    <?php
                                    echo '<select class="form-control area" id="nivelEditar" name="nivelEditar" required style="color: black;">';
                                    foreach ($nivel as $op) {
                                        echo '<option value="' . $op['nivel_id'] . '">' . $op['nivel_nombre'] . '</option>';
                                    }
                                    echo '</select>';
                                    ?>
                                </div>
                            </div>

                            <div class="form-group row">
                                <label for="nombreCarreraEditar" class="col-4 col-form-label">Clave de la Carrera*</label>
                                <div class="col-8">
                                    <input id="nombreCarreraEditar" name="nombreCarreraEditar" type="text" class="form-control"
                                        maxlength="100">
                                </div>
                            </div>

                            <div class="form-group row">
                                <div class="offset-4 col-8">
                                    <button type="submit" class="btn btn-outline-primary" id="submitBtn" data-tipo="1">
                                        <span class="ing-aceptar ing-fw"></span> Guardar
                                    </button>
                                    <button type="reset" class="btn btn-outline-danger" data-dismiss="modal">
                                        <span class="ing-cancelar ing-fw"></span> Cancelar
                                    </button>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
    <!-- End Modal -->


    

    <script src="js/jquery.min.js"></script>
    <script src="js/bootstrap/bootstrap.min.js"></script>
    <script src="js/datalist.js"></script>
    <?php
    require_once 'js/messages.php';
    ?>
    <script>

        function valida_campos_editar() {
            $('.is-invalid').removeClass('is-invalid'); 
            var ok = true;
            
            if ($("#carreraEditar").val().trim() === "") {
            $("#carreraEditar").addClass("is-invalid");
            ok = false;
            }

            if ($("#nombreCarreraEditar").val().trim() === "") {
            $("#nombreCarreraEditar").addClass("is-invalid");
            ok = false;
            }

            return ok;

        }


        function valida_campos_crear() {
            $('.is-invalid').removeClass('is-invalid'); 
            var ok = true;
            
            if ($("#carreraCrear").val().trim() === "") {
            $("#carreraCrear").addClass("is-invalid");
            ok = false;
            }

            if ($("#nombreCarreraCrear").val().trim() === "") {
            $("#nombreCarreraCrear").addClass("is-invalid");
            ok = false;
            }

            return ok;

        }



        $(document).on("click", ".btn-reset", function (event) {
            var forma = $(this).parents("form");
            forma.find("input[type=text]").val("");
            setDatalistFirst("#filter_carrera");
            forma.submit();
        });

        $('#modal').on('show.bs.modal', function (event) {
            var button = $(event.relatedTarget);  
            var id = button.closest("tr").data("id");
            
            //var id = $(event.relatedTarget).parents("tr").data("id");
            $.ajax({
                url: "action/action_carreras_select.php",
                type: "post",
                dataType: "json",
                data: {
                    idmateria: id
                },
                success: function (result) {
                    $("#carreraEditar").val(result["carrera_nombre"]); 
                    $("#facultadEditar").val(result["facultad_id"]);
                    $("#nivelEditar").val(result["nivel_id"]);
                    $("#nombreCarreraEditar").val(result["clave_carrera"]); 
                    $("#carrera_id").val(result["carrera_id"]); 
                    
                },
                error: function () {
                    console.log("Error")
                }
            });
        });

    </script>
</body>

</html>