<?php

namespace backend\models;

use Yii;

/**
 * This is the model class for table "admin_menu".
 *
 * @property integer $admin_menu_id
 * @property integer $admin_menu_pid
 * @property integer $status
 * @property integer $hide_min
 * @property integer $sort
 * @property string $name
 * @property string $path
 * @property string $param
 *
 * @property AdminMenu $parent
 * @property AdminMenu[] $adminMenus
 * @property AdminMenuAccessGroup[] $adminMenuAccessGroups
 * @property AdminMenuAccessUser[] $adminMenuAccessUsers
 */
class AdminMenu extends \yii\db\ActiveRecord
{

    public $parentt;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'admin_menu';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['admin_menu_pid', 'status', 'hide_min', 'sort'], 'integer'],
            [['name'], 'required'],
            [['name', 'path', 'param','parentt'], 'string']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'admin_menu_id' => Yii::t('app', 'Admin menu ID'),
            'admin_menu_pid' => Yii::t('app', 'Admin menu parent ID'),
            'status' => Yii::t('app', 'Status'),
            'hide_min' => Yii::t('app', 'Hide Min'),
            'sort' => Yii::t('app', 'Sort'),
            'name' => Yii::t('app', 'Name'),
            'path' => Yii::t('app', 'Path'),
            'param' => Yii::t('app', 'Params'),
            'parentt' => Yii::t('app', 'Parent item')
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getParent()
    {
        return $this->hasOne(AdminMenu::className(), ['admin_menu_id' => 'admin_menu_pid']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getAdminMenus()
    {
        return $this->hasMany(AdminMenu::className(), ['admin_menu_pid' => 'admin_menu_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getAdminMenuAccessGroups()
    {
        return $this->hasMany(AdminMenuAccessGroup::className(), ['admin_menu_id' => 'admin_menu_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getAdminMenuAccessUsers()
    {
        return $this->hasMany(AdminMenuAccessUser::className(), ['admin_menu_id' => 'admin_menu_id']);
    }

    public static function buildMenu($withValues = false)
    {
        $result = [];
        $roots = self::find()->where(['admin_menu_pid' => NULL])->with(['adminMenus'])->all();
        foreach($roots as $root) {
            if($root->adminMenus) {
                $result[] = ['label' => Yii::t('app', $root->name), 'id' => $root->admin_menu_id, 'options' => ['class' => 'header']];
                foreach($root->adminMenus as $submenu) {
                    if($submenu->adminMenus) {
                        $items = [];
                        foreach($submenu->adminMenus as $item) {
                            $items[] = ['label' => Yii::t('app', $item->name), 'id' => $item->admin_menu_id, 'icon' => 'fa fa-circle-o', 'url' => array_merge([$item->path], \common\models\Tools::parseUrlParams($item->param))];
                        }
                        $result[] = ['label' => Yii::t('app', $submenu->name), 'id' => $submenu->admin_menu_id, 'icon' => 'fa fa-circle-o', 'url' => '#', 'items' => $items];
                        unset($items);
                    } else {
                        $result[] = ['label' => Yii::t('app', $submenu->name), 'id' => $submenu->admin_menu_id, 'icon' => 'fa fa-circle-o', 'url' => array_merge([$submenu->path], \common\models\Tools::parseUrlParams($submenu->param))];
                    }
                }
            }
        }
        return $result;
    }

    public static function buildMenuSelect()
    {
        $result = [];
        $roots = self::find()->where(['admin_menu_pid' => NULL])->with(['adminMenus'])->all();
        foreach($roots as $root) {
            if($root->adminMenus) {
                $items = [];
                foreach($root->adminMenus as $submenu) {
                    $items[] = ['label' => Yii::t('app', $submenu->name), 'id' => $submenu->admin_menu_id];
                }
                $result[] = ['label' => Yii::t('app', $root->name), 'id' => $root->admin_menu_id, 'items' => $items];
                unset($items);
            } else {
                $result[] = ['label' => Yii::t('app', $root->name), 'id' => $root->admin_menu_id];
            }
        }
        return $result;
    }

    public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)) {
            if(!$this->admin_menu_pid) {
                $this->admin_menu_pid = NULL;
            }
            return true;
        } else {
            return false;
        }
    }
}
