<?php

require_once("../config/conexion.php");
require_once("../models/Detalle.php");
require_once("../models/Permisos.php");
$detalle = new Detalle();
/* =============================================
  JAVASCRIPT
  ============================================= */
switch ($_GET["op"]) {
    case "consultar_detalle":
        $datos = $detalle->Detalle_por_ID($_POST["detalledbk"]);
        if (is_array($datos) == true and count($datos) <> 0) {
            foreach ($datos as $row) {
                $output["detalledbk"] = $row["detalledbk"];
                $output["matricula"] = $row["matricula"];
                $output["N11"] = $row["N11"];
                $output["N12"] = $row["N12"];
                $output["N13"] = $row["N13"];
                $output["N14"] = $row["N14"];
                $output["nfinal"] = $row["nfinal"];
                $output["aprada"] = $row["aprada"];
                $output["estado"] = $row["estado"];
                $output["asignatura"] = $row["asignatura"];
                $output["tipo"] = $row["tipo"];
                $output["paralelo"] = $row["paralelo"];
            }
            echo json_encode($output);
        }
        break;
    case "aportes_matricula":
        $datos = $detalle->Detalle_por_Matricula($_GET["mtr"]);
        $data = Array();
        foreach ($datos as $row) {
            $sub_array = array();
            $sub_array[] = $row["detalledbk"];
            $sub_array[] = $row["descripcion"];
            $sub_array[] = $row["N11"];
            $sub_array[] = $row["N12"];
            $sub_array[] = $row["N13"];
            $sub_array[] = $row["N14"];
            $sub_array[] = $row["nfinal"];
            $data[] = $sub_array;
        }
        $results = array(
            "sEcho" => 1,
            "iTotalRecords" => count($data),
            "iTotalDisplayRecords" => count($data),
            "aaData" => $data
        );
        echo json_encode($results);
        break;
    case "listar":
        $datos = $detalle->Consultar($_GET["fil"], $_GET["m"]);
        $data = Array();
        $permiso = new Permisos();
        $resultado = $permiso->Permisos_por_Perfil("MATRICULA", $_SESSION["perfil"]);
        foreach ($resultado as $row) {
            $eliminar = $row["eliminar"];
            $actualizar = $row["actualizar"];
        }
        $accesos = "";
        foreach ($datos as $row) {
            $sub_array = array();
            $sub_array[] = $row["detalledbk"];
            $sub_array[] = $row["desasig"];
            $sub_array[] = $row["paralelo"];
            $sub_array[] = $row["tipo"];
            if ($actualizar == 1) {
                $accesos = $accesos . '
                <button type="button" onClick="editar(' . $row["detalledbk"] . ');" id="' . $row["detalledbk"] . '" class="btn btn-outline-warning btn-icon"><div><i class="fa fa-edit"></i></div></button>';
            }
            if ($eliminar == 1) {
                $accesos = $accesos . '
                <button type="button" onClick="eliminar(' . $row["detalledbk"] . ');" id="' . $row["detalledbk"] . '" class="btn btn-outline-danger btn-icon"><div><i class="fa fa-times"></i></div></button>';
            }
            $sub_array[] = $accesos;
            $data[] = $sub_array;
            $accesos = "";
        }
        $results = array(
            "sEcho" => 1,
            "iTotalRecords" => count($data),
            "iTotalDisplayRecords" => count($data),
            "aaData" => $data
        );
        echo json_encode($results);
        break;
    case "eliminar_detalle":
        $datos = $detalle->Eliminar_por_ID($_POST["detalledbk"]);
        break;
    case "comboAsignatura":
        $datos = $detalle->ConsultarAsignatura($_GET["m"]);
        if (is_array($datos) == true and count($datos) > 0) {
            foreach ($datos as $row) {
                $html .= "<option value='" . $row['asignatura'] . "'>" . $row['descripcion'] . " - " . $row['nivel'] . "</option>";
            }
            echo $html;
        }
        break;
    case "guardar":
        $datos = array("tabla" => "detalle",
            "asignatura" => $_POST["asignatura"],
            "tipo" => $_POST["tipo"],
            "matricula" => $_POST["matricula"]
        );
        $detalle->mdlGuardarDetalle($datos);
        break;
    case "actualizar":
        $datos = array("tabla" => "detalle",
            "tipo" => $_POST["tipo"],
            "detalledbk" => $_POST["detalledbk"]
        );
        $detalle->mdlActualizarDetalle($datos);
        break;
    case "guardarAutomatico":
        $datos = array("tabla" => "detalle",
            "codcar" => $_POST["codcar"],
            "periodbk" => $_POST["periodbk"],
            "nivel" => $_POST["nivel"],
            "matricula" => $_POST["matricula"]
        );
        $detalle->mdlGuardarDetalleAutomatico($datos);
        break;
    case "calificar":
        if ($_SESSION["perfil"] != "DOCENTE") {
            $datos = $detalle->Calificar($_GET["periodo"], $_GET["carrera"], $_GET["asignatura"]);
        } else {
            $datos = $detalle->CalificarD($_GET["mallacurriculardbk"]);
        }


        $data = Array();
        $permiso = new Permisos();
        $resultado = $permiso->Permisos_por_Perfil("CALIFICACIONES", $_SESSION["perfil"]);
        foreach ($resultado as $row) {
            $actualizar = $row["actualizar"];
        }
        $accesos = "";
        foreach ($datos as $row) {
            $sub_array = array();
            $sub_array[] = $row["detalledbk"];
            $sub_array[] = $row["estudiante"];
            $sub_array[] = "<td><input type='text' size='2' class='form-control input-sm' name='na" . $row["detalledbk"] . "' id='na" . $row["detalledbk"] . "' value='" . $row["n11"] . "'></td>";
            $sub_array[] = "<td><input type='text' size='2' class='form-control input-sm' name='nb" . $row["detalledbk"] . "' id='nb" . $row["detalledbk"] . "' value='" . $row["n12"] . "'></td>";
            $sub_array[] = "<td><input type='text' size='2' class='form-control input-sm' name='nc" . $row["detalledbk"] . "' id='nc" . $row["detalledbk"] . "' value='" . $row["n13"] . "'></td>";
            $sub_array[] = "<td><input type='text' size='2' class='form-control input-sm' name='nd" . $row["detalledbk"] . "' id='nd" . $row["detalledbk"] . "' value='" . $row["n14"] . "'></td>";
            if ($row["tipo"] == 1) {
                $sub_array[] = "<td><input type='text' size='2' class='form-control input-sm' name='ne" . $row["detalledbk"] . "' id='ne" . $row["detalledbk"] . "' value='" . $row["nfinal"] . "' readonly></td>";
            } else {
                $sub_array[] = "<td><input type='text' size='2' class='form-control input-sm' name='ne" . $row["detalledbk"] . "' id='ne" . $row["detalledbk"] . "' value='" . $row["nfinal"] . "'></td>";
            }
            if ($actualizar == 1) {
                $accesos = $accesos . '
                <button class="btn btn-primary btnActualizarDetalle" idDetalle="' . $row["detalledbk"] . '">Guardar</button>
                <input type="hidden" id="tipo' . $row["detalledbk"] . '" name="tipo' . $row["detalledbk"] . '" value="' . $row["tipo"] . '"/>';
            }
            $sub_array[] = $accesos;
            $data[] = $sub_array;
            $accesos = "";
        }
        $results = array(
            "sEcho" => 1,
            "iTotalRecords" => count($data),
            "iTotalDisplayRecords" => count($data),
            "aaData" => $data
        );
        echo json_encode($results);
        break;
    case "actualizarAsignatura":
        if (isset($_POST["detalledbk"])) {
            $datos = array("tabla" => "detalle",
                "detalledbk" => $_POST["detalledbk"],
                "n11" => $_POST["n11"],
                "n12" => $_POST["n12"],
                "n13" => $_POST["n13"],
                "n14" => $_POST["n14"],
                "nfinal" => $_POST["nfinal"]
            );
            $detalle->mdlCalificarDetalle($datos);
        }
        break;
    case "calificarEstudiante":
        $datos = $detalle->CalificarEstudiante($_GET["carrera"], $_GET["estudiante"]);
        $data = Array();
        $permiso = new Permisos();
        $resultado = $permiso->Permisos_por_Perfil("CALIFICACIONES", $_SESSION["perfil"]);
        foreach ($resultado as $row) {
            $actualizar = $row["actualizar"];
        }
        $accesos = "";
        foreach ($datos as $row) {
            $sub_array = array();
            $sub_array[] = $row["detalledbk"];
            $sub_array[] = $row["periodo"];
            $sub_array[] = $row["asignatura"];
            $sub_array[] = "<td><input type='text' size='2' class='form-control input-sm' name='na" . $row["detalledbk"] . "' id='na" . $row["detalledbk"] . "' value='" . $row["n11"] . "'></td>";
            $sub_array[] = "<td><input type='text' size='2' class='form-control input-sm' name='nb" . $row["detalledbk"] . "' id='nb" . $row["detalledbk"] . "' value='" . $row["n12"] . "'></td>";
            $sub_array[] = "<td><input type='text' size='2' class='form-control input-sm' name='nc" . $row["detalledbk"] . "' id='nc" . $row["detalledbk"] . "' value='" . $row["n13"] . "'></td>";
            $sub_array[] = "<td><input type='text' size='2' class='form-control input-sm' name='nd" . $row["detalledbk"] . "' id='nd" . $row["detalledbk"] . "' value='" . $row["n14"] . "'></td>";
            if ($row["tipo"] == 1) {
                $sub_array[] = "<td><input type='text' size='2' class='form-control input-sm' name='ne" . $row["detalledbk"] . "' id='ne" . $row["detalledbk"] . "' value='" . $row["nfinal"] . "' readonly></td>";
            } else {
                $sub_array[] = "<td><input type='text' size='2' class='form-control input-sm' name='ne" . $row["detalledbk"] . "' id='ne" . $row["detalledbk"] . "' value='" . $row["nfinal"] . "'></td>";
            }
            if ($actualizar == 1) {
                $accesos = $accesos . '
                <button class="btn btn-primary btnActualizarDetalle" idDetalle="' . $row["detalledbk"] . '">Guardar</button>
                <input type="hidden" id="tipo' . $row["detalledbk"] . '" name="tipo' . $row["detalledbk"] . '" value="' . $row["tipo"] . '"/>';
            }
            $sub_array[] = $accesos;
            $data[] = $sub_array;
            $accesos = "";
        }
        $results = array(
            "sEcho" => 1,
            "iTotalRecords" => count($data),
            "iTotalDisplayRecords" => count($data),
            "aaData" => $data
        );
        echo json_encode($results);
        break;
    case "grafico-ResumenRecordAcademico":
        $datos = $detalle->GraficoResumenRecordAcademico();
        $data = Array();
        foreach ($datos as $row) {
            $sub_array = array();
            $sub_array[] = $row["nfinal"];
            $sub_array[] = $row["asignatura"];
            $data[] = $sub_array;
        }
        echo json_encode($data);
        break;        
}
?>
