<?php
    namespace common\modules\comment\models;

    use common\models\Currency;
    use common\models\File;
    use common\models\User;
    use yii\data\ActiveDataProvider;
    use yii\db\ActiveQuery;
    use yii\db\ActiveRecord;
    use yii\web\UploadedFile;

    /**
     * Class Comment
     * @property bool     $guestComment
     * @property integer  $comment_id
     * @property string   $text
     * @property int      $user_id
     * @property int      $status
     * @property string   $date_add
     * @property string   $date_update
     * @property string   $date_delete
     * @property string   $model
     * @property int      $model_id
     * @property string   $files
     * @property float    $budget_from
     * @property float    $budget_to
     * @property int      $term_from
     * @property int      $term_to
     * @property int      $state
     * @property Currency $currency
     * @package common\modules\comment\models
     */
    class CommentProjectSearch extends CommentProject
    {

        const SCENARIO_SEARCH = 'search';

        public function rules()
        {
            return [
                [
                    [
                        'state',
                    ],
                    'integer',
                    'max' => 5,
                    'min' => 1,
                ],
                [
                    [
                        'state',
                    ],
                    'default',
                    'value' => self::STATE_NEW,
                ],
            ];
        }

        public function scenarios()
        {
            return array_merge(parent::scenarios(), [
                self::SCENARIO_SEARCH => [
                    'state',
                ],
            ]);
        }

        public function search($params)
        {
            $query = CommentProject::find()
                                   ->with('project')
                                   ->with('project.budgetCurrency')
                                   ->with('project.comments')
                                   ->where([ 'user_id' => \Yii::$app->user->getId() ]);

            $dataProvider = new ActiveDataProvider([
                'query' => $query,
            ]);

            $this->load($params);

            if(!$this->validate()) {
                $query->andWhere('0=1');
                return $dataProvider;
            }

            $query->andWhere([ 'state' => $this->state ]);

            return $dataProvider;
        }

    }
