<?php

namespace common\modules\rubrication\models;

use common\modules\product\models\Category;
use Yii;

/**
 * This is the model class for table "{{%tax_group}}".
 *
 * @property integer $tax_group_id
 * @property string $alias
 * @property string $name
 * @property string $description
 * @property string $module
 * @property boolean $hierarchical
 * @property string $settings
 * @property boolean $is_filter
 * @property integer $level
 * @property integer $sort
 * @property boolean $display
 * @property boolean $is_menu
 * @property TaxGroupToGroup[] $taxGroupToGroups
 * @property TaxGroupToGroup[] $taxGroupToGroups0
 * @property TaxOption[] $taxOptions
 */
class TaxGroup extends \yii\db\ActiveRecord
{
    public $_options = [];

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'slug' => [
                'class' => 'common\behaviors\Slug',
                'in_attribute' => 'name',
                'out_attribute' => 'alias',
                'translit' => true
            ],
        ];
    }

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

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name', 'module'], 'required'],
            [['description', 'settings'], 'string'],
            [['hierarchical', 'is_filter', 'display','is_menu'], 'boolean'],
            [['level', 'sort'], 'integer'],
            [['alias', 'module'], 'string', 'max' => 50],
            [['name'], 'string', 'max' => 255],
            [['categories'], 'safe']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'tax_group_id' => 'Tax Group ID',
            'alias' => 'Alias',
            'name' => 'Name',
            'description' => 'Description',
            'module' => 'Module',
            'hierarchical' => 'Hierarchical',
//            'settings' => 'Settings',
            'is_filter' => 'Use in filter',
            'sort' => 'Sort',
            'display' => 'Display',
            'is_menu' => 'Отображать в меню',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getTaxGroupToGroups()
    {
        return $this->hasMany(TaxGroupToGroup::className(), ['tax_group1_id' => 'tax_group_id'])->inverseOf('taxGroup1');
    }

    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)
    {

        $this->unlinkAll('categories',true);
        $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');
    }

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

    public function getValueModelName($full_path = true) {
        $valueClass = 'TaxValue'. ucfirst($this->module);
        $fullClass = '\common\modules\rubrication\models\\'. $valueClass;
        return class_exists($fullClass) ? $full_path ? $fullClass : $valueClass : FALSE;
    }
}
