<?php
/* @var $this yii\web\View */

use backend\assets\AppAsset;

backend\assets\ChartjsAsset::register($this);

$this->title = 'DASHBOARD';
$this->registerCssFile('https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css');
//$this->registerJsFile('@web/js/areaChart.js', [
//    'depends' => [yii\web\JqueryAsset::className('ChartjsAsset')],
//    'position' => \yii\web\View::POS_END,
//        ]
//);
$jsLabel = $jsData = [];
/*
 * foreach ($tramitesTodosSemana as $tramite):
    array_push($jsLabel, "'" . $tramite->comentario . "'");
    array_push($jsData, $tramite->registros);
endforeach;
 */
//echo var_dump($tramitesTodosAgente);
foreach ($tramitesTodosAgenteHoy as $tramite):
    array_push($jsLabel, "'" . $tramite->agente . "'");
    array_push($jsData, $tramite->registros);
endforeach;
$jsLabel = implode(",", $jsLabel);
$jsData = implode(",", $jsData);

$js = <<< JS
      // Get context with jQuery - using jQuery's .get() method.
    var salesChartCanvas = $("#TramitesSemanaChart").get(0).getContext("2d");
    // This will get the first returned node in the jQuery collection.
    var salesChart = new Chart(salesChartCanvas);
    var salesChartData = {
        labels: [$jsLabel],
        datasets: [
            {
                fillColor: "rgba(60,141,188,0.9)",
                strokeColor: "rgba(60,141,188,0.8)",
                pointColor: "#3b8bba",
                pointStrokeColor: "rgba(60,141,188,1)",
                pointHighlightFill: "#fff",
                pointHighlightStroke: "rgba(60,141,188,1)",
                data: [{$jsData}]
            },
        ]
    };

    var salesChartOptions = {
        //Boolean - If we should show the scale at all
        showScale: true,
        //Boolean - Whether grid lines are shown across the chart
        scaleShowGridLines: false,
        //String - Colour of the grid lines
        scaleGridLineColor: "rgba(0,0,0,.05)",
        //Number - Width of the grid lines
        scaleGridLineWidth: 1,
        //Boolean - Whether to show horizontal lines (except X axis)
        scaleShowHorizontalLines: true,
        //Boolean - Whether to show vertical lines (except Y axis)
        scaleShowVerticalLines: true,
        //Boolean - Whether the line is curved between points
        bezierCurve: true,
        //Number - Tension of the bezier curve between points
        bezierCurveTension: 0.3,
        //Boolean - Whether to show a dot for each point
        pointDot: false,
        //Number - Radius of each point dot in pixels
        pointDotRadius: 4,
        //Number - Pixel width of point dot stroke
        pointDotStrokeWidth: 1,
        //Number - amount extra to add to the radius to cater for hit detection outside the drawn point
        pointHitDetectionRadius: 20,
        //Boolean - Whether to show a stroke for datasets
        datasetStroke: true,
        //Number - Pixel width of dataset stroke
        datasetStrokeWidth: 2,
        //Boolean - Whether to fill the dataset with a color
        datasetFill: true,
        //String - A legend template
        legendTemplate: "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<datasets.length; i++){%><li><span style=\"background-color:<%=datasets[i].lineColor%>\"></span><%=datasets[i].label%></li><%}%></ul>",
        //Boolean - whether to maintain the starting aspect ratio or not when responsive, if set to false, will take up entire container
        maintainAspectRatio: true,
        //Boolean - whether to make the chart responsive to window resizing
        responsive: true
    };

    //Create the line chart
    salesChart.Line(salesChartData, salesChartOptions);  
JS;
$this->registerJs($js, \yii\web\View::POS_END);
?>

<div class="row">
    <div class="col-md-12">
        <div class="box box-default">
            <div class="box-header with-border">
                <h3 class="box-title">Registros Portabilidad</h3>
                <div class="box-tools pull-right">
                    <!-- Buttons, labels, and many other things can be placed here! -->
                    <!-- Here is a label for example -->
                    <span class="label label-primary">Tramites</span>
                </div><!-- /.box-tools -->
            </div><!-- /.box-header -->
            <div class="box-body">
                <div class="row">
                    <div class="col-md-8">
                        <p class="text-center">
                            <strong>Registros: <?= $semana['startPresenta'] ?> - <?= $semana['endPresenta'] ?></strong>
                        </p>
                        <div class="chart">
                            <!-- Sales Chart Canvas -->
                            <canvas id="TramitesSemanaChart" style="height: 280px;"></canvas>
                        </div><!-- /.chart-responsive -->
                    </div><!-- /.col -->
                    <div class="col-md-4">
                        <p class="text-center">
                            <strong>Tramites Hoy</strong>
                        </p>
                        <div class="progress-group">
                            <span class="progress-text">En Proceso</span>
                            <span class="progress-number"><b><?= $tramitesHoy['EnProceso']; ?></b>/<?= $tramitesHoy['Total']; ?></span>
                            <div class="progress sm">
                                <div class="progress-bar progress-bar-green" style="width: <?php echo $tramitesHoy['Total'] == 0 ? 0 : ($tramitesHoy['EnProceso'] / $tramitesHoy['Total']) * 100; ?>%"></div>
                            </div>
                        </div><!-- /.progress-group -->
                        <div class="progress-group">
                            <span class="progress-text">Reingresados</span>
                            <span class="progress-number"><b><?= $tramitesHoy['Reingresados']; ?></b>/<?= $tramitesHoy['Total']; ?></span>
                            <div class="progress sm">
                                <div class="progress-bar progress-bar-yellow" style="width: <?php echo $tramitesHoy['Total'] == 0 ? 0 : ($tramitesHoy['Reingresados'] / $tramitesHoy['Total']) * 100; ?>%"></div>
                            </div>
                        </div><!-- /.progress-group -->
                        <div class="progress-group">
                            <span class="progress-text">Aprobados ABD</span>
                            <span class="progress-number"><b><?= $tramitesHoy['AprobadosABD']; ?></b>/<?= $tramitesHoy['Total']; ?></span>
                            <div class="progress sm">
                                <div class="progress-bar progress-bar-green" style="width: <?php echo $tramitesHoy['Total'] == 0 ? 0 : ($tramitesHoy['AprobadosABD'] / $tramitesHoy['Total']) * 100; ?>%"></div>
                            </div>
                        </div><!-- /.progress-group -->
                        <div class="progress-group">
                            <span class="progress-text">Rechazados ABD</span>
                            <span class="progress-number"><b><?= $tramitesHoy['RechazadosABD']; ?></b>/<?= $tramitesHoy['Total']; ?></span>
                            <div class="progress sm">
                                <div class="progress-bar progress-bar-red" style="width: <?php echo $tramitesHoy['Total'] == 0 ? 0 : ($tramitesHoy['RechazadosABD'] / $tramitesHoy['Total']) * 100; ?>%"></div>
                            </div>
                        </div><!-- /.progress-group -->
                        <div class="progress-group">
                            <span class="progress-text">Aprobados CURP</span>
                            <span class="progress-number"><b><?= $tramitesHoy['AprobadosCURP']; ?></b>/<?= $tramitesHoy['Total']; ?></span>
                            <div class="progress sm">
                                <div class="progress-bar progress-bar-yellow" style="width:<?php echo $tramitesHoy['Total'] == 0 ? 0 : ($tramitesHoy['AprobadosCURP'] / $tramitesHoy['Total']) * 100; ?>%"></div>
                            </div>
                        </div><!-- /.progress-group -->
                        <div class="progress-group">
                            <span class="progress-text">Rechazados CURP</span>
                            <span class="progress-number"><b><?= $tramitesHoy['RechazadosCURP']; ?></b>/<?= $tramitesHoy['Total']; ?></span>
                            <div class="progress sm">
                                <div class="progress-bar progress-bar-red" style="width:<?php echo $tramitesHoy['Total'] == 0 ? 0 : ($tramitesHoy['RechazadosCURP'] / $tramitesHoy['Total']) * 100; ?>%"></div>
                            </div>
                        </div><!-- /.progress-group -->
                    </div><!-- /.col -->
                </div><!-- /.row -->
            </div><!-- /.box-body -->
            <div class="box-footer">
                <div class="row">
                    <div class="col-sm-3 col-xs-3">
                        <div class="description-block border-right">
                            <?php
                            $crecimiento = $ordenesAyer == 0 ? 0 : $tramitesHoy['Total'] / $ordenesAyer;
                            switch ($crecimiento):
                                case (0):
                                    echo '<span class="description-percentage text-yellow"><i class="fa fa-caret-left"></i> 0%';
                                    break;
                                case ($crecimiento >= 0.01):
                                    echo '<span class="description-percentage text-green"><i class="fa fa-caret-up"></i>' . number_format($crecimiento, 2) . "%";
                                    break;
                                case ($crecimiento < 0):
                                    echo '<span class="description-percentage text-red"><i class="fa fa-caret-down"></i>' . number_format($crecimiento, 2) . "%";
                                    break;
                            endswitch;
                            echo "</span>";
                            ?>
                            <h5 class="description-header"><?= $tramitesHoy['Total']; ?></h5>
                            <span class="description-text">Tramites hoy</span>
                        </div><!-- /.description-block -->
                    </div><!-- /.col -->
                    <div class="col-sm-3 col-xs-3">
                        <div class="description-block border-right">
                            <span class="description-percentage text-yellow"><i class="fa fa-caret-left"></i> 0%</span>
                            <h5 class="description-header"><?= $tramitesSemana['Total']; ?></h5>
                            <span class="description-text">Tramites de la semana</span>
                        </div><!-- /.description-block -->
                    </div><!-- /.col -->
                    <div class="col-sm-3 col-xs-3">
                        <div class="description-block border-right">
                            <?php
                            switch ($tramitesHoy['AprobadosABD']):
                                case (0):
                                    echo '<span class="description-percentage text-yellow"><i class="fa fa-caret-left"></i>Hoy: ' . $tramitesHoy['AprobadosABD'];
                                    break;
                                case ($tramitesHoy['AprobadosABD'] >= 0.01):
                                    echo '<span class="description-percentage text-green"><i class="fa fa-caret-up"></i>Hoy: ' . $tramitesHoy['AprobadosABD'];
                                    break;
                                case ($tramitesHoy['AprobadosABD'] < 0):
                                    echo '<span class="description-percentage text-red"><i class="fa fa-caret-down"></i>Hoy: ' . $tramitesHoy['AprobadosABD'];
                                    break;
                            endswitch;
                            echo "</span>";
                            ?>
                            <h5 class="description-header"><?= $tramitesHoy['AprobadosABD']; ?></h5>
                            <span class="description-text">APROBADOS ABD</span>
                        </div><!-- /.description-block -->
                    </div><!-- /.col -->
                    <div class="col-sm-3 col-xs-3">
                        <div class="description-block">
                            <?php
                            switch ($tramitesHoy['Reingresados']):
                                case (0):
                                    echo '<span class="description-percentage text-yellow"><i class="fa fa-caret-left"></i>Hoy: ' . $tramitesHoy['Reingresados'];
                                    break;
                                case ($tramitesHoy['Reingresados'] >= 0.01):
                                    echo '<span class="description-percentage text-green"><i class="fa fa-caret-up"></i>Hoy: ' . $tramitesHoy['Reingresados'];
                                    break;
                                case ($tramitesHoy['Reingresados'] < 0):
                                    echo '<span class="description-percentage text-red"><i class="fa fa-caret-down"></i>Hoy: ' . $tramitesHoy['Reingresados'];
                                    break;
                            endswitch;
                            echo "</span>";
                            ?>
                            <h5 class="description-header"><?= $tramitesHoy['Reingresados']; ?></h5>
                            <span class="description-text">TRAMITES REINGRESADOS</span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-md-12">
        <div class="box box-default">
            <div class="box-header with-border">
                <h3 class="box-title">Registros por Agente</h3>
                <strong>Registros: <?= $semana['startPresenta'] ?> - <?= $semana['endPresenta'] ?></strong>

                <div class="box-tools pull-right">
                    <!-- Buttons, labels, and many other things can be placed here! -->
                    <!-- Here is a label for example -->
                    <span class="label label-primary">Tramites</span>
                </div><!-- /.box-tools -->
            </div><!-- /.box-header -->
            <div class="box-body">
                <div class="col-md-12">
                    <table class="table table-bordered">
                        <tbody>
                            <tr>
                                <th>Agente</th>
                                <th>Total</th>
                                <th>Curp<br /><span class="badge bg-red"><i class="fa fa-fw fa-times-circle"></i></span></th>
                                <th>ABD<br /><span class="badge bg-red"><i class="fa fa-fw fa-times-circle"></i></span></th>
                                <th>Reingresado</th>
                            </tr>
                            <?php
                            foreach ($tramitesTodosAgente as $tramites):
                                ?>
                                <tr>
                                    <td><?= $tramites->agente; ?></td>
                                    <td><?= $tramites->registros; ?></td>
                                    <td><?= app\models\Registros::getTramiteById($semana['start'], $semana['end'], 3, $tramites->created_by) ?></td>
                                    <td><?= app\models\Registros::getTramiteById($semana['start'], $semana['end'], 5, $tramites->created_by) ?></td>
                                    <td><?= app\models\Registros::getTramiteById($semana['start'], $semana['end'], 6, $tramites->created_by) ?></td>
                                </tr>
                                <?php
                            endforeach;
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>