<?php

namespace backend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\Registros;

/**
 * RegistrosSearch represents the model behind the search form about `app\models\Clientes`.
 */
class RegistrosSearchAbd extends Registros {

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['numero', 'nombre', 'apellidop', 'apellidom', 'nip', 'fechanac', 'lugarnac', 'curp', 'dnref', 'correo', 'entrega', 'modelo', 'created', 'modified'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios() {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params) {

        if (\Yii::$app->user->can('isAdmin')) {
            $query = Registros::find()->where(['status_id' => 5]);
        } else {
            $query = Registros::find()->where(['status_id' => 5, 'created_by' => Yii::$app->user->identity->id]);
        }

        if (isset($params['RegistrosSearch'])) {
            $from = date_format(date_create('1987-07-16'), 'Y-m-d H:m:s');
            $to = date('Y-m-d H:m:s', strtotime('-1 Day'));
            if (isset($params['RegistrosSearch']['from']) || isset($params['RegistrosSearch']['to'])) {
                if ($params['RegistrosSearch']['from'] && $params['RegistrosSearch']['to']) {
//                    echo 'Desde: ' . $params['RegistrosSearch']['from'] . ' Hasta ' . $params['RegistrosSearch']['to'];
                    $from = $params['RegistrosSearch']['from'] . " 00:00:00";
                    $to = $params['RegistrosSearch']['to'] . " 23:59:59";
                }
                if ($params['RegistrosSearch']['from'] && !$params['RegistrosSearch']['to']) {
//                    echo 'Desde: ' . $params['RegistrosSearch']['from'] . ' Hasta ' . date('Y-m-d', strtotime('-3 Day'));
                    $from = $params['RegistrosSearch']['from'] . " 00:00:00";
                    //$to = date('Y-m-d', strtotime('-1 Day'));
                }
                if (!$params['RegistrosSearch']['from'] && $params['RegistrosSearch']['to']) {
//                    echo 'Desde: ' . date_format(date_create('1987-07-16'), 'Y-m-d') . ' Hasta ' . $params['RegistrosSearch']['to'];
                    $from = date_format(date_create('1987-07-16'), 'Y-m-d H:m:s');
                    $to = $params['RegistrosSearch']['to'] . " 23:59:59";
                }
            }
            if (isset($params['RegistrosSearch']['numero'])) {
                $query->andFilterWhere(['numero' => $params['RegistrosSearch']['numero']]);
            }
            $query->andFilterWhere(['between', 'created', $from, $to]);
            $query->andFilterWhere(['created_by' => $params['RegistrosSearch']['created_by']]);
        }
        $query->orderBy(['id' => SORT_DESC]);
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => array(
                'pageSize' => 30,
            ),
        ]);

        return $dataProvider;
    }

}
