<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>

      <row-list :col="2">
        <row-item>
          <a-form-item
            label="设备id"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
            v-show="false"
          >
            <a-input
              v-decorator="['sbId', {rules: [{required: true, message: '设备id不能为空'}]}]" />
          </a-form-item>
          <a-form-item
            label="设备名称"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              disabled
              v-decorator="['sbName', {rules: [{required: true, message: '设备名称不能为空'}]}]" />
           </a-form-item>
        </row-item>
        <!--<row-item>
          <a-form-item
            label="部件id"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
            v-show="false"
          >
            <a-input
              v-decorator="['partId', {rules: [{required: false, message: '设备id不能为空'}]}]" />
          </a-form-item>
          <a-form-item
            label="部件名称"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              disabled
              style="width: 70%"
              v-decorator="['partName', {rules: [{required: false, message: '部件名称不能为空'}]}]" />
            <a-button :disabled="!BaseTool.Object.isNotBlank(this.form.getFieldValue('sbId'))" style="width: 30%" type="primary" @click="handlePartSelect">选择</a-button>
          </a-form-item>
        </row-item>-->
        <row-item>
          <a-form-item
            label="是否停机"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-select v-decorator="['needStop', {initialValue: DictCache.VALUE.YES_NO.NO,rules: [{required: true, message: '是否停机不能为空'}]}]" placeholder="请选择">
              <a-select-option
                v-for="(label,value) in needStopMap"
                :key="value"
                :label="label"
                :value="parseInt(value)">{{ label }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </row-item>
        <!--<row-item>
          <a-form-item
            label="报修人"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-select v-decorator="['repairUserId', {rules: [{required: true, message: '报修人不能为空'}]}]" placeholder="请选择">
              <a-select-option
                v-for="item in userList"
                :key="item.userId"
                :label="item.realName"
                :value="item.userId">{{ item.realName }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </row-item>-->
        <row-item>
          <a-form-item
            label="报修单号"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              disabled
              v-decorator="['no', {rules: [{required: false, message: '报修单号不能为空'}]}]" />
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="报修来源"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-select v-decorator="['source', {initialValue: DictCache.VALUE.REPAIR_APPLICATION_FORM_SOURCE.CHECK,rules: [{required: true, message: '报修来源不能为空'}]}]" placeholder="请选择">
              <a-select-option
                v-for="(label,value) in sourceMap"
                :key="value"
                :label="label"
                :value="parseInt(value)">{{ label }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="紧急等级"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-select v-decorator="['level', {initialValue: 1, rules: [{required: true, message: '紧急等级不能为空'}]}]" placeholder="请选择">
              <a-select-option
                v-for="(label,value) in levelMap"
                :key="value"
                :label="label"
                :value="parseInt(value)">{{ label }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </row-item>
      </row-list>

      <row-list :col="1">
        <row-item v-if="false">
          <a-form-item
            label="报修状态"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-select disabled v-decorator="['status', {initialValue: DictCache.VALUE.REPAIR_APPLICATION_FORM_STATUS.NOT_ALLOCATED,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>
        </row-item>
        <row-item>
          <a-form-item
            label="问题描述"
            :labelCol="BaseTool.Constant.labelCol2"
            :wrapperCol="BaseTool.Constant.wrapperCol2"
          >
            <a-textarea
              :rows="4"
              v-decorator="['content', {rules: [{required: true, message: '问题描述不能为空'}]}]"/>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="备注"
            :labelCol="BaseTool.Constant.labelCol2"
            :wrapperCol="BaseTool.Constant.wrapperCol2"
          >
            <a-textarea
              :rows="4"
              v-decorator="['remark']"/>
          </a-form-item>
        </row-item>
      </row-list>
    </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 { addRepairApplicationForm, updateRepairApplicationForm } from '@/api/repair/application-form'
import { queryUser } from '@/api/upms/user'

export default {
  name: 'ImpBaseRepairApplicationForm',
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      form: this.$form.createForm(this),
      visible: false,
      // 下拉框map
      sourceMap: {},
      levelMap: {},
      needStopMap: {},
      statusMap: {},
      userList: []
    }
  },
  components: {
  },
  props: {
  },
  created () {
    // 下拉框map
    this.sourceMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.REPAIR_APPLICATION_FORM_SOURCE)
    this.levelMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.REPAIR_APPLICATION_FORM_LEVEL)
    this.statusMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.REPAIR_APPLICATION_FORM_STATUS)
    this.needStopMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.YES_NO)
    this.getUsers()
  },
  methods: {
    base (record) {
      this.visible = true

      // 如果是空标识添加
      if (this.BaseTool.Object.isBlank(record.id)) {
        this.modalTitle = '添加'
        if(!this.BaseTool.Object.isBlank(record.sbId)){
          console.log("111record.sbId: " + record.sbId)
          console.log("111record.sbName: " + record.sbName)
          const { form: { setFieldsValue } } = this
          this.$nextTick(() => {
            setFieldsValue(Object.assign(pick(record, [
              'sbId',
              'sbName'
            ])))
          })
        }
        return
      }
      this.modalTitle = '编辑'
      const { form: { setFieldsValue } } = this
      // 日期处理
      record.applyTime = this.BaseTool.Moment(record.applyTime, this.BaseTool.Date.PICKER_NORM_DATETIME_PATTERN)
      this.$nextTick(() => {
        setFieldsValue(Object.assign(pick(record, [
          'id',
          'sbId',
          'partId',
          'repairUserId',
          'no',
          'source',
          'level',
          'content',
          'status',
          'sbName',
          'partName',
          'remark'
        ])))
      })
    },
    getUsers () {
      queryUser({ status: 1 }).then(res => {
        this.userList = res.data
      })
    },
    save () {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
          return
        }
        // 日期处理
        values.applyTime = this.BaseTool.Date.formatter(values.applyTime, this.BaseTool.Date.PICKER_NORM_DATETIME_PATTERN)
        if (this.BaseTool.String.isBlank(values.id)) {
          addRepairApplicationForm(values)
            .then(() => {
              this.handleCancel(values)
            }).catch(() => {
              this.confirmLoading = false
            })
        } else {
          updateRepairApplicationForm(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>
