<?php

namespace app\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;

/**
 * AuctionsSearch represents the model behind the search form about `app\models\Auctions`.
 */
class AuctionsSearch extends Auctions
{
    public $lot;
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'user_id', 'lot_id', 'date_start', 'bidding_date', 'last_price', 'last_user', 'last_date'], 'integer'],
            [['userName', 'name'], 'safe'],
            [['lot'], 'string']
        ];
    }

    /**
     * @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 = Auctions::find();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'sort' => [
                'defaultOrder' => ['id' => SORT_DESC],
            ]
        ]);

        $this->load($params);

        if (!$this->validate()) {
            return $dataProvider;
        }

        $query->joinWith(['lot', 'user']);

        $query->andFilterWhere([
            'date_start' => $this->date_start,
            'bidding_date' => $this->date_stop,
            'last_price' => $this->last_price,
            'last_user' => $this->last_user,
            'last_date' => $this->last_date,
        ]);
        $query->andFilterWhere(['like', 'lots.name', $this->lot]);
        $query->andFilterWhere(['like', 'auctions.name', $this->name]);
        //$query->andFilterWhere(['like','lots.start_price',$this->lot]);


        return $dataProvider;
    }
}
