<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Admin extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model("mgeneral");
        $this->load->library("Funciones");
    }
    public function boletas()
    {
        $ugel = $this->session->userdata("ugel_ide");
        $aactual = $this->input->get("a");
        $afin = $this->mgeneral->get_data("year(now()) as anio");
        $afin = $afin[0]->anio;
        $aini = 2005;
        if ($aactual == false) {
            $aactual = $afin;
        }
        if ($aactual < $aini) {
            return;
        }
        $tabla = "data_" . $ugel . "_" . $aactual;
        $tabla_data = $this->mgeneral->get_data("*", "information_schema.TABLES", array("Table_Name = '$tabla'", "table_schema = 'eboleta'"));
        $vistas = array(
            "admin/vboletas",
        );
        $datas = array(
            array(
                "formas" => $this->mgeneral->get_data("*", "formas", array("form_ugel_ide = $ugel", "form_esta_ide = 1")),
                "aactual" => $aactual,
                "aini" => $aini,
                "afin" => $afin,
                "subidos" => $this->mgeneral->get_data("*", "subidos", array("subi_ugel_ide = $ugel", "subi_anio = $aactual", "subi_esta_ide = 1")),
                "tabla_data" => count($tabla_data)
            ),
        );
        $this->funciones->crearInterface($vistas, $datas);
    }
    public function foreg()
    {
        $ugel = $this->session->userdata("ugel_ide");
        $vistas = array(
            "admin/vforeg",
        );
        $datas = array(
            array(
                "formas" => $this->mgeneral->get_data("*", "formas,estados", array("form_ugel_ide = $ugel", "form_esta_ide = esta_ide"))
            ),
        );
        $this->funciones->crearInterface($vistas, $datas);
    }
    public function set_estado()
    {
        print_r($_POST);
        $data = array(
            "form_esta_ide" => $this->input->post("esta_ide"),
        );
        $where = array(
            "form_ide" => $this->input->post("form_ide"),
            "form_ugel_ide" => $this->session->userdata("ugel_ide"),
        );
        $this->mgeneral->actualizar("formas", $where, $data);
    }
    public function subir_boletas()
    {
        print_r($_POST);
    }
    public function insboleta($anio)
    {
        $ugel = $this->session->userdata("ugel_ide");
        $_POST["data_ugel_ide"] = $this->session->userdata("ugel_ide");
        $_POST["data_usua_ide"] = $this->session->userdata("usua_ide");
        $_POST["data_esta_ide"] = 1;
        echo $this->mgeneral->insertar("data_" . $ugel . "_" . $anio, $_POST);
    }
    public function inssubidos()
    {
        $ugel_ide = $this->session->userdata("ugel_ide");
        $usua_ide = $this->session->userdata("usua_ide");
        $form_ide = $_POST["subi_form_ide"];
        $anio = $_POST["subi_anio"];
        $mes = $_POST["subi_mes"];
        $_POST["subi_ugel_ide"] = $ugel_ide;
        $_POST["subi_usua_ide"] = $usua_ide;
        $_POST["subi_esta_ide"] = 1;
        $where = array(
            "subi_form_ide = $form_ide",
            "subi_anio = $anio",
            "subi_mes = $mes",
            "subi_ugel_ide = $ugel_ide",
            "subi_usua_ide = $usua_ide",
            "subi_esta_ide = 1",
        );
        if (count($this->mgeneral->get_data("*", "subidos", $where)) == 0) {
            $this->mgeneral->insertar("subidos", $_POST);
        }
    }
    public function delbol()
    {
        print_r($_POST);
        $ugel_ide = $this->session->userdata("ugel_ide");
        $anio = $this->input->post("anio");
        $mes = $this->input->post("mes");
        $form_ide = $this->input->post("form_ide");
        $this->mgeneral->actualizar(
            "data" . $anio,
            array(
                "data_mes" => $mes,
                "data_form_ide" => $form_ide,
                "data_ugel_ide" => $ugel_ide
            ),
            array(
                "data_esta_ide" => 3
            )
        );
        $this->mgeneral->actualizar(
            "subidos",
            array(
                "subi_anio" => $anio,
                "subi_mes" => $mes,
                "subi_form_ide" => $form_ide,
                "subi_ugel_ide" => $ugel_ide
            ),
            array(
                "subi_esta_ide" => 3
            )
        );
    }
    public function usuarios()
    {
        $ugel_ide = $this->session->userdata("ugel_ide");
        $vistas = array(
            "admin/vusuarios",
        );
        $datas = array(
            array(),
        );
        $this->funciones->crearInterface($vistas, $datas);
    }
    public function get_usuarios()
    {
        $ugel_ide = $this->session->userdata("ugel_ide");
        $m = $this->input->post("m");
        $p = $this->input->post("p");
        $b = $this->input->post("b");
        if ($p == 0) {
            $p = 1;
        }
        $total = $this->mgeneral->get_usuarios($b, 0, 90000);
        $pgs = intval(count($total) / $m);
        if (count($total) % $m > 0) {
            $pgs++;
        }
        if ($p > $pgs) {
            $p = 1;
        }
        $ini = $m * ($p - 1);
        $fin = $m * $p - 1;
        $usuarios = $this->mgeneral->get_usuarios($b, $ini, $m);
        $data = "";
        $c = $ini + 1;
        foreach ($usuarios as $reg) {
            $data .= "
            <tr>
                <td width='05%'>$c</td>
                <td width='10%'>$reg->usua_user</td>
                <td width='20%'>$reg->usua_apellidos</td>
                <td width='20%'>$reg->usua_nombres</td>
                <td width='20%'>$reg->usua_correo</td>
                <td width='10%'>$reg->usua_cell</td>
                <td width='15%'></td>
            </tr>";
            $c++;
        }
        $pgs_opt = "";
        for ($i = 1; $i <= $pgs; $i++) {
            $pgs_opt .= "<option value=$i>$i</option>";
        }

        $result = array(
            "tabla" => $data,
            "cont" => count($usuarios),
            "ini" => $ini,
            "fin" => $fin,
            "pag" => $p,
            "pgs" => $pgs,
            "pgs_opt" => $pgs_opt
        );
        echo json_encode($result);
    }
    public function get_usuarios_excel()
    {
        $ugel_ide = $this->session->userdata("ugel_ide");
        $b = $this->input->get("b");
        $usuarios = $this->mgeneral->get_usuarios($b, 0, 90000);
        header("Content-type: application/vnd.ms-excel; name='excel'");
        header("Content-Disposition: filename=Usuarios.xls");
        header("Pragma: no-cache");
        header("Expires: 0");
        $this->load->view("admin/vget_usuarios_excel", array("usuarios" => $usuarios));
    }
    public function nuevo_usuario()
    {
        $ugel = $this->session->userdata("ugel_ide");
        $m = "10" . $this->input->post("d");
        $d = $this->input->post("d");
        $n = $this->input->post("n");
        $a = $this->input->post("a");
        $i = $this->input->post("i");
        $user_ugel = count($this->mgeneral->get_data("*", "usuarios", array("usua_user = '$m'", "usua_ugel_ide = $ugel")));
        $user_otra = count($this->mgeneral->get_data("*", "usuarios", array("usua_user = '$m'", "usua_ugel_ide <> $ugel")));
        if ($user_ugel > 0) {
            $col = "info";
            $txt = "El usuario ya se encuentra registrado en esta UGEL";
        } else if ($user_otra > 0) {
            $where = array(
                "usua_user" => $m,
                "usua_tipo_nombre" => "USUARIO"
            );
            $data = array(
                "usua_ugel_ide" => $ugel,
                "usua_esta_ide" => "1",
                "usua_tipo_nombre" => "USUARIO"
            );
            $this->mgeneral->actualizar("usuarios", $where, $data);
            $col = "warning";
            $txt = "El usuario se encontraba registrado en otra UGEL ahora esta registrada en esta UGEL";
        } else if ($user_otra == 0 && $user_ugel == 0) {
            $data = array(
                "usua_ugel_ide" => $ugel,
                "usua_user" => $m,
                "usua_pass" => $d,
                "usua_nombres" => $n,
                "usua_apellidos" => $a,
                "usua_esta_ide" => "1",
                "usua_tipo_nombre" => "USUARIO"
            );
            $this->mgeneral->insertar("usuarios", $data);
            $col = "success";
            $txt = "El usuario no se encontraba registrado en ninguna UGEL, ahora esta registrada en esta UGEL";
        }
        $res = array(
            "col" => $col,
            "txt" => $txt,
            "i" => $i
        );
        echo json_encode($res);
    }
    public function habilitar()
    {
        $anio = $this->input->post("anio");
        $ugel_ide = $this->session->userdata("ugel_ide");
        $this->mgeneral->tabla_data($anio, $ugel_ide);
    }
    public function config()
    {
        $ugel_ide = $this->session->userdata("ugel_ide");
        $vistas = array(
            "admin/vconfig",
        );
        $datas = array(
            array(
                "config" => $this->mgeneral->get_data("*", "ugel", array("ugel_ide = $ugel_ide")),
            ),
        );
        $this->funciones->crearInterface($vistas, $datas);
    }
    public function datos_ugel()
    {
        $config['overwrite'] = true;
        $config['upload_path'] = './img/';
        $config['allowed_types'] = 'jpg';
        $config['max_size'] = '10000';
        $config['encrypt_name'] = true;
        $this->load->library('upload', $config);
        if ($this->upload->do_upload("ugel_logo")) {
            $mi_file = $this->upload->data();
            $_POST["ugel_logo"] = $mi_file["file_name"];
        }
        $ugel_ide = $this->session->userdata("ugel_ide");
        $this->mgeneral->actualizar("ugel", array("ugel_ide" => $ugel_ide), $_POST);
        redirect(site_url("admin/config?i=0&j=3&nombre=Configuracion"));
    }
    public function datos_boletas()
    {
        $config['overwrite'] = true;
        $config['upload_path'] = './img/';
        $config['allowed_types'] = 'jpg';
        $config['max_size'] = '10000';
        $config['encrypt_name'] = true;
        $this->load->library('upload', $config);
        if ($this->upload->do_upload("ugel_sello")) {
            $mi_file = $this->upload->data();
            $_POST["ugel_sello"] = $mi_file["file_name"];
        }
        $ugel_ide = $this->session->userdata("ugel_ide");
        $this->mgeneral->actualizar("ugel", array("ugel_ide" => $ugel_ide), $_POST);
        redirect(site_url("admin/config?i=0&j=3&nombre=Configuracion"));
    }
}
