<template>
	<view class="page-wrap">
		<view class="page-bg">
			<u-navbar leftIconColor="#FFFFFF" :autoBack="true" bg-color="transparent" safeAreaInsetTop
				placeholder></u-navbar>
			<view class="notice">
				请如实填写个人资料，真实的资料会更受欢迎
			</view>
			<view class="title">
				上传形象照
			</view>
			<view class="desc">
				可上传工作照、生活照，能获得更多青睐喔~
			</view>
			<view class="nickname">
				<button class="btn-avatar u-reset-button" @click="onChooseAvatar(0)">
					<image class="btn-avatar-plus" v-if="!fileList1[0].url" src="@/static/icon/path.png" mode="widthFix"></image>
					<image class="avatar-img" v-else :src="fileList1[0].url" mode="widthFix"></image>
				</button>
				<button class="btn-avatar u-reset-button" @click="onChooseAvatar(1)">
					<image class="btn-avatar-plus" v-if="!fileList1[1].url" src="@/static/icon/path.png" mode="widthFix"></image>
					<image class="avatar-img" v-else :src="fileList1[1].url" mode="widthFix"></image>
				</button>
				<button class="btn-avatar u-reset-button" @click="onChooseAvatar(2)">
					<image class="btn-avatar-plus" v-if="!fileList1[2].url" src="@/static/icon/path.png" mode="widthFix"></image>
					<image class="avatar-img" v-else :src="fileList1[2].url" mode="widthFix"></image>
				</button>
			</view>
			<view class="tips">
					<view class="tips-item">
						<image src="https://file-jimei.oss-cn-shenzhen.aliyuncs.com/static/user-base/5.png" mode="aspectFill" class="tips-item-image"></image>
						<view class="tips-item-text">999+喜欢</view>
						<image class="tips-ft" src="@/static/user-base/y.png" mode="widthFix"></image>
					</view>
					<view class="tips-item">
						<image src="https://file-jimei.oss-cn-shenzhen.aliyuncs.com/static/user-base/4.png" mode="aspectFill" class="tips-item-image"></image>
						<view class="tips-item-text">暴露照</view>
						<image class="tips-ft" src="@/static/user-base/n.png" mode="widthFix"></image>
					</view>
					<view class="tips-item">
						<image src="https://file-jimei.oss-cn-shenzhen.aliyuncs.com/static/user-base/3.png" mode="aspectFill" class="tips-item-image"></image>
						<view class="tips-item-text">模糊照</view>
						<image class="tips-ft" src="@/static/user-base/n.png" mode="widthFix"></image>
					</view>
					<view class="tips-item">
						<image src="https://file-jimei.oss-cn-shenzhen.aliyuncs.com/static/user-base/2.png" mode="aspectFill" class="tips-item-image"></image>
						<view class="tips-item-text">非人物照</view>
						<image class="tips-ft" src="@/static/user-base/n.png" mode="widthFix"></image>
					</view>
					<view class="tips-item">
						<image src="https://file-jimei.oss-cn-shenzhen.aliyuncs.com/static/user-base/1.png" mode="aspectFill" class="tips-item-image"></image>
						<view class="tips-item-text">网络图片</view>
						<image class="tips-ft" src="@/static/user-base/n.png" mode="widthFix"></image>
					</view>
					
			</view>
			
			<view class="btn-wrapper-pla">
				
			</view>
			<view class="btn-wrapper">
				<u-button v-if="fileList1 == null || fileList1.length == 0" text="继续" color="#837C8B"  shape="circle"  @click="handleSubmit"></u-button>
				<u-button v-else text="继续" color="linear-gradient(#95258d, #440a5f);" shape="circle" @click="handleSubmit"></u-button>
				<view @click="handleSubmit" class="btn-skip">
					跳过
				</view>
			</view>
			<u-safe-bottom></u-safe-bottom>
		</view>
	</view>
</template>

<script>
	import * as UserProfileApi from '@/api/love-user-profile.js'
	import * as UploadApi from '@/api/upload'
	import {
		dateFormat
	} from '@/utils/util.js'
	export default {
		data() {
			return {
				avatarTemp: '',
				fileList1: [{url: ''},{url: ''},{url: ''}],
				avatar: '',
				
			};
		},
		onLoad() {
		},
		methods: {
			async cropperOk(e) {
				const res = await this.uploadFilePromise(e.path)
				this.avatarTemp = ''
				this.avatar = res.url
			},
			async onChooseAvatar({
				detail
			}) {
				uni.chooseImage({
					count: 1,
					success: (res) => {
						this.avatarTemp = res.tempFilePaths[0]
					}
				})
				
			},
			onChooseAvatar(index) {
				const app = this
				uni.chooseImage({
					count: 1,
					success: (res) => {
						app.uploadFilePromise(res.tempFilePaths[0]).then(result => {
							console.log('index: ',index);
							console.log('app.fileList1: ',app.fileList1);
							app.fileList1[index].url = result.url
						})
					}
				})
				
			},
			handleSubmit() {

				
				const userBase = uni.getStorageSync('UserBase') || {}
				userBase.images = this.fileList1.filter(item => item.url)
				if(userBase.images.length == 0){
					userBase.images = [{url: userBase.avatar}]
				}
				uni.setStorageSync("UserBase", userBase)
				this.$push('/sub_pages/user/base/nickname/nickname')
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				console.log("event: ", event);
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result.url,
						thumb: result.thumbUrl
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					UploadApi.uploadFile({
						filePath: url,
						fileType: 'image',
						name: 'file',
						header: {
							Authorization: uni.getStorageSync('AccessToken')
						},
					}).then(result => {
						setTimeout(() => {
							resolve(result.data)
						}, 1000)
					}).catch(err => {
						console.log("err: ", err);
					})
					// UploadApi.uploadFile()
					// let a = uni.uploadFile({
					// 	url: fileUrl + '/file/image', // 仅为示例，非真实的接口地址
					// 	filePath: url,
					// 	name: 'file',
					// 	header: {
					// 		Authorization: uni.getStorageSync('AccessToken')
					// 	},
					// 	formData: {
					// 		user: 'test'
					// 	},
					// 	success: (res) => {
					// 		const result = JSON.parse(res.data)
					// 		setTimeout(() => {
					// 			resolve(result)
					// 		}, 1000)
					// 	}
					// });
				})
			},
		}
	}
</script>

<style lang="scss">
	@import "../base.scss";
	.avatar{
		margin: 100rpx 0 100rpx 0;
	}
	
	.btn-avatar{
		display: flex;
		justify-content: center;
	}
	.nickname {
		width: 690rpx;
		margin: 160rpx auto 120rpx;
		display: flex;
		justify-content: center;

		&-input {
			background-color: #FFFFFF;
			color: #FFFFFF;
		}
		
		/deep/.u-upload__wrap {
			justify-content: space-between;
			flex-wrap: wrap;
			&::after {
				content: '';
				width: 200rpx;
				display: block;
			}
		}

		/deep/.u-upload__button {
			background-color: rgba(#6A6A6A, .6) !important;
			border-radius: 10rpx !important;
			border: 4rpx rgba(#BBBBBB, .5) solid;
		}
	}


	.tips {
		display: flex;
		width: 640rpx;
		margin: 10rpx auto;
		justify-content: space-between;
		
	
		&-item {
			width: 104rpx;
			font-size: 24rpx;
			text-align: center;
			position: relative;
			&-image {
				width: 104rpx;
				height: 104rpx;
			}
			
			&-text {
				white-space: nowrap;
			}
	
		}
		.tips-ft{
			width: 20rpx;
			position: absolute;
			right: 6rpx;
			top: 82rpx;
			border-radius: 50%;
		}
	}
	
	.btn-avatar{
		display: flex;
		justify-content: center;
		align-items: center;
		width: 320rpx;
		height: 320rpx;
		background: rgba(15, 0, 32, 0.5);
		border: 2rpx solid rgb(151, 99, 157);
		
		border-radius: 10rpx;
		overflow: hidden;
		
		.btn-avatar-plus{
			width: 72rpx;
			height: 72rpx;
		}
		.avatar-img{
			width: 320rpx;
			height: 320rpx;
			border-radius: 10rpx;
		}
	}
	
	.btn-skip{
		font-size: 32rpx;
		text-align: center;
		margin-top: 64rpx;
		color: #FFFFFF;
	}
	
	.btn-avatar{
		display: flex;
		justify-content: center;
		align-items: center;
		width: 200rpx;
		height: 200rpx;
		background: rgba(15, 0, 32, 0.5);
		border: 2rpx solid rgb(151, 99, 157);
		
		border-radius: 10rpx;
		overflow: hidden;
		
		.btn-avatar-plus{
			width: 72rpx;
			height: 72rpx;
		}
		.avatar-img{
			width: 320rpx;
			height: 320rpx;
			border-radius: 10rpx;
		}
	}
</style>