<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-row class="form-row" :gutter="BaseTool.Constant.row.gutter">
        <a-col :lg="12" :md="24" :sm="24">
          <a-form-item
            label="报修单号"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              disabled
              style="width:70%"
              v-decorator="['applicationNo', {rules: [{required: true, message: '报修单号不能为空'}]}]" />
            <a-button style="width: 30%;" type="primary" @click="handleAppNoSelect">选择</a-button>
          </a-form-item>

        </a-col>
        <a-col :lg="12" :md="24" :sm="24">
          <a-form-item
            label="维修单号"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              disabled
              style="width:70%"
              v-decorator="['repairNo', {rules: [{required: true, message: '维修单号不能为空'}]}]" />
            <a-button style="width: 30%;" type="primary" @click="handleRepairNoSelect">选择</a-button>
          </a-form-item>

        </a-col>
      </a-row>
      <a-row class="form-row" :gutter="BaseTool.Constant.row.gutter">
        <a-col :lg="12" :md="24" :sm="24">
          <a-form-item
            label="报修单id"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
            v-show="false"
          >
            <a-input
              v-decorator="['applicationId', {rules: [{required: true, message: '报修单id不能为空'}]}]" />
          </a-form-item>

        </a-col>
        <a-col :lg="12" :md="24" :sm="24">
          <a-form-item
            label="维修单id"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
            v-show="false"
          >
            <a-input
              v-decorator="['repairId', {rules: [{required: true, message: '维修单id不能为空'}]}]" />
          </a-form-item>

        </a-col>
      </a-row>
      <a-row class="form-row" :gutter="BaseTool.Constant.row.gutter">
        <a-col :lg="12" :md="24" :sm="24">
          <a-form-item
            label="验收开始时间"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-date-picker
              showTime
              style="width: 100%"
              :format="BaseTool.Date.PICKER_NORM_DATETIME_PATTERN"
              v-decorator="['startTime', {rules: [{required: true, message: '验收开始时间不能为空'}]}]" />
          </a-form-item>

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

        </a-col>
      </a-row>
      <a-row class="form-row" :gutter="BaseTool.Constant.row.gutter">
        <a-col :lg="12" :md="24" :sm="24">
          <a-form-item
            label="维修人员"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              v-decorator="['repairUseId', {rules: [{required: true, message: '维修人员id不能为空'}]}]" />
          </a-form-item>

        </a-col>
        <a-col :lg="12" :md="24" :sm="24">
          <a-form-item
            label="验收描述"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              v-decorator="['descripition', {rules: [{required: true, message: '验收描述不能为空'}]}]" />
          </a-form-item>

        </a-col>
      </a-row>
      <a-row class="form-row" :gutter="BaseTool.Constant.row.gutter">
        <a-col :lg="12" :md="24" :sm="24">
          <a-form-item
            label="验收状态"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-select v-decorator="['status', {initialValue: DictCache.VALUE.REPAIR_CHECK_STATUS.NOT_ACCEPTANCED,rules: [{required: true, message: '验收状态不能为空'}]}]" placeholder="请选择">
              <a-select-option
                v-for="(label,value) in statusMap"
                :key="value"
                :label="label"
                :value="parseInt(value)">{{ label }}
              </a-select-option>
            </a-select>
          </a-form-item>

        </a-col>
        <a-col :lg="12" :md="24" :sm="24">
          <a-form-item
            label="备注"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              v-decorator="['remark', {rules: [{required: false, message: '备注不能为空'}]}]" />
          </a-form-item>

        </a-col>
      </a-row>
    </a-form>
    <template slot="footer">
      <a-button :loading="confirmLoading" type="primary" @click="save()">保存</a-button>
    </template>
    <repair-application-form-select-modal ref="rafSelectModal" @selected="handleAppNoSelected"/>
    <repair-form-select-modal ref="repairSelectModal" @selected="handleRepairNoSelected"/>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { addRepairCheck, updateRepairCheck } from '@/api/repair/check'
import RepairApplicationFormSelectModal from '@/views/repair/application-form/modules/RepairApplicationFormSelectModal'
import RepairFormSelectModal from '@/views/repair/form/modules/RepairFormSelectModal'
export default {
  name: 'BaseRepairCheck',
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      form: this.$form.createForm(this),
      visible: false,
      // 下拉框map
      statusMap: {}
    }
  },
  components: {
    RepairApplicationFormSelectModal,
    RepairFormSelectModal
  },
  props: {
  },
  created () {
    // 下拉框map
    this.statusMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.REPAIR_CHECK_STATUS)
  },
  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_DATETIME_PATTERN)
      record.endTime = this.BaseTool.Moment(record.endTime, this.BaseTool.Date.PICKER_NORM_DATETIME_PATTERN)
      this.$nextTick(() => {
        setFieldsValue(Object.assign(pick(record, [
          'id',
          'applicationNo',
          'repairNo',
          'applicationId',
          'repairId',
          'startTime',
          'endTime',
          'repairUseId',
          'descripition',
          'status',
          'remark'
        ])))
      })
    },
    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_DATETIME_PATTERN)
        values.endTime = this.BaseTool.Date.formatter(values.endTime, this.BaseTool.Date.PICKER_NORM_DATETIME_PATTERN)
        if (this.BaseTool.String.isBlank(values.id)) {
          addRepairCheck(values)
            .then(() => {
              this.handleCancel(values)
            }).catch(() => {
              this.confirmLoading = false
            })
        } else {
          updateRepairCheck(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)
      }
    },
    handleAppNoSelect () {
      this.$refs.rafSelectModal.base()
    },
    handleAppNoSelected (record, keys, rows) {
      console.log(keys, rows)
      const [ key ] = keys
      const [ row ] = rows
      const { form: { setFieldsValue } } = this
      this.$nextTick(() => {
        setFieldsValue(Object.assign({
          'applicationId': key,
          'applicationNo': row.no
        }))
      })
    },
    handleRepairNoSelect () {
      this.$refs.repairSelectModal.base()
    },
    handleRepairNoSelected (record, keys, rows) {
      const [ key ] = keys
      const [ row ] = rows
      const { form: { setFieldsValue } } = this
      this.$nextTick(() => {
        setFieldsValue(Object.assign({
          'repairId': key,
          'repairNo': row.no
        }))
      })
    }

  }
}
</script>
