<?php

namespace backend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\Log;
use yii\db\Query;
use common\components\Helper;

/**
 * LogSearch represents the model behind the search form about `common\models\Log`.
 */
class LogSearch extends Log
{
   
	public $start;
	public $end;
	public $timestampStart;
	public $timestampEnd;
	
	/**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id_log', 'type', 'id_user', 'id_transfer', 'id_money_movement', 'id_ticket', 'id_sale', 'id_customer', 'id_account', 'id_account_state', 'id_key', 'id_product', 'id_door_log'], 'integer'],
            [['message', 'url', 'app', 'created_at', 'updated_at'], 'safe'],
       		[[ 'start',  ], 'date', 'format' =>Yii::$app->formatter->datetimeFormat  , 'timestampAttribute' => 'timestampStart' ,'timestampAttributeFormat' => 'yyyy-MM-dd HH:mm' ,'timeZone' => 'UTC' ],
       		[[ 'end' , ], 'date'  ,'format' =>Yii::$app->formatter->datetimeFormat  , 'timestampAttribute' => 'timestampEnd' ,'timestampAttributeFormat' => 'yyyy-MM-dd HH:mm' ,'timeZone' => 'UTC' ],
        ];
    }

    /**
     * @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)
    {
        $query = new Query();
        $query->select([ 
        		'log.id_log as log_id_log',
        		'log.created_at as log_created_at',
        		'log.message as log_message',
        		'log.app as log_app',
        		'log.type as log_type',
        		'user.username as user_username',
        		'customer.name as customer_name',
        		
        ]);
        $query->from("log");
        $query->leftJoin("user"," user.id = log.id_user");
        $query->leftJoin("customer"," customer.id_customer = log.id_customer");
        

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        	'sort' =>[
        		'defaultOrder' => [ 'log_created_at' => SORT_DESC],
        		'attributes' => Helper::mkYiiSortItems([
        				['log_id_log'],
        				['log_created_at'],
        				['log_message'],
        				['log_app'],
        				['log_app'],
        				['user_username'],
        				['customer_name'],
        				
        		])
        			
        	]
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        $query->andFilterWhere(['>=', 'log.created_at', $this->timestampStart]);
        $query->andFilterWhere(['<', 'log.created_at', $this->timestampEnd]);
        
        $query->andFilterWhere([
            'id_log' => $this->id_log,
            'type' => $this->type,
            'id_user' => $this->id_user,
            'id_transfer' => $this->id_transfer,
            'id_money_movement' => $this->id_money_movement,
            'id_ticket' => $this->id_ticket,
            'id_sale' => $this->id_sale,
            'id_customer' => $this->id_customer,
            'id_account' => $this->id_account,
            'id_account_state' => $this->id_account_state,
            'id_key' => $this->id_key,
            'id_product' => $this->id_product,
            'id_door_log' => $this->id_door_log,
            'created_at' => $this->created_at,
        ]);

        $query->andFilterWhere(['like', 'message', $this->message])
            ->andFilterWhere(['like', 'url', $this->url])
            ->andFilterWhere(['like', 'app', $this->app]);

        return $dataProvider;
    }
}
