<template>
  <el-dialog
    class="sl-menu-item"
    width="465px"
    :title="!dataForm.id ?  buttons.add :buttons.edit"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item :label="buttons.type || '类型'" prop="type">
        <el-radio-group v-model="dataForm.type">
          <el-radio v-for="(type, index) in dataForm.typeList" :label="index" :key="index">{{ type }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item   :label="dataForm.typeList[dataForm.type] + buttons.code ||'编号'" prop="menuId">
        <el-input :readonly="dataForm.id?true:false" style="width: 349px;" v-model="dataForm.menuId" ></el-input>
      </el-form-item>
      <el-form-item :label="dataForm.typeList[dataForm.type] + buttons.typeName ||'名称'" prop="name">
        <el-input style="width: 349px;" v-model="dataForm.name" ></el-input>
      </el-form-item>
      <el-form-item :label="buttons.parentName ||'上级菜单'" prop="parentName">
        <el-popover
          ref="menuListPopover"
          placement="bottom-start"
          v-model="treeVisible"
          onclick="treeVisible=true"
          trigger="click">
          <el-tree
            :data="menuList"
            :props="menuListTreeProps"
            node-key="menuId"
            ref="menuListTree"
            @current-change="menuListTreeCurrentChangeHandle"
            :default-expand-all="false"
            :highlight-current="true"
            :expand-on-click-node="false">
          </el-tree>
        </el-popover>
        <el-input style="width: 349px;"  v-model="dataForm.parentName" v-popover:menuListPopover :readonly="true"  class="menu-list__input"></el-input>
      </el-form-item>
      <el-form-item v-if="dataForm.type === 1" :label="buttons.route || '菜单路由'" prop="url">
        <el-input style="width: 349px;"  v-model="dataForm.url" placeholder="菜单路由"></el-input>
      </el-form-item>
      <el-form-item v-if="dataForm.type !== 0" :label="buttons.perms || '授权标识'" prop="perms">
        <el-input style="width: 349px;"  v-model="dataForm.perms" placeholder="多个用逗号分隔, 如: user:list,user:create"></el-input>
      </el-form-item>
      <el-form-item v-if="dataForm.type !== 2" :label="buttons.orderNum || '排序号'" prop="orderNum">
        <el-input style="width: 349px;" oninput="value=value.replace(/[^\d]/g, '')" v-model="dataForm.orderNum"  :min="0" ></el-input>
      </el-form-item>
      <el-form-item  :label="buttons.menuType || '菜单类型'" prop="menuType">
        <el-select v-model="dataForm.menuType">
          <el-option label="pc" value="pc"></el-option>
          <el-option label="pda" value="pda"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item v-if="dataForm.type !== 2" label="菜单图标" prop="icon">
        <el-row>
          <el-col :span="22">
            <el-popover
              ref="iconListPopover"
              placement="bottom-start"
              trigger="click"
              popper-class="mod-menu__icon-popover">
              <div class="mod-menu__icon-inner">
                <div class="mod-menu__icon-list">
                  <el-button
                    v-for="(item, index) in iconList"
                    :key="index"
                    @click="iconActiveHandle(item)"
                    :class="{ 'is-active': item === dataForm.icon }">
                    <icon-svg :name="item"></icon-svg>
                  </el-button>
                </div>
              </div>
            </el-popover>
            <el-input style="width: 349px;"  v-model="dataForm.icon" v-popover:iconListPopover :readonly="true"  class="icon-list__input"></el-input>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="dataFormSubmit()">{{ buttons.submit || '确定' }}</el-button>
      <el-button type="primary" @click="visible = false">{{ buttons.close || '关闭' }}</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { treeDataTranslate } from '@/utils'
  import Icon from '@/icons'
  import {
    searchFunctionButtonList,
  } from "@/api/sysLanguage.js"
  export default {
    data () {
      var validateUrl = (rule, value, callback) => {
        if (this.dataForm.type === 1 && !/\S/.test(value)) {
          callback(new Error('菜单URL不能为空'))
        } else {
          callback()
        }
      }
      return {
        visible: false,
        treeVisible: false,
        dataForm: {
          id: 0,
          type: 1,
          typeList: ['目录', '菜单', '按钮'],
          name: '',
          parentId: 0,
          parentName: '',
          url: '',
          perms: '',
          orderNum: 0,
          icon: '',
          iconList: [],
          menuType: '',
          menuId:'',
          typeName:'名称'
        },
        dataRule: {
          name: [
            { required: true, message: '菜单名称不能为空', trigger: 'blur' }
          ],
          parentName: [
            { required: true, message: '上级菜单不能为空', trigger: 'change' }
          ],
          menuId: [
            { required: true, message: '上级菜单不能为空', trigger: 'change' }
          ],
          url: [
            { validator: validateUrl, trigger: 'blur' }
          ],
        },
        buttons: {
          cz: '操作',
          add: '添加',
          edit: '编辑',
          close: '关闭',
          submit: '确定',
          menuType:'菜单类型',
          name:'名称',
          parentName:'上级菜单',
          icon:'图标',
          type:'类型',
          menu:'菜单',
          button:'按钮',
          orderNum:'排序号',
          url:'菜单URL',
          perms:'授权标识',
          helpFile : '帮助文档',
          language:'语言',
          route:'菜单路由',
          type1: '目录',
          type2: '菜单',
          type3: '按钮',
          code:'编号'
        },
        menuList: [],
        menuListTreeProps: {
          label: 'name',
          children: 'children'
        }
      }
    },
    created () {
      this.iconList = Icon.getNameList()
      this.getFunctionButtonList()
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.$http({
          url: this.$http.adornUrl('/sys/menu/select'),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          this.menuList = treeDataTranslate(data.menuList, 'menuId')
          console.log( this.menuList)
        }).then(() => {
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
          })
        }).then(() => {
          if (!this.dataForm.id) {
            // 新增
            this.menuListTreeSetCurrentNode()
          } else {
            // 修改
            this.$http({
              url: this.$http.adornUrl(`/sys/menu/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              this.dataForm.id = data.menu.menuId
              this.dataForm.type = data.menu.type
              this.dataForm.name = data.menu.name
              this.dataForm.parentId = data.menu.parentId
              this.dataForm.url = data.menu.url
              this.dataForm.perms = data.menu.perms
              this.dataForm.orderNum = data.menu.orderNum
              this.dataForm.icon = data.menu.icon
              this.dataForm.menuType = data.menu.menuType
              this.dataForm.menuId = data.menu.menuId
              this.menuListTreeSetCurrentNode()
            })
          }
        })
      },
      // 获取button的词典
      getFunctionButtonList() {
        let queryButton = {
          functionId: this.$route.meta.menuId,
          tableId: '*',
          languageCode: this.$i18n.locale,
          objectType: 'button'
        }
        searchFunctionButtonList(queryButton).then(({data}) => {
          if (data.code == 0 && data.data) {
            this.buttons = data.data
            this.typeList= [this.buttons.type1,this.buttons.type2,this.buttons.type3]
          }
        })
      },
      // 菜单树选中
      menuListTreeCurrentChangeHandle (data, node) {
        this.dataForm.parentId = data.menuId
        this.dataForm.parentName = data.name
        this.treeVisible = false
      },
      // 菜单树设置当前选中节点
      menuListTreeSetCurrentNode () {
        this.$refs.menuListTree.setCurrentKey(this.dataForm.parentId)
        this.dataForm.parentName = (this.$refs.menuListTree.getCurrentNode() || {})['name']

      },
      // 图标选中
      iconActiveHandle (iconName) {
        this.dataForm.icon = iconName
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/menu/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'menuId': this.dataForm.menuId,
                'type': this.dataForm.type,
                'name': this.dataForm.name,
                'parentId': this.dataForm.parentId,
                'url': this.dataForm.url,
                'perms': this.dataForm.perms,
                'orderNum': this.dataForm.orderNum,
                'icon': this.dataForm.icon,
                'menuType': this.dataForm.menuType,
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message.success('操作成功')
                this.visible = false
                this.$emit('refreshDataList')
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    },
  }
</script>

<style lang="scss" >
  .mod-menu {
    .menu-list__input,
    .icon-list__input {
       > .el-input__inner {
        cursor: pointer;
      }
    }
    &__icon-popover {
      width: 458px;
      overflow: hidden;
    }
    &__icon-inner {
      width: 478px;
      max-height: 258px;
      overflow-x: hidden;
      overflow-y: auto;
    }
    &__icon-list {
      width: 458px;
      padding: 0;
      margin: -8px 0 0 -8px;
      > .el-button {
        padding: 8px;
        margin: 8px 0 0 8px;
        > span {
          display: inline-block;
          vertical-align: middle;
          width: 18px;
          height: 18px;
          font-size: 18px;
        }
      }
    }
    .icon-list__tips {
      font-size: 18px;
      text-align: center;
      color: #e6a23c;
      cursor: pointer;
    }
  }
  .el-popover {
    height: 50%;
    width: 350px;
    overflow:auto;

  }

  .sl-menu-item .el-form-item {
    margin-bottom: 5px;
  }
</style>
