<template>
	<view class="flex-col page" :style="{ backgroundImage: 'url(' + bkgPubilcPath + ')' }">
	  <view class="flex-row section_2">
	    <image
	      class="shrink-0 image"
	      src="https://www.carboner.cn:8888/api/file/downloadFile?objectKey=test_joXykCyMkUPipYIi_173dc3d4a3850b0bb353efc1a2b99624.png"
	    />
	    <view class="flex-col flex-1 ml-7">
	      <text class="self-start font text">AI换装</text>
	      <text class="self-stretch text_2 mt-6">
	        无需更衣室,轻松试穿全球华服!AI智能试衣系统,1秒精准贴合身形,真实还原汉服非遗刺绣、丝绸光泽、现代剪裁等细节。所见即所穿——助你告别网购“想象落差”,让每一件衣服都为你量身呈现。
	      </text>
	      <text class="self-start text_3 mt-6">注:收集的人像将会在本日自动删除(换装服务由即梦AI提供)</text>
	    </view>
	  </view>
	  <view class="flex-col items-start section_3">
	    <text class="font text_4">肖像照</text>
			<uni-file-picker
				fileMediatype="image" 
				@select="select" 
				limit="1"
			/>
	  </view>
	  <view class="flex-col items-start section_4">
	    <text class="font">AI换装效果</text>
	    <text class="text_5 mt-14">说明:AI换装仅供参考,到店试装最佳</text>
	    <image
	      class="image_3 "
	      :src="showImg"
				mode="widthFix"
	    />
	  </view>
	  <view class="flex-col justify-start items-center section_5">
	    <view class="flex-col justify-start items-center text-wrapper" @click="changeClothing"><text class="text_6">一键换装</text></view>
	  </view>
	</view>
	
	<!-- 遮罩层 -->
	    <view v-if="isShow" class="overlay"></view>
</template>

<script setup>
	import { onMounted,ref } from 'vue';
	import { onLoad } from '@dcloudio/uni-app';
import { baseUrl, domain } from '../../../api/request';
import { publicPath,bkgPubilcPath } from '../../../common/globalImagesUrl';
	
	const clothesImg = ref('');
	const modelImg = ref('')
	const cookie = wx.getStorageSync("cookie")  //请求头
	const responseObj = ref('')   //后端返回的对象
	const showImg = ref('https://www.carboner.cn:8888/api/file/downloadFile?objectKey=test_XuemOldYRiPrCypr_3e9ee919e2a801d0cb845076a6d3f486.png')  //展示样片
	
	onLoad((options)=>{
		console.log('服装图片URL---->',options.imgUrl);
		clothesImg.value = publicPath + options.imgUrl;
	})
	
	//肖像上传
	const select = async (e)=> {
		// console.log('选择文件:',e.tempFiles[0].file);
		const res = await uni.uploadFile({
			url: baseUrl + '/file/upload',
			filePath: e.tempFilePaths[0],
			name: 'file',
			header: {
				cookie
			},
			formData: {
				biz: "test"
			}
		})
		console.log('上传结果为--->',JSON.parse(res.data));
		responseObj.value = JSON.parse(res.data);
		if(responseObj.value.code === 1) {
			modelImg.value = publicPath + responseObj.value.data;
		}
		console.log('模特url为--->',modelImg.value);
	}
	
	
	//防抖
	let debounceTimer = null;
	let wxPayTimer = null;
	let loading = false;
	let isLoading = ref(false)
	let isShow = ref(false)
	
	
	//一键换装 + 防抖
	const changeClothing = async()=> {
		if(modelImg.value === '') {
			uni.showModal({
				content: '模特图片不能为空'
			})
			return
		}
		showLoading()
		isShow.value = true  //打开遮罩
		// console.log('服装url----->',clothesImg.value);
		// console.log('模特url--->',modelImg.value);
		const res = await uni.request({
			url: domain + '/clothes/facelift',
			method: 'GET',
			header: {
				cookie
			},
			data: {
				modelURL: modelImg.value,
				clothesURL: clothesImg.value
			}
		})
		console.log('----->返回结果',res.data);
		if(res.data.code === 1) {
			hideLoading()
			console.log('换装返回的结果---->',res.data.data);
			showImg.value = res.data.data
		}
		
	}
	
	const wxPayFd = (order) => {  //微信支付按钮防抖
		showLoading()  
		isShow.value = true  //打开遮罩
	}
	
	function showLoading() {  //加载弹窗
	  if (!loading) {
	    wx.showLoading({
	      title: '加载中...',
	    });
	    loading = true;
		isLoading.value = true
	  }
	}
	
	function hideLoading() {  //关闭弹窗
	  if (loading) {
	    wx.hideLoading();
	    loading = false;
		isLoading.value = false
		isShow.value = false
	  }
	}
</script>

<style lang="scss" scoped>
	.overlay {
	  position: fixed;
	  top: 0;
	  left: 0;
	  width: 100%;
	  height: 100%;
	  background-color: rgba(0, 0, 0, 0.4);  /* 半透明黑色背景 */
	  z-index: 999;
	}
	.ml-7 {
	  margin-left: 13.13rpx;
	}
	.mt-9 {
	  margin-top: 16.88rpx;
	}
	.page {
	  padding-top: 26.25rpx;
	  background-color: #ffffff;
	  // background-image: url('https://ide.code.fun/api/image?token=6815eae14ae84d00122e93ec&name=cf84478130b4f1e349c5b866f9c1a049.png');
	  background-size: 100% 100%;
	  background-repeat: no-repeat;
	  height: 100vh;
	  width: 100%;
		overflow-x: hidden;
		overflow-y: auto;
	}
	.section_2 {
	  margin-left: 16.88rpx;
	  margin-right: 15rpx;
	  padding: 10.2rpx 9.38rpx 15rpx;
	  background-color: #ffffff;
	  border-radius: 18.75rpx;
	}
	.image {
	  margin-top: 4.8rpx;
	  border-radius: 28.13rpx;
	  width: 191.25rpx;
	  height: 191.25rpx;
	}
	.font {
	  font-size: 37.5rpx;
	  line-height: 36.04rpx;
	  color: #000000;
	}
	.text {
	  color: #323233;
	}
	.text_2 {
	  color: #6b6e72;
	  font-size: 22.5rpx;
	  line-height: 26.25rpx;
	}
	.text_3 {
	  color: #6b6e72;
	  font-size: 15rpx;
	  line-height: 14.59rpx;
	}
	.section_3 {
	  margin: 20.63rpx 15rpx 0 16.88rpx;
	  padding: 19.57rpx 16.88rpx 24.38rpx;
	  background-color: #ffffff;
	  border-radius: 18.75rpx;
	}
	.text_4 {
	  margin-left: 2.17rpx;
		margin-bottom: 16.88rpx;
	  line-height: 35.89rpx;
	}
	.image_2 {
	  width: 256.88rpx;
	  height: 255rpx;
	}
	.section_4 {
	  margin: 20.63rpx 15rpx 0 16.88rpx;
	  padding: 30.13rpx 17.18rpx 73.13rpx;
	  background-color: #ffffff;
	  border-radius: 18.75rpx;
	}
	.text_5 {
	  color: #6b6e72;
	  font-size: 20.63rpx;
	  line-height: 19.82rpx;
	}
	.image_3 {
	  // margin-left: 5.32rpx;
	  // width: 673.13rpx;
	  // height: 673.13rpx;
	}
	.section_5 {
	  // margin-top: 181.88rpx;
	  padding: 16.88rpx 0 15rpx;
	  background-color: #ffffff;
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
	}
	.text-wrapper {
	  padding: 20.16rpx 0 22.28rpx;
	  background-color: #ffaaa5;
	  border-radius: 75rpx;
	  width: 536.25rpx;
	}
	.text_6 {
	  color: #ffffff;
	  font-size: 30rpx;
	  line-height: 28.84rpx;
	}
	@import url(../../../common/css/global.css);
</style>