<template>
  <div>
    <el-button
      type="warning"
      plain
      icon="el-icon-close"
      style="margin: 5px 20px 20px 20px;position:sticky;top: 10px;z-index: 200"
      size="small"
      @click="closePrintViewChild">关闭预览
    </el-button>
    <el-button
      v-print="printConfig"
      type="success"
      plain
      icon="el-icon-document-copy"
      style="margin: 5px 20px 20px 20px;position:sticky;top: 10px;z-index: 200"
      size="small">打印
    </el-button>
    <div id="loadingDocuments" class="loading-Documents">
      <el-row style="border-left: 1px solid #000000;border-right: 1px solid #000000;background: #d9d9d9;-webkit-print-color-adjust: exact;">
        <div>
          <div class="title">
            <div class="logo"> <img :src="require('@/assets/img/leftPic.png')" width="220px"></div>
            <div>Product shipment inspection report</div>
            <div>产 品 出 厂 报 告</div>
          </div>
          <div class="text1">
            宁德时代智能（上海）科技有限公司<br>
          </div>
          <div class="text1">Contemporary Amperex Intelligent Technology(Shanghai)Co.,Ltd</div>
        </div>
        <div class="title2">
          <div>
            <div class="text2" style="width: 230px;font-weight: bolder">
              Product Name<br>
              产品名称:
            </div>
            <div class="bdiv" style="width: 500px">{{ fromTextObj.productName }}</div>
            <div class="text2" style="width: 220px;font-weight: bolder">
              Project Status<br>
              项目状态:
            </div>
            <div class="bdiv" style="width: 200px">{{ fromTextObj.productStatus }}</div>
          </div>
          <div>
            <div class="text2" style="width: 230px;font-weight: bolder">
              Part No<br>
              产品编号:
            </div>
            <div class="bdiv" style="width: 500px">{{ fromTextObj.productCode }}</div>
            <div class="text2" style="width: 220px;font-weight: bolder">
              Time<br>
              检查时间:
            </div>
            <div style="width: 200px" class="bdiv">{{ fromTextObj.checkTime }}</div>
          </div>
        </div>
      </el-row>
      <el-row style="overflow: hidden">
        <el-table
          v-loading="loadingDocumentsLoading"
          ref="table"
          :data="loadingDocumentsTableData"
          :header-cell-style="{borderColor: '#000',fontSize: '20px',color: '#000'}"
          :span-method="complexSpanMethod"
          :cell-style="cellStyle"
          class="loading-Documents-table"
          size="mini"
          style="width: 1200px;overflow: hidden;border: 1px solid #000000"
          border>
          <el-table-column align="center" label="序号" type="index" width="65px"/>
          <el-table-column align="center" label="类别" width="144px">
            <template slot-scope="scope">
              {{ scope.row.类别 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="测试方法" min-width="280px">
            <template slot-scope="scope">
              {{ scope.row.测试方法 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="检测标准" width="170px">
            <template slot-scope="scope">
              {{ scope.row.检测要求 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="实测值" width="201px">
            <template slot-scope="scope">
              {{ scope.row.实测值 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="判定" width="183px" style="background-color:#92d050;">
            <template slot-scope="scope">
              {{ scope.row.判定 }}
            </template>
          </el-table-column>
        </el-table>
      </el-row>
      <!--      <el-row class="conclusionResult">-->
      <!--        <div style="width: 207px;border-bottom:1px solid #000;border-right:1px solid #000 ">Conclusion结论:</div>-->
      <!--        <div style="width: 1000px;border-bottom:1px solid #000">-->
      <!--          <div-->
      <!--            v-if="fromTextObj.conclusion === '合格 OK'"-->
      <!--            style="background-color:#92d050;-webkit-print-color-adjust: exact;">-->
      <!--            {{ fromTextObj.conclusion }}-->
      <!--          </div>-->
      <!--          <div v-else style="background-color:#ed2929;-webkit-print-color-adjust: exact;">{{ fromTextObj.conclusion }}</div>-->
      <!--        </div>-->
      <!--      </el-row>-->
      <el-row class="buttonBox">
        <div>OQA/检查人员 :{{ fromTextObj.checkPerson }}</div>
        <div>Checked by 审核 :{{ fromTextObj.reviewPerson }}</div>
        <div>质量盖章 :</div>
      </el-row>
    </div>
  </div>
</template>
<script>
import axios from 'axios'
import print from 'vue-print-nb'

export default {
  name: 'LoadingDocuments',
  components: { print },
  directives: {
    print
  },
  props: {
    fromTextObj: {
      type: Object,
      default: function() {
        return {
          productName: '',
          productStatus: '',
          productCode: '',
          checkPerson: '',
          reviewPerson: '',
          checkTime: '',
          conclusion: ''
        }
      }
    },
    loadingDocumentsTableData: {
      type: Array,
      default: function() {
        return []
      }
    },
    closePrintView: {
      type: Function,
      default: null
    }
  },
  data() {
    return {
      leftPic: '',

      loadingDocumentsLoading: false,
      printConfig: {
        id: 'loadingDocuments', // 被打印部分的id
        popTitle: '', // 打印配置页上方的标题
        extraHead: '', // 最上方的头部文字，附加在head标签上的额外标签，使用逗号分割
        preview: false, // 是否启动预览模式，默认是false
        previewTitle: '预览的标题', // 打印预览的标题
        previewPrintBtnLabel: '预览结束，开始打印', // 打印预览的标题下方的按钮文本，点击可进入打印
        zIndex: 20002, // 预览窗口的z-index，默认是20002，最好比默认值更高
        previewBeforeOpenCallback() {
          console.log('正在加载预览窗口！')
          // console.log(that.msg, this)
        }, // 预览窗口打开之前的callback
        previewOpenCallback() {
          console.log('已经加载完预览窗口，预览打开了！')
        }, // 预览窗口打开时的callback
        beforeOpenCallback() {
          console.log('开始打印之前！')
        }, // 开始打印之前的callback
        openCallback() {
          console.log('执行打印了！')
        }, // 调用打印时的callback
        closeCallback() {
          console.log('关闭了打印工具！')
        }, // 关闭打印的callback(无法区分确认or取消)
        clickMounted() {
          console.log('点击v-print绑定的按钮了！')
        },
        // url: 'http://localhost:8080/', // 打印指定的URL，确保同源策略相同
        // asyncUrl (reslove) {
        //   setTimeout(() => {
        //     reslove('http://localhost:8080/')
        //   }, 2000)
        // },
        standard: '',
        extarCss: ''
      }
    }
  },
  mounted() {
    // this.getLoadingDocuments()
    // this.getLogo()
  },
  methods: {
    closePrintViewChild() {
      this.closePrintView()
    },
    complexSpanMethod({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 1) {
        if (rowIndex === 0 || row.类别 !== this.loadingDocumentsTableData[rowIndex - 1].类别) {
          let rowspan = 1
          for (let i = rowIndex + 1; i < this.loadingDocumentsTableData.length; i++) {
            if (this.loadingDocumentsTableData[i].类别 === row.类别) {
              rowspan++
            } else {
              break
            }
          }
          return {
            rowspan,
            colspan: 1
          }
        } else {
          return {
            rowspan: 0,
            colspan: 0
          }
        }
      }
      // else if (columnIndex === 4) {
      //   if (rowIndex === 0 || row.工具设备 !== this.loadingDocumentsTableData[rowIndex - 1].工具设备) {
      //     let rowspan = 1
      //     for (let i = rowIndex + 1; i < this.loadingDocumentsTableData.length; i++) {
      //       if (this.loadingDocumentsTableData[i].工具设备 === row.工具设备) {
      //         rowspan++
      //       } else {
      //         break
      //       }
      //     }
      //     return {
      //       rowspan,
      //       colspan: 1
      //     }
      //   } else {
      //     return {
      //       rowspan: 0,
      //       colspan: 0
      //     }
      //   }
      // }
    },
    cellStyle({ row, column, rowIndex, columnIndex }) {
      // 根据条件设置背景颜色
      if (columnIndex === '5' && row.判定 === 'OK') {
        return 'background-color: #92d050;-webkit-print-color-adjust: exact;'
      }
      return ''
    },
    getLogo() {
      axios.get('../leftPic.png', {
        responseType: 'arraybuffer'
      }).then(res => {
        this.leftPic = 'data:image/jpeg;base64,' + btoa(new Uint8Array(res.data).reduce((data, byte) => data +
          String.fromCharCode(byte), ''))
      })
    }
  }
}
</script>

<style scoped lang="scss">

.loading-Documents {
  width: 1050px;
  border: 1px solid #676767;

  ::v-deep .el-input__inner {
    border: none !important;
    border-radius: 0px;
    border-bottom: 1px solid #676767 !important
  }

  .loading-Documents-table {
    border-bottom: 1px solid #000;
    border-left: 1px solid #000;
    border-right: 1px solid #000;
  }

  ::v-deep .el-table th.el-table__cell.is-leaf {
    border-bottom: 1px solid #000 !important;
    border-right: 1px solid #000 !important;
  }

  ::v-deep .el-table td.el-table__cell,
  .el-table th.el-table__cell.is-leaf {
    border-bottom: 1px solid #000 !important;
    border-right: 1px solid #000 !important;
  }

}

::v-deep .el-table {
  margin: 0;
}

::v-deep .el-table tr {
  height: 40px !important;
}

::v-deep .el-table th {
  height: 20px !important;
}

::v-deep .el-table th.el-table__cell {
  background-color: #d9d9d9 !important;
  height: 20px !important;
  -webkit-print-color-adjust: exact;
}
::v-deep .el-table td.el-table__cell div {
  font-size:  16px !important;
  color: #000000 !important;
}
#loadingDocuments {
  background-color: #fff;

  .title {
    text-align: center;
    color: #000;
    font-weight: bolder;
    .logo {
      width: 220px;
      height: 70px;
      top: 20px;
      left: 20px;
      background-size: 100% 100%;
      position: absolute;
    }

    div {
      width: 1050px;
      height: 50px;
      font-size: 30px;
      line-height: 60px;
      font-weight: 700;
    }
  }

  .title2 {
    color: #000;
    > div {
      display: flex;

      > div {
        font-size: 22px;
        background-color: #d9d9d9;
        text-align: center;
        height: 60px;
        line-height: 25px;
        padding-top: 5px;
        border: 1px solid #000;
        margin: -1px 0 0 -1px;
      }

      > .bdiv {
        background-color: #fff !important;
        line-height: 50px;
        -webkit-print-color-adjust: exact;
      }
    }
  }

  .conclusionResult {
    display: flex;
    font-size: 22px;
    text-align: center;
    color: #000;
    div {
      height: 40px;
      line-height: 40px;
    }
  }

  .buttonBox {
    display: flex;
    font-size: 20px;
    color: #000;
    div {
      width: 360px;
      padding-left: 40px;
      height: 100px;
      line-height: 100px;
    }
    span{
      font-size: 20px;
      width: 360px;
      height: 100px;
      line-height: 100px;
    }
  }

  .text1 {
    margin: 5px 5px;
    font-size: 18px;
    color: #000;
    font-weight: bolder;
  }

  ::v-deep .el-table tr td {
    page-break-inside: avoid; /* 避免在单元格内分页 */
  }
}
</style>

