<?php

namespace modules\custom\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use modules\custom\models\Contacts;

/**
 * ContactsSearch represents the model behind the search form about `modules\custom\models\Contacts`.
 */
class ContactsSearch extends Contacts
{
	public $accountname;
    /**
     * @inheritdoc
     */
    public function rules(): array
    {
        return [
            [['accountid'], 'integer'],
            [['firstname', 'lastname', 'homephone', 'mobileno', 'email', 'efirstname', 'elastname', 'ephone', 'emobile', 'erelationship', 'address1', 'address2', 'town', 'county', 'postcode', 'drivinglno', 'drivinglexpiry', 'drivingcategories', 'sentidno', 'sentidexpiry', 'created', 'eemail', 'isengineer','accountname'], '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)
    {
        $query = Contacts::find()
			->where(['or', ['!=','contacts.deleted',1], ['is', 'contacts.status', null]])
            ->joinwith('accounts');

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            
        ]);
        $dataProvider->sort->attributes['accountname'] = [
            'asc' => ['accounts.account_name' => SORT_ASC],
            'desc' => ['accounts.account_name' => SORT_DESC],
        ];
        $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;
        }
        $this->accountname !== null ? $query->joinWith('account') : false;

        $query->andFilterWhere([
            'contacts.created' => $this->created,
            'contacts.accountid' => $this->accountid,
        ]);

        $query->andFilterWhere(['like', 'contacts.firstname', $this->firstname])
            ->andFilterWhere(['like', 'contacts.lastname', $this->lastname])
            ->andFilterWhere(['like', 'contacts.homephone', $this->homephone])
            ->andFilterWhere(['like', 'contacts.mobileno', $this->mobileno])
            ->andFilterWhere(['like', 'contacts.email', $this->email])
            ->andFilterWhere(['like', 'contacts.efirstname', $this->efirstname])
            ->andFilterWhere(['like', 'contacts.elastname', $this->elastname])
            ->andFilterWhere(['like', 'contacts.ephone', $this->ephone])
            ->andFilterWhere(['like', 'contacts.emobile', $this->emobile])
            ->andFilterWhere(['like', 'contacts.erelationship', $this->erelationship])
            ->andFilterWhere(['like', 'contacts.address1', $this->address1])
            ->andFilterWhere(['like', 'contacts.address2', $this->address2])
            ->andFilterWhere(['like', 'contacts.town', $this->town])
            ->andFilterWhere(['like', 'contacts.county', $this->county])
            ->andFilterWhere(['like', 'contacts.postcode', $this->postcode])
            ->andFilterWhere(['like', 'contacts.drivinglno', $this->drivinglno])
            ->andFilterWhere(['like', 'contacts.drivinglexpiry', $this->drivinglexpiry])
            ->andFilterWhere(['like', 'contacts.drivingcategories', $this->drivingcategories])
            ->andFilterWhere(['like', 'contacts.sentidno', $this->sentidno])
            ->andFilterWhere(['like', 'contacts.sentidexpiry', $this->sentidexpiry])
            ->andFilterWhere(['like', 'contacts.eemail', $this->eemail])
            ->andFilterWhere(['like', 'contacts.isengineer', $this->isengineer])
            ->andFilterWhere(['like', 'accounts.account_name', $this->accountname]);

        return $dataProvider;
    }

    public function deleted($params) {
        $query = Contacts::find()
		->where(['contacts.deleted' => 1]);

        $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([
            'Created' => $this->Created,
            'accountid' => $this->accountid,
        ]);

        $query->andFilterWhere(['like', 'Firstname', $this->Firstname])
            ->andFilterWhere(['like', 'lastname', $this->lastname])
            ->andFilterWhere(['like', 'homephone', $this->homephone])
            ->andFilterWhere(['like', 'mobileno', $this->mobileno])
            ->andFilterWhere(['like', 'email', $this->email])
            ->andFilterWhere(['like', 'efirstname', $this->efirstname])
            ->andFilterWhere(['like', 'elastname', $this->elastname])
            ->andFilterWhere(['like', 'ephone', $this->ephone])
            ->andFilterWhere(['like', 'emobile', $this->emobile])
            ->andFilterWhere(['like', 'erelationship', $this->erelationship])
            ->andFilterWhere(['like', 'address1', $this->address1])
            ->andFilterWhere(['like', 'address2', $this->address2])
            ->andFilterWhere(['like', 'town', $this->town])
            ->andFilterWhere(['like', 'county', $this->county])
            ->andFilterWhere(['like', 'postcode', $this->postcode])
            ->andFilterWhere(['like', 'drivinglno', $this->drivinglno])
            ->andFilterWhere(['like', 'drivinglexpiry', $this->drivinglexpiry])
            ->andFilterWhere(['like', 'drivingcategories', $this->drivingcategories])
            ->andFilterWhere(['like', 'sentidno', $this->sentidno])
            ->andFilterWhere(['like', 'sentidexpiry', $this->sentidexpiry])
            ->andFilterWhere(['like', 'eemail', $this->eemail])
            ->andFilterWhere(['like', 'isengineer', $this->isengineer]);

        return $dataProvider;
    }

}