<?php

namespace backend\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use common\models\LoginForm;
use yii\filters\VerbFilter;
use app\models\Registros;

/**
 * Site controller
 */
class SiteController extends Controller {

    /**
     * @inheritdoc
     */
    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['login', 'error'],
                        'allow' => true,
                    ],
                    [
                        'actions' => ['logout', 'index'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function actions() {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }

    public function actionIndex() {

        $hoy = ['medianoche' => date('Y-m-d H:i:s', strtotime('midnight today')), 'ahora' => date('Y-m-d H:i:s')];
        $ayer = ['start' => date('Y-m-d H:i:s', strtotime('midnight yesterday')), 'end' => date('Y-m-d H:i:s', strtotime('midnight today'))];
        $semana['start'] = date('N') == 6 ? date('Y-m-d H:i:s', strtotime('midnight today')) : date('Y-m-d H:i:s', strtotime('midnight last saturday'));
        $semana['end'] = date('N') == 5 ? date('Y-m-d H:i:s') : date('Y-m-d H:i:s', strtotime('next friday'));
        $semana['startPresenta'] = date('N') == 6 ? date('l d-M', strtotime('midnight today')) : date('l d-M', strtotime('midnight last saturday'));
        $semana['endPresenta'] = date('N') == 5 ? date('l d-M') : date('l d-M', strtotime('next friday'));

        $tramitesAyer = Registros::getRegistrosByDate($ayer['start'], $ayer['end']);
        $tramitesTodosSemana = Registros::getTodosTramites($semana['start'], $semana['end']);

        $tramitesTodosAgente = Registros::getTodosTramitesByAgente($semana['start'], $semana['end']);
        $tramitesTodosAgenteHoy = Registros::getTodosTramitesByAgente($hoy['medianoche'], $hoy['ahora']);

        $tramitesSemana = [
            'Total' => Registros::getRegistrosByDate($semana['start'], $semana['end']),
            'EnProceso' => Registros::getTramiteById($semana['start'], $semana['end'], 1),
            'AprobadosCURP' => Registros::getTramiteById($semana['start'], $semana['end'], 2),
            'RechazadosCURP' => Registros::getTramiteById($semana['start'], $semana['end'], 3),
            'AprobadosABD' => Registros::getTramiteById($semana['start'], $semana['end'], 4),
            'RechazadosABD' => Registros::getTramiteById($semana['start'], $semana['end'], 5),
            'Reingresados' => Registros::getTramiteById($semana['start'], $semana['end'], 6)
        ];
        $tramitesHoy = [
            'Total' => Registros::getRegistrosByDate($hoy['medianoche'], $hoy['ahora']),
            'EnProceso' => Registros::getTramiteById($hoy['medianoche'], $hoy['ahora'], 1),
            'AprobadosCURP' => Registros::getTramiteById($hoy['medianoche'], $hoy['ahora'], 2),
            'RechazadosCURP' => Registros::getTramiteById($hoy['medianoche'], $hoy['ahora'], 3),
            'AprobadosABD' => Registros::getTramiteById($hoy['medianoche'], $hoy['ahora'], 4),
            'RechazadosABD' => Registros::getTramiteById($hoy['medianoche'], $hoy['ahora'], 5),
            'Reingresados' => Registros::getTramiteById($hoy['medianoche'], $hoy['ahora'], 6)
        ];

        return $this->render('index_admin', [
                    'ordenesAyer' => $tramitesAyer,
                    'tramitesSemana' => $tramitesSemana,
                    'tramitesHoy' => $tramitesHoy,
                    'tramitesTodosSemana' => $tramitesTodosSemana,
                    'tramitesTodosAgente' => $tramitesTodosAgente,
                    'tramitesTodosAgenteHoy' => $tramitesTodosAgenteHoy,
                    'semana' => $semana,
        ]);
    }

    public function actionLogin() {
        if (!\Yii::$app->user->isGuest) {
            return $this->goHome();
        }

        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            return $this->goBack();
        } else {
            return $this->render('login', [
                        'model' => $model,
            ]);
        }
    }

    public
            function actionLogout() {
        Yii::$app->user->logout();

        return $this->goHome();
    }

}
