<?php

namespace App\Controllers;

use App\Models\Generaldb;

class Admin extends BaseController
{
    public function index(): string
    {
        return view('welcome_message');
    }
    public function login($iiee_ide)
    {
        $general = new Generaldb();
        $ie = $general->selectSomeData(
            "*",
            "iiee i",
            "
                i.iiee_ide = $iiee_ide
            "
        );
        $dataLogin = array(
            "nombre_ie" => $ie[0]->iiee_nombre,
            "insignia" => $ie[0]->iiee_insignia,
            "fondo" => $ie[0]->iiee_fondo,
            "ubica" => $ie[0]->iiee_depa . "-" . $ie[0]->iiee_prov . "-" . $ie[0]->iiee_dist,
            "iiee_ide" => $iiee_ide,
        );
        return view("votacion/vlogin_admin", $dataLogin);
    }
    public function verificar()
    {
        $iiee = $this->request->getPost("iiee");
        $user = $this->request->getPost("user");
        $pass = $this->request->getPost("pass");
        $general = new Generaldb();
        $where = "
            usua_iiee_ide = '$iiee' AND 
            usua_user = '$user' AND
            usua_pass = '$pass' AND
            usua_esta_ide = 1 
        ";
        $usua = $general->selectSomeData("*", "usuarios", $where);
        if (count($usua) == 1) {
            $data_session = array(
                "user" => $usua[0]->usua_user,
                "datos" => $usua[0]->usua_datos,
                "iiee" => $usua[0]->usua_iiee_ide,
                "login" => "verificado"
            );
            $session = session();
            $session->set($data_session);
            return redirect()->to(site_url("web/"));
        } else {
            return redirect()->to(site_url("admin/" . $iiee));
        }
    }
    public function vista($vista, $dataVista, $iiee_ide, $menu)
    {
        $general = new Generaldb();
        $ie = $general->selectSomeData(
            "*",
            "iiee i",
            "
                i.iiee_ide = $iiee_ide
            "
        );
        $dataMenu = array(
            "nombre_ie" => $ie[0]->iiee_nombre,
            "insignia" => $ie[0]->iiee_insignia,
            "fondo" => $ie[0]->iiee_fondo,
            "ubica" => $ie[0]->iiee_depa . "-" . $ie[0]->iiee_prov . "-" . $ie[0]->iiee_dist,
            "iiee_ide" => $iiee_ide,
            "menu" => $menu
        );

        return
            view("sistema/vheader") .
            view("sistema/vmenu", $dataMenu) .
            view("sistema/vbarra", $dataBarra = array()) .
            view($vista, $dataVista) .
            view("sistema/vfooter");
    }
    public function sistema()
    {
        $session = session();
        if ($session->login != "verificado") {
            echo "No permitido...";
            return false;
        }
        $dataVista = array();
        $iiee_ide = $session->iiee;
        $menu = array(
            array(
                "icono" => "ph ph-stack",
                "modulo" => "Elecciones",
                "items" => array(
                    array("url" => "acta", "label" => "Acta Electoral"),
                )
            )
        );
        return $this->vista("votacion/vsistema", $dataVista, $iiee_ide, $menu);
    }
    public function get_ahora()
    {
        date_default_timezone_set('America/Lima');
        return date('Y-m-d H:i:s');
    }
    public function acta_elecoral()
    {
        $session = session();
        if ($session->login != "verificado") {
            echo "No permitido...";
            return false;
        }
        $general = new Generaldb();
        $proc = $general->selectSomeData("*", "procesos p", "p.proc_esta_ide = 1");
        $iiee_ide = $session->iiee;
        $proc_ide = $proc[0]->proc_ide;
        $dataVista = array(
            "result" => $general->selectSomeData(
                "
                    v.voto_list_ide,
                    l.list_nombre,
                    l.list_foto,
                    COUNT(v.voto_ide) AS conteo
                ",
                "
                    votos v,
	                listas l
                ",
                "
                    v.voto_list_ide = l.list_ide AND
                    voto_iiee_ide = $iiee_ide AND
                    voto_proc_ide = $proc_ide
                ",
                "
                    COUNT(v.voto_ide) desc
                ",
                "
                    voto_list_ide
                "
            ),
            "proc" => $proc,
            "iiee" => $general->selectSomeData("*", "iiee i", "i.iiee_ide = $iiee_ide"),
            "fecha" => $this->get_ahora()
        );
        $menu = array(
            array(
                "icono" => "ph ph-stack",
                "modulo" => "Elecciones",
                "items" => array(
                    array("url" => "acta", "label" => "Acta Electoral"),
                )
            )
        );
        return $this->vista("votacion/vacta_electoral", $dataVista, $iiee_ide, $menu);
    }
}
