<?php
 // FECHA Y HORA
 $dateStart = $_GET['dateStart'];
 $dateEnd = $_GET['dateEnd'];
 $timeStart = $_GET['timeStart'];
 $timeEnd = $_GET['timeEnd'];
    
include_once('../head.php');
require_once('../models/queryReports.php');
?>
<br>
<!-- Columns start at 50% wide on mobile and bump up to 33.3% wide on desktop -->
<body>
        <div class="row">
        <div class="col-6 col-md-1"></div>
        <div class="col-6 col-md-10">

        <h3 class="text-center"> Report: Answered Calls < 40 and > 40 Sec. <br>
        <strong> <?php echo $dateStart?></strong> to <strong><?php echo $dateEnd?></strong>
        </h3>
            <br>
            <div class="text-center">
                <a href="../index.php?dateStart=<?php echo urlencode($dateStart); ?>&dateEnd=<?php echo urlencode($dateEnd); ?>&timeStart=<?php echo urlencode($timeStart); ?>&timeEnd=<?php echo urlencode($timeEnd); ?>&service=<?php echo urlencode($serviceName); ?>" class="btn btn-info" id="submit">
                    <i class="fa fa-chevron-left"></i> Back
                </a>
            </div>

            <br><br>
            <table class="table table-hover table-bordered" border="1" bordercolor="#666666" id="calls_answered" style="border-collapse:collapse;">
                <thead>
                    <tr>
                        <th class="th2 text-center">Date</th>
                        <th class="th2 text-center">Answered Calls < 40 sec</th>
                        <th class="th2 text-center">Answered Calls > 40 sec</th>
                    </tr>
                    
                </thead>
                <tbody>
                <?php
                // Answered query
                $AnsAvgWT=0;
                $queryAnswered="SELECT Date(FROM_UNIXTIME(time_id)) As Date,
                COUNT(DISTINCT(uniqueid)) As Answered
                FROM asteriskcdrdb.cdr
                INNER JOIN aasterisk.queue_log On(queue_log.call_id=cdr.uniqueid)
                WHERE lastapp = 'Queue'
                AND verb IN('COMPLETEAGENT', 'COMPLETECALLER')
                AND FROM_UNIXTIME(time_id) BETWEEN '$dateStart $timeStart' AND '$dateEnd $timeEnd'
                GROUP BY Date
                WITH ROLLUP";

                $resultAnswered = $mysqli->query($queryAnswered);
                while ($rowAswered=mysqli_fetch_array($resultAnswered)){
                $date = $rowAswered["Date"];     
                $AnsAvgWT = $rowAswered["Answered"];


                $queryAnsL40S="SELECT Date(FROM_UNIXTIME(time_id)) As Date1,
                COUNT(DISTINCT(uniqueid)) As AnsL40S
                FROM asteriskcdrdb.cdr
                INNER JOIN aasterisk.queue_log On(queue_log.call_id=cdr.uniqueid)
                WHERE lastapp = 'Queue'
                AND data1 <= 40
                AND verb IN('COMPLETEAGENT', 'COMPLETECALLER')
                AND FROM_UNIXTIME(time_id) BETWEEN '$dateStart $timeStart' AND '$dateEnd $timeEnd'
                GROUP BY Date1
                WITH ROLLUP";

                $resultAnsL40S = $mysqli->query($queryAnsL40S);
                while ($rowAnsL40S=mysqli_fetch_array($resultAnsL40S)){
                $date1 = $rowAnsL40S["Date1"];

                if($date1 == $date) {
                $AnsL40S = $rowAnsL40S["AnsL40S"];
                }    
                }
                 
                $queryAnsG40S="SELECT Date(FROM_UNIXTIME(time_id)) As Date2,
                COUNT(DISTINCT(uniqueid)) As AnsG40S
                FROM asteriskcdrdb.cdr
                INNER JOIN aasterisk.queue_log On(queue_log.call_id=cdr.uniqueid)
                WHERE lastapp = 'Queue'
                AND data1 > 40
                AND verb IN('COMPLETEAGENT', 'COMPLETECALLER')
                AND FROM_UNIXTIME(time_id) BETWEEN '$dateStart $timeStart' AND '$dateEnd $timeEnd'
                GROUP BY Date2
                WITH ROLLUP";

                $resultAnsG40S = $mysqli->query($queryAnsG40S);
                while ($rowAnsG40S=mysqli_fetch_array($resultAnsG40S)){
                $date2 = $rowAnsG40S["Date2"];

                if($date2 == $date) {
                $AnsG40S = $rowAnsG40S["AnsG40S"];
                }     
                }

                if($date == NULL){$date = "Total";}

                // Rellenar vacíos con 0's
                if($AnsL40S == ""){$AnsL40S = "0";}
                if($AnsG40S == ""){$AnsG40S = "0";}
                   
                ?>
                <tr>
                    <td><?php echo $date ?></td>
                    <td><?php echo $AnsL40S  ?></td>
                    <td><?php echo $AnsG40S  ?></td>
                </tr>
            <?php
                    }
            ?>
            </tbody>
            </table>

            <br/>

        </div>
    </div>

      
    <!-- Datatables CSS -->
    <link rel="stylesheet" type="text/css" href="datatables/datatables.min.css"/>
    <!-- Datatables Bootstrap 4 CSS -->
    <link rel="stylesheet" type="text/css" href="datatables/DataTables-1.10.18/css/dataTables.bootstrap4.min.css">
    <!-- Datatables JS -->
    <script type="text/javascript" src="datatables/datatables.min.js"></script>
    <!-- Usar botones en datatables JS -->
    <script src="datatables/Buttons-1.5.6/js/dataTables.buttons.min.js"></script>
    <script src="datatables/JSZip-2.5.0/jszip.min.js"></script>
    <script src="datatables/pdfmake-0.1.36/pdfmake.min.js"></script>
    <script src="datatables/pdfmake-0.1.36/vfs_fonts.js"></script>
    <script src="datatables/Buttons-1.5.6/js/buttons.html5.min.js"></script>

    <script type="text/javascript" src="js/tables.js"></script>
    
    <?php include_once('foot.php');?>
    
</body>
</html>
