<?php

namespace app\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\Lots;

/**
 * LotSearch represents the model behind the search form about `app\models\Lots`.
 */
class LotSearch extends Lots
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'user_id', 'category_id', 'requisites_id', 'status','num'], 'integer'],
            [['aukname','name', 'description', 'address', 'delivery_time', 'delivery_term', 'requires', 'payment_term', 'payment_order', 'member_require', 'member_docs', 'notes', 'date', 'auction_date'], 'safe'],
            [['start_price', 'step'], 'number'],
        ];
    }

    /**
     * @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)
    {
        if(Yii::$app->user->can('admin'))
        {
            $query = Lots::find()->orderBy('id DESC');
        }
        else
        {
            $query = Lots::find()->where(['user_id' => Yii::$app->user->identity->id])->orderBy('date DESC');
        }

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

        $query->andFilterWhere([
            'id' => $this->id,
            'aukname' => $this->aukname,
            'start_price' => $this->start_price,
            'step' => $this->step,
            'docs_id' => $this->docs_id,
            'delivery_time' => $this->delivery_time,
            'category_id' => $this->category_id,
            'requisites_id' => $this->requisites_id,
            'dogovor_id' => $this->dogovor_id,
            'date' => $this->date,
            'auction_date' => $this->auction_date,
            'status' => $this->status,
        ]);

        $query->andFilterWhere(['like', 'aukname', $this->aukname])
        ->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'description', $this->description])
            ->andFilterWhere(['like', 'address', $this->address])
            ->andFilterWhere(['like', 'delivery_term', $this->delivery_term])
            ->andFilterWhere(['like', 'requires', $this->requires])
            ->andFilterWhere(['like', 'payment_term', $this->payment_term])
            ->andFilterWhere(['like', 'payment_order', $this->payment_order])
            ->andFilterWhere(['like', 'member_require', $this->member_require])
            ->andFilterWhere(['like', 'member_docs', $this->member_docs])
            ->andFilterWhere(['like', 'notes', $this->notes])
            ->andFilterWhere(['like', 'status', $this->status]);

        return $dataProvider;
    }
}
