<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-input v-decorator="['category']" type="hidden"/>

      </a-form-item>

      <row-list :col="2" v-if="type!==20">
        <row-item>
          <a-form-item
            label="审核"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-select v-model="check" >
              <a-select-option :value="1">
                通过
              </a-select-option>
              <a-select-option :value="0">
                拒绝
              </a-select-option>
            </a-select>
          </a-form-item>
        </row-item>
      </row-list>
      <row-list v-if="(this.type === 17 && this.check === 1) || this.type === 20" :col="2">
        <row-item >
          <a-form-item
            label="生产审核人"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-select
              v-decorator="['produceVerifyUserId', {rules: [{required: true, message: '生产审核人不能为空'}]}]"
              placeholder="请选择">
              <a-select-option
                v-for="item in userMap"
                :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 pick from 'lodash.pick'
import {
  approve,
  returnRepair,
  verifyPassRepair,
  verifyRefusedRepair,
  verifyProduceRefusedRepair,
  verifyProduceRepair,
  applyVerifyRepair,
  getProducerUser
} from '@/api/repair/application-form'

export default {
  name: 'BaseRepairApplicationForm',
  data () {
    return {
      confirmLoading: false,
      modalTitle: '审核',
      form: this.$form.createForm(this),
      visible: false,
      check: 1,
      type: null,
      // 下拉框map
      model: null,
      userMap: []
    }
  },
  components: {
  },
  props: {
  },
  created () {
    // 下拉框map
  },
  methods: {
    base (record, type) {
      this.visible = true
      this.model = record
      this.type = type
      this.getUser()
      const { form: { setFieldsValue } } = this
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign(pick(record, [
          'id',
          'category'
        ])))
      })
    },
    getUser () {
      getProducerUser().then(res => {
        this.userMap = res.data
      })
    },
    save () {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
          return
        }
        switch (true) {
          case this.type === 15 && this.check === 1:
            approve(values).then(res => {
              this.$message.success(res.message)
              this.handleCancel()
            })
            break
          case this.type === 15 && this.check === 0:
            returnRepair(values).then(res => {
              this.$message.success(res.message)
              this.handleCancel()
            })
            break
          case this.type === 17 && this.check === 1:
            verifyPassRepair(values).then(res => {
              this.$message.success(res.message)
              this.handleCancel()
            })
            break
          case this.type === 17 && this.check === 0:
            verifyRefusedRepair(values).then(res => {
              this.$message.success(res.message)
              this.handleCancel()
            })
            break
          case this.type === 19 && this.check === 1:
            verifyProduceRepair(values).then(res => {
              this.$message.success(res.message)
              this.handleCancel()
            })
            break
          case this.type === 19 && this.check === 0:
            verifyProduceRefusedRepair(values).then(res => {
              this.$message.success(res.message)
              this.handleCancel()
            })
            break
          case this.type === 20:
            applyVerifyRepair(values).then(res => {
              this.$message.success(res.message)
              this.handleCancel()
            })
            break
        }
      })
    },
    handleCancel () {
      this.visible = false
      this.check = 1
      this.type = null
      this.confirmLoading = false
      this.form.resetFields()
      this.$emit('ok')
    }
  }
}
</script>
