<?php
require_once("../config/conexion.php");
require_once("../modelos/Material.php");
$material = new material();

switch ($_GET["op"]) {
    case "combo":
        $datos = $material->get_material_estado();
        if (is_array($datos) == true and count($datos) > 0) {
            $html = "";
            foreach ($datos as $row) {
                $html .= "<option value='" . $row['Ccod_mat'] . "'>" . $row['Ccod_mat'] . "&nbsp;" . "/" . "&nbsp;" . $row['Cdescrip'] . "</option>";
            }
            echo $html;
        }
        break;

    case "listar":
        $datos = $material->get_material();
        $data = [];
        foreach ($datos as $row) {
            $sub_array = [];
            $sub_array[] = $row["Ccod_mat"];
            $sub_array[] = $row["Cdescrip"];
            if ($row["Cest_mat"] == '1') {
                $sub_array[] = '<span class="label label-pill label-success">Activo</span>';
            } else {
                $sub_array[] = '<span class="label label-pill label-default">Inactivo</span>';
            };
            $sub_array[] = '<button type="button" onClick="editar(' . $row["Ccod_matID"] . ');" id="' . $row["Ccod_matID"] . '" class="btn btn-info btn-sm"><i class="fa fa-cogs"></i></button>';
            $data[] = $sub_array;
        }
        $results = [
            "sEcho" => 1,
            "iTotalRecords" => count($data),
            "iTotalDisplayRecords" => count($data),
            "aaData" => $data
        ];
        echo json_encode($results);
        break;

    case "guardaryeditar":
        $Ccod_matID = isset($_POST["Ccod_matID"]) ? $_POST["Ccod_matID"] : "";
        $Ccod_mat = $_POST["Ccod_mat"];
        $Cdescrip = $_POST["Cdescrip"];
        $Cest_mat = $_POST["Cest_mat"];

        if (empty($Ccod_matID)) {
            $material->insert_material($Ccod_mat, $Cdescrip);
        } else {
            $material->update_material($Ccod_matID, $Ccod_mat, $Cdescrip, $Cest_mat);
        }
        break;

    case "mostrar":
        if (isset($_POST["Ccod_matID"])) {
            $datos = $material->get_material_id($_POST["Ccod_matID"]);
            if (is_array($datos) && count($datos) > 0) {
                $output["Ccod_matID"] = $datos["Ccod_matID"];
                $output["Ccod_mat"] = $datos["Ccod_mat"];
                $output["Cdescrip"] = $datos["Cdescrip"];
                $output["Cest_mat"] = $datos["Cest_mat"];
                echo json_encode($output);
            }
        }
        break;
}
?>
