<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="deptName">
        <a-input v-model="model.deptName" />
      </a-form-model-item>
      <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="label"
          :replace-fields="{children:'children', title:'label', key:'id', value: 'id' }"
        />
      </a-form-model-item>
      <!--      <a-form-model-item label="主管人员" prop="leader">-->
      <!--        <sys-list-select-tag-->
      <!--          v-model="model.userId"-->
      <!--          dict-code="user"-->
      <!--        />-->
      <!--      </a-form-model-item>-->
      <a-form-model-item label="部门描述">
        <a-textarea v-model="model.remark" placeholder="请输入" :rows="4" />
      </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: {
        name: [{ required: true, message: '请输入' }],
        type: [{ required: true, message: '请选择' }]
      },
      url: {
        add: '/system/dept',
        edit: '/system/dept',
        tree: '/system/dept/treeSelect'
      }
    }
  },
  created() {
  },
  methods: {
    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()
    },
    loadTree() {
      this.treeData = []
      getAction(this.url.tree).then((res) => {
        if (res.code === 200) {
          this.treeData = res.data
        }
      })
    },
    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.id) {
        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>
