<?php

namespace frontend\models;

use common\components\DateUtil;
use common\models\Account;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\AccountState;
use common\components\Helper;

/**
 * AccountstateSearch represents the model behind the search form about `common\models\AccountState`.
 */
class AccountstateSearchToday extends AccountState
{
	
	public $start;
	public $end;
	
	public $timestampStart;
	public $timestampEnd;
	
		
    /**
     * @inheritdoc
     */
    public function rules()
    {
       return [
        ];
    }

    /**
     * @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 = AccountState::find();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        	'sort' => false
        ]);

        $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->innerJoinWith('account');
        $query->innerJoinWith('account.userAccountAssignments');
        $query->andWhere(['user_account_assignment.id_user' => Yii::$app->user->id]);
        $query->andWhere([
        		'account_state.id_user' => \Yii::$app->user->id,
        		'account_state.id_account' => Account::readDefault()
        ]);

        $query->andWhere( [ '>=','account_state.created_at',  DateUtil::formatDateUtc( DateUtil::todayStart() ) ] );

        $query->orderBy( 'account_state.created_at desc'  );

        $query->limit = 20;
        
        return $dataProvider;
    }
}
