<?php

namespace app\models;

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

/**
 * RefierensSearch represents the model behind the search form about `app\models\Refieren`.
 */
class RefierensSearch extends Refieren {

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['id', 'bono_id', 'created_by', 'modified_by', 'cliente_id', 'pdv_id'], 'integer'],
            [['nombre', 'correo', 'created', 'modified', 'telefono', 'observaciones'], '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 = \app\models\Referidos::find()
                    ->joinWith('refieren');
        } else {
            $query = \app\models\Referidos::find()
                    ->joinWith('refieren')
                    ->where(['refieren.created_by' => Yii::$app->user->identity->id]);
        }

        if (isset($params['RefierensSearch'])) {
            $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['RefierensSearch']['from']) || isset($params['RefierensSearch']['to'])) {
                if ($params['RefierensSearch']['from'] && $params['RefierensSearch']['to']) {
//                    echo 'Desde: ' . $params['RefierensSearch']['from'] . ' Hasta ' . $params['RefierensSearch']['to'];
                    $from = $params['RefierensSearch']['from'] . " 00:00:00";
                    $to = $params['RefierensSearch']['to'] . " 23:59:59";
                }
                if ($params['RefierensSearch']['from'] && !$params['RefierensSearch']['to']) {
//                    echo 'Desde: ' . $params['RefierensSearch']['from'] . ' Hasta ' . date('Y-m-d', strtotime('-3 Day'));
                    $from = $params['RefierensSearch']['from'] . " 00:00:00";
                    //$to = date('Y-m-d', strtotime('-1 Day'));
                }
                if (!$params['RefierensSearch']['from'] && $params['RefierensSearch']['to']) {
//                    echo 'Desde: ' . date_format(date_create('1987-07-16'), 'Y-m-d') . ' Hasta ' . $params['RefierensSearch']['to'];
                    $from = date_format(date_create('1987-07-16'), 'Y-m-d H:m:s');
                    $to = $params['RefierensSearch']['to'] . " 23:59:59";
                }
            }
            if (isset($params['RefierensSearch']['telefono'])) {
                $query->andFilterWhere(['like', 'referidos.telefono', $params['RefierensSearch']['telefono']]);
            }
            if (isset($params['RefierensSearch']['nombre'])) {
                $query->andFilterWhere(['like', 'referidos.nombre', $params['RefierensSearch']['nombre']]);
                $query->orFilterWhere(['like', 'refieren.nombre', $params['RefierensSearch']['nombre']]);
            }
            $query->andFilterWhere(['between', 'refieren.created', $from, $to]);
            $query->andFilterWhere([
                'refieren.created_by' => $params['RefierensSearch']['created_by'],
            ]);
            $query->andFilterWhere([
                'refieren.bono_id' => $params['RefierensSearch']['bono_id'],
            ]);
        }


        $query->orderBy(['id' => SORT_DESC]);
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => array(
                'pageSize' => 30,
            ),
        ]);

        return $dataProvider;
    }

}
