<template>
  <a-modal
    :title="modalTitle"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @cancel="handleCancel"
  >
    <a-form :form="form">
      <row-list :col="1">
        <row-item>
          <a-form-item
            label="转派对象"
            :labelCol="BaseTool.Constant.labelCol2"
            :wrapperCol="BaseTool.Constant.wrapperCol2"
          >
            <a-select
              v-decorator="['dispatchUserId']"
              placeholder="请选择">
              <a-select-option
                v-for="item in roleUserMap"
                :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.labelCol2"
            :wrapperCol="BaseTool.Constant.wrapperCol2"
          >
            <a-textarea
              :rows="4"
              v-decorator="['repairDispatchRemark', {rules: [{required: true, message: '转派理由不能为空'}]}]"/>
          </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 { transferApplyByDTO } from '@/api/repair/application-form'
import { queryRepairUser } from '@/api/upms/user-dept'

export default {
  name: 'TransferForm',
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      form: this.$form.createForm(this),
      visible: false,
      roleType: null,
      // 下拉框map
      record: {},
      roleUserMap: []
    }
  },
  props: {
  },
  created () {
    // 下拉框map
  },
  methods: {
    base (record, roleType) {
      this.visible = true
      // 如果是空标识添加
      this.modalTitle = '转派申请'
      this.record = record
      this.roleType = roleType
      // 转派工程师集合查询
      const params = { roleType: this.roleType, deptId: record.repairDeptId, queryType: 3 }
      this.getUsers(params)
    },
    save () {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
          return
        }
        values.id = this.record.id
        values.sbId = this.record.sbId
        values.no = this.record.no
        // values.dispatchUserId = this.record.dispatchUserId
        values.sbCph = this.record.sbCph
        values.status = this.record.status
        values.content = this.record.content
        values.category = this.record.category
        values.actualUser = this.record.actualUser
        values.roleType = this.roleType
        transferApplyByDTO(values)
          .then(() => {
            this.handleCancel(values)
          }).catch(() => {
            this.confirmLoading = false
          })
      })
    },
    getUsers (params) {
      queryRepairUser(params).then(res => {
        this.roleUserMap = res.data
      })
    },
    handleCancel (values) {
      this.visible = false
      this.confirmLoading = false
      this.form.resetFields()
      if (this.BaseTool.Object.isNotBlank(values)) {
        this.$emit('ok')
      }
    }
  }
}
</script>
