<template>
  <a-modal
    :title="modalTitle"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @cancel="handleCancel"
  >
    <a-form :form="form">

      <row-list :col="2">
        <row-item>
          <a-form-item
            label="设备名称"
            class="marginBottom"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <label>{{ record.sbName }}</label>
          </a-form-item>
        </row-item>
        <!--<row-item>
          <a-form-item
            label="部位名称"
            class="marginBottom"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <label>{{ record.partName }}</label>
          </a-form-item>
        </row-item>-->
        <row-item>
          <a-form-item
            label="报修人"
            class="marginBottom"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <label>{{ record.repairUserName }}</label>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="报修单号"
            class="marginBottom"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <label>{{ record.no }}</label>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            class="marginBottom"
            label="保修时间"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <label>{{ record.applyTime }}</label>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="报修来源"
            class="marginBottom"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <label>{{ BaseTool.Object.getField(sourceMap,record.source) }}</label>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="紧急等级"
            class="marginBottom"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <label>{{ BaseTool.Object.getField(levelMap,record.level) }}</label>
          </a-form-item>
        </row-item>
      </row-list>

      <row-list :col="1">
        <row-item v-if="false">
          <a-form-item
            label="报修状态"
            class="marginBottom"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <label>{{ BaseTool.Object.getField(statusMap,record.status) }}</label>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="问题描述"
            class="marginBottom"
            :labelCol="BaseTool.Constant.labelCol2"
            :wrapperCol="BaseTool.Constant.wrapperCol2"
          >
            <label>{{ record.content }}</label>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="备注"
            class="marginBottom"
            :labelCol="BaseTool.Constant.labelCol2"
            :wrapperCol="BaseTool.Constant.wrapperCol2"
          >
            <label>{{ record.remark }}</label>
          </a-form-item>
        </row-item>
      </row-list>
      <a-divider />
      <row-list :col="2">
        <row-item>
          <a-form-item
            label="维修负责人"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-select v-decorator="['repairPersonId', {rules: [{required: true, message: '维修人负责人不能为空'}]}]" placeholder="请选择">
              <a-select-option
                v-for="item in deptUserList"
                :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-select v-decorator="['checkUserId', {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-list>
      <row-list :col="1">
        <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 { saveFormByApplication } from '@/api/repair/application-form'
import { queryUserDept } from '@/api/upms/user-dept'
import { queryUser } from '@/api/upms/user'

export default {
  name: 'DispatchBaseForm',
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      form: this.$form.createForm(this),
      visible: false,
      // 下拉框map
      sourceMap: {},
      levelMap: {},
      statusMap: {},
      record: {},
      userList: [],
      deptUserList: []
    }
  },
  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.getUsers()
    this.getDeptUsers()
  },
  methods: {
    base (record) {
      this.visible = true
      // 如果是空标识添加
      this.modalTitle = '派工'
      this.record = record
    },
    getDeptUsers () {
      queryUserDept({ deptCode: this.DictCache.VALUE.SYS_DEPT_CODE.REPAIR, userStatus: 1 }).then(res => {
        this.deptUserList = res.data
      })
    },
    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.id = this.record.id
        saveFormByApplication(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>
