<?php

namespace app\models;

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

/**
 * EventlogSearch represents the model behind the search form about `app\models\Eventlog`.
 */
class EventlogSearch extends Eventlog
{
    /**
     * @inheritdoc
     */
    public $at_org;
    public $name;

    public function rules()
    {
        return [
            [['id', 'user_id', 'auk_id'], 'integer'],
            [['ip', 'action', 'at_org','name','date'], '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 = Eventlog::find();
        $query->joinWith('user');
        $query->joinWith('auctions');
        $query->joinWith('lots');

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

        $dataProvider->sort->attributes['at_org'] = [
                    'asc' => ['user.at_org' => SORT_ASC],
                    'desc' => ['user.at_org' => SORT_DESC],
                ];
        $dataProvider->sort->attributes['name'] = [
            'asc' => ['lots.name' => SORT_ASC],
            'desc' => ['lots.name' => SORT_DESC],
        ];

        //$dataProvider->sort['defaultOrder'] = ['eventlog.id' => SORT_DESC];

        $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,
            //'user_id' => $this->user_id,
            'auk_id' => $this->auk_id,
            //'date' => $this->date,
        ]);

        $query->andFilterWhere(['like', 'ip', $this->ip])
            ->andFilterWhere(['like', 'at_org', $this->at_org])
            ->andFilterWhere(['like', 'lots.name', $this->name])
            ->andFilterWhere(['like', 'action', $this->action])
        ->andFilterWhere(['like', 'eventlog.date', $this->date]);

        return $dataProvider;
    }
}
