<?php

namespace App\Controllers;

use App\Models\Generaldb;

class Votar extends BaseController
{
    public function index(): string
    {
        return view('welcome_message');
    }
    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 voto($iiee_ide)
    {
        /*$menu = array(
            array(
                "icono" => "ph ph-stack",
                "modulo" => "Elecciones",
                "items" => array(
                    array("url" => "google", "label" => "Google"),
                    array("url" => "facebook", "label" => "Facebook"),
                )
            )
        );*/
        $general = new Generaldb();
        $listas = $general->selectSomeData(
            "*",
            "listas l",
            "
                l.list_iiee_ide = $iiee_ide
            "
        );
        $estu = $general->selectSomeData(
            "*",
            "estudiantes e",
            "
                e.estu_dni = " . $this->request->getGet("dni") . "
            "
        );
        $proc = $general->selectSomeData(
            "*",
            "procesos p",
            "
                p.proc_esta_ide = 1
            "
        );
        $menu = array();
        $dataVista = array(
            "listas" => $listas,
            "estu" => $estu,
            "proc" => $proc
        );
        if (count($estu) == 0) {
            $dataVista = array(
                "dni" => $this->request->getGet("dni"),
                "iiee_ide" => $iiee_ide
            );
            return $this->vista("votacion/vnoexistedni", $dataVista, $iiee_ide, $menu);
        }
        $mivoto = $general->selectSomeData(
            "*",
            "votos v,listas l",
            "
                v.voto_list_ide = l.list_ide AND
                v.voto_iiee_ide = " . $iiee_ide . " AND
                v.voto_proc_ide = " . $proc[0]->proc_ide . " AND
                v.voto_estu_ide = " . $estu[0]->estu_ide . "
            "
        );
        if (count($mivoto) > 0) {
            $dataVista["mivoto"] = $mivoto;
            return $this->vista("votacion/vyavoto", $dataVista, $iiee_ide, $menu);
        }
        return $this->vista("sistema/vmain", $dataVista, $iiee_ide, $menu);
    }
    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", $dataLogin);
    }
    public function get_ahora()
    {
        date_default_timezone_set('America/Lima');
        return date('Y-m-d H:i:s');
    }
    public function guardar_voto()
    {
        $data = array(
            "voto_iiee_ide" => $this->request->getPost("iiee"),
            "voto_proc_ide" => $this->request->getPost("proc"),
            "voto_list_ide" => $this->request->getPost("list"),
            "voto_estu_ide" => $this->request->getPost("estu"),
            "voto_create" => $this->get_ahora(),
        );
        $general = new Generaldb();
        $general->insertData("votos", $data);
        print_r($data);
    }
}

/*$ie = $general->selectSomeData(
            '*',
            'iiee i',
            "
                i.iiee_ide = $iiee_ide
            ",
            'ins_nombre ASC',
            false,
            false,
            array(
                'sd_plan_instrumento',
                'sd_plan_instrumento.ins_ide = sd_instrumento.ins_ide',
                'JOIN'
            )
);*/