<?php
    
    namespace common\modules\rubrication\models;
    
    use common\modules\language\behaviors\LanguageBehavior;
    use common\modules\language\models\Language;
    use common\modules\product\models\Category;
    use yii\db\ActiveQuery;
    use yii\db\ActiveRecord;
    use yii\web\Request;
    
    /**
     * This is the model class for table "{{%tax_group}}".
     * @property integer        $tax_group_id
     * @property boolean        $is_filter
     * @property integer        $level
     * @property integer        $sort
     * @property boolean        $display
     * @property boolean        $is_menu
     * @property TaxOption[]    $taxOptions
     * @property Category[]     $categories
     * @property TaxOption[]    $options
     * * From language behavior *
     * @property TaxGroupLang   $lang
     * @property TaxGroupLang[] $langs
     * @property TaxGroupLang   $object_lang
     * @property string         $ownerKey
     * @property string         $langKey
     * @property TaxGroupLang[] $model_langs
     * @property bool           $transactionStatus
     * @method string           getOwnerKey()
     * @method void             setOwnerKey( string $value )
     * @method string           getLangKey()
     * @method void             setLangKey( string $value )
     * @method ActiveQuery      getLangs()
     * @method ActiveQuery      getLang( integer $language_id )
     * @method TaxGroupLang[]    generateLangs()
     * @method void             loadLangs( Request $request )
     * @method bool             linkLangs()
     * @method bool             saveLangs()
     * @method bool             getTransactionStatus()
     * * End language behavior *
     */
    class TaxGroup extends ActiveRecord
    {
        
        /**
         * @var TaxOption[] $_options
         */
        public $_options = [];
        
        /**
         * @inheritdoc
         */
        public function behaviors()
        {
            return [
                'language' => [
                    'class' => LanguageBehavior::className(),
                ],
            ];
        }
        
        /**
         * @inheritdoc
         */
        public static function tableName()
        {
            return 'tax_group';
        }
        
        /**
         * @inheritdoc
         */
        public function rules()
        {
            return [
                [
                    [
                        'is_filter',
                        'display',
                        'is_menu',
                    ],
                    'boolean',
                ],
                [
                    [
                        'level',
                        'sort',
                    ],
                    'integer',
                ],
                [
                    [ 'categories' ],
                    'safe',
                ],
            ];
        }
        
        /**
         * @inheritdoc
         */
        public function attributeLabels()
        {
            return [
                'tax_group_id' => 'Tax Group ID',
                'is_filter'    => 'Use in filter',
                'sort'         => 'Sort',
                'display'      => 'Display',
                'is_menu'      => 'Отображать в меню',
            ];
        }
        
        public function getCategories()
        {
            return $this->hasMany(Category::className(), [ 'category_id' => 'category_id' ])
                        ->viaTable('tax_group_to_category', [ 'tax_group_id' => 'tax_group_id' ]);
        }
        
        public function setCategories($values)
        {
            $this->categories = $values;
        }
        
        public function afterSave($insert, $changedAttributes)
        {
            parent::afterSave($insert, $changedAttributes);
            $this->unlinkAll('categories', true);
            $categories = [];
            if(!empty( $this->categories )) {
                $categories = Category::findAll($this->categories);
            }
            foreach($categories as $category) {
                $this->link('categories', $category);
            }
        }
        
        /**
         * @return \yii\db\ActiveQuery
         */
        public function getOptions()
        {
            return $this->getTaxOptions();
        }
        
        public function getTaxOptions()
        {
            return $this->hasMany(TaxOption::className(), [ 'tax_group_id' => 'tax_group_id' ])
                        ->inverseOf('taxGroup');
        }
        
        public function getAlias()
        {
            $default_lang = Language::getDefaultLanguage();
            /**
             * @var TaxGroupLang $lang
             */
            $lang = $this->getLang($default_lang->language_id)
                         ->one();
            return $lang->alias;
        }
    }
