<template>
  <div v-loading="loading">
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="150px" class="">
      <el-form-item label="父级分类" prop="parentids">
        <el-cascader
          v-model="parentids"
          :options="menuDropDatas"
          :props="props"
          :placeholder="classificationReminder"
          clearable
          filterable
          change-on-select
          :disabled="judgmentAddEdit"
          style="width: 100%;"/>
      </el-form-item>
      <el-form-item label="菜单名称" prop="name">
        <el-input v-model="ruleForm.name" placeholder="请输入菜单名称"/>
      </el-form-item>
      <el-form-item label="菜单代码" prop="code">
        <el-input v-model="ruleForm.code" placeholder="请输入菜单代码"/>
      </el-form-item>
      <el-form-item label="菜单链接地址" prop="url">
        <el-input v-model="ruleForm.url" placeholder="请输入菜单链接地址"/>
      </el-form-item>
      <el-form-item label="排序编号" prop="sort">
        <el-input v-model="ruleForm.sort" placeholder="请输入排序编号"/>
      </el-form-item>
      <el-form-item label="菜单类型" prop="target">
        <el-radio-group v-model="ruleForm.target">
          <el-radio label="iframe">框架页</el-radio>
          <el-radio label="expand">无页面</el-radio>
          <el-radio label="open">弹出页</el-radio>
          <el-radio label="blank">新窗口</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="菜单图标" prop="imgUrl">
        <el-select v-model="ruleForm.imgUrl" filterable clearable placeholder="请选择菜单图标">
          <el-option v-for="item in iconDatas" :key="item.value" :label="item.label" :value="item.value">
            <svg-icon :icon-class="item.value" style="color: #409eff; margin-right: 5px;" />
            <span style="color: #8492a6; font-size: 13px">{{ item.label }}</span>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否启用" prop="flag">
        <el-switch v-model="ruleForm.flag" active-value="1" inactive-value="0"/>
      </el-form-item>
      <el-form-item label="菜单介绍" prop="remark">
        <el-input v-model="ruleForm.remark" type="textarea" autosize placeholder="请输入菜单介绍"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">保存</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getMenuDrop, addMenu, getMenu, editMenu } from '@/api/systemSetup/roleSetting/menuSetup'
import { iconDatas } from '../iconDatas.js'
import { validateSort } from '@/utils/validate'
import { filterContent } from '@/utils'

const validateSortRule = (rule, value, callback) => {
  if (!validateSort(value)) {
    callback(new Error('请输入有效的排序编号（正整数、负整数、0）'))
  } else {
    callback()
  }
}

export default {
  name: 'AddOrEditMenu',
  props: {
    rowid: {
      type: String,
      default: ''
    },
    parentidArr: {
      type: Array,
      default: () => ['000000000000000000000000'] // 父级ID,无父级ID为'000000000000000000000000'
    },
    layerid: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      ruleForm: {
        id: '', // 当前选择节点ID
        parentid: '', // 父级ID,无父级ID为'000000000000000000000000'
        name: '', // 菜单名称
        code: '', // 编码
        url: '', // 链接地址
        target: 'iframe', // 菜单类型
        flag: '1', // 1为启用，0不启用
        sort: '0', // 排列序号
        imgUrl: 'shouye', // icon
        remark: '' // 备注信息
      },
      rules: {
        name: [{
          required: true,
          message: '请输入菜单名称',
          trigger: 'blur'
        }],
        code: [{
          required: true,
          message: '请输入菜单代码',
          trigger: 'blur'
        }],
        url: [{
          required: true,
          message: '请输入菜单链接',
          trigger: 'blur'
        }],
        sort: [{
          required: true,
          trigger: 'blur',
          validator: validateSortRule
        }]
      },
      // 菜单下拉绑定的值
      parentids: [], // 父级ID,无父级ID为'000000000000000000000000'
      // 菜单下拉数据
      menuDropDatas: [],
      // 自定义菜单下拉数据的key值
      props: {
        value: 'id',
        label: 'text'
      },
      iconDatas,
      loading: false,
      judgmentAddEdit: false,
      classificationReminder: '请选择父级分类，默认是顶级分类',
    }
  },
  created() {
    this.getMenuDrop().then(() => {
      this.parentids = this.parentidArr
      if (this.rowid) {
        this.ruleForm.id = this.rowid
        this.getMenuDetail(this.rowid)
        this.judgmentAddEdit = true
      }
    })
  },
  methods: {
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.ruleForm.parentid = this.parentids[this.parentids.length - 1]
          this.ruleForm.remark = filterContent.delHtmlTag(this.ruleForm.remark)
          this.loading = true
          // 添加
          if (!this.rowid) {
            addMenu(this.ruleForm).then(response => {
              this.loading = false
              if (response.state.toLowerCase() === 'success') {
                this.$parent.$layer.close(this.layerid)
                this.$parent.getDatas() // 重新加载父级数据
                this.$message.success('恭喜你，菜单信息添加成功!')
              }
            }).catch(() => {
              this.loading = false
            })
            return
          }
          // 编辑
          editMenu(this.ruleForm).then(response => {
            this.loading = false
            if (response.state.toLowerCase() === 'success') {
              this.$parent.$layer.close(this.layerid)
              this.$parent.getDatas() // 重新加载父级数据
              this.$message.success('恭喜你，菜单信息编辑成功!')
            }
          }).catch(() => {
            this.loading = false
          })
        } else {
          this.$message.error('请输入有效的必填项信息!')
          return false
        }
      })
    },
    resetForm() {
      this.parentids = []
      this.$refs.ruleForm.resetFields()
    },
    // 获取菜单详情
    getMenuDetail(rid) {
      getMenu(rid).then(response => {
        if (response.state.toLowerCase() === 'success') {
          const res = response.data
          this.ruleForm.name = res.name // 菜单名称
          this.ruleForm.code = res.code // 编码
          this.ruleForm.url = res.url // 链接地址
          this.ruleForm.target = res.target // 菜单类型
          this.ruleForm.flag = res.enable.toString() // 1为启用，0不启用
          this.ruleForm.sort = res.sort // 排列序号
          this.ruleForm.imgUrl = res.imgUrl // icon
          this.ruleForm.remark = res.remark // 备注信息
          this.classificationReminder = res.name //父级分类名称
        }
      })
    },
    // 获取菜单下拉
    getMenuDrop() {
      return new Promise(resolve => {
        getMenuDrop().then(response => {
          if (response.state.toLowerCase() === 'success') {
            this.menuDropDatas = response.data
          }
        })
        resolve()
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>

</style>
