<template>
  <a-modal
    :title="modalTitle"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    class="ant-modal2"
    @cancel="handleCancel(false)"
  >
    <a-form :form="form">

      <a-form-item v-show="false" >
        <a-input v-decorator="['repairId']" type="hidden"/>
      </a-form-item>

      <row-list :col="2">
        <row-item>
          <a-form-item
            label="委外单号"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              v-decorator="['entrustNo']"
              placeholder="留空自动生成"/>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="委外类别"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-select v-decorator="['outType']" placeholder="请选择">
              <a-select-option
                v-for="(label,value) in outTypeMap"
                :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 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="['reason', {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 { queryUserDept } from '@/api/upms/user-dept'
import { createRepairEntrust } from '@/api/repair/entrust'

export default {
  name: 'BaseOutForm',
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      form: this.$form.createForm(this),
      visible: false,
      outTypeMap: {},
      userInfo: this.$store.getters.userInfo,
      deptUserList: []
    }
  },
  components: {
  },
  props: {
  },
  created () {
    this.outTypeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.REPAIR_OUT_TYPE)
    this.getDeptUsers()
  },
  methods: {
    base (record, mainForm = {}) {
      this.visible = true
      const { form: { setFieldsValue } } = this
      this.modalTitle = '委外'
      if (mainForm != null && mainForm.id != null) {
        this.$nextTick(() => {
          setFieldsValue({
            repairId: mainForm.id
          })
        })
      }
    },
    getDeptUsers () {
      queryUserDept({ deptCode: this.DictCache.VALUE.SYS_DEPT_CODE.CHANG_NEI_WEI_XIU_ZU, userStatus: 1 }).then(res2 => {
        this.deptUserList = res2.data
      })
    },
    save () {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
          return
        }
        createRepairEntrust(values)
          .then(() => {
            this.handleCancel(true, values)
          }).catch(() => {
            this.confirmLoading = false
          })
      })
    },
    handleCancel (isSave, values) {
      this.visible = false
      this.confirmLoading = false
      this.form.resetFields()
      if (isSave) {
        this.$emit('ok', values)
      }
    }

  }
}
</script>
