<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item label="需求名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'requirementName', validatorRules.requirementName]" placeholder="请输入需求名称"></a-input>
        </a-form-item>
        <a-form-item label="需求描述" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea v-decorator="[ 'requirementDescription', validatorRules.requirementDescription]"
                      rows="4" placeholder="请输入需求描述"></a-textarea>
        </a-form-item>
        <a-form-item label="需求时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date v-decorator="['requirmentTime', validatorRules.requirmentTime]" dateFormat="YYYY-MM-DD"
                  placeholder="请选择日期" style="width: 100%"/>
        </a-form-item>


      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

  import { httpAction, putAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import JDate from '@/components/jeecg/JDate'

  export default {
    name: 'HlgPaymentRelatedRequirementsModal',
    components: {
      JDate
    },
    data() {
      return {
        form: this.$form.createForm(this),
        title: '操作',
        width: 800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        confirmLoading: false,
        validatorRules: {
          requirementName: { rules: [{ required: true, message: '请输入需求名称!' }] },
          requirementDescription: { rules: [{ required: true, message: '请输入需求描述!' }] },
          requirmentTime: {
            rules: [{ required: true, message: '请选择需求日期!' }, {
              validator: this.checkRequirmentTime
            }]
          }
        },
        url: {
          edit: '/hlgpayment/hlgPayment/relateRequirement'
        }

      }
    },
    created() {
    },
    methods: {
      add() {
        this.edit({})
      },
      edit(record) {
        this.form.resetFields()
        this.model = Object.assign({}, record)
        this.paymentTime = record.paymentTime === null ? new Date() : record.paymentTime.replace(/-/g, '/')
        this.visible = true
        this.$nextTick(() => {
          this.form.setFieldsValue({
            requirementName: this.model.name,
            requirementDescription: this.model.description
          })
        })
      },
      close() {
        this.visible = false
      },
      handleOk() {
        const that = this
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true
            let formData = Object.assign({}, values)
            formData.requirementId = this.model.id
            formData.id = this.model.paymentId;
            console.log('表单提交数据', formData)
            putAction(this.url.edit, formData).then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            }).finally(() => {
              that.confirmLoading = false
              that.close()
            })
          }
        })
      },
      checkRequirmentTime(rule, value, callback) {
        if (new Date(value) > new Date(this.paymentTime)) {
          callback('需求时间必须比付款时间早！')
        }
        callback()
      },
      handleCancel() {
        this.$emit('close')
        this.close()
      }
    }
  }
</script>