<template>
  <div>
    <el-dialog
      v-dialogDrag
      :visible.sync="showEditModal"
      :close-on-click-modal="false"
      :title="modalTitle"
      width="700px"
    >
      <div>
        <Form
          id="edit-form"
          ref="formObj"
          :model="formObj"
          :rules="ruleValidate"
          :label-width="80"
          inline
        >
          <Form-item label="上级类别" prop="parentName" class="width-48">
            <el-input
              v-model="formObj.parentName"
              readonly
              placeholder="请选择上级类别"
              @click.native="_selectZtree"
            />
            <input v-model.number="formObj.pid" name="pid" hidden>
          </Form-item>
          <Form-item label="类别名称" prop="name" class="width-48">
            <el-input v-model="formObj.name" name="name" placeholder="请输入类别名称" clearable />
          </Form-item>
          <Form-item label="备注" prop="remark" class="width-48">
            <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>
    <!--选择上级菜单tree-->
    <selectResourceTree ref="treeModal" @on-result-change="_treeData" />
    <!--选择图标-->
    <icons ref="iconsModal" @on-result-change="_iconsData" />
  </div>
</template>
<script>
/**
 * 添加编辑菜单表
 */
import { consumeCategory } from '../../../api'
import icons from '../../../components/base/icons'
import selectResourceTree from './tree/selectConsumeCategoryTree'

export default {
  components: { selectResourceTree, icons },
  data() {
    return {
      id: '',
      modalTitle: '添加',
      formObj: {
        code: '',
        ctime: '',
        icon: 'pt-config',
        name: '',
        remark: '',
        sort: 1,
        type: 0,
        status: 1,
        systemId: '',
        uri: '',
        pid: '0',
        parentName: ''
      },
      ruleValidate: {
        name: [{ required: true, message: '名称不能为空', trigger: 'blur' }]
      },
      showEditModal: false,
      footerList: [
        { id: '', name: '取消', type: '' },
        { id: '', name: '保存', type: 'primary' }
      ],
      typeList: [{ value: 0, name: 'PC 菜单' }, { value: 2, name: 'APP 菜单' }],
      statusList: [{ value: 1, name: '正常' }, { value: 0, name: '禁用' }]
    }
  },
  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('edit-form')
          data.status = this.formObj.status
          data.type = this.formObj.type
          this._save(data)
        } else {
          this.$message.error('表单验证失败')
          this._hideLoading()
        }
      })
    },
    // 添加、编辑保存
    _save: async function(data) {
      // 添加、编辑
      if (this.$string(this.id).isEmpty()) {
        const result = await consumeCategory.add(data)
        this._resultChange(result, '添加成功')
      } else if (
        this.$judgeClassify({ id: this.id, classifyId: this.formObj.pid })
      ) {
        const result = await consumeCategory.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._hideLoading()
        if (this.$string(formObj).isEmpty()) {
          this.id = ''
          this.modalTitle = '添加耗材类别'
          if (JSON.stringify(treeObj) !== '{}') {
            $('input[name=pid]').val(treeObj.id)
            this.formObj.pid = treeObj.id
            this.formObj.parentName = treeObj.name
          } else {
            this.formObj.pid = '0'
            this.formObj.parentName = ''
          }
        } else {
          this.id = formObj.id
          this.formObj = formObj
          this.modalTitle = '编辑位置'
          // this.$refs.systemModal._open(formObj.systemId)
        }
      })
    },
    // 选择系统返回
    _systemResult(data) {
      this.formObj.systemId = data
    },
    // 选择tree
    _selectZtree() {
      this.$refs.treeModal._open()
    },
    _treeData(result) {
      if (result === undefined) {
        // 未选中数据的时候
        this.formObj.pid = '0'
        this.formObj.parentName = ''
      } else {
        // 选中数据以后
        this.formObj.pid = result.id
        this.formObj.parentName = result.name
      }
    },
    // 选择图标
    _selectIcon() {
      this.$refs.iconsModal._open()
    },
    _iconsData(data) {
      this.formObj.icon = data
    }
  }
}
</script>
