<?php

namespace common\modules\rubrication\models;

use Yii;
use common\components\artboxtree\ArtboxTreeBehavior;
use yii\db\ActiveRecord;

/**
 * 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 string $alias
 * @property integer $sort
 * @property integer $default_value
 * @property integer $name
 * @property array $image
 *
 * @property TaxEntityRelation[] $taxEntityRelations
 * @property TaxGroup $taxGroup
 * @property TaxOption $parent
 * @property TaxOption[] $taxOptions
 * @property TaxOptionToGroup[] $taxOptionToGroups
 * @property TaxOptionToOption[] $taxOptionToOptions
 * @property TaxOptionToOption[] $taxOptionToOptions0
 * @property TaxValueFloat[] $taxValueFloats
 * @property TaxValueInt[] $taxValueInts
 * @property TaxValueString[] $taxValueStrings
 * @property TaxValueText[] $taxValueTexts
 */
class TaxOption extends \yii\db\ActiveRecord
{
    public $_items_count;


    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'artboxtree' => [
                'class' => ArtboxTreeBehavior::className(),
                'keyNameGroup' => 'tax_group_id',
            ],
            'slug' => [
                'class' => 'common\behaviors\Slug',
                'in_attribute' => 'name',
                'out_attribute' => 'alias',
                'translit' => true
            ],

        ];
    }

        /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%tax_option}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['tax_group_id','name'], 'required'],
            [['tax_group_id',  'parent_id', 'sort', 'default_value'], 'integer'],
            [['image','alias', 'value'], 'string', 'max' => 255],
            [['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'),
            'alias' => Yii::t('app', 'Alias'),
            'sort' => Yii::t('app', 'Sort'),
            'default_value' => Yii::t('app', 'Default Value'),
            'image' => Yii::t('product', 'Image'),
        ];
    }

    public static function find() {
        return new TaxOptionQuery(get_called_class());
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getTaxEntityRelations()
    {
        return $this->hasMany(TaxEntityRelation::className(), ['tax_option_id' => 'tax_option_id'])->inverseOf('taxOption');
    }


    /**
     * @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');
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getTaxOptionToGroups()
    {
        return $this->hasMany(TaxOptionToGroup::className(), ['tax_option_id' => 'tax_option_id'])->inverseOf('taxOption');
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getTaxOptionToOptions()
    {
        return $this->hasMany(TaxOptionToOption::className(), ['tax_option1_id' => 'tax_option_id'])->inverseOf('taxOption1');
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getTaxOptionToOptions0()
    {
        return $this->hasMany(TaxOptionToOption::className(), ['tax_option2_id' => 'tax_option_id'])->inverseOf('taxOption2');
    }


    public function getTaxValueString(){
        return $this->name;
    }


    /**
     */
    public function getValue()
    {
        return $this->name;
    }
    public function setValue($value){
        return $this->name = $value;
    }
    /**
     */
    public function getValueRenderFlash()
    {
        return $this->name;
    }

    /**
     */
    public function getValueRenderHTML()
    {
        return $this->name;
    }



    /**
     * @return \yii\db\ActiveQuery
     */
    public function getValues()
    {
        if ($valueClass = $this->getValueModelName())
            return $this->hasMany($valueClass, ['tax_option_id' => 'tax_option_id'])->inverseOf('taxOption')->cascadeOnDelete();
    }

    public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)) {

            if (empty($this->parent_id))
                $this->parent_id = 0;



            return true;
        }
        return false;
    }

    public function getImageFile() {
        return empty($this->image) ? null : Yii::getAlias('@imagesDir/tax_option/'. $this->image);
    }

    public function getImageUrl()
    {
        return empty($this->image) ? null : Yii::getAlias('@imagesUrl/tax_option/' . $this->image);
    }
}
