<template>
  <view class="page">
    <view class="title">
      <!-- 提示文字 -->
      <text class="tips">照片不符合规范，请重新上传</text>
      <text v-show="false" class="tips">添加一张正面照片</text>
      <text v-show="false" class="tips">数字形象生成中</text>
    </view>
    <!-- 上传图片和展示 -->
    <view class="uploadBox imgContenBox scan">
      <!-- 扫描线 -->
      <view class="scan-line"></view>
      <image
        class="scan-border"
        src="../../assets/img/scanBorder.png"
        mode="aspectFit"
      />
      <!-- 上传按钮 -->
      <image
        class="addImg"
        v-show="!showImgUrl"
        src="../../assets/icon/add.png"
        alt=""
        @click="chooseImage"
      />
      <image
        class="showImg"
        v-show="showImgUrl"
        :src="showImgUrl"
        alt=""
        @click="chooseImage"
        mode="aspectFit"
      />
    </view>
    <view @click="toCloseStyle" class="uploadBtn">
      <text>上传我的照片</text>
      <view class="free">
        <image src="../../assets/icon/blackBG.png" mode="aspectFit" />
        <text>新用户免费制作</text>
      </view>
    </view>
    <!-- 底部盒子 -->
    <view class="bottomBox">
      <view class="tips2">请按照样片上传您五官清晰的正面照片</view>
      <view class="demoImgBox">
        <view class="imgBox">
          <image src="../../assets/img/img1.png" mode="aspectFit" />
          <view class="icon">
            <image src="../../assets/icon/error.png" mode="aspectFit" />
          </view>
        </view>
        <view class="imgBox">
          <image
            class="rightImg"
            src="../../assets/img/img2.png"
            mode="aspectFit"
          />
          <view class="icon">
            <image src="../../assets/icon/correct.png" mode="aspectFit" />
          </view>
        </view>
        <view class="imgBox">
          <image src="../../assets/img/img3.png" mode="aspectFit" />
          <view class="icon">
            <image src="../../assets/icon/error.png" mode="aspectFit" />
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
//#region 导入
import { ref, reactive } from "vue";
import { voiceTotalApi } from "../../api/login.js";
import { userInfoModules } from "@/store/modules/userInfo";
import imageCompression from "browser-image-compression"; //压缩图片插件
//#endregion
const userInfoModulesPinia = userInfoModules();

//#region 上传
const uploadState = ref(3); //上传状态
const showImgUrl = ref(null);
function chooseImage() {
  // 调用uni.chooseImage方法选择图片
  uni.chooseImage({
    count: 1, // 最多可以选择的图片张数，这里设置为1，只选择一张图片
    success: async (res) => {
      // 图片压缩
      console.log(res.tempFiles[0].size);
      if (userInfoModulesPinia.platForm != 1) {
        uni.compressImage({
          src: res.tempFilePaths[0],
          quality: 20,
          success: (res2) => {
            showImgUrl.value = res2.tempFilePath;
            console.log(res2.tempFilePath);
          },
        });
      } else {
        const options = {
          maxSizeMB: 2, // 最大压缩大小为 4MB
          useWebWorker: true, // 使用 Web Worker 进行压缩，提高性能
        };
        const compressedFile = await imageCompression(
          res.tempFiles[0],
          options
        );
        //compressedFile是一个blob对象
        showImgUrl.value = URL.createObjectURL(compressedFile);
      }
    },
    fail: (err) => {
      console.error("选择图片失败", err);
    },
  });
}

function toCloseStyle() {
  if (showImgUrl.value) {
    uni.redirectTo({
      url: "/pages/closeStyle/closeStyle",
    });
  } else {
    uni.showToast({
      title: "请先上传图片",
      icon: "none",
    });
  }
}

//#endregion ---------------------

//#region

//#endregion ---------------------
</script>

<style lang="scss">
.page {
  position: relative;
  min-height: 100vh;
  background-color: rgba(0, 0, 0, 0.8);
  .bgc {
    position: absolute;
    width: 750rpx;
    height: 750rpx;
    transform: scale(1.5);
    top: 187rpx;
    left: 0;
    object-fit: cover;
    z-index: -2;
    backdrop-filter: blur(3px);
  }
}
.title {
  margin-bottom: 40rpx;
  .tips {
    margin-top: 20rpx;
    color: #fff;
    font-size: 44rpx;
    font-weight: 900;
  }
}
.uploadBox {
  position: relative;
  width: 420rpx;
  height: 560rpx;
  background-color: rgba(255, 255, 255, 0.1);
  border-radius: 50rpx;
  .scan-border {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 360rpx;
    height: 480rpx;
  }
  .addImg {
    width: 80rpx;
    height: 80rpx;
  }
  .showImg {
    max-width: 90%;
    max-height: 90%;
  }
  .u-upload {
    position: absolute;
    top: 0;
    left: 0;
    width: 600rpx;
    height: 600rpx;
    opacity: 0;
  }
}
// 扫描
.scan {
  overflow: hidden;
  .scan-line {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 40%;
    background: linear-gradient(180deg, rgba(0, 255, 51, 0) 50%, #00ff33 300%);
    border-bottom: 2px solid #00ff33;
    animation: scanAnimation 2s linear infinite;
    z-index: 9;
  }

  @keyframes scanAnimation {
    0% {
      transform: translateY(-150%);
    }
    100% {
      transform: translateY(150%);
    }
  }
}
// 上传
.uploadBtn {
  position: relative;
  margin-top: 75rpx;
  width: 520rpx;
  height: 100rpx;
  line-height: 100rpx;
  text-align: center;
  background: #ff4f00;
  border-radius: 50rpx;
  font-size: 30rpx;
  color: #fff;
  font-weight: 900;
  .free {
    position: absolute;
    top: -25rpx;
    right: -80rpx;
    width: 185rpx;
    height: 50rpx;
    image {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      z-index: 1;
    }
    text {
      position: absolute;
      top: -25rpx;
      left: 50%;
      transform: translate(-50%, 0);
      width: 100%;
      height: 100%;
      font-size: 22rpx;
      font-weight: 900;
      z-index: 2;
    }
  }
}
// 底部盒子
.bottomBox {
  flex: 1;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  padding: 0 0 90rpx 0;
  .tips2 {
    margin-top: 20rpx;
    margin-bottom: 15rpx;
    font-size: 22rpx;
  }
  .demoImgBox {
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    padding: 0 20rpx;
    width: 100%;
    .imgBox {
      position: relative;
      image {
        width: 180rpx;
        height: 180rpx;
      }
      .rightImg {
        width: 200rpx;
        height: 200rpx;
      }
      .icon {
        position: absolute;
        top: -8rpx;
        right: -8rpx;
        width: 36rpx;
        height: 36rpx;
        image {
          width: 100%;
          height: 100%;
        }
      }
    }
  }
}
</style>
