<?php

require_once "BASEPATH.php";

class CallTime extends MY_Controller
{
    public function addCallTime()
    {
        $idioma = isset($_COOKIE["idioma"]) ? $_COOKIE["idioma"] : "es";
        switch ($idioma) {
            case "es":
                $this->lang->load('calltime', "spanish");
                break;
            case "en":
                $this->lang->load('calltime', "english");
                break;
            default:
                $this->lang->load('calltime', "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"];

            $call_time_id = limpia($_POST["call_time_id"]);
            $call_time_name = limpia($_POST["call_time_name"]);
            $call_time_comments = limpia($_POST["call_time_comments"]);
            $ct_default_start = limpia($_POST["ct_default_start"]);
            $ct_default_stop = limpia($_POST["ct_default_stop"]);
            $ct_sunday_start = limpia($_POST["ct_sunday_start"]);
            $ct_sunday_stop = limpia($_POST["ct_sunday_stop"]);
            $ct_monday_start = limpia($_POST["ct_monday_start"]);
            $ct_monday_stop = limpia($_POST["ct_monday_stop"]);
            $ct_tuesday_start = limpia($_POST["ct_tuesday_start"]);
            $ct_tuesday_stop = limpia($_POST["ct_tuesday_stop"]);
            $ct_wednesday_start = limpia($_POST["ct_wednesday_start"]);
            $ct_wednesday_stop = limpia($_POST["ct_wednesday_stop"]);
            $ct_thursday_start = limpia($_POST["ct_thursday_start"]);
            $ct_thursday_stop = limpia($_POST["ct_thursday_stop"]);
            $ct_friday_start = limpia($_POST["ct_friday_start"]);
            $ct_friday_stop = limpia($_POST["ct_friday_stop"]);
            $ct_saturday_start = limpia($_POST["ct_saturday_start"]);
            $ct_saturday_stop = limpia($_POST["ct_saturday_stop"]);
            $ct_state_call_times = limpia($_POST["ct_state_call_times"]);
            $default_afterhours_filename_override = limpia($_POST["default_afterhours_filename_override"]);
            $sunday_afterhours_filename_override = limpia($_POST["sunday_afterhours_filename_override"]);
            $monday_afterhours_filename_override = limpia($_POST["monday_afterhours_filename_override"]);
            $tuesday_afterhours_filename_override = limpia($_POST["tuesday_afterhours_filename_override"]);
            $wednesday_afterhours_filename_override = limpia($_POST["wednesday_afterhours_filename_override"]);
            $thursday_afterhours_filename_override = limpia($_POST["thursday_afterhours_filename_override"]);
            $friday_afterhours_filename_override = limpia($_POST["friday_afterhours_filename_override"]);
            $saturday_afterhours_filename_override = limpia($_POST["saturday_afterhours_filename_override"]);
            $user_group = $cliente;
            $ct_holidays = limpia($_POST["ct_holidays"]);
            $query = "INSERT INTO vicidial_call_times(call_time_id,call_time_name,call_time_comments,ct_default_start,ct_default_stop,ct_sunday_start,ct_sunday_stop,ct_monday_start,ct_monday_stop,ct_tuesday_start,ct_tuesday_stop,ct_wednesday_start,ct_wednesday_stop,ct_thursday_start,ct_thursday_stop,ct_friday_start,ct_friday_stop,ct_saturday_start,ct_saturday_stop,ct_state_call_times,default_afterhours_filename_override,sunday_afterhours_filename_override,monday_afterhours_filename_override,tuesday_afterhours_filename_override,wednesday_afterhours_filename_override,thursday_afterhours_filename_override,friday_afterhours_filename_override,saturday_afterhours_filename_override,user_group,ct_holidays) VALUES('$call_time_id','$call_time_name','$call_time_comments','$ct_default_start','$ct_default_stop','$ct_sunday_start','$ct_sunday_stop','$ct_monday_start','$ct_monday_stop','$ct_tuesday_start','$ct_tuesday_stop','$ct_wednesday_start','$ct_wednesday_stop','$ct_thursday_start','$ct_thursday_stop','$ct_friday_start','$ct_friday_stop','$ct_saturday_start','$ct_saturday_stop','$ct_state_call_times','$default_afterhours_filename_override','$sunday_afterhours_filename_override','$monday_afterhours_filename_override','$tuesday_afterhours_filename_override','$wednesday_afterhours_filename_override','$thursday_afterhours_filename_override','$friday_afterhours_filename_override','$saturday_afterhours_filename_override','$user_group','$ct_holidays')";
            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 updateCallTime()
    {
        $idioma = isset($_COOKIE["idioma"]) ? $_COOKIE["idioma"] : "es";
        switch ($idioma) {
            case "es":
                $this->lang->load('calltime', "spanish");
                break;
            case "en":
                $this->lang->load('calltime', "english");
                break;
            default:
                $this->lang->load('calltime', "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"];

            $call_time_id = limpia($_POST["call_time_id"]);
            $call_time_name = limpia($_POST["call_time_name"]);
            $call_time_comments = limpia($_POST["call_time_comments"]);
            $ct_default_start = limpia($_POST["ct_default_start"]);
            $ct_default_stop = limpia($_POST["ct_default_stop"]);
            $ct_sunday_start = limpia($_POST["ct_sunday_start"]);
            $ct_sunday_stop = limpia($_POST["ct_sunday_stop"]);
            $ct_monday_start = limpia($_POST["ct_monday_start"]);
            $ct_monday_stop = limpia($_POST["ct_monday_stop"]);
            $ct_tuesday_start = limpia($_POST["ct_tuesday_start"]);
            $ct_tuesday_stop = limpia($_POST["ct_tuesday_stop"]);
            $ct_wednesday_start = limpia($_POST["ct_wednesday_start"]);
            $ct_wednesday_stop = limpia($_POST["ct_wednesday_stop"]);
            $ct_thursday_start = limpia($_POST["ct_thursday_start"]);
            $ct_thursday_stop = limpia($_POST["ct_thursday_stop"]);
            $ct_friday_start = limpia($_POST["ct_friday_start"]);
            $ct_friday_stop = limpia($_POST["ct_friday_stop"]);
            $ct_saturday_start = limpia($_POST["ct_saturday_start"]);
            $ct_saturday_stop = limpia($_POST["ct_saturday_stop"]);
            $ct_state_call_times = limpia($_POST["ct_state_call_times"]);
            $default_afterhours_filename_override = limpia($_POST["default_afterhours_filename_override"]);
            $sunday_afterhours_filename_override = limpia($_POST["sunday_afterhours_filename_override"]);
            $monday_afterhours_filename_override = limpia($_POST["monday_afterhours_filename_override"]);
            $tuesday_afterhours_filename_override = limpia($_POST["tuesday_afterhours_filename_override"]);
            $wednesday_afterhours_filename_override = limpia($_POST["wednesday_afterhours_filename_override"]);
            $thursday_afterhours_filename_override = limpia($_POST["thursday_afterhours_filename_override"]);
            $friday_afterhours_filename_override = limpia($_POST["friday_afterhours_filename_override"]);
            $saturday_afterhours_filename_override = limpia($_POST["saturday_afterhours_filename_override"]);
            $user_group = $cliente;
            $query = "UPDATE vicidial_call_times SET call_time_name='$call_time_name',call_time_comments='$call_time_comments',ct_default_start='$ct_default_start',ct_default_stop='$ct_default_stop',ct_sunday_start='$ct_sunday_start',ct_sunday_stop='$ct_sunday_stop',ct_monday_start='$ct_monday_start',ct_monday_stop='$ct_monday_stop',ct_tuesday_start='$ct_tuesday_start',ct_tuesday_stop='$ct_tuesday_stop',ct_wednesday_start='$ct_wednesday_start',ct_wednesday_stop='$ct_wednesday_stop',ct_thursday_start='$ct_thursday_start',ct_thursday_stop='$ct_thursday_stop',ct_friday_start='$ct_friday_start',ct_friday_stop='$ct_friday_stop',ct_saturday_start='$ct_saturday_start',ct_saturday_stop='$ct_saturday_stop',ct_state_call_times='$ct_state_call_times',default_afterhours_filename_override='$default_afterhours_filename_override',sunday_afterhours_filename_override='$sunday_afterhours_filename_override',monday_afterhours_filename_override='$monday_afterhours_filename_override',tuesday_afterhours_filename_override='$tuesday_afterhours_filename_override',wednesday_afterhours_filename_override='$wednesday_afterhours_filename_override',thursday_afterhours_filename_override='$thursday_afterhours_filename_override',friday_afterhours_filename_override='$friday_afterhours_filename_override',saturday_afterhours_filename_override='$saturday_afterhours_filename_override' WHERE call_time_id='$call_time_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 addHolidayToCallTime()
    {
        $idioma = isset($_COOKIE["idioma"]) ? $_COOKIE["idioma"] : "es";
        switch ($idioma) {
            case "es":
                $this->lang->load('calltime', "spanish");
                break;
            case "en":
                $this->lang->load('calltime', "english");
                break;
            default:
                $this->lang->load('calltime', "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"];

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

            $user_group = $cliente;
            $holiday = limpia($_POST["holiday"]);

            $query = "SELECT ct_holidays FROM  vicidial_call_times WHERE call_time_id='$call_time_id' AND user_group='$user_group'";
            $result = sv_mysqli_query($con, $query);
            if ($row = sv_mysqli_fetch_array($result)) {
                if (empty($row["ct_holidays"])) {
                    $ct_holidays = "|$holiday|";
                } else {
                    $ct_holidays = $row["ct_holidays"];
                    if (strpos($ct_holidays, "|$holiday|") == false) {
                        $ct_holidays .= "$holiday|";
                    }
                }
                $query = "UPDATE vicidial_call_times SET ct_holidays='$ct_holidays' WHERE call_time_id='$call_time_id' AND user_group='$user_group'";
                sv_mysqli_query($con, $query);
                $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 deleteHolidayToCallTime()
    {
        $idioma = isset($_COOKIE["idioma"]) ? $_COOKIE["idioma"] : "es";
        switch ($idioma) {
            case "es":
                $this->lang->load('calltime', "spanish");
                break;
            case "en":
                $this->lang->load('calltime', "english");
                break;
            default:
                $this->lang->load('calltime', "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"];

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

            $user_group = $cliente;
            $holiday = limpia($_POST["holiday"]);

            $query = "SELECT ct_holidays FROM  vicidial_call_times WHERE call_time_id='$call_time_id' AND user_group='$user_group'";
            $result = sv_mysqli_query($con, $query);
            if ($row = sv_mysqli_fetch_array($result)) {
                if (empty($row["ct_holidays"])) {
                    $sal["Estado"] = "error";
                    $sal["Descripcion"] = $this->lang->line("desc_holidaysAusencia");

                } else {
                    $ct_holidays = $row["ct_holidays"];
                    if (strpos($ct_holidays, "|$holiday|") != false) {
                        $holidays = explode("|", substr($ct_holidays, 1, -1));
                        if (($indice = array_search($holiday, $holidays)) !== false) {
                            unset($holidays[$indice]);
                        }
                        $ct_holidays = "|" . implode($holidays) . "|";
                    }
                    $query = "UPDATE vicidial_call_times SET ct_holidays='$ct_holidays' WHERE call_time_id='$call_time_id' AND user_group='$user_group'";
                    sv_mysqli_query($con, $query);
                    $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 deleteCallTime()
    {
        $idioma = isset($_COOKIE["idioma"]) ? $_COOKIE["idioma"] : "es";
        switch ($idioma) {
            case "es":
                $this->lang->load('calltime', "spanish");
                break;
            case "en":
                $this->lang->load('calltime', "english");
                break;
            default:
                $this->lang->load('calltime', "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"];

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

            $user_group = $cliente;

            $query = "DELETE FROM  vicidial_call_times WHERE call_time_id='$call_time_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 getCallTime()
    {
        $idioma = isset($_COOKIE["idioma"]) ? $_COOKIE["idioma"] : "es";
        switch ($idioma) {
            case "es":
                $this->lang->load('calltime', "spanish");
                break;
            case "en":
                $this->lang->load('calltime', "english");
                break;
            default:
                $this->lang->load('calltime', "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;
            $call_time_id = limpia($_POST["call_time_id"]);
            $query = "SELECT * FROM vicidial_call_times WHERE call_time_id='$call_time_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 getAllCallTimeSelect()
    {
        $idioma = isset($_COOKIE["idioma"]) ? $_COOKIE["idioma"] : "es";
        switch ($idioma) {
            case "es":
                $this->lang->load('calltime', "spanish");
                break;
            case "en":
                $this->lang->load('calltime', "english");
                break;
            default:
                $this->lang->load('calltime', "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 call_time_id,call_time_name FROM vicidial_call_times 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 getAllCallTime()
    {
        $idioma = isset($_COOKIE["idioma"]) ? $_COOKIE["idioma"] : "es";
        switch ($idioma) {
            case "es":
                $this->lang->load('calltime', "spanish");
                break;
            case "en":
                $this->lang->load('calltime', "english");
                break;
            default:
                $this->lang->load('calltime', "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 call_time_id,call_time_name,call_time_comments,ct_default_start,ct_default_stop FROM vicidial_call_times 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('calltime/head');
        $this->load->view('calltime/vw_calltime');
        $this->load->view('calltime/foot');
    }
}
