<?php

require_once "BASEPATH.php";

class Televox extends MY_Controller
{
    private function addZero($numero)
    {
        if ($numero < 10) {
            return "0$numero";
        }
        return "$numero";
    }
    public function index()
    {
        $this->load->view('televox/head');
        $this->load->view('televox/televox');
    }

    public function searchRecording()
    {

        $this->load->helper('download');
        include_once RUTA_APP . 'Conexion.php';
        $con = getConnection();
        $codigo = limpia($_GET["clave"]);
        $lead_id = substr($codigo, 0, -12);
        $fecha = substr($codigo, -12, 4) . "-" . substr($codigo, -8, 2) . "-" . substr($codigo, -6, 2); ///Formato YYYY-MM-DD
        $horaCompleta = substr($codigo, -4); //Formato HH:MM
        $agente = limpia($_GET["agente"]);

        $hora = intval(substr($horaCompleta, 0, 2));
        $minuto = intval(substr($horaCompleta, 2));
        $horaA = $hora;
        $minutoA = $minuto;
        $horaD = $hora;
        $minutoD = $minuto;
        if ($minuto < 5) {
            if ($hora == 0) {
                $horaA += 23;
            } else {
                $hora -= 1;
            }
            $minutoA += 60;
        }
        $minutoA -= 5;
        if ($minuto >= 55) {
            if ($hora == 23) {
                $horaD = 0;
            } else {
                $horaD += 1;
            }
            $minutoD -= 60;
        }
        $minutoD += 5;
        $antes = $this->addZero($horaA) . ":" . $this->addZero($minutoA);
        $despues = $this->addZero($horaD) . ":" . $this->addZero($minutoD);
        $result = sv_mysqli_query($con, "SELECT substr(rl.location,Locate('MP3/',rl.location)+4) AS 'GRAB_FILE', rl.filename AS GRAB_ID,date(rl.start_time) as 'FechaInicio' FROM recording_log AS rl  WHERE rl.user='$agente' AND DATE(rl.start_time)=DATE('$fecha') AND TIME('$antes:00')<=TIME(rl.start_time) AND TIME(rl.start_time)<= TIME('$despues:00') AND rl.lead_id='$lead_id'");
        if ($row = sv_mysqli_fetch_array($result)) {
            $GRAB_FILE = $row["GRAB_FILE"];
            $GRAB_ID = $row["GRAB_FILE"];
            $FechaInicio = $row["FechaInicio"];
            $attachment_location = "/mnt/Grabaciones" . DIRECTORY_SEPARATOR . $GRAB_FILE;
            if (file_exists($attachment_location)) {
                force_download($attachment_location, null);
                readfile($attachment_location);
            } else {
                $attachment_location = "/mnt/Grabaciones" . DIRECTORY_SEPARATOR . str_replace("-", "/", $FechaInicio) . "/" . str_replace(str_replace("-", "/", $FechaInicio) . "/", "", $GRAB_FILE);
                if (file_exists($attachment_location)) {
                    force_download($attachment_location, null);
                    readfile($attachment_location);
                } else {
                    $attachment_location = "/home/vicidial/MP3" . DIRECTORY_SEPARATOR . $GRAB_FILE;
                    if (file_exists($attachment_location)) {
                        force_download($attachment_location, null);
                        readfile($attachment_location);
                    } else {
                        $attachment_location = "/home/vicidial/MP3" . DIRECTORY_SEPARATOR . str_replace("-", "/", $FechaInicio) . "/" . str_replace(str_replace("-", "/", $FechaInicio) . "/", "", $GRAB_FILE);
                        if (file_exists($attachment_location)) {
                            force_download($attachment_location, null);
                            readfile($attachment_location);
                        } else {
                            $attachment_location = "/var/spool/asterisk/monitorDONE/MP3" . DIRECTORY_SEPARATOR . $GRAB_FILE;
                            if (file_exists($attachment_location)) {
                                force_download($attachment_location, null);
                                readfile($attachment_location);
                            } else {
                                $attachment_location = "/var/spool/asterisk/monitorDONE/MP3" . DIRECTORY_SEPARATOR . str_replace("-", "/", $FechaInicio) . "/" . str_replace(str_replace("-", "/", $FechaInicio) . "/", "", $GRAB_FILE);
                                if (file_exists($attachment_location)) {
                                    force_download($attachment_location, null);
                                    readfile($attachment_location);
                                } else {
                                    //echo "No se encontro la grabacion";
                                    $this->output->set_output("No se encontro la grabacion");

                                }
                            }

                        }
                    }

                }
            }
        } else {
            //echo "No se encontro el registro de la grabacion";
            $this->output->set_output("No se encontro el registro de la grabacion");
        }
    }
}
