<template>
  <a-modal
    centered
    class="uploadVoucher-modal"
    :footer="null"
    :maskClosable="false"
    title="上传支付凭证"
    v-model="isShow"
    @cancel="isShow=false"
    :width="420">
    <!-- 上传支付凭证 -->
    <div class="uploadVoucher-con">
      <a-form-model
        ref="ruleForm"
        :label-col="formItemLayout.labelCol"
        :wrapper-col="formItemLayout.wrapperCol"
      >
        <a-form-model-item label="附件">
          <Upload
            id="uploadVoucher-attachList"
            ref="importUpload"
            :maxNums="10"
            :fileSize="10"
            uploadType="attach"
            :action="attachAction"
            upText="添加文件"
            fileType="application/pdf"
            @change="changeImport"></Upload>
          <div style="color:#999999;">（只支持PDF格式，最多10个附件）</div>
        </a-form-model-item>
      </a-form-model>
      <!-- 按钮 -->
      <div class="btn-con">
        <a-button
          id="uploadVoucher-cancel"
          size="large"
          class="button-cancel"
          @click="isShow=false"
          style="margin-right: 15px;">取消</a-button>
        <a-button
          type="primary"
          id="uploadVoucher-nextStep"
          size="large"
          class="button-primary"
          @click="handleSaveVoucher">保存</a-button>
      </div>
    </div>
  </a-modal>
</template>

<script>
// 组件
import { Upload } from '@/components'
// 接口
import { saveAttachment } from '@/api/purchase'
export default {
  name: 'UploadVoucherModal',
  components: { Upload },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    itemSn: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      // 表单布局设置
      formItemLayout: {
        labelCol: { span: 4 },
        wrapperCol: { span: 20 }
      },
      attachList: null, // 上传附件列表
      attachAction: process.env.VUE_APP_API_BASE_URL + '/uploadGetFileInfo'// 上传附件地址
    }
  },
  methods: {
    // 上传凭证
    handleSaveVoucher () {
      saveAttachment({
        sparePartsSn: this.itemSn,
        attachmentList: this.attachList
      }).then(res => {
        if (res.status == 200) {
          this.$message.success('上传成功')
          this.$emit('ok')
          this.isShow = false
        }
      })
    },
    // 上传文件  change
    changeImport (file) {
      if (file) {
        this.attachList = JSON.parse(file)
        this.attachList.map(item => {
          item.fileType = item.extName
        })
      }
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.$refs.importUpload.setFileList('')
      }
    }
  }
}
</script>

<style lang="less" scoped>
  .uploadVoucher-modal{
    .uploadVoucher-con{
     .btn-con{
       margin: 30px 0 0;
       text-align: center;
       .button-cancel{
         font-size: 12px;
       }
     }

    }
  }
</style>
