<!-- 合同 -->
<template>
  <modal v-model="modal" width="700px" class-name="vertical-center-modal" :footer-hide="true" :styles="{backgroundColor: '#fff',borderRadius: '6px'}">
    <!--  表单主要内容  -->
    <div class="title" style="text-align: left;" slot="header">
      合同打印
    </div>
    <div class="content" ref="print">
      <div class="row">
        <div class="col">
          <div class="table" style="padding: 10px 0">
            <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>
                </td>
              </tr>
            </table>
          </div>
        </div>
      </div>
    </div>
  </modal>
</template>

<script>
import { GetContractBills } 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) {
        console.log('?????????????')
        console.log(this.data)
        this.getContractBills()
      }
    }
  },
  methods: {
    /* 获取附件 */
    getContractBills () {
      GetContractBills({
        purchaseBillNo: this.data.purchaseBillNo,
        fileType: 3
      }).then(res => {
        if (res.code === 0) {
          this.picList = res.result
        }
      })
    },
    openPic (url) {
      let imgReg = /\.(png|jpg|gif|jpeg|webp)$/
      if (imgReg.test(url)) {
        let flag = false
        let newWindow = window.open()
        let img = new Image()
        img.src = url
        img.onload = function () {
          console.log('???????????load0-----------')
          newWindow.document.write("<div id='test' style='width: 100%;height: 100%;display: flex;justify-content: center;align-items: center;'></div>")
          newWindow.document.getElementById('test').appendChild(img)
          newWindow.document.getElementsByTagName('body')[0].style.margin = '0'
          newWindow.document.getElementsByTagName('img')[0].style.height = '100%'
          newWindow.document.getElementsByTagName('img')[0].style.objectFit = 'cover'
          newWindow.document.close()
          flag = true
        }
        let timer = setInterval(() => {
          if (flag) {
            clearInterval(timer)
            newWindow.print()
          }
        }, 200)
        // newWindow.document.write('<scr' + 'ipt type="text/javascript" >alert("你好，谢谢你的回答!")</scri' + 'pt>')
        // newWindow.document.getElementsByTagName('body')[0].style.margin = '0'

        // let newWindow = window.open()
        // newWindow.document.write("<div style='width: 100%;height: 100%;display: flex;justify-content: center;align-items: center;'><img style='height: 100%;object-fit: cover'" + "' src='" + url + "'/></div>")
        // newWindow.document.getElementsByTagName('body')[0].style.margin = '0'
        // newWindow.document.close()
        // newWindow.print()
      } else {
        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 {
          padding: 10px 0;
          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;
    }
  }
}
/depp/ .ivu-modal-header {
  background-color: #FFFFFF;
}
</style>
