<template>
  <div>
    <el-dialog
      v-dialogDrag
      :visible.sync="showEditModal"
      :close-on-click-modal="false"
      width="700px"
      :title="modalTitle"
    >
      <div>
        <Form
          :id="formId"
          ref="formObj"
          :model="formObj"
          :rules="ruleValidate"
          :label-width="80"
        >
          <Form-item label="所属菜单" prop="parentName">
            <el-input
              v-model="formObj.parentName"
              readonly
              placeholder="请选择所属菜单"
              @click.native="_selectZtree"
            />
          </Form-item>
          <Form-item label="按钮名称" prop="name">
            <el-input v-model="formObj.name" name="name" placeholder="请输入按钮名称" clearable />
          </Form-item>
          <Form-item label="编码" prop="code">
            <el-tooltip content="编码不能重复" placement="right-start">
              <el-input v-model="formObj.code" name="code" placeholder="请输入编码，如：sys-city-add" style="width:100%" clearable />
            </el-tooltip>
          </Form-item>
          <Form-item label="类型" prop="uri">
            <el-radio-group v-model="formObj.uri">
              <el-radio v-for="item in judgeList" :key="item.value" :label="item.value">
                {{ item.name }}
              </el-radio>
            </el-radio-group>
          </Form-item>
          <Form-item label="状态" prop="status">
            <el-radio-group v-model="formObj.status">
              <el-radio v-for="item in statusList" :key="item.value" :label="item.value">
                {{ item.name }}
              </el-radio>
            </el-radio-group>
          </Form-item>
          <Form-item label="排序" prop="sort">
            <el-input-number
              v-model.number="formObj.sort"
              name="sort"
              placeholder="请输入排序"
              :min="1"
              style="width: 100%"
              size="small"
              controls-position="right"
              step-strictly
            />
          </Form-item>
          <Form-item label="备注" prop="remark">
            <el-input v-model="formObj.remark" name="remark" placeholder="请输入备注" />
          </Form-item>
        </Form>
      </div>
      <div slot="footer">
        <modal-footer ref="footerModal" :footer="footerList" @on-result-change="_footerResult" />
      </div>
    </el-dialog>
    <!--选择上级菜单btn-tree-->
    <selectBtnResourceTree ref="treeModal" @on-result-change="_treeData" />
  </div>
</template>
<script>
/**
 * 添加编辑按钮菜单表
 */
import { sysResource } from '../../../api'
import selectBtnResourceTree from '../resource/tree/selectBtnResourceTree'

export default {
  components: { selectBtnResourceTree },
  data() {
    return {
      id: '',
      modalTitle: '添加',
      formObj: {
        name: '',
        code: '',
        status: 1,
        sort: 1,
        remark: '',
        parentName: '',
        pid: '',
        uri: '/'
      },
      ruleValidate: {
        name: [
          { required: true, message: '按钮名称不能为空', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '权限编码不能为空', trigger: 'blur' }
        ],
        parentName: [
          { required: true, message: '所属菜单不能为空', trigger: 'blur' }
        ]
      },
      showEditModal: false,
      footerList: [
        { id: '', name: '取消', type: '' },
        { id: '', name: '保存', type: 'primary' }
      ],
      statusList: [{ value: 1, name: '正常' }, { value: 0, name: '禁用' }],
      judgeList: [
        { name: '普通', value: '/' },
        { name: '报表', value: 'reportBtn' },
        { name: '表单', value: 'formBtn' }
      ],
      menuId: '',
      formId: ''
    }
  },
  methods: {
    // modal
    _footerResult(name) {
      switch (name) {
        case '取消':
          this._cancel()
          break
        case '保存':
          this._ok()
          break
      }
    },
    _hideLoading() {
      this.$refs.footerModal._hideLoading()
    },
    _resultChange(result, msg) {
      if (result) {
        this.showEditModal = false
        this.$message.success(msg)
        this.$emit('on-result-change')
      }
      this._hideLoading()
    },
    _ok() {
      this.$refs.formObj.validate(valid => {
        if (valid) {
          const data = this.$serialize(this.formId)
          data.status = this.formObj.status
          data.uri = this.formObj.uri
          data.form = this.formObj.form
          data.pid = this.formObj.pid
          this._save(data)
        } else {
          this.$message.error('表单验证失败')
          this._hideLoading()
        }
      })
    },
    // 添加、编辑保存
    _save: async function(data) {
      // 添加、编辑
      if (this.$string(this.id).isEmpty()) {
        const result = await sysResource.btnAdd({
          menuId: this.menuId,
          obj: data
        })
        this._resultChange(result, '添加成功')
      } else if (
        this.$judgeClassify({ id: this.id, classifyId: this.formObj.pid })
      ) {
        const result = await sysResource.edit({
          id: this.formObj.id,
          obj: data
        })
        this._resultChange(result, '编辑成功')
      } else {
        this.$message.warning('上级菜单与菜单名不能重复，请重新选择~')
        this._hideLoading()
      }
    },
    _cancel() {
      this.showEditModal = false
    },
    _open(treeObj, formObj) {
      this.showEditModal = true
      this.$nextTick(() => {
        this.$refs.formObj.resetFields()
        this.formId = 'btn-edit' + this.$randomCode()
        this._hideLoading()
        if (this.$string(formObj).isEmpty()) {
          this.modalTitle = '添加按钮'
          this.id = ''
          this.formObj.name = ''
          this.formObj.remark = ''
          this.formObj.code = ''
          if (JSON.stringify(treeObj) !== '{}') {
            this.menuId = treeObj.id
            this.formObj.pid = treeObj.id
            this.formObj.parentName = treeObj.name
          } else {
            this.menuId = ''
            this.formObj.pid = '0'
            this.formObj.parentName = ''
          }
        } else {
          this.modalTitle = '编辑按钮'
          this.id = formObj.id
          this.formObj = formObj
          this.formObj.status = formObj.status
        }
      })
    },
    // 复制
    _openCopy(treeObj, formObj) {
      this.showEditModal = true
      this.$nextTick(() => {
        this.$refs.formObj.resetFields()
        this.formId = 'btn-edit' + this.$randomCode()
        this._hideLoading()
        this.modalTitle = '复制按钮'
        this.id = ''
        this.menuId = formObj.pid
        this.formObj = formObj
        this.formObj.status = formObj.status
      })
    },
    // 选择tree
    _selectZtree() {
      this.$refs.treeModal._open()
    },
    _treeData(result) {
      this.menuId = result.id
      this.formObj.pid = result.id
      this.formObj.parentName = result.name
    }
  }
}
</script>
