<template>
  <div id="app" class="page__view">

		<div class="card">
			<div class="report" :style="style">
				<!-- 01 -->
				<div class="export">
					<img class="img fg" :src="configList.report_page_01" crossorigin=“anonymous />
				</div>
				<!-- 02 -->
				<div class="export">
					<img class="img fg" :src="configList.report_page_02" crossorigin=“anonymous />
				</div>
				<!-- 03 -->
				<div class="export">
					<img class="img" :src="configList.report_page_03" crossorigin=“anonymous />
					<img class="img fg" style="top: 76rpx; left: 90rpx; width: 82rpx; height: 104rpx;" src="@/static/image/temp-14.png" crossorigin=“anonymous />
					<div class="text" style="top: 76rpx; left: 206rpx;">姓名</div>
					<div class="text" style="top: 76rpx; left: 298rpx;">性别</div>
					<div class="text" style="top: 116rpx; left: 206rpx;">年龄</div>
					<div class="text" style="top: 116rpx; left: 298rpx;">学段</div>
					<div class="text" style="top: 156rpx; left: 206rpx;">学校</div>
					<div class="text" style="top: 180rpx; left: 118rpx;">城市</div>
					<div class="text" style="top: 208rpx; left: 138rpx;">出勤情况</div>
				</div>
				<!-- 04 -->
				<div class="export">
					<img class="img fg" style="top: 70rpx; left: 120rpx; width: 258rpx; height: 154rpx; border-radius: 8rpx; overflow: hidden;" src="@/static/image/temp-20.png" crossorigin=“anonymous />
					<img class="img fg" :src="configList.report_page_04" crossorigin=“anonymous />
				</div>
				<!-- 05 -->
				<div class="export">
					<img class="img fg" style="top: 216rpx; left: 62rpx; width: 60rpx; height: 60rpx; transform: rotate(14deg);" src="@/static/image/temp-20.png" crossorigin=“anonymous />
					<img class="img fg" style="top: 170rpx; left: 12rpx; width: 60rpx; height: 60rpx; transform: rotate(-7deg);" src="@/static/image/temp-14.png" crossorigin=“anonymous />
					<img class="img fg" style="top: 30rpx; left: 56rpx; width: 74rpx; height: 74rpx; transform: rotate(15deg);" src="@/static/image/temp-20.png" crossorigin=“anonymous />
					<img class="img fg" style="top: 80rpx; left: 84rpx; width: 74rpx; height: 74rpx; transform: rotate(-6deg);" src="@/static/image/temp-14.png" crossorigin=“anonymous />
					<img class="img fg" style="top: 120rpx; left: 196rpx; width: 84rpx; height: 84rpx; transform: rotate(-14deg);" src="@/static/image/temp-20.png" crossorigin=“anonymous />
					<img class="img fg" style="top: 174rpx; left: 262rpx; width: 84rpx; height: 84rpx; transform: rotate(14deg);" src="@/static/image/temp-14.png" crossorigin=“anonymous />
					<img class="img fg" style="top: 26rpx; left: unset; right: -24rpx; width: 134rpx; height: 134rpx; transform: rotate(14deg);" src="@/static/image/temp-20.png" crossorigin=“anonymous />
					<img class="img fg" :src="configList.report_page_05" crossorigin=“anonymous />
					<div class="text text-5" style="top: 228rpx; left: 6rpx; transform: rotate(-7deg);">标签1</div>
					<div class="text text-5" style="top: 160rpx; left: 136rpx; transform: rotate(7deg);">标签2</div>
					<div class="text text-5" style="top: 266rpx; left: 318rpx; transform: rotate(7deg);">标签3</div>
					<div class="text text-5" style="top: 42rpx; left: unset; right: 100rpx; transform: rotate(-7deg);">标签4</div>
				</div>
				<!-- 06 -->
				<div class="export">
					<img class="img fg" style="top: 118rpx; left: 92rpx; width: 78rpx; height: 50rpx; transform: skew(50deg, -34deg);" src="@/static/image/temp-20.png" crossorigin=“anonymous />
					<img class="img fg" style="top: 110rpx; left: 244rpx; width: 68rpx; height: 60rpx; transform: skew(50deg, 317deg);" src="@/static/image/temp-20.png" crossorigin=“anonymous />
					<img class="img fg" style="top: 76rpx; left: 173rpx; width: 68rpx; height: 46rpx; transform: skew(50deg, -34deg);" src="@/static/image/temp-14.png" crossorigin=“anonymous />
					<img class="img fg" style="top: 176rpx; left: 162rpx; width: 92rpx; height: 60rpx; transform: skew(54deg, 324deg);" src="@/static/image/temp-14.png" crossorigin=“anonymous />
					<img class="img fg" :src="configList.report_page_06" crossorigin=“anonymous />
				</div>
				<!-- 07 -->
				<div class="export">
					<img class="img fg" style="top: 54rpx; left: 106rpx; width: 96rpx; height: 74rpx;" src="@/static/image/temp-20.png" crossorigin=“anonymous />
					<img class="img fg" style="top: 54rpx; left: 204rpx; width: 96rpx; height: 74rpx;" src="@/static/image/temp-14.png" crossorigin=“anonymous />
					<img class="img fg" style="top: 54rpx; left: 300rpx; width: 96rpx; height: 74rpx;" src="@/static/image/temp-20.png" crossorigin=“anonymous />
					<img class="img fg" style="top: 152rpx; left: 84rpx; width: 142rpx; height: 96rpx;" src="@/static/image/temp-20.png" crossorigin=“anonymous />
					<img class="img fg" style="top: 152rpx; left: 250rpx; width: 142rpx; height: 96rpx;" src="@/static/image/temp-14.png" crossorigin=“anonymous />
					<img class="img fg" :src="configList.report_page_07" crossorigin=“anonymous />
				</div>
				<!-- 08 -->
				<div class="export">
					<img class="img fg" style="top: 46rpx; left: 68rpx; width: 70rpx; height: 70rpx;" src="@/static/image/temp-20.png" crossorigin=“anonymous />
					<img class="img fg" style="top: 118rpx; left: 68rpx; width: 70rpx; height: 70rpx;" src="@/static/image/temp-14.png" crossorigin=“anonymous />
					<img class="img fg" style="top: 46rpx; left: 148rpx; width: 70rpx; height: 70rpx;" src="@/static/image/temp-14.png" crossorigin=“anonymous />
					<img class="img fg" style="top: 118rpx; left: 148rpx; width: 70rpx; height: 70rpx;" src="@/static/image/temp-20.png" crossorigin=“anonymous />
					<img class="img fg" style="top: 192rpx; left: 148rpx; width: 70rpx; height: 70rpx;" src="@/static/image/temp-14.png" crossorigin=“anonymous />
					<img class="img fg" style="top: 86rpx; left: 228rpx; width: 70rpx; height: 70rpx;" src="@/static/image/temp-14.png" crossorigin=“anonymous />
					<img class="img fg" style="top: 158rpx; left: 228rpx; width: 70rpx; height: 70rpx;" src="@/static/image/temp-20.png" crossorigin=“anonymous />
					<img class="img fg" style="top: 118rpx; left: 310rpx; width: 70rpx; height: 70rpx;" src="@/static/image/temp-20.png" crossorigin=“anonymous />
					<img class="img fg" style="top: 192rpx; left: 310rpx; width: 70rpx; height: 70rpx;" src="@/static/image/temp-14.png" crossorigin=“anonymous />
					<img class="img fg" :src="configList.report_page_08" crossorigin=“anonymous />
				</div>
				<!-- 09 -->
				<div class="export">
					<img class="img fg" style="top: 0; left: unset; right: 0; width: auto; height: 100%;" src="@/static/image/temp-20.png" crossorigin=“anonymous />
					<img class="img fg" style="width: 162rpx; height: 112rpx;" src="@/static/image/temp-14.png" crossorigin=“anonymous />
					<img class="img img-08 fg" :src="configList.report_page_09" crossorigin=“anonymous />
				</div>
				<!-- 10 -->
				<div class="export">
					<img class="img fg" style="top: 78rpx; left: 64rpx; width: 98rpx; height: 66rpx;" src="@/static/image/temp-20.png" crossorigin=“anonymous />
					<img class="img fg" style="top: 80rpx; left: 276rpx; width: 94rpx; height: 64rpx;" src="@/static/image/temp-20.png" crossorigin=“anonymous />
					<img class="img fg" style="top: 80rpx; left: 372rpx; width: 56rpx; height: 62rpx;" src="@/static/image/temp-14.png" crossorigin=“anonymous />
					<img class="img fg" style="top: 148rpx; left: 32rpx; width: 132rpx; height: 88rpx;" src="@/static/image/temp-14.png" crossorigin=“anonymous />
					<img class="img fg" style="top: 148rpx; left: 276rpx; width: 76rpx; height: 50rpx;" src="@/static/image/temp-14.png" crossorigin=“anonymous />
					<img class="img fg" style="top: 148rpx; left: 356rpx; width: 75rpx; height: 125rpx;" src="@/static/image/temp-20.png" crossorigin=“anonymous />
					<img class="img fg" style="top: 202rpx; left: 166rpx; width: 98rpx; height: 66rpx;" src="@/static/image/temp-20.png" crossorigin=“anonymous />
					<img class="img fg" style="top: 42rpx; left: 166rpx; width: 108rpx; height: 160rpx;" src="@/static/image/temp-20.png" crossorigin=“anonymous />
					<img class="img fg" :src="configList.report_page_10" crossorigin=“anonymous />
				</div>
				<!-- 11 -->
				<div class="export">
					<img class="img fg" style="top: unset; bottom: 164rpx; left: 34rpx; width: 74rpx; height: 50rpx;" src="@/static/image/temp-14.png" crossorigin=“anonymous />
					<img class="img fg" style="top: unset; bottom: 164rpx; left: 112rpx; width: 48rpx; height: 70rpx;" src="@/static/image/temp-20.png" crossorigin=“anonymous />
					<img class="img fg" style="top: unset; bottom: 164rpx; left: 164rpx; width: 135rpx; height: 100rpx;" src="@/static/image/temp-14.png" crossorigin=“anonymous />
					<img class="img fg" style="top: 148rpx; left: 34rpx; width: 106rpx; height: 88rpx;" src="@/static/image/temp-20.png" crossorigin=“anonymous />
					<img class="img fg" style="top: 148rpx; left: 232rpx; width: 74rpx; height: 50rpx;" src="@/static/image/temp-20.png" crossorigin=“anonymous />
					<img class="img fg" :src="configList.report_page_11" crossorigin=“anonymous />
				</div>
				<!-- 12 -->
				<div class="export">
					<img class="img fg" style="top: 82rpx; left: 76rpx; width: 50rpx; height: 62rpx;" src="@/static/image/temp-20.png" crossorigin=“anonymous />
					<img class="img fg" style="top: 82rpx; left: 140rpx; width: 50rpx; height: 62rpx;" src="@/static/image/temp-14.png" crossorigin=“anonymous />
					<img class="img fg" style="top: 82rpx; left: 206rpx; width: 50rpx; height: 62rpx;" src="@/static/image/temp-20.png" crossorigin=“anonymous />
					<img class="img fg" style="top: 82rpx; left: 270rpx; width: 50rpx; height: 62rpx;" src="@/static/image/temp-14.png" crossorigin=“anonymous />
					<img class="img fg" style="top: 82rpx; left: 336rpx; width: 50rpx; height: 62rpx;" src="@/static/image/temp-20.png" crossorigin=“anonymous />
					<img class="img fg" style="top: 166rpx; left: 76rpx; width: 50rpx; height: 62rpx;" src="@/static/image/temp-14.png" crossorigin=“anonymous />
					<img class="img fg" style="top: 166rpx; left: 140rpx; width: 50rpx; height: 62rpx;" src="@/static/image/temp-20.png" crossorigin=“anonymous />
					<img class="img fg" style="top: 166rpx; left: 206rpx; width: 50rpx; height: 62rpx;" src="@/static/image/temp-14.png" crossorigin=“anonymous />
					<img class="img fg" style="top: 166rpx; left: 270rpx; width: 50rpx; height: 62rpx;" src="@/static/image/temp-20.png" crossorigin=“anonymous />
					<img class="img fg" style="top: 166rpx; left: 336rpx; width: 50rpx; height: 62rpx;" src="@/static/image/temp-14.png" crossorigin=“anonymous />
					<img class="img fg" :src="configList.report_page_12" crossorigin=“anonymous />
				</div>
				<!-- 13 -->
				<div class="export">
					<img class="img" :src="configList.report_page_13" crossorigin=“anonymous />
					<div class="text text-12" style="top: 84rpx; left: 100rpx;">技能01</div>
					<progressSegment class="progress" style="top: 80rpx; left: 162rpx;" :value="20"></progressSegment>
					<div class="text text-12" style="top: 114rpx; left: 100rpx;">技能02</div>
					<progressSegment class="progress" style="top: 112rpx; left: 162rpx;" :value="40"></progressSegment>
					<div class="text text-12" style="top: 146rpx; left: 100rpx;">技能03</div>
					<progressSegment class="progress" style="top: 144rpx; left: 162rpx;" :value="60"></progressSegment>
					<div class="text text-12" style="top: 176rpx; left: 100rpx;">技能04</div>
					<progressSegment class="progress" style="top: 174rpx; left: 162rpx;" :value="80"></progressSegment>
					<div class="text text-12" style="top: 206rpx; left: 100rpx;">技能05</div>
					<progressSegment class="progress" style="top: 204rpx; left: 162rpx;" :value="100"></progressSegment>
				</div>
				<!-- 14 -->
				<div class="export">
					<img class="img fg" :src="configList.report_page_14" crossorigin=“anonymous />
					<div class="text text-13" style="top: 94rpx; left: 74rpx;">同学寄语同学寄语同学寄语同学寄语同学寄语同学寄语同学寄语同学寄语</div>
					<div class="text text-13" style="top: 162rpx; left: 248rpx;">导师寄语导师寄语导师寄语导师寄语导师寄语导师寄语导师寄语导师寄语导师寄语</div>
				</div>
				<!-- 15 -->
				<div class="export">
					<img class="img fg" style="top: 28rpx; left: unset; right: 18rpx; width: 316rpx; height: 244rpx;" src="@/static/image/temp-20.png" crossorigin=“anonymous />
					<img class="img fg" :src="configList.report_page_15" crossorigin=“anonymous />
				</div>
				<!-- 00 -->
				<div class="export">
					<img class="img fg" :src="configList.report_page_00" crossorigin=“anonymous />
				</div>
			</div>
		</div>
    
		<!-- <img v-if="current > 0" class="img turn left" src="@/static/image/icon-left.png" @click="prev" />
		<img v-if="current < 15" class="img turn right" src="@/static/image/icon-right.png" @click="next" /> -->

  </div>
</template>

<script>
  // import html2canvas from 'html2canvas'
  import html2canvas from '@wtto00/html2canvas'
  import { jsPDF } from "jspdf";

	import progressSegment from '@/components/progressSegment.vue';

  export default {
		components: {
			progressSegment,
		},
    data() {
      return {
				mode: null,
        id: null,
        details: {},
				current: 0,
      }
    },
		computed: {
			style() {
				// return `transform: translateX(calc(-${this.current}*(100vh * 841.89 / 595.28)))`
			},
		},
    onLoad(arg) {
      document.addEventListener('UniAppJSBridgeReady', function() {
        console.log('UniAppJSBridgeReady', uni)
        uni.getEnv(function(res) {
            console.log('当前环境：' + JSON.stringify(res));
        });
      })
      
      const { id, mode, token } = arg
      token && uni.setStorageSync('token', token)
      this.id = id
			this.mode = mode
    },
    async mounted() {
      await this.getData(this.id)
			console.log('mode', this.mode)

			if (this.mode === 'export') {
				// todo: await load image
				setTimeout(() => {
					this.createPdf()
				}, 3000)
			}
    },
    methods: {
      async getData(id) {
				// todo
      },
			prev() {
				this.current -= 1
			},
			next() {
				this.current += 1
			},
      async upload(pdfBlob) {
        const pdfUrl = URL.createObjectURL(pdfBlob);
        
				const url = await this.$Oss.ossUpload(pdfUrl)
        console.log('ossUpload', url)

        return url
      },
      createPdf() {
        
				uni.showLoading({
					title: '生成中...'
				})

        console.log('createPdf')

        const fillPage = () => {

          // 设置背景色
          PDF.setFillColor(251, 254, 255); // 例如，红色背景 

          // 绘制一个填充的矩形覆盖整个页面
          PDF.rect(0, 0, PDF.internal.pageSize.getWidth(), PDF.internal.pageSize.getHeight(), 'F'); // 'F'表示填充
          
        }

        let PDF = new jsPDF('l', 'pt', 'a4')
        console.log('PDF', PDF)
        console.log('size', PDF.internal.pageSize.getWidth(), PDF.internal.pageSize.getHeight())
        const WIDTH = PDF.internal.pageSize.getWidth() // 841.89 
        const HEIGHT = PDF.internal.pageSize.getHeight() // 595.28


        let request = [] 
        let nodeList = document.querySelectorAll('.export')
        for(let i = 0; i < nodeList.length; i++) {
          request.push(html2canvas(nodeList[i], { allowTaint: true, useCORS: true, }))
        }

        return Promise.allSettled(request).then(res => {
          console.log('res', res)

          fillPage()

          res.forEach((item, index) => {
            if (index > 0) {
              PDF.addPage()
              fillPage()
            }
            const canvas = item.value
            let pageData = canvas.toDataURL('image/jpeg', 1.0)
            PDF.addImage(pageData, 'JPEG', 0, 0, WIDTH, HEIGHT)

          })

          // 将 PDF 文件转换为 Blob 对象
          const pdfBlob = PDF.output('blob');
          return this.upload(pdfBlob)
        }).then(pdfUrl => {
          this.$jWeixin.miniProgram.postMessage({
            data: pdfUrl
          });
          // this.$jWeixin.miniProgram.navigateBack()
          this.$jWeixin.miniProgram.redirectTo({
						url: `/pages_order/growing/activity/applyEmail?id=${this.id}&pdfUrl=${pdfUrl}`
					})
        }).catch(err => {
          console.log('err', err)
        }).finally(() => {
					uni.hideLoading()
				})

      },
    }
  }
</script>

<style lang="scss" scoped>

	$pages: 16;
	// $w: calc(100vw - 24rpx * 2);
	// $h: calc(#{$w} / 841.89 * 595.28);
	$h: 100vh;
	$w: calc(#{$h} * 841.89 / 595.28);

  .page__view {
    width: 100vw;
    min-height: 100vh;
		background: #F7F8FA;
    position: relative;
		box-sizing: border-box;
    font-family: PingFang SC;
    font-weight: 400;
    line-height: 1.4;
  }

	.card {
		// padding: 0 calc((100vw - #{$w}) / 2);
    overflow-x: auto;

		// position: absolute;
		// top: 50vh;
		// transform: translateY(-50%);
		// width: $w;
		// margin: 0 24rpx;
    // padding: 250rpx 0;
    // overflow: hidden;
		// background: #FFFFFF;
	}

	.report {
		width: calc(#{$pages}*#{$w});
    height: 100vh;
		// overflow-x: visible;
		padding: 0 calc((100vw - #{$w}) / 2);
		// overflow-x: auto;
    overflow-y: hidden;
	}

	.export {
		position: relative;
		display: inline-block;
		width: $w;
		min-height: $h;
		overflow: hidden;
	}

	.fg {
		position: absolute;
		top: 0;
		left: 0;
	}

	.img {
		display: block;
		width: $w;
		height: auto;
		object-fit: cover;

		&-08 {
			width: auto;
			height: $h;
		}
	}

	.text {
		position: absolute;
		font-size: 16rpx;
		font-weight: 600;
		color: #FFFFFF;

		&-5 {
			font-size: 8rpx;
			color: #000000;
		}

		&-12 {
			font-size: 8rpx;
			color: #000000;
		}

		&-13 {
			width: 118rpx;
			white-space: pre-wrap;
			font-size: 8rpx;
			color: #000000;
		}
	}

	.progress {
		position: absolute;
		width: 200rpx;
	}

  // .export__view {
  //   position: fixed;
  //   top: 100vh;
  // }

	.turn {
		width: 80rpx;
		height: 80rpx;
		position: fixed;
		top: 50vh;
		transform: translateY(-50%);

		&.left {
			left: 0;
		}
		&.right {
			right: 0;
		}
	}

  .bottom {
		position: fixed;
		left: 0;
		bottom: 0;
		width: 100vw;
		background: #FFFFFF;
		box-sizing: border-box;
    padding: 32rpx 40rpx;
    padding-bottom: calc(env(safe-area-inset-bottom) + 32rpx);
    box-sizing: border-box;

    .btn {
      width: 100%;
      padding: 14rpx 0;
      font-family: PingFang SC;
      font-weight: 500;
      font-size: 36rpx;
      line-height: 1.4;
      color: #FFFFFF;
      background: linear-gradient(to right, #21FEEC, #019AF9);
      border: 2rpx solid #00A9FF;
      border-radius: 41rpx;
    }

	}
</style>
