<!--菜单管理(超管)-->
<template>
  <div class="app-container menu">
    <div class="page-container menuPage">
      <div class="page-form-title-box">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
          <el-form-item label="" prop="menuName">
            <el-input
              v-model="queryParams.menuName"
              placeholder="请输入菜单名称"
              clearable
              size="small"
            />
          </el-form-item>
          <el-form-item label="" prop="status">
            <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
              <el-option
                v-for="dict in statusOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
          <p class="page-inquire-common-style-button" @click="handleQuery">查询</p>
          <p class="page-reset-common-style-button" @click="resetQuery">重置</p>
          <p class="page-submit-common-style-button"
             style="float: right;"
             @click="handleAdd"
             v-hasPermiRouter="['system:menu:add']"
          >新增</p>
        </el-form>
      </div>
      <div class="page-content-box">
        <el-table class="table-box" border
                  v-loading="loading"
                  :data="menuList"
                  row-key="menuId"
                  :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
        >
          <el-table-column prop="menuName" label="菜单名称" :show-overflow-tooltip="true" width="160"></el-table-column>
          <el-table-column prop="icon" label="图标" align="center" width="100">
            <template slot-scope="scope">
              <svg-icon :icon-class="scope.row.icon" />
            </template>
          </el-table-column>
          <el-table-column prop="orderNum" label="排序" width="60"></el-table-column>
          <el-table-column prop="perms" label="权限标识" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column prop="path" label="路由地址" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column prop="component" label="组件路径" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column prop="menuType" label="类型" width="60">
            <template slot-scope="scope">
              <span>{{scope.row.menuType==0?'目录':(scope.row.menuType==1?'菜单':(scope.row.menuType==2?'按钮':''))}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="status" label="状态" width="120">
            <template slot-scope="scope">
          <span>
            {{scope.row.visible?'显示':(!scope.row.visible?'隐藏':'')}}
            |
            {{scope.row.status?'启用':(!scope.row.status?'停用':'')}}
          </span>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" align="center" prop="createTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime,"{y}-{m}-{d} {h}:{i}") }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180"
                           v-if="tableButtonType">
            <template slot-scope="scope">
              <div class="table-button-box">
                <p class="table-button-null"></p>
                <p class="table-button-p"
                   v-if="!scope.row.isFrame&&scope.row.menuType!=2"
                   @click="handleAdd(scope.row)"
                   v-hasPermiRouter="['system:menu:add']"
                >新增</p>
                <p class="table-button-p"
                   @click="handleUpdate(scope.row)"
                   v-hasPermiRouter="['system:menu:edit']"
                >修改</p>
                <p class="table-button-p"
                   @click="handleDelete(scope.row)"
                   v-hasPermiRouter="['system:menu:del']"
                >删除</p>
                <p class="table-button-null"></p>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <!-- 添加或修改菜单对话框 -->
    <el-dialog class="menu-dialog-max-big-box" v-if="open" :title="title" :visible.sync="open" width="680px"
               append-to-body :close-on-click-modal="false">
      <div class="menu-dialog-big-box scrollbar-box">
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
          <el-row>
            <el-col :span="24">
              <el-form-item label="上级菜单">
                <treeselect
                  style="width:500px;"
                  @select="parentSelect"
                  v-model="form.parentId"
                  :options="menuOptions"
                  :normalizer="normalizer"
                  :show-count="true"
                  placeholder="选择上级菜单"
                />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="菜单类型" prop="menuType">
                <el-radio-group v-model="form.menuType" >
                  <el-radio v-if="catalogueRadioType"
                            :disabled="false" :label="0">
                    目录
                  </el-radio>
                  <el-radio v-if="buttonRadioType"
                            :disabled="false" :label="1">
                    菜单
                  </el-radio>
                  <el-radio v-if="menuRadioType"
                            :disabled="false" :label="2" >
                    按钮
                  </el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="24" v-if="form.menuType != '2'">
              <el-form-item label="菜单图标">
                <el-popover
                  placement="bottom-start"
                  width="500"
                  trigger="click"
                  @show="$refs['iconSelect'].reset()"
                >
                  <IconSelect ref="iconSelect" @selected="selected" />
                  <el-input slot="reference"
                            style="width:500px;"
                            v-model="form.icon"
                            placeholder="点击选择图标"
                            readonly>
                    <svg-icon
                      v-if="form.icon"
                      slot="prefix"
                      :icon-class="form.icon"
                      class="el-input__icon"
                      style="height: 32px;width: 16px;"
                    />
                    <i v-else slot="prefix" class="el-icon-search el-input__icon" />
                  </el-input>
                </el-popover>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="菜单名称" prop="menuName">
                <el-input v-model="form.menuName" placeholder="请输入菜单名称" style="width:180px;" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="显示排序" prop="orderNum">
                <el-input-number v-model="form.orderNum" controls-position="right" :min="0"
                                 style="width:180px;"/>
              </el-form-item>
            </el-col>
            <el-col :span="12" v-if="form.menuType != '2'">
              <el-form-item>
              <span slot="label">
                <el-tooltip content="选择是时不在进行路由跳转path自动替换为menuName" placement="top">
                <i class="el-icon-question" style="color:#999;"></i>
                </el-tooltip>
                链接跳转
              </span>
                <el-radio-group v-model="form.isFrame">
                  <el-radio :label="true" style="width:60px;">是</el-radio>
                  <el-radio :label="false" style="width:60px;">否</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="12" v-if="form.menuType != '2'&&form.isFrame">
              <el-form-item prop="linkId">
              <span slot="label">
                <el-tooltip content="跳转地址可在服务管理下的链接管理中配置" placement="top">
                <i class="el-icon-question" style="color:#999;"></i>
                </el-tooltip>
                跳转地址
              </span>
                <el-select v-model="form.linkId" placeholder="请选择参数类型" style="width:180px;">
                  <el-option
                    v-for="dict in externalOptions"
                    :key="dict.linkId"
                    :label="dict.linkName"
                    :value="dict.linkId"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12" v-if="form.menuType != '2'">
              <el-form-item prop="path">
              <span slot="label">
                <el-tooltip content="访问的路由地址，如：`user`，如外网地址需内链访问则以`http(s)://`开头" placement="top">
                <i class="el-icon-question" style="color:#999;"></i>
                </el-tooltip>
                路由地址
              </span>
                <el-input v-model="form.path" placeholder="请输入路由地址" style="width:180px;"/>
              </el-form-item>
            </el-col>
            <el-col :span="12" v-if="form.menuType == '1' && !form.isFrame">
              <el-form-item prop="component">
              <span slot="label">
                <el-tooltip content="访问的组件路径，如：`system/user/index`，默认在`views`目录下" placement="top">
                <i class="el-icon-question" style="color:#999;"></i>
                </el-tooltip>
                组件路径
              </span>
                <el-input v-model="form.component" placeholder="请输入组件路径" />
              </el-form-item>
            </el-col>
            <el-col :span="12" v-if="form.menuType != '0' || form.isFrame">
              <el-form-item :prop="form.isFrame?'perms':''">
                <el-input v-model="form.perms" placeholder="请输入权限标识" maxlength="100" style="width:180px;"/>
                <span slot="label">
                <el-tooltip content="控制器中定义的权限字符，如：@PreAuthorize(`@ss.hasPermiRouter('system:user:list')`)" placement="top">
                <i class="el-icon-question" style="color:#999;"></i>
                </el-tooltip>
                权限字符
              </span>
              </el-form-item>
            </el-col>
            <el-col :span="12" v-if="form.menuType != '2'">
              <el-form-item>
              <span slot="label">
                <el-tooltip content="选择隐藏则路由将不会出现在侧边栏，但仍然可以访问" placement="top">
                <i class="el-icon-question" style="color:#999;"></i>
                </el-tooltip>
                显示状态
              </span>
                <el-radio-group v-model="form.visible">
                  <el-radio
                    style="width:60px;"
                    v-for="dict in visibleOptions"
                    :key="dict.dictValue"
                    :label="dict.dictValue"
                  >{{dict.dictLabel}}</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="12" v-if="form.menuType != '2'">
              <el-form-item>
              <span slot="label">
                <el-tooltip content="选择停用则路由将不会出现在侧边栏，也不能被访问" placement="top">
                <i class="el-icon-question" style="color:#999;"></i>
                </el-tooltip>
                菜单状态
              </span>
                <el-radio-group v-model="form.status">
                  <el-radio
                    style="width:60px;"
                    v-for="dict in statusOptions"
                    :key="dict.dictValue"
                    :label="dict.dictValue"
                  >{{dict.dictLabel}}</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="12" v-if="form.menuType == '1'">
              <el-form-item>
              <span slot="label">
                <el-tooltip content="选择是则会被`keep-alive`缓存，需要匹配组件的`name`和地址保持一致" placement="top">
                <i class="el-icon-question" style="color:#999;"></i>
                </el-tooltip>
                是否缓存
              </span>
                <el-radio-group v-model="form.isCache">
                  <el-radio :label="true" style="width:60px;">缓存</el-radio>
                  <el-radio :label="false" style="width:60px;">不缓存</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item>
              <span slot="label">
                <el-tooltip content="选择是后在权限配置时此项为选中并且无法操作" placement="top">
                <i class="el-icon-question" style="color:#999;"></i>
                </el-tooltip>
                是否必选
              </span>
                <el-radio-group v-model="form.isRequired">
                  <el-radio :label="true" style="width:60px;">是</el-radio>
                  <el-radio :label="false" style="width:60px;">否</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer dialog-footer-box">
        <p class="dialog-footer-button-null"></p>
        <p class="dialog-footer-button-info" @click="cancel">取消</p>
        <p class="dialog-footer-button-primary" @click="submitForm">提交</p>
        <p class="dialog-footer-button-null"></p>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { systemLinkDropList } from "@/api/commonality/permission";
import { listMenu, getMenu, delMenu, addMenu, updateMenu } from "@/api/systemManagement/index";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import IconSelect from "@/components/IconSelect";

export default {
  name: "Menu",
  components: { Treeselect, IconSelect },
  data() {
    return {
      tableButtonType:this.hasPermiDom(['system:menu:add','system:menu:edit','system:menu:detail','system:menu:del']),
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 菜单表格树数据
      menuList: [],
      menuTypeList:[
        {label:0,value:'目录',disabled:false,},
        {label:1,value:'菜单',disabled:false,},
        {label:2,value:'按钮',disabled:false,},
      ],
      // 菜单树选项
      menuOptions: [],
      menuOptionsList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 显示状态数据字典
      visibleOptions: [
        {dictValue:true,dictLabel:"显示"},
        {dictValue:false,dictLabel:"隐藏"},
      ],
      // 菜单状态数据字典
      statusOptions: [
        {dictValue:true,dictLabel:"启用"},
        {dictValue:false,dictLabel:"停用"},
      ],
      // 查询参数
      queryParams: {
        menuName: undefined,
        visible: undefined
      },
      //dialog菜单类型展示判断
      catalogueRadioType:false,
      buttonRadioType:false,
      menuRadioType:false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        menuName: [
          { required: true, message: "菜单名称不能为空", trigger: "change" },
          { required: true, message: "菜单名称不能为空", validator: this.spaceJudgment, trigger: "change" }
        ],
        menuType: [
          { required: true, message: "请选择类型", trigger: "change" },
          { required: true, message: "请选择类型", validator: this.spaceJudgment, trigger: "change" }
        ],
        orderNum: [
          { required: true, message: "菜单顺序不能为空", trigger: "change" },
          { required: true, message: "菜单顺序不能为空", validator: this.spaceJudgment, trigger: "change" }
        ],
        path: [
          { required: true, message: "路由地址不能为空", trigger: "change" },
          { required: true, message: "路由地址不能为空", validator: this.spaceJudgment, trigger: "change" }
        ],
        linkId: [
          { required: true, message: "请选择跳转地址", trigger: "change" },
          { required: true, message: "请选择跳转地址", validator: this.spaceJudgment, trigger: "change" }
        ],
        perms: [
          { required: true, message: "请输入权限字符", trigger: "change" },
          { required: true, message: "请输入权限字符", validator: this.spaceJudgment, trigger: "change" }
        ],
      },
      //跳转地址列表
      externalOptions:[],
    };
  },
  created() {
    this.getList();
    this.systemLinkDropList();
    // this.getDicts("sys_show_hide").then(response => {
    //   this.visibleOptions = response.data;
    // });
    // this.getDicts("sys_normal_disable").then(response => {
    //   this.statusOptions = response.data;
    // });
  },
  methods: {
    //获取链接下拉列表
    systemLinkDropList(){
      systemLinkDropList({}).then(response => {
        this.$set(this,'externalOptions',response.data);
      });
    },
    //选中上级菜单
    parentSelect(item){
      this.$set(this.form,'menuType','');
      this.lookUpParent(4,item);
    },
    //查找上级数据
    lookUpParent(type,row){
      if(type == 1){
        //独立新增按钮
        this.$set(this,'catalogueRadioType',true);
        this.$set(this,'buttonRadioType',false);
        this.$set(this,'menuRadioType',false);
      }else if(type == 3){
        //修改
        if(row.parentId == '0'){
          //主类目
          this.$set(this,'catalogueRadioType',true);
          this.$set(this,'buttonRadioType',false);
          this.$set(this,'menuRadioType',false);
        }else if(row.menuType == '0'){
          //目录
        }else if(row.menuType == '1'){
          //菜单
          if(row.children){
            this.$set(this,'catalogueRadioType',false);
            this.$set(this,'buttonRadioType',true);
            this.$set(this,'menuRadioType',false);
          }else{
            if(row.level == 2){
              this.$set(this,'catalogueRadioType',true);
              this.$set(this,'buttonRadioType',true);
              this.$set(this,'menuRadioType',false);
            }else if(row.level == 3){
              this.$set(this,'catalogueRadioType',false);
              this.$set(this,'buttonRadioType',true);
              this.$set(this,'menuRadioType',false);
            }
          }
        }else if(row.menuType == '2'){
          //按钮
          this.$set(this,'catalogueRadioType',false);
          this.$set(this,'buttonRadioType',false);
          this.$set(this,'menuRadioType',true);
        }
      }else if(type == 2 || type == 4){
        //下拉框变动时
        if(row.menuId == '0'){
          //主类目
          this.$set(this,'catalogueRadioType',true);
          this.$set(this,'buttonRadioType',false);
          this.$set(this,'menuRadioType',false);
        }else if(row.menuType == '0'){
          //一级/二级目录
          if (row.level == 1){
            this.$set(this,'catalogueRadioType',true);
            this.$set(this,'buttonRadioType',true);
            this.$set(this,'menuRadioType',false);
          }else if(row.level == 2){
            this.$set(this,'catalogueRadioType',false);
            this.$set(this,'buttonRadioType',true);
            this.$set(this,'menuRadioType',false);
          }
        }else if(row.menuType == '1'){
          //菜单
          this.$set(this,'catalogueRadioType',false);
          this.$set(this,'buttonRadioType',false);
          this.$set(this,'menuRadioType',true);
        }
      }
    },
    // 选择图标
    selected(name) {
      this.form.icon = name;
    },
    /** 查询菜单列表 */
    getList() {
      this.loading = true;
      listMenu(this.queryParams).then(response => {
        this.menuList = this.handleTree(response.data, "menuId");
        this.loading = false;
      });
    },
    /** 转换菜单数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.menuId,
        label: node.menuName,
        children: node.children
      };
    },
    /** 查询菜单下拉树结构 */
    getTreeselect(row) {
      let self = this;
      listMenu({}).then(response => {
        let list = [];
        for(let i=0;i<response.data.length;i++){
          if(!response.data[i].isFrame && response.data[i].component){
            if(row){
              if(response.data[i].menuId != row.menuId){
                list.push(response.data[i])
              }
            }else{
              list.push(response.data[i])
            }
          }
        }
        this.menuOptions = [];
        const menu = { menuId: '0', menuName: '主类目', children: [] };
        menu.children = this.handleTree(list, "menuId");
        this.menuOptions.push(menu);
        this.$set(this,'menuOptionsList',list);
      });
    },
    // 菜单状态字典翻译
    statusFormat(row, column) {
      if (row.menuType == "2") {
        return "";
      }
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.$set(this,'form',{
        menuId: undefined,
        parentId: 0,
        menuName: undefined,
        icon: undefined,
        menuType: 0,
        orderNum: undefined,
        isFrame: false,
        linkId: null,
        isCache: true,
        visible: true,
        status: true,
        isRequired: false
      });
      this.$set(this,'typeButtonType','');
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 新增按钮操作 */
    handleAdd(row) {
      this.getTreeselect();
      this.reset();
      if (row != null && row.menuId) {
        this.lookUpParent(2,row);
        this.form.parentId = row.menuId;
      } else {
        this.lookUpParent(1);
        this.form.parentId = 0;
      }
      this.open = true;
      this.title = "添加菜单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.getTreeselect(row);
      getMenu({menuId:row.menuId}).then(response => {
        this.lookUpParent(3,row);
        this.$set(this,'form',response.data);
        this.open = true;
        this.title = "修改菜单";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      let self = this;
      this.$refs["form"].validate(valid => {
        if (valid) {
          // let obj = JSON.parse(JSON.stringify(this.form))
          let obj = {
            menuId: this.form.menuId,
            menuName: this.form.menuName,
            parentId: this.form.parentId,
            orderNum: this.form.orderNum,
            isFrame: this.form.isFrame,
            linkId: this.form.isFrame?this.form.linkId:'',
            path: this.form.path,
            isCache: this.form.isCache,
            menuType: this.form.menuType,
            visible: this.form.visible,
            status: this.form.status,
            perms: this.form.perms,
            icon: this.form.icon,
            isRequired: this.form.isRequired
          }
          obj.component = obj.parentId == 0 && obj.menuType == 0 ? 'Layout':(obj.parentId != 0 && obj.menuType == 0 ?'ParentView':((obj.parentId != 0 && obj.isFrame ?'ParentView':this.form.component)));
          if (obj.menuId != undefined) {
            updateMenu(obj).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMenu(obj).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除名称为"' + row.menuName + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delMenu({menuId:row.menuId});
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    //新增自定义参数
    addCustomParameter(){
      this.form.customParameter.push({
        key:'',
        value:'',
      })
    },
    //新增自定义参数
    delCustomParameter(index){
      this.form.customParameter.splice(index,1);
    },
  }
};
</script>
<style scoped lang="scss">
  .menu {
    .menuPage{

    }
  }
</style>
<style lang="scss">
  .menu-dialog-max-big-box{
    .menu-dialog-big-box{
      height:600px;
    }
    .for-custom-parameter-max-big-box{
      display: flex;
      width:180px;
      margin-bottom:20px;
      .input-box{
        flex:1;
        border: 1px solid #999;
        border-right:none;
        border-top-left-radius:4px;
        border-bottom-left-radius:4px;
        .top-input{
          .el-input__inner{
            border:none;
          }
        }
        .bottom-input{
          border-top:1px solid #999;
          .el-input__inner{
            border:none;
          }
        }
      }
      .del-button{
        width:30px;
        height:83px;
        line-height:83px;
        text-align: center;
        font-size:20px;
        color:#fff;
        background-color: #FF6666;
        border:1px solid #FF6666;
        border-top-right-radius:4px;
        border-bottom-right-radius:4px;
        cursor: pointer;
      }
    }
    .add-button{
      width:180px;
      height:40px;
      line-height:40px;
      text-align: center;
      font-size:20px;
      color:#fff;
      background-color: #0183FA;
      border:1px solid #0183FA;
      border-radius:4px;
      cursor: pointer;
    }
  }
</style>
