<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="委外订单号"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              v-decorator="['outNo', {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="['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="['content']"/>
          </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 { queryUserDept } from '@/api/upms/user-dept'
import { addRepairApplicationForm, updateRepairApplicationForm } from '@/api/repair/application-form'
import PartInfoSelectModal from '@/views/part/info/modules/PartInfoSelectModal'
import SbInfoSelectModal from '@/views/sb/info/modules/SbInfoSelectModal'

export default {
  name: 'BaseRepairApplicationForm',
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      form: this.$form.createForm(this),
      visible: false,
      // 下拉框map
      sourceMap: {},
      outTypeMap: {},
      levelMap: {},
      questionMap: {},
      needStopMap: {},
      statusMap: {},
      userInfo: this.$store.getters.userInfo,
      deptUserList: []
    }
  },
  components: {
    SbInfoSelectModal,
    PartInfoSelectModal
  },
  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.questionMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.REPAIR_QUESTION)
    this.needStopMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.YES_NO)
    this.outTypeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.REPAIR_OUT_TYPE)
    this.getDeptUsers()
  },
  methods: {
    base (record, mainForm = {}) {
      this.visible = true
      // 如果是空标识添加
      const { form: { setFieldsValue } } = this
      if (this.BaseTool.Object.isBlank(record)) {
        this.modalTitle = '添加'
        if (mainForm != null && mainForm.id != null) {
          this.$nextTick(() => {
            setFieldsValue({
              id: mainForm.id
            })
          })
        }
        return
      }
      this.modalTitle = '编辑'
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign(pick(record, [
          'id',
          'sbId',
          'partId',
          'type',
          'outType',
          'mainRepairId',
          'repairUserId',
          'outNo',
          'source',
          'level',
          'content',
          'status',
          'sbName',
          'partName',
          'remark'
        ])))
      })
    },
    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
        }
        values.type = 2
        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>
