<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.applicationApplyTime }}</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.applicationSource) }}</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.applicationLevel) }}</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.descripition }}</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-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="['dispatchRemark']"/>
          </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 { dispatchModelByDTO } from '@/api/repair/form'
import { queryUserDept } from '@/api/upms/user-dept'
import { usersByUserParentDeptNatureAll, 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.getDeptUsers()
  },
  methods: {
    base (record) {
      this.visible = true
      // 如果是空标识添加
      this.modalTitle = '派工'
      this.record = record
    },
    getDeptUsers () {
      usersByUserParentDeptNatureAll({nature:this.DictCache.VALUE.SYS_DEPT_NATURE.XIANG_MU_BU,code:this.DictCache.VALUE.SYS_DEPT_CODE.XIAN_CHANG_WEI_XIU_ZU }).then(res => {
        console.log(11111, res.data)
        this.deptUserList = res.data
      })
    },
    save () {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
          return
        }
        values.id = this.record.id
        dispatchModelByDTO(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>
