<template>
  <a-modal
    :title="modalTitle"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    class="ant-modal2"
    @cancel="handleCancel"
  >
    <a-form :form="form">

      <a-form-item v-show="false" >
        <a-input v-decorator="['id']" type="hidden"/>
      </a-form-item>

      <a-form-item
        label="单位名称"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input
          v-decorator="['unitName', {rules: [{required: true, message: '单位名称不能为空'}]}]" />
      </a-form-item>
      <a-form-item
        label="类型"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input
          v-decorator="['type', {rules: [{required: true, message: '类型不能为空'}]}]" />
      </a-form-item>
      <a-form-item
        label="目标ID"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input
          v-decorator="['targetId', {rules: [{required: true, message: '目标ID不能为空'}]}]" />
      </a-form-item>
      <a-form-item
        label="项目ID"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input
          v-decorator="['projectId', {rules: [{required: true, message: '项目ID不能为空'}]}]" />
      </a-form-item>
      <a-form-item
        label="项目名称"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input
          v-decorator="['projectName', {rules: [{required: true, message: '项目名称不能为空'}]}]" />
      </a-form-item>
      <a-form-item
        label="任务创建时间"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input
          v-decorator="['taskCreateTime', {rules: [{required: true, message: '任务创建时间不能为空'}]}]" />
      </a-form-item>
      <a-form-item
        label="状态"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input
          v-decorator="['status', {rules: [{required: true, message: '状态不能为空'}]}]" />
      </a-form-item>
      <a-form-item
        label="经办人ID"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input
          v-decorator="['assigneeUserId', {rules: [{required: true, message: '经办人ID不能为空'}]}]" />
      </a-form-item>
      <a-form-item
        label="经办人"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input
          v-decorator="['assigneeName', {rules: [{required: true, message: '经办人不能为空'}]}]" />
      </a-form-item>
      <a-form-item
        label="待办日期"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input
          v-decorator="['handleDate', {rules: [{required: true, message: '待办日期不能为空'}]}]" />
      </a-form-item>
    </a-form>
    <template slot="footer">
      <a-button :loading="confirmLoading" type="primary" @click="save()">保存</a-button>
    </template>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { addWorkplaceBacklog, updateWorkplaceBacklog } from '@/api/workplace/backlog'

export default {
  name: 'BaseWorkplaceBacklog',
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      form: this.$form.createForm(this),
      visible: false
    }
  },
  props: {
  },
  methods: {
    base (record) {
      this.visible = true
      // 如果是空标识添加
      if (this.BaseTool.Object.isBlank(record)) {
        this.modalTitle = '添加'
        return
      }
      this.modalTitle = '编辑'
      const { form: { setFieldsValue } } = this
      this.$nextTick(() => {
        setFieldsValue(Object.assign(pick(record, [
          'id',
          'unitName',
          'type',
          'targetId',
          'projectId',
          'projectName',
          'taskCreateTime',
          'status',
          'assigneeUserId',
          'assigneeName',
          'handleDate'
        ])))
      })
    },
    save () {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
          return
        }
        if (this.BaseTool.String.isBlank(values.id)) {
          addWorkplaceBacklog(values)
            .then(() => {
              this.handleCancel(values)
            }).catch(() => {
              this.confirmLoading = false
            })
        } else {
          updateWorkplaceBacklog(values)
            .then(() => {
              this.handleCancel(values)
            }).catch(() => {
              this.confirmLoading = false
            })
        }
      })
    },
    handleCancel (values) {
      this.visible = false
      this.confirmLoading = false
      this.form.resetFields()
      if (this.BaseTool.Object.isNotBlank(values)) {
        this.$emit('ok', values)
      }
    }

  }
}
</script>
