<template>
  <j-modal
    :title="title"
    :width="700"
    :visible="visible"
    :mask-closable="false"
    cancel-text="关闭"
    @close="close"
  >
    <a-form-model ref="form" :label-col="labelCol" :wrapper-col="wrapperCol" :rules="validatorRules" :model="model">
      <a-form-model-item label="上级菜单" prop="parentId">
          <a-tree-select
            v-model="model.parentId"
            :show-search="true"
            allow-clear
            tree-default-expand-all
            style="width: 100%"
            :tree-data="treeData"
            tree-node-filter-prop="menuName"
            :replace-fields="{children:'children', title:'menuName', key:'menuId', value: 'menuId' }"
          />
      </a-form-model-item>
      <a-form-model-item label="菜单名称" prop="menuName">
        <a-input v-model="model.menuName" />
      </a-form-model-item>
      <a-form-model-item label="类型" prop="menuType">
        <j-dict-select-tag
          v-model="model.menuType"
          style="width: 100%"
          placeholder="菜单类型"
          dict-code="menu_type"
        />
      </a-form-model-item>
      <a-form-model-item label="权限标识" prop="perms">
        <a-input v-model="model.perms" style="width: 100%" />
      </a-form-model-item>
      <a-form-model-item label="排序">
        <a-input-number v-model="model.orderNum" style="width: 100%" :min="0" :max="100000" />
      </a-form-model-item>
    </a-form-model>
  </j-modal>
</template>
<script>
import { getAction, httpAction } from '@/api/request'
import JModal from '@/components/JModal'

export default {
  name: 'CheckAndEditModel',
  components: {
    JModal
  },
  data() {
    return {
      treeData: [],
      labelCol: { span: 4 },
      wrapperCol: { span: 19 },
      dataSource: [],
      title: '',
      visible: false,
      isCheck: false,
      model: {},
      validatorRules: {
        menuName: [{ required: true, message: '请输入' }],
        menuType: [{ required: true, message: '请输入' }],
        perms: [{ required: true, message: '请输入' }],
        orderNum: [{ required: true, message: '请输入' }]
      },
      url: {
        add: '/system/menu',
        edit: '/system/menu',
        tree: '/system/menu/tree'
      }
    }
  },
  created() {
  },
  methods: {
    loadTree() {
      this.treeData = []
      getAction(this.url.tree).then((res) => {
        if (res.code === 200) {
          this.treeData = res.data
        }
      })
    },
    add() {
      this.model = {}
      this.visible = true
      this.loadTree()
    },
    edit(record) {
      this.model = Object.assign({}, record)
      if (this.model.parentId === 0) {
        this.model.parentId = null
      }
      this.visible = true
      this.loadTree()
    },
    close(isSubmit) {
      if (isSubmit) {
        this.checkData()
      } else {
        this.visible = false
      }
    },
    checkData() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.saveData()
        } else {
          return false
        }
      })
    },
    saveData() {
      let url, type
      if (!this.model.menuId) {
        url = this.url.add
        type = 'post'
      } else {
        url = this.url.edit
        type = 'put'
      }
      httpAction(url, this.model, type).then((res) => {
        if (res.code === 200) {
          this.$message.success(res.msg)
          this.$emit('ok')
          this.visible = false
        } else {
          console.log(res)
        }
      })
    }
  }
}
</script>
