<?php

namespace modules\custom\models;

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

/**
 * AccountsSearch represents the model behind the search form about `modules\custom\models\Accounts`.
 */
class AccountsSearch extends Accounts
{
    /**
     * @inheritdoc
     */
    public function rules(): array
    {
        return [
            [['accountid', 'contact'], 'integer'],
            [['account_name', 'address1', 'address2', 'town', 'county', 'post_code', 'telephone_no', 'fax_no', 'email'], '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 = Accounts::find();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'sort'=> ['defaultOrder' => ['account_name' => 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([
            'accountid' => $this->accountid,
            'contact' => $this->contact,
        ]);

        $query->andFilterWhere(['like', 'account_name', $this->account_name])
            ->andFilterWhere(['like', 'address1', $this->address1])
            ->andFilterWhere(['like', 'address2', $this->address2])
            ->andFilterWhere(['like', 'town', $this->town])
            ->andFilterWhere(['like', 'county', $this->county])
            ->andFilterWhere(['like', 'post_code', $this->post_code])
            ->andFilterWhere(['like', 'telephone_no', $this->telephone_no])
            ->andFilterWhere(['like', 'fax_no', $this->fax_no])
            ->andFilterWhere(['like', 'email', $this->email]);

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

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

        $query->andFilterWhere(['like', 'account_ame', $this->account_name])
            ->andFilterWhere(['like', 'address1', $this->address1])
            ->andFilterWhere(['like', 'address2', $this->address2])
            ->andFilterWhere(['like', 'town', $this->town])
            ->andFilterWhere(['like', 'county', $this->county])
            ->andFilterWhere(['like', 'post_code', $this->post_code])
            ->andFilterWhere(['like', 'telephone_no', $this->telephone_no])
            ->andFilterWhere(['like', 'fax_no', $this->fax_no])
            ->andFilterWhere(['like', 'email', $this->email]);

        return $dataProvider;
       }
}
