<?php

namespace common\modules\support\models\redmine;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\modules\support\models\redmine\Issue;

/**
 * IssueSearch represents the model behind the search form about `common\modules\support\models\redmine\Issue`.
 */
class IssueSearch extends Issue
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'tracker_id', 'project_id', 'category_id', 'status_id', 'assigned_to_id', 'priority_id', 'fixed_version_id', 'author_id', 'lock_version', 'done_ratio', 'parent_id', 'root_id', 'lft', 'rgt', 'is_private', 'assigned_to_manager_id', 'assigned_to_customer_id', 'paid'], 'integer'],
            [['subject', 'description', 'due_date', 'created_on', 'updated_on', 'start_date', 'closed_on', 'type_price'], 'safe'],
            [['estimated_hours', 'price_for_customer', 'max_price_for_developer', 'price_developer'], '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)
    {
        $query = Issue::find();

        // add conditions that should always apply here

        $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->andWhere(['author_id' => Yii::$app->support->identity->support_id]);

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'tracker_id' => $this->tracker_id,
            'project_id' => $this->project_id,
            'due_date' => $this->due_date,
            'category_id' => $this->category_id,
            'status_id' => $this->status_id,
            'assigned_to_id' => $this->assigned_to_id,
            'priority_id' => $this->priority_id,
            'fixed_version_id' => $this->fixed_version_id,
            'author_id' => $this->author_id,
            'lock_version' => $this->lock_version,
            'created_on' => $this->created_on,
            'updated_on' => $this->updated_on,
            'start_date' => $this->start_date,
            'done_ratio' => $this->done_ratio,
            'estimated_hours' => $this->estimated_hours,
            'parent_id' => $this->parent_id,
            'root_id' => $this->root_id,
            'lft' => $this->lft,
            'rgt' => $this->rgt,
            'is_private' => $this->is_private,
            'closed_on' => $this->closed_on,
            'assigned_to_manager_id' => $this->assigned_to_manager_id,
            'assigned_to_customer_id' => $this->assigned_to_customer_id,
            'price_for_customer' => $this->price_for_customer,
            'max_price_for_developer' => $this->max_price_for_developer,
            'price_developer' => $this->price_developer,
            'paid' => $this->paid,
        ]);

        $query->andFilterWhere(['like', 'subject', $this->subject])
            ->andFilterWhere(['like', 'description', $this->description])
            ->andFilterWhere(['like', 'type_price', $this->type_price]);

        return $dataProvider;
    }
}
