<template>
  <view class="page__view">

		<navbar title="人脸验证" leftClick @leftClick="$utils.navigateBack" color="#191919" bgColor="#FFFFFF" />

    <view class="main">

      <view class="card">
        <view class="card-header">
          <view>为保证本人操作，请进行人脸验证</view>
          <view class="card-desc">请保持正脸在取景框根据屏幕提示完成识别</view>
        </view>
        
        <view class="camera-box">
          <!-- todo: check clear -->
          <image class="camera-bg" src="@/pages_order/static/order/camera-bg.png" mode="scaleToFill" />

          <template v-if="countdown">
            <view class="camera-countdown">
              <uv-count-down 
                :time="3 * 1000" 
                autoStart
                @change="onCountDownChange"
                @finish="onCountDownFinish"
              >
                <view class="camera-countdown-text">{{ timeData.seconds }}s</view>
              </uv-count-down>
            </view>
          </template>

          <template v-if="scanning">
            <view class="flex camera-fg">
              <view style="position: relative;">
                <camera class="camera" device-position="front" flash="auto" @initdone="onCameraInited" @error="onCameraError"></camera>
                <cover-image class="camera-tag" src="@/pages_order/static/order/tag.png"></cover-image>
              </view>
            </view>
          </template>
          <!-- todo: delete -->
          <template v-else-if="src">
            <view class="flex camera-fg">
              <image class="camera" :src="src" mode="scaleToFill" />
            </view>
          </template>

        </view>

        <view class="flex tips">
          <view class="flex flex-column tips-item">
				    <image class="tips-icon" src="@/pages_order/static/order/tips-1.png" mode="widthFix"></image>
            <view class="tips-text">保持光线充足</view>
          </view>
          <view class="flex flex-column tips-item">
				    <image class="tips-icon" src="@/pages_order/static/order/tips-2.png" mode="widthFix"></image>
            <view class="tips-text">脸在取景框内</view>
          </view>
          <view class="flex flex-column tips-item">
				    <image class="tips-icon" src="@/pages_order/static/order/tips-3.png" mode="widthFix"></image>
            <view class="tips-text">面部正对平面</view>
          </view>
        </view>
			</view>
    </view>
  
    <view class="bottom">
      <button class="btn" @click="onVerify">开始识别</button>
    </view>

  </view>  
</template>

<script>
  export default {
    data() {
      return {
        src: null,
        scanning: false,
        countdown: false,
        timeData: {},
      }
    },
    methods: {
      onVerify() {
        // todo
        this.scanning = true
      },
      onCameraInited() {
        this.countdown = true
      },
			onCountDownChange(e) {
				this.timeData = e
			},
      onCountDownFinish() {
        this.countdown = false
        this.takePhoto()
      },
      takePhoto() {
        const ctx = uni.createCameraContext();
        ctx.takePhoto({
            quality: 'high',
            success: (res) => {
              this.src = res.tempImagePath
              this.scanning = false

              // todo: fetch match

              setTimeout(() => {
                this.$utils.navigateBack()
              }, 1500)
            }
        });
      },
      onCameraError(e) {
        console.log(e.detail);
      }
    },
  }
</script>

<style lang="scss" scoped>

	.page__view {
		width: 100vw;
		min-height: 100vh;
		background-color: $uni-bg-color;
		position: relative;
		
		/deep/ .nav-bar__view {
			position: fixed;
			top: 0;
			left: 0;
		}
	}

	.main {
		padding: calc(var(--status-bar-height) + 144rpx) 32rpx 224rpx 32rpx;
	}

	.card {
		padding: 32rpx;
		background: #FAFAFF;
		border: 2rpx solid #FFFFFF;
		border-radius: 32rpx;

		& + & {
			margin-top: 40rpx;
		}

		&-header {
			font-family: PingFang SC;
			font-weight: 500;
			font-size: 36rpx;
			line-height: 1.4;
			color: #252545;
			margin-bottom: 48rpx;
		}

    &-desc {
      margin-top: 4rpx;
      font-family: PingFang SC;
      font-weight: 400;
      font-size: 26rpx;
      line-height: 1.4;
      color: #4E4E69;
    }
	}

  .camera {
    &-box {
      width: 622rpx;
      height: 678rpx;
      position: relative;
    }

    &-bg {
      width: 100%;
      height: 100%;
    }

    &-countdown {
      position: absolute;
      top: 14rpx;
      left: 50%;
      transform: translateX(-50%);

      &-text {
        font-family: PingFang SC;
        font-weight: 600;
        font-size: 72rpx;
        line-height: 1.4;
        color: $uni-color;
      }
    }

    &-fg {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
    }

    & {
      width: 310rpx; 
      height: 310rpx; 
      border-radius: 50%;
    }

    &-tag {
      position: absolute; 
      left: 50%; 
      bottom: 0;
      transform: translate(-50%, 60%);
      width: 248rpx;
      height: 160rpx;
    }
  }

  .tips {
    margin-top: 32rpx;
    padding: 0 32rpx;
    column-gap: 24rpx;

    &-item {
      flex: 1;
      row-gap: 24rpx;
    }

    &-icon {
      width: 72rpx;
      height: auto;
    }

    &-text {
      font-family: PingFang SC;
      font-weight: 400;
      font-size: 22rpx;
      line-height: 1.4;
      color: #989898;
    }
  }

  .bottom {
		position: fixed;
		left: 0;
		bottom: 0;

		width: 100vw;
		// height: 200rpx;
		padding: 24rpx 40rpx;
		padding-bottom: calc(env(safe-area-inset-bottom) + 24rpx);
		background: #FFFFFF;
		box-sizing: border-box;

		.btn {
			width: 100%;
			padding: 16rpx 0;
			box-sizing: border-box;
			font-family: PingFang SC;
			font-weight: 500;
			font-size: 36rpx;
			line-height: 1;
			color: #FFFFFF;
			background-image: linear-gradient(to right, #4B348F, #845CFA);
			border-radius: 41rpx;
		}
	}

</style>