<?php

namespace modules\activities\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use modules\activities\models\Todaysactivities;

/**
 * activitiesSearch represents the model behind the search form about `modules\activities\models\Activities`.
 */
class TodaysactivitiesSearch extends Activities
{
    /**
     * @inheritdoc
     */
    public function rules() : array
    {
        return [
            [['id', 'shiftlength', 'jobno'], 'integer'],
            [['welder', 'travelstarttime', 'sdate', 'edate', 'travelfinishtime', 'company', 'assistant', 'contact', 'contactno', 'location', 'createdby', 'createdon', 'noofwelds', 'contactemail'], '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)
    {
    	$today = strtotime('today - 1 day');
        $week = strtotime("today +3 days");
        
        $query = Activities::find()
            ->andwhere(['!=', 'status','deleted'])
            ->andfilterWhere(['like', 'welder', Yii::$app->user->identity->username])
		    ->andFilterWhere(['between', 'starttime', $today, $week]);
		
		$dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }
		
		$query->andFilterWhere(['!=', 'jobstatus', 'Hidden']);
        $query->andFilterWhere([
            'id' => $this->id,
            'shiftlength' => $this->shiftlength,
            'createdon' => $this->createdon,
        ]);

        $query
       		->andFilterWhere(['like', 'jobno', $this->jobno])
        	->andFilterWhere(['like', 'welder', $this->welder])
            ->andFilterWhere(['like', 'travelstarttime', $this->travelstarttime])
            ->andFilterWhere(['like', 'sdate', $this->sdate])
            ->andFilterWhere(['like', 'edate', $this->edate])
            ->andFilterWhere(['like', 'travelfinishtime', $this->travelfinishtime])
            ->andFilterWhere(['like', 'company', $this->company])
            ->andFilterWhere(['like', 'assistant', $this->assistant])
            ->andFilterWhere(['like', 'contact', $this->contact])
            ->andFilterWhere(['like', 'contactno', $this->contactno])
            ->andFilterWhere(['like', 'location', $this->location])
            ->andFilterWhere(['like', 'createdby', $this->createdby])
            ->andFilterWhere(['like', 'noofwelds', $this->noofwelds])
            ->andFilterWhere(['like', 'contactemail', $this->contactemail]);
        return $dataProvider;
    }

    public function adminsearch($params)
    {
    	$today = strtotime('today -1 day');
        $week = strtotime("today +13 days");
        
        $query = Activities::find()
            ->andwhere(['!=', 'status','deleted'])
            ->andFilterWhere(['between', 'starttime', $today, $week]);
        
        $dataProvider = new ActiveDataProvider(['query' => $query,
            'sort' => [
                'defaultOrder' => [
                    'jobno' => SORT_ASC,
                ]
            ],
		]);
		$this->load($params);
        if (!$this->validate()) {
            // uncomment the following line if you do not want to any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }
		
        $query->andFilterWhere([
            'id' => $this->id,
            'shiftlength' => $this->shiftlength,
            'createdon' => $this->createdon,
        ]);

        $query
       		->andFilterWhere(['like', 'jobno', $this->jobno])
        	->andFilterWhere(['like', 'welder', $this->welder])
            ->andFilterWhere(['like', 'travelstarttime', $this->travelstarttime])
            ->andFilterWhere(['like', 'sdate', $this->sdate])
            ->andFilterWhere(['like', 'edate', $this->edate])
            ->andFilterWhere(['like', 'travelfinishtime', $this->travelfinishtime])
            ->andFilterWhere(['like', 'company', $this->company])
            ->andFilterWhere(['like', 'assistant', $this->assistant])
            ->andFilterWhere(['like', 'contact', $this->contact])
            ->andFilterWhere(['like', 'contactno', $this->contactno])
            ->andFilterWhere(['like', 'location', $this->location])
            ->andFilterWhere(['like', 'createdby', $this->createdby])
            ->andFilterWhere(['like', 'noofwelds', $this->noofwelds])
            ->andFilterWhere(['like', 'contactemail', $this->contactemail]);

        return $dataProvider;
    }

}
