<?php
    
    namespace common\modules\product\models;
    
    use yii\base\Model;
    use yii\data\ActiveDataProvider;
    
    /**
     * ProductUnitSearch represents the model behind the search form about
     * `common\modules\product\models\ProductUnit`.
     */
    class ProductUnitSearch extends ProductUnit
    {
        
        public $name;
        
        public function behaviors()
        {
            return [];
        }
        
        public function attributeLabels()
        {
            $labels = parent::attributeLabels();
            $new_labels = [
                'name' => \Yii::t('product', 'Product Unit Name'),
            ];
            return array_merge($labels, $new_labels);
        }
        
        /**
         * @inheritdoc
         */
        public function rules()
        {
            return [
                [
                    [ 'name' ],
                    'safe',
                ],
                [
                    [ 'product_unit_id' ],
                    'integer',
                ],
                [
                    [ 'is_default' ],
                    'boolean',
                ],
            ];
        }
        
        /**
         * @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 = ProductUnit::find()->joinWith('lang');
            
            // add conditions that should always apply here
            
            $dataProvider = new ActiveDataProvider([
                'query' => $query,
                'sort'  => [
                    'attributes' => [
                        'product_unit_id',
                        'is_defaut',
                        'name' => [
                            'asc' => [ 'product_unit_lang.name' => SORT_ASC ],
                            'desc' => [ 'product_unit_lang.name' => 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;
            }
            
            // grid filtering conditions
            $query->andFilterWhere([
                'product_unit_id' => $this->product_unit_id,
                'is_default'      => $this->is_default,
            ])
                  ->andFilterWhere([
                      'ilike',
                      'product_unit_lang.name',
                      $this->name,
                  ]);
            
            return $dataProvider;
        }
    }
