<?php

namespace app\models;

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

/**
 * PublishingSearch represents the model behind the search form about `app\models\Publishing`.
 */
class PublishingSearch extends Publishing
{
    public $userName;
    public $lotName;
    public $endBidding;
    public $statusName;


    public $main_search;
    public $status;
    public $org_name;
    public $name;
    public $type;
    public $category;
    /**
     * @inheritdoc
     */
    public function rules()
    {

//        $rules[] = [['status'], 'in', 'range' => array_keys((new Auctions())->statusNames)];
//        $rules[] = [['type'], 'in', 'range' => array_keys(Lots::$procurementMethodTypes)];

        return [
            [['id', 'user_id', 'lot_id', 'last_user', 'lot_num'], 'integer'],
            [['name', 'date_start', 'date_stop', 'last_date','userName','lotName','statusName','endBidding','lot_num','userName','statusName', 'status', 'type'], 'safe'],
            [['last_price'], 'number'],
            [['main_search', 'org_name', 'name', 'category'], 'string', 'max' => 255]
        ];
    }

    /**
     * @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 = Publishing::find()->orderBy('id DESC');;

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                'defaultPageSize' => 4,
                'pageSize' => 4,
            ],
            'sort' => [
                'defaultOrder' => [
                    'id' => SORT_DESC
                ]
            ]
        ]);
        $this->load($params);

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

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

        $query->andFilterWhere([
            'or',
            ['like', 'auctions.lot_num', $this->main_search],
            ['like', 'lots.name', $this->main_search],
        ]);

        $query->andFilterWhere(['like', 'auctions.type', $this->type]);
        $query->andFilterWhere(['like', 'auctions.status', $this->status]);
        $query->andFilterWhere(['like', 'lots.category_id', $this->category]);
        $query->andFilterWhere(['like', 'profile.at_org', $this->org_name]);

        $query
            ->andFilterWhere(['like', 'auctions.lot_num', $this->lot_num])
            ->andFilterWhere(['like', 'profile.at_org', $this->userName])
            ->andFilterWhere(['like', 'lots.bidding_date', $this->endBidding])
            ->andFilterWhere(['like', 'lots.name', $this->name]);

        return $dataProvider;
    }

    public function isClear(){
        return !$this->status
            && !$this->org_name
            && !$this->category
            && !$this->type
            && !$this->name;
    }
}
