<?php
    
    namespace common\modules\rubrication\models;
    
    use common\behaviors\SaveImgBehavior;
    use common\modules\language\behaviors\LanguageBehavior;
    use common\modules\product\models\Product;
    use common\modules\product\models\ProductVariant;
    use Yii;
    use common\components\artboxtree\ArtboxTreeBehavior;
    use yii\db\ActiveQuery;
    use yii\db\ActiveRecord;
    use yii\web\Request;
    
    /**
     * This is the model class for table "{{%tax_option}}".
     * @property string           $tax_option_id
     * @property integer          $tax_group_id
     * @property integer          $parent_id
     * @property integer          $tree
     * @property string           $path_int
     * @property integer          $depth
     * @property integer          $sort
     * @property string           $image
     * @property TaxGroup         $taxGroup
     * @property TaxOption        $parent
     * @property TaxOption[]      $taxOptions
     * @property Product[]        $products
     * @property ProductVariant[] $productVariants
     * * From language behavior *
     * @property TaxOptionLang    $lang
     * @property TaxOptionLang[]  $langs
     * @property TaxOptionLang    $object_lang
     * @property string           $ownerKey
     * @property string           $langKey
     * @property TaxOptionLang[]  $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 TaxOptionLang[]    generateLangs()
     * @method void             loadLangs( Request $request )
     * @method bool             linkLangs()
     * @method bool             saveLangs()
     * @method bool             getTransactionStatus()
     * * End language behavior *
     */
    class TaxOption extends ActiveRecord
    {
        
        public $_items_count;
        
        /**
         * @inheritdoc
         */
        public function behaviors()
        {
            return [
                [
                    'class'  => SaveImgBehavior::className(),
                    'fields' => [
                        [
                            'name'      => 'image',
                            'directory' => 'tax_option',
                        ],
                    ],
                ],
                'artboxtree' => [
                    'class'        => ArtboxTreeBehavior::className(),
                    'keyNameGroup' => 'tax_group_id',
                ],
                'language'   => [
                    'class' => LanguageBehavior::className(),
                ],
            ];
        }
        
        /**
         * @inheritdoc
         */
        public static function tableName()
        {
            return '{{%tax_option}}';
        }
        
        /**
         * @inheritdoc
         */
        public function rules()
        {
            return [
                [
                    [
                        'tax_group_id',
                        'parent_id',
                        'sort',
                    ],
                    'integer',
                ],
                [
                    [ 'tax_group_id' ],
                    'exist',
                    'skipOnError'     => true,
                    'targetClass'     => TaxGroup::className(),
                    'targetAttribute' => [ 'tax_group_id' => 'tax_group_id' ],
                ],
            ];
        }
        
        /**
         * @inheritdoc
         */
        public function attributeLabels()
        {
            return [
                'tax_option_id' => Yii::t('app', 'Tax Option ID'),
                'tax_group_id'  => Yii::t('app', 'Tax Group ID'),
                'parent_id'     => Yii::t('app', 'Parent ID'),
                'sort'          => Yii::t('app', 'Sort'),
                'image'         => Yii::t('product', 'Image'),
            ];
        }
        
        public static function find()
        {
            return new TaxOptionQuery(get_called_class());
        }
        
        /**
         * @return \yii\db\ActiveQuery
         */
        public function getGroup()
        {
            return $this->getTaxGroup();
        }
        
        /**
         * @return \yii\db\ActiveQuery
         */
        public function getTaxGroup()
        {
            return $this->hasOne(TaxGroup::className(), [ 'tax_group_id' => 'tax_group_id' ])
                        ->inverseOf('taxOptions');
        }
        
        /**
         * @return \yii\db\ActiveQuery
         */
        public function getTaxOptions()
        {
            return $this->hasMany(TaxOption::className(), [ 'parent_id' => 'tax_option_id' ])
                        ->inverseOf('parent');
        }
        
        public function beforeSave($insert)
        {
            if(parent::beforeSave($insert)) {
                if(empty( $this->parent_id )) {
                    $this->parent_id = 0;
                }
                return true;
            }
            return false;
        }
        
        public function getProducts()
        {
            return $this->hasMany(Product::className(), [ 'product_id' => 'product_id' ])
                        ->viaTable('product_option', [ 'option_id' => 'tax_option_id' ]);
        }
        
        public function getProductVariants()
        {
            return $this->hasMany(ProductVariant::className(), [ 'product_variant_id' => 'product_variant_id' ])
                        ->viaTable('product_variant_option', [ 'option_id' => 'tax_option_id' ]);
        }
    }
