<template>
  <a-card :bordered='false' v-show='visible' class='card' :title='modalTitle'>
    <a-row :gutter='48' slot='extra'>
      <a-col :md='48' :sm='48'>
              <span class='table-page-search-submitButtons' style='float: right'>
                <a-button :loading='confirmLoading' type='primary' @click='save()'>保存</a-button>
                <a-button style='margin-left: 8px' type='default' @click='handleCancel()'>返回</a-button>
              </span>
      </a-col>
    </a-row>
    <a-form :form='form'>

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

      <row-list :col='2'>
        <row-item>

          <a-form-item
            label='设备ID'
            :labelCol='BaseTool.Constant.labelCol'
            :wrapperCol='BaseTool.Constant.wrapperCol'
          >
            <a-input
              v-decorator="['sbId', {rules: [{required: true, message: '设备ID不能为空'}]}]" />
          </a-form-item>
        </row-item>
        <row-item>

          <a-form-item
            label='更改内容'
            :labelCol='BaseTool.Constant.labelCol'
            :wrapperCol='BaseTool.Constant.wrapperCol'
          >
            <a-input
              v-decorator="['content', {rules: [{required: true, message: '更改内容不能为空'}]}]" />
          </a-form-item>
        </row-item>
        <row-item>

          <a-form-item
            label='原设备位置'
            :labelCol='BaseTool.Constant.labelCol'
            :wrapperCol='BaseTool.Constant.wrapperCol'
          >
            <a-input
              v-decorator="['oldPositionId', {rules: [{required: true, message: '原设备位置不能为空'}]}]" />
          </a-form-item>
        </row-item>
        <row-item>

          <a-form-item
            label='修改后设备位置'
            :labelCol='BaseTool.Constant.labelCol'
            :wrapperCol='BaseTool.Constant.wrapperCol'
          >
            <a-input
              v-decorator="['positionId', {rules: [{required: true, message: '修改后设备位置不能为空'}]}]" />
          </a-form-item>
        </row-item>
        <row-item>

          <a-form-item
            label='位号'
            :labelCol='BaseTool.Constant.labelCol'
            :wrapperCol='BaseTool.Constant.wrapperCol'
          >
            <a-input
              v-decorator="['sbNoId', {rules: [{required: true, message: '位号不能为空'}]}]" />
          </a-form-item>
        </row-item>
        <row-item>

          <a-form-item
            label='原父设备ID'
            :labelCol='BaseTool.Constant.labelCol'
            :wrapperCol='BaseTool.Constant.wrapperCol'
          >
            <a-input
              v-decorator="['oldParentId', {rules: [{required: true, message: '原父设备ID不能为空'}]}]" />
          </a-form-item>
        </row-item>
        <row-item>

          <a-form-item
            label='修改后父设备ID'
            :labelCol='BaseTool.Constant.labelCol'
            :wrapperCol='BaseTool.Constant.wrapperCol'
          >
            <a-input
              v-decorator="['parentId', {rules: [{required: true, message: '修改后父设备ID不能为空'}]}]" />
          </a-form-item>
        </row-item>
        <row-item>

          <a-form-item
            label='开始时间'
            :labelCol='BaseTool.Constant.labelCol'
            :wrapperCol='BaseTool.Constant.wrapperCol'
          >
            <a-date-picker
              showTime
              style='width: 100%'
              :format='BaseTool.Date.PICKER_NORM_DATE_PATTERN'
              v-decorator="['startTime', {rules: [{required: true, message: '开始时间不能为空'}]}]" />
          </a-form-item>
        </row-item>
        <row-item>

          <a-form-item
            label='结束时间'
            :labelCol='BaseTool.Constant.labelCol'
            :wrapperCol='BaseTool.Constant.wrapperCol'
          >
            <a-date-picker
              showTime
              style='width: 100%'
              :format='BaseTool.Date.PICKER_NORM_DATE_PATTERN'
              v-decorator="['endTime', {rules: [{required: true, message: '结束时间不能为空'}]}]" />
          </a-form-item>
        </row-item>
        <row-item>

          <a-form-item
            label='更改类型 1 位置 2 位号 3 父设备'
            :labelCol='BaseTool.Constant.labelCol'
            :wrapperCol='BaseTool.Constant.wrapperCol'
          >
            <a-input-number
              style='width: 100%'
              :min='0'
              :formatter='BaseTool.Amount.formatter'
              :parser='BaseTool.Amount.parser'
              v-decorator="['changeType', {rules: [{required: true, message: '更改类型 1 位置 2 位号 3 父设备不能为空'}]}]" />
          </a-form-item>
        </row-item>
      </row-list>
    </a-form>
  </a-card>
</template>

<script>
import pick from 'lodash.pick'
import { addSbChangeRecord, updateSbChangeRecord } from '@/api/sb/changeRecord'

export default {
  name: 'BaseSbChangeRecord',
  data() {
    return {
      confirmLoading: false,
      modalTitle: null,
      form: this.$form.createForm(this),
      visible: false
      // 下拉框map
    }
  },
  props: {},
  created() {
    // 下拉框map
  },
  methods: {
    base(record) {
      this.visible = true
      // 如果是空标识添加
      if (this.BaseTool.Object.isBlank(record)) {
        this.modalTitle = '添加'
        return
      }
      this.modalTitle = '编辑'
      const { form: { setFieldsValue } } = this
      // 日期处理
      record.startTime = this.BaseTool.Moment(record.startTime, this.BaseTool.Date.PICKER_NORM_DATE_PATTERN)
      record.endTime = this.BaseTool.Moment(record.endTime, this.BaseTool.Date.PICKER_NORM_DATE_PATTERN)
      this.$nextTick(() => {
        setFieldsValue(Object.assign(pick(record, [
          'id',
          'sbId',
          'content',
          'oldPositionId',
          'positionId',
          'sbNoId',
          'oldParentId',
          'parentId',
          'startTime',
          'endTime',
          'changeType'
        ])))
      })
    },
    save () {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
          return
        }
        // 日期处理
        values.startTime = this.BaseTool.Date.formatter(values.startTime, this.BaseTool.Date.PICKER_NORM_DATE_PATTERN)
        values.endTime = this.BaseTool.Date.formatter(values.endTime, this.BaseTool.Date.PICKER_NORM_DATE_PATTERN)
        if (this.BaseTool.String.isBlank(values.id)) {
          addSbChangeRecord(values)
            .then(() => {
              this.handleCancel(values)
            }).catch(() => {
            this.confirmLoading = false
          })
        } else {
          updateSbChangeRecord(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)
      } else {
        this.$emit('ok')
      }
    }
  }
}
</script>
