<?php

namespace modules\cors\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use modules\cors\models\Vehicleservicehistorybe;

/**
 * vehicleservicehistorybeSearch represents the model behind the search form about `backend\models\Vehicleservicehistorybe`.
 */
class VehicleservicehistorybeSearch extends vehicleservicehistorybe
{
	public $reg;
    /**
     * @inheritdoc
     */
    public function rules(): array
    {
        return [
            [['id', 'created_by', 'updated_by', 'deleted_by', 'actid', 'status', 'padno', 'relatedid', 'description', 'warnings', 'furtheractions','reg'], 'safe'],
            [['created_at', 'updated_at', 'deleted_at', 'deleted', 'jobno', 'mileage', 'date'], 'integer'],
        ];
    }

    /**
     * @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 = vehicleservicehistorybe::find()
        ->where(['or', ['is','vehicleservicehistory.status',null], ['!=','vehicleservicehistory.status','deleted']])
		->joinwith(['vehicle'])
		//->groupBy('vehicle.reg')
		;
        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
			'sort'=> ['defaultOrder' => ['reg' => SORT_DESC,'date'  => SORT_ASC ]]
		]);
		$dataProvider->sort->attributes['reg'] = [
        // The tables are the ones our relation are configured to
        // in my case they are prefixed with "tbl_"
        'asc' => ['vehicle.reg' => SORT_ASC],
        'desc' => ['vehicle.reg' => SORT_DESC],
    ];
        $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;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'vehicleservicehistory.created_at' => $this->created_at,
            'vehicleservicehistory.updated_at' => $this->updated_at,
            'vehicleservicehistory.deleted_at' => $this->deleted_at,
            'vehicleservicehistory.deleted' => $this->deleted,
            'vehicleservicehistory.jobno' => $this->jobno,
            'vehicleservicehistory.mileage' => $this->mileage,
            'vehicleservicehistory.date' => $this->date,
        ]);

        $query->andFilterWhere(['like', 'id', $this->id])
            ->andFilterWhere(['like', 'vehicle.reg', $this->reg])
            ->andFilterWhere(['like', 'vehicleservicehistory.created_by', $this->created_by])
            ->andFilterWhere(['like', 'vehicleservicehistory.updated_by', $this->updated_by])
            ->andFilterWhere(['like', 'vehicleservicehistory.deleted_by', $this->deleted_by])
            ->andFilterWhere(['like', 'vehicleservicehistory.actid', $this->actid])
            ->andFilterWhere(['like', 'vehicleservicehistory.status', $this->status])
            ->andFilterWhere(['like', 'vehicleservicehistory.padno', $this->padno])
            ->andFilterWhere(['like', 'vehicleservicehistory.relatedid', $this->relatedid])
            ->andFilterWhere(['like', 'vehicleservicehistory.description', $this->description])
            ->andFilterWhere(['like', 'vehicleservicehistory.warnings', $this->warnings])
            ->andFilterWhere(['like', 'vehicleservicehistory.furtheractions', $this->furtheractions]);

        return $dataProvider;
    }
    public function deleted($params)
    {
        $query = vehicleservicehistorybe::find()
        ->where(['=','status','deleted']);

        // add conditions that should always apply here

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

        $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;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
            'deleted_at' => $this->deleted_at,
            'deleted' => $this->deleted,
            'jobno' => $this->jobno,
            'mileage' => $this->mileage,
            'date' => $this->date,
        ]);

        $query->andFilterWhere(['like', 'id', $this->id])
            ->andFilterWhere(['like', 'created_by', $this->created_by])
            ->andFilterWhere(['like', 'updated_by', $this->updated_by])
            ->andFilterWhere(['like', 'deleted_by', $this->deleted_by])
            ->andFilterWhere(['like', 'actid', $this->actid])
            ->andFilterWhere(['like', 'status', $this->status])
            ->andFilterWhere(['like', 'padno', $this->padno])
            ->andFilterWhere(['like', 'relatedid', $this->relatedid])
            ->andFilterWhere(['like', 'description', $this->description])
            ->andFilterWhere(['like', 'warnings', $this->warnings])
            ->andFilterWhere(['like', 'furtheractions', $this->furtheractions]);

        return $dataProvider;
    }
}
