<?php
/**
 * Created by PhpStorm.
 * User: qiuzijian
 * Date: 2021-04-15
 * Time: 11:24
 */

namespace Modules\Admin\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Modules\Admin\Auxiliary\View\FromAuxiliary;
use Modules\Admin\Auxiliary\View\TreeAuxiliary;
use Modules\Admin\Http\Requests\MenusRequest;
use Modules\Admin\Services\MenusService;
use Spatie\Permission\Models\Permission;

class MenusController extends BaseController
{
    public function __construct()
    {
        parent::__construct();
        $this->menusActive[0] = 'adminSetting';
        $this->menusActive[1] = 'adminSettingMenus';
    }

    /**
     * 菜单列表
     * @return mixed
     */
    public function index()
    {
        $menusService = new MenusService();
        $menusAll     = $menusService->getMenusList();

        $treeObj = new TreeAuxiliary('menus', $menusAll);
        $treeObj->column('title', '菜单名')
                ->column('slug', '标识')
                ->column('path', '路由')
                ->column('sort', '排序')
                ->column('show', '是否可见')->display(
                function ($show) {
                    $value = ['否', '是'];
                    return $value[$show];
                }
            );
        return $this->tree($treeObj);
    }

    /**
     * 添加菜单页面
     * @return mixed
     */
    public function add()
    {
        $menusService   = new MenusService();
        $menusAll       = $menusService->getMenusList();
        $fromObj        = new FromAuxiliary('menus/add');
        $fromObj->title = '添加菜单';
        $fromObj->tree_select('parent_id', '父级菜单', $menusAll)
                ->icon('icon', '图标')
                ->input('title', '菜单名称')
//            ->input('slug', '菜单标识')
                ->input('path', '访问路由')
                ->input('sort', '排序', 10)
                ->radio('show', '是否显示', [1 => '是', 0 => '否'], 1);

        return $this->from($fromObj);
    }

    /**
     * 菜单添加请求
     * @param MenusRequest $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function addPost(Request $request)
    {
        $menus  = new MenusService();
        $result = $menus->addMenu($request->all());
        if ($result == 'has') {
            return $this->error(1, '菜单标识重复，请修改后重新提交');
        }
        return $this->redirect('admin/menus/list');
    }

    /**
     * 修改菜单页面
     * @param Request $request
     * @return mixed
     */
    public function edit(Request $request)
    {
        $menuId = $request->input('id');
        if (!$menuId) {
            return false;
        }
        $menusService = new MenusService();
        $menusAll     = $menusService->getMenusList();
        $currentMenu  = $menusService->getMenuInfo($menuId);

        $fromObj        = new FromAuxiliary('menus/edit', $currentMenu);
        $fromObj->title = '修改菜单';
        $fromObj->tree_select('parent_id', '父级菜单', $menusAll, $currentMenu['parent_id'])
                ->icon('icon', '图标')
                ->input('title', '菜单名称')
//            ->input('slug', '菜单标识')
                ->input('path', '访问路由')
                ->input('sort', '排序')
                ->radio('show', '是否显示', [1 => '是', 0 => '否']);

        return $this->from($fromObj);
    }

    /**
     * 修改菜单请求
     * @param MenusRequest $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function editPost(MenusRequest $request)
    {
        if ($request->input('id') == $request->input('parent_id')) {
            return $this->error(1, '父级不能选择自己');
        }
        $menus = new MenusService();
        $menus->editMenu($request->all());
        return $this->redirect('admin/menus/list');
    }

    public function delPost(Request $request)
    {
        $menuId = $request->input('id');
        if (!$menuId) {
            return false;
        }
        $menus = new MenusService();
        $menus->delMenu($menuId);
        return $this->redirect('admin/menus/list');
    }

    /**
     * 菜单导出文件
     * @return string
     */
    public function makeMenuToFile()
    {
        $menusService = new MenusService();
        $menusAll     = $menusService->getMenusList();
        $menus        = $this->menuFilter($menusAll);
//        $apiMenusAll  = $menusService->getMenusList('', 'api');
//        $apiMenus     = $this->menuFilter($apiMenusAll);

        Storage::put('menulist.php', var_export($menus, true));
//        Storage::put('apimenulist.php', var_export($apiMenus, true));
        return 'ok';
    }

    protected function menuFilter($menuList)
    {
        $menus = [];
        foreach ($menuList as $menu) {
            $_menu = [
                "icon"  => $menu['icon'],
                "title" => $menu['title'],
                "slug"  => $menu['slug'],
                "path"  => $menu['path'],
                "sort"  => $menu['sort'],
                "show"  => $menu['show'],
                "child" => $menu['child'],
            ];
            if (count($menu['child']) > 0) {
                $_menu['child'] = $this->menuFilter($menu['child']);
            }
            $menus[] = $_menu;
        }
        return $menus;
    }
}