<?php

require_once "BASEPATH.php";

class Holiday extends MY_Controller
{
    public function addHoliday()
    {
        $idioma = isset($_COOKIE["idioma"]) ? $_COOKIE["idioma"] : "es";
        switch ($idioma) {
            case "es":
                $this->lang->load('holiday', "spanish");
                break;
            case "en":
                $this->lang->load('holiday', "english");
                break;
            default:
                $this->lang->load('holiday', "spanish");
                break;
        }
        include_once RUTA_APP . 'Conexion.php';
        if (!empty($_SESSION["Usuario"]) && strpos($_SESSION["Grupo"], GRUPO_ADMINS) !== false) {
            $usuario = $_SESSION["Usuario"];
            include_once RUTA_APP . 'Sesiones.php';
            $sal = array();
            $con = getConnection();
            $cliente = $_SESSION["Cliente"];
            $holiday_id = limpia($_POST["holiday_id"]);
            $holiday_name = limpia($_POST["holiday_name"]);
            $holiday_comments = limpia($_POST["holiday_comments"]);
            $holiday_date = limpia($_POST["holiday_date"]);
            $holiday_status = limpia($_POST["holiday_status"]);
            $ct_default_start = limpia($_POST["ct_default_start"]);
            $ct_default_stop = limpia($_POST["ct_default_stop"]);
            $default_afterhours_filename_override = limpia($_POST["default_afterhours_filename_override"]);
            $user_group = $cliente;
            $query = "INSERT INTO vicidial_call_time_holidays(holiday_id,holiday_name,holiday_comments,holiday_date,holiday_status,ct_default_start,ct_default_stop,default_afterhours_filename_override,user_group) VALUES('$holiday_id','$holiday_name','$holiday_comments','$holiday_date','$holiday_status','$ct_default_start','$ct_default_stop','$default_afterhours_filename_override','$user_group')";
            sv_mysqli_query($con, $query);
            $sal["Estado"] = "ok";
            $this->output->set_output(json_encode($sal));
            sv_mysqli_close($con);
        } else {
            $sal = array();
            $sal["Estado"] = "error";
            $sal["Descripcion"] = $this->lang->line("desc_permisosAccion");
            $this->output->set_output(json_encode($sal));
        }
    }
    public function updateHoliday()
    {
        $idioma = isset($_COOKIE["idioma"]) ? $_COOKIE["idioma"] : "es";
        switch ($idioma) {
            case "es":
                $this->lang->load('holiday', "spanish");
                break;
            case "en":
                $this->lang->load('holiday', "english");
                break;
            default:
                $this->lang->load('holiday', "spanish");
                break;
        }
        include_once RUTA_APP . 'Conexion.php';
        if (!empty($_SESSION["Usuario"]) && strpos($_SESSION["Grupo"], GRUPO_ADMINS) !== false) {
            $usuario = $_SESSION["Usuario"];
            include_once RUTA_APP . 'Sesiones.php';
            $sal = array();
            $con = getConnection();
            $cliente = $_SESSION["Cliente"];

            $holiday_id = limpia($_POST["holiday_id"]);
            $holiday_name = limpia($_POST["holiday_name"]);
            $holiday_comments = limpia($_POST["holiday_comments"]);
            $holiday_date = limpia($_POST["holiday_date"]);
            $holiday_status = limpia($_POST["holiday_status"]);
            $ct_default_start = limpia($_POST["ct_default_start"]);
            $ct_default_stop = limpia($_POST["ct_default_stop"]);
            $default_afterhours_filename_override = limpia($_POST["default_afterhours_filename_override"]);
            $user_group = $cliente;
            $query = "UPDATE vicidial_call_time_holidays SET holiday_name='$holiday_name',holiday_comments='$holiday_comments',holiday_date='$holiday_date',holiday_status='$holiday_status',ct_default_start='$ct_default_start',ct_default_stop='$ct_default_stop',default_afterhours_filename_override='$default_afterhours_filename_override',user_group='$user_group' WHERE holiday_id='$holiday_id' AND user_group='$user_group'";
            sv_mysqli_query($con, $query);
            $sal["Estado"] = "ok";

            $this->output->set_output(json_encode($sal));
            sv_mysqli_close($con);
        } else {
            $sal = array();
            $sal["Estado"] = "error";
            $sal["Descripcion"] = $this->lang->line("desc_permisosAccion");
            $this->output->set_output(json_encode($sal));
        }
    }

    public function deleteHoliday()
    {
        $idioma = isset($_COOKIE["idioma"]) ? $_COOKIE["idioma"] : "es";
        switch ($idioma) {
            case "es":
                $this->lang->load('holiday', "spanish");
                break;
            case "en":
                $this->lang->load('holiday', "english");
                break;
            default:
                $this->lang->load('holiday', "spanish");
                break;
        }
        include_once RUTA_APP . 'Conexion.php';
        if (!empty($_SESSION["Usuario"]) && strpos($_SESSION["Grupo"], GRUPO_ADMINS) !== false) {
            $usuario = $_SESSION["Usuario"];
            include_once RUTA_APP . 'Sesiones.php';
            $sal = array();
            $con = getConnection();
            $cliente = $_SESSION["Cliente"];

            $holiday_id = limpia($_POST["holiday_id"]);

            $user_group = $cliente;

            $query = "DELETE FROM  vicidial_call_time_holidays WHERE holiday_id='$holiday_id' AND user_group='$user_group'";
            sv_mysqli_query($con, $query);
            $sal["Estado"] = "ok";

            $this->output->set_output(json_encode($sal));
            sv_mysqli_close($con);
        } else {
            $sal = array();
            $sal["Estado"] = "error";
            $sal["Descripcion"] = $this->lang->line("desc_permisosAccion");
            $this->output->set_output(json_encode($sal));
        }

    }
    public function getHoliday()
    {
        $idioma = isset($_COOKIE["idioma"]) ? $_COOKIE["idioma"] : "es";
        switch ($idioma) {
            case "es":
                $this->lang->load('holiday', "spanish");
                break;
            case "en":
                $this->lang->load('holiday', "english");
                break;
            default:
                $this->lang->load('holiday', "spanish");
                break;
        }
        include_once RUTA_APP . 'Conexion.php';
        if (!empty($_SESSION["Usuario"]) && strpos($_SESSION["Grupo"], GRUPO_ADMINS) !== false) {
            $usuario = $_SESSION["Usuario"];
            include_once RUTA_APP . 'Sesiones.php';
            $sal = array();
            $con = getConnection();
            $cliente = $_SESSION["Cliente"];

            $user_group = $cliente;
            $holiday_id = limpia($_POST["holiday_id"]);
            $query = "SELECT * FROM vicidial_call_time_holidays WHERE holiday_id='$holiday_id' AND user_group='$user_group' LIMIT 1";
            $result = sv_mysqli_query($con, $query);
            if ($row = sv_mysqli_fetch_array($result)) {
                $sal["CallTime"] = $row;
                $sal["Estado"] = "ok";
            } else {
                $sal["Estado"] = "error";
                $sal["Descripcion"] = $this->lang->line("desc_ausencia");

            }
            $this->output->set_output(json_encode($sal));
            sv_mysqli_close($con);
        } else {
            $sal = array();
            $sal["Estado"] = "error";
            $sal["Descripcion"] = $this->lang->line("desc_permisosAccion");
            $this->output->set_output(json_encode($sal));
        }

    }
    public function getAllHolidaysSelect()
    {
        $idioma = isset($_COOKIE["idioma"]) ? $_COOKIE["idioma"] : "es";
        switch ($idioma) {
            case "es":
                $this->lang->load('holiday', "spanish");
                break;
            case "en":
                $this->lang->load('holiday', "english");
                break;
            default:
                $this->lang->load('holiday', "spanish");
                break;
        }
        include_once RUTA_APP . 'Conexion.php';
        if (!empty($_SESSION["Usuario"]) && strpos($_SESSION["Grupo"], GRUPO_ADMINS) !== false) {
            $usuario = $_SESSION["Usuario"];
            include_once RUTA_APP . 'Sesiones.php';
            $sal = array();
            $con = getConnection();
            $cliente = $_SESSION["Cliente"];

            $user_group = $cliente;

            $query = "SELECT holiday_id,holiday_name FROM vicidial_call_time_holidays WHERE user_group='$user_group'";
            $result = sv_mysqli_query($con, $query);
            $sal["Registros"] = array();
            while ($row = sv_mysqli_fetch_array($result)) {
                $sal["Registros"][] = $row;
            }
            $sal["Estado"] = "ok";
            $this->output->set_output(json_encode($sal));
            sv_mysqli_close($con);
        } else {
            $sal = array();
            $sal["Estado"] = "error";
            $sal["Descripcion"] = $this->lang->line("desc_permisosAccion");
            $this->output->set_output(json_encode($sal));
        }

    }
    public function getAllHolidays()
    {
        $idioma = isset($_COOKIE["idioma"]) ? $_COOKIE["idioma"] : "es";
        switch ($idioma) {
            case "es":
                $this->lang->load('holiday', "spanish");
                break;
            case "en":
                $this->lang->load('holiday', "english");
                break;
            default:
                $this->lang->load('holiday', "spanish");
                break;
        }
        include_once RUTA_APP . 'Conexion.php';
        if (!empty($_SESSION["Usuario"]) && strpos($_SESSION["Grupo"], GRUPO_ADMINS) !== false) {
            $usuario = $_SESSION["Usuario"];
            include_once RUTA_APP . 'Sesiones.php';
            $sal = array();
            $con = getConnection();
            $cliente = $_SESSION["Cliente"];
            $pagina = limpia($_POST["pagina"]);
            $limit = 10;
            $skip = ($pagina - 1) * $limit;

            $user_group = $cliente;

            $query = "SELECT holiday_id,holiday_name,holiday_comments,holiday_date,holiday_status FROM vicidial_call_time_holidays WHERE  user_group='$user_group' LIMIT $limit OFFSET $skip";
            $result = sv_mysqli_query($con, $query);
            $sal["Registros"] = array();
            while ($row = sv_mysqli_fetch_array($result)) {
                $sal["Registros"][] = $row;
            }
            $sal["Estado"] = "ok";
            $this->output->set_output(json_encode($sal));
            sv_mysqli_close($con);
        } else {
            $sal = array();
            $sal["Estado"] = "error";
            $sal["Descripcion"] = $this->lang->line("desc_permisosAccion");
            $this->output->set_output(json_encode($sal));
        }

    }
    public function index()
    {
        $this->load->view('holiday/head');
        $this->load->view('holiday/vw_holiday');
        $this->load->view('holiday/foot');
    }
}
