<template>
  <p-spin :spinning='spinning'>
    <p-card>
      <div slot='title' style='display: inline-block; cursor: pointer' @click='back'>
        <a-icon class='mr-10' style='color: #1990ff' type='arrow-left'/>
        {{ typeTitle }}
      </div>
      <div v-if="type !== 'view'" slot='extra'>
        <p-button type='primary' @click='onSubmit'>保存</p-button>
      </div>
      <p-form-model ref='formTarget' :label-col='{ span: 8 }' :model='form' :wrapper-col='{ span: 16 }'
                    class='plg-grid-from'>
        <a-row>
          <a-col :span='8' :xxl='6'>
            <p-form-model-item label='仓库名称'>
              <p-input v-model='form.warehouseName' disabled/>
            </p-form-model-item>
          </a-col>
          <a-col :span='8' :xxl='6'>
            <p-form-model-item label='货主名称'>
              <p-input v-model='form.cargoOwnerName' disabled/>
            </p-form-model-item>
          </a-col>
          <a-col :span='8' :xxl='6' v-if="businessType === '1'">
            <p-form-model-item label='货位'>
              <p-input v-model='form.locatorCode' disabled/>
            </p-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span='8' :xxl='6'>
            <p-form-model-item label='商品编码'>
              <p-input v-model='form.itemCode' disabled/>
            </p-form-model-item>
          </a-col>
          <a-col :span='8' :xxl='6'>
            <p-form-model-item label='商品名称'>
              <p-input v-model='form.itemName' disabled/>
            </p-form-model-item>
          </a-col>
          <a-col :span='8' :xxl='6'>
            <p-form-model-item label='商品规格'>
              <p-input v-model='form.specification' disabled/>
            </p-form-model-item>
          </a-col>
          <a-col :span='8' :xxl='6'>
            <p-form-model-item label='商品大类'>
              <p-input v-model='form.itemCategoryName' disabled/>
            </p-form-model-item>
          </a-col>
          <a-col :span='8' :xxl='6'>
            <p-form-model-item label='批号'>
              <p-input v-model='form.manufacturerLotNum' disabled/>
            </p-form-model-item>
          </a-col>
          <a-col :span='8' :xxl='6'>
            <p-form-model-item label='数量' v-if="businessType === '1'">
              <p-input v-model='form.pieceQty' disabled/>
            </p-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span='8' :xxl='6'>
            <p-form-model-item label='库存状态'>
              <p-input v-model='form.stockStatus' disabled/>
            </p-form-model-item>
          </a-col>
        </a-row>
      </p-form-model>
      <fileUpload ref="passUpload" title="放行单" :type="type!=='add'?'view':'add'" :editData="editData" bizType="06"
                  directory="keb_fxd" @delChange="delChange($event,'isUploadPass')"/>
      <fileUpload ref="qcUpload" title="质检报告单" :type="type!=='add'?'view':'add'" :editData="editData" bizType="07"
                  directory="keb_zjbgd" @delChange="delChange($event,'isUploadQc')"/>
    </p-card>
  </p-spin>
</template>

<script>
import {audit, add} from './api'
import fileUpload from './modules/fileUpload'

export default {
  components: {fileUpload},
  props: {
    type: '',
    editData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      spinning: false,
      form: {}
    }
  },
  mounted() {
    if (this.editData.rqcPassCode) {
      console.log(this.editData)
      this.form = {...this.editData}
    }
  },
  computed: {
    typeTitle() {
      const dictionary = {
        add: '图片上传',
        audit: '审核质检记录',
        view: '审核质检记录'
      }
      return dictionary[this.type] || this.type
    },
    businessType() {
      return this.editData.businessType
    }
  },
  methods: {
    back() {
      this.$emit('back')
    },
    // 保存提交
    onSubmit() {
      this.$refs.formTarget.validate(flag => {
        if (!flag) return
        if (this.type === 'add') {
          const isUploadPass = this.$refs.passUpload.getList().length > 0 ? '1' : '0'
          const isUploadQc = this.$refs.qcUpload.getList().length > 0 ? '1' : '0'
          this.onChangeCount(isUploadPass, isUploadQc)
        }
        if (this.type === 'audit') this.onAudit()
      })
    },
    // 更改图片上传状态
    onChangeCount(isUploadPass = '0', isUploadQc = '0') {
      this.spinning = true
      add({...this.form, isUploadPass, isUploadQc}).then(res => {
        if (res.success) {
          this.$message.success(res.message)
          this.back()
        } else {
          this.$message.warn(res.message)
        }
      }).finally(() => {
        this.spinning = false
      })
    },
    // 审核
    onAudit() {
      const isUploadPass = this.$refs.passUpload.getList().length > 0 ? '1' : '0'
      const isUploadQc = this.$refs.qcUpload.getList().length > 0 ? '1' : '0'
      if (isUploadPass === '0' && isUploadQc === '0') {
        this.$message.warn('没有上传图片,无法进行判定')
        return
      }
      this.spinning = true
      audit({...this.form}).then(res => {
        if (res.success) {
          this.$message.success('操作成功')
          this.back()
        } else {
          this.$message.warn(res.message)
        }
      }).finally(() => {
        this.spinning = false
      })
    },
    delChange(len, type) {
      if (type === 'isUploadPass') {
        const isUploadPass = len > 0 ? '1' : '0'
        const isUploadQc = this.$refs.qcUpload.getList().length > 0 ? '1' : '0'
        this.onChangeCount(isUploadPass, isUploadQc)
      }
      if (type === 'isUploadQc') {
        const isUploadQc = len > 0 ? '1' : '0'
        const isUploadPass = this.$refs.passUpload.getList().length > 0 ? '1' : '0'
        this.onChangeCount(isUploadPass, isUploadQc)
      }
    }
  }
}
</script>

<style lang='less' scoped>
::v-deep .ant-select, ::v-deep .ant-input-number {
  width: 100%;
}
</style>