<template>
  <a-modal
    title="编辑"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item v-show="false"><a-input v-decorator="['id']" /></a-form-item>
        <a-form-item
          label="发生时间"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-date-picker show-time style="width: 100%" placeholder="请选择发生时间" v-decorator="['happenTime',{rules: [{ required: true, message: '请选择发生时间！' }]}]" @change="happenTimeOnChange"/>
        </a-form-item>
        <a-form-item
          label="事件类型"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入事件类型" v-decorator="['eventType', {rules: [{required: true, message: '请输入事件类型！'}]}]" />
        </a-form-item>
        <a-form-item
          label="标题"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入标题" v-decorator="['title', {rules: [{required: true, message: '请输入标题！'}]}]" />
        </a-form-item>
        <a-form-item
          label="内容"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-textarea placeholder="请输入内容" v-decorator="['content', {rules: [{required: true, message: '请输入内容！'}]}]" :auto-size="{ minRows: 3, maxRows: 6 }"/>
        </a-form-item>
        <a-form-item
          label="应对处理"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-textarea placeholder="请输入应对处理" v-decorator="['cope', {rules: [{required: true, message: '请输入应对处理！'}]}]" :auto-size="{ minRows: 3, maxRows: 6 }"/>
        </a-form-item>
        <a-form-item
          label="责任主体"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-textarea placeholder="请输入责任主体" v-decorator="['responsible', {rules: [{required: true, message: '请输入责任主体！'}]}]" :auto-size="{ minRows: 3, maxRows: 6 }"/>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import moment from 'moment'
  import { scrSecurityEdit } from '@/api/modular/main/scrsecurity/scrSecurityManage'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        happenTimeDateString: '',
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      moment,
      // 初始化方法
      edit (record) {
        this.visible = true
        setTimeout(() => {
          this.form.setFieldsValue(
            {
              id: record.id,
              eventType: record.eventType,
              title: record.title,
              content: record.content,
              cope: record.cope,
              responsible: record.responsible
            }
          )
        }, 100)
        // 时间单独处理
        if (record.happenTime) {
            this.form.getFieldDecorator('happenTime', { initialValue: moment(record.happenTime, 'YYYY-MM-DD HH:mm:ss') })
            this.happenTimeDateString = moment(record.happenTime).format('YYYY-MM-DD HH:mm:ss')
        }
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object' && values[key] != null) {
                values[key] = JSON.stringify(values[key])
              }
            }
            values.happenTime = this.happenTimeDateString || null
            scrSecurityEdit(values).then((res) => {
              if (res.success) {
                this.$message.success('编辑成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error('编辑失败')//  + res.message
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      happenTimeOnChange(date, dateString) {
        this.happenTimeDateString = dateString
      },
      handleCancel () {
        this.happenTimeDateString = ''
        this.form.getFieldDecorator('happenTime', { initialValue: null })
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
