<?php
declare (strict_types=1);

namespace app\model\system;

use library\basic\BaseModel;
use library\utils\Arr;
use think\db\exception\DbException;
use think\Model;

/**
 * @mixin \think\Model
 */
class AdminMenu extends BaseModel
{
    private $_data;

    private $rolePath;


    /**
     * 设置显示状态
     * @param $id
     * @param $status
     */
    public function setStatus($id, $status)
    {
        self::beginTrans();
        try {
            $this->where('id', $id)->save(['is_show' => $status]);
            $this->where('pid', $id)->save(['is_show' => $status]);
            self::commitTrans();
            return true;
        } catch (DbException $db) {
            self::rollbackTrans();
            return false;
        }

    }

    /**
     * 保存分类
     * @param $post
     * @return bool
     */
    public function saveMenu($post)
    {
        if (!empty($post['id'])) {
            $this->where('id', $post['id'])->save($post);
            return true;
        } else {
            unset($post['id']);
            $bool = $this->insert($post);
            return $bool;
        }
    }


    /**
     * 删除栏目
     * @param $id
     */
    public function delMenu($id)
    {
        $this->where('pid', $id)->delete();
        $this->where('id', $id)->delete();
        return true;
    }


    /**
     * 获取admin端用户菜单
     * @param $roule_id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getRoute($roule_id)
    {
        $rules  = (new AdminRole())->getRoleId($roule_id);
        $menuAr = $this->getArMenu(1);
        $paths = [];
        //判断权限
        if ($rules['is_system']) {
            $resultAr = Arr::toIviewUi($menuAr);
        } else {
            //获取权限
            $rolePathData = RolePath::getMoule($rules['module']);
            //权限对比
            $resultAr = $this->permissions($rolePathData, $menuAr);
            foreach ($rolePathData as $v) {
                if(strpos($v['role_path'],',') !== false) {
                    $aAr = explode(',',$v['role_path']);
                    $aAr = array_filter($aAr,function ($item){ return empty(trim($item)) ? false  :true; });
                   foreach ($aAr as $v2) {
                       $paths[] = trim($v2);
                   }

                } else {
                    if(!empty($v['role_path'])) $paths[] = trim($v['role_path']);
                }
            }
        }
        $resultAr = array_merge([[
            'header'    => '0',
            'icon'      => 'md-home',
            'is_header' => 0,
            'path'      => '/system/index',
            'title'     => '主页',
        ]], $resultAr);
        return [$resultAr, ['is_system' => $rules['is_system'], 'data' => $paths]];
    }

    /**
     * 获取分类数据
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getArMenu($show = 0)
    {
        $menus = $this->when(!empty($show), function ($query) {
            $query->where('is_show', 1);
        })->order("seq", "desc")->select();
        $data  = [];
        foreach ($menus as $item) {
            $data[] = $item->getData();
        }
        $menuAr = Arr::getTree($data);
        return $menuAr;
    }

    /**
     * 权限分析
     * @param $rolePath 权利组
     * @param $menuAr 后台菜单组
     */
    public function permissions($rolePath, $menuAr)
    {
        $mPath   = array_column($rolePath, 'path_menu_id');
        $nMenuAr = [];
        foreach ($menuAr as $v) {
            $d = [];
            if(!empty($v['children'])) {
                foreach ($v['children'] as $v2) {
                    if (in_array($v2['id'], $mPath)) {
                        $d[] = $v2;
                    }
                }
            }
            if (!empty($d)) {
                $v['children'] = $d;
                $nMenuAr[]     = $v;
            }
        }
        return $nMenuAr;
    }

}
