<!-- 合同 -->
<template>
  <modal v-model="modal" width="700px" class-name="vertical-center-modal" :footer-hide="true">
    <!--  表单主要内容  -->
    <div class="title" style="text-align: left" slot="header">
      {{ data.applyBillNo }}
    </div>
    <div class="content" ref="print">

      <div class="row">
        <div class="col" style="align-items: center">
          <div class="value" style="flex: 1">
            <Upload action="" ref="uploadFileFlag" :before-upload="beforeFile">
              <Button type="primary">上传合同</Button>
            </Upload>
          </div>
        </div>
      </div>
      <div class="row">
        <div class="col">
          <div class="table">
            <table style="border-collapse:collapse;border:none;width: 100%;">
              <tr>
                <th style="border:solid #dcdee2 1px;padding: 5px;text-align: center;position: relative;font-size: 12px;">文件名称</th>
                <th style="border:solid #dcdee2 1px;padding: 5px;text-align: center;position: relative;font-size: 12px;">操作</th>
              </tr>
              <tr v-for="(item, index) in picList" :key="index">
                <td style="border:solid #dcdee2 1px;padding: 5px;text-align: center;position: relative;font-size: 12px;">{{ item.fileName }}</td>
                <td style="border:solid #dcdee2 1px;padding: 5px;text-align: center;position: relative;font-size: 12px;">
                  <Button type="primary" size="small" @click="openPic(item.filePath)" style="margin-right: 10px">查看</Button>
                  <Button type="error" size="small" @click="removePic(index)">删除</Button>
                </td>
              </tr>
            </table>
          </div>
        </div>
      </div>
    </div>
  </modal>
</template>

<script>
import { UploadBase64 } from '@/api/upload'
import { GetContractBills, UploadContractBills } from '@/api/applyPurchase'
import { filePreview } from '@/libs/util'

export default {
  name: 'store-in-form',
  data () {
    return {
      modal: false,
      picList: [],
      contractsList: []
    }
  },
  components: {
  },
  props: {
    data: {
      type: Object,
      default () {
        return {}
      }
    },
    value: {
      type: Boolean,
      default: false,
      require: true
    }
  },
  watch: {
    value: {
      handler (n) {
        this.modal = n
      },
      immediate: true
    },
    modal (n) {
      this.$emit('input', n)
      if (n) {
        this.getContractBills()
      }
    }
  },
  methods: {
    /* 上传附件 */
    uploadContractBills () {
      UploadContractBills({
        purchaseBillNo: this.data.purchaseBillNo,
        contractsList: this.contractsList
      }).then(res => {
        if (res.code === 0) {
          this.contractsList = []
          this.getContractBills()
          this.$emit('getlist')
        }
      })
    },
    /* 移出附件 */
    removePic (index) {
      if (this.picList[index].id) {
        UploadContractBills({
          purchaseBillNo: this.data.purchaseBillNo,
          contractsList: [],
          delItems: [ this.picList[index].id ]
        }).then(res => {
          if (res.code === 0) {
            this.getContractBills()
            this.$emit('getlist')
          }
        })
      }
    },
    /* 获取附件 */
    getContractBills () {
      GetContractBills({
        purchaseBillNo: this.data.purchaseBillNo,
        fileType: 3
      }).then(res => {
        if (res.code === 0) {
          this.picList = res.result
        }
      })
    },
    beforeFile (file) {
      const reader = new FileReader()
      reader.readAsDataURL(file)
      reader.onload = (event) => {
        UploadBase64({
          fileBase64: event.srcElement.result,
          fileName: file.name,
          floder: 'order'
        }).then(res => {
          if (res.code === 0) {
            let fileObj = {
              size: file.size,
              fileName: file.name,
              filePath: res.result,
              fileType: 3
            }
            this.contractsList.push(fileObj)
            this.uploadContractBills()
          }
        })
      }
      return false
    },
    openPic (url) {
      filePreview(url)
    },
    cfm () {
      // 未使用打印功能，未改成vue-easy-print
      this.$print(this.$refs['print'])
    },
    cancel () {
      this.modal = false
    }
  }
}
</script>

<style lang="less" scoped>
.content {
  .title {
    text-align: center;
    font-size: 20px;
    font-weight: bold;
  }
  .sub-title {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin: 10px;
    font-size: 14px;
    font-weight: bold;
    &.border {
      border-bottom: 1px solid #000000;
    }
    .go {
      font-size: 10px;
      font-weight: normal;
      color: #0000ff;
    }
  }
  .row {
    padding: 0 20px;
    margin-bottom: 5px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .col {
      flex: 1;
      display: flex;
      align-items: center;
      .table {
        width: 100%;
        table {
          border-collapse:collapse;
          border:none;
          width: 100%;
          tr {
            th {
              background-color: #eeeeee;
            }
            td,th {
              padding: 5px;
              text-align: center;
              border:solid #dcdee2 1px;
              position: relative;
              font-size: 12px;
            }
          }
        }
      }
    }
  }
  .card {
    margin-bottom: 5px;
    background-color: #FFFFFF;
    border: 1px solid rgba(215, 215, 215, 1);
    border-radius: 5px;
    .border {
      border-bottom: 1px solid #5d6c7b;
    }
    .line {
      width: 100%;
      border-bottom: 1px solid #5d6c7b;
    }
  }
}
</style>
