<template>
	<view>
		<view class="content" v-for="(item,idx) in orderDetail.goods" :key="idx">
			<view class="goods-title">
				<image class="goods-img" :src="item.goods_image" mode="scaleToFill" />
				<view class="title"> {{ item.goods_name }} </view>
			</view>
			<view class="rate">
				<view class="_label"> 商品评价 </view>
				<u-rate :count="count" v-model="value" size="22"></u-rate>
			</view>
			<!-- accept="all" 可以上传视频是配置该字段 -->
			<!-- 			<u-upload :previewFullImage='true' @clickPreview="openvideo" accept="all" :fileList="item.list" @afterRead="afterRead" @delete="deletePic" :name="idx" multiple
				:maxCount="10"> -->
			<u-upload accept=" image/*,video/*" :fileList="fileList0" @afterRead="afterRead" @delete="deletePic" name="0" multiple :maxCount="10"
				>
				<view class="upload-photo">
					<u-icon name="camera" size="26"></u-icon>
					<text>添加图片/视频</text>
				</view>
			</u-upload>
			<view class="_text">
				<u--textarea border="none" v-model="value1" height="150" placeholder="展开说说对商品的想法"></u--textarea>
			</view>
		</view>
		<button class="btn-1" @click="submit">提交评价</button>   
	</view>
</template>

<script>
	import list from '../../uview-ui/libs/config/props/list';
	export default {
		data() {
			return {
				count: 5,
				value: 5,
				value1: "",
				status: "",
				orderDetail: {
					goods:[]
				},
				fileList0: [],
				goods_name: ''
			};
		},
		onLoad(options) {
			this.status = options.orderId;
			console.log(JSON.parse(options.goods));
			this.orderDetail.goods.push(JSON.parse(options.goods))
			console.log(this.orderDetail.goods);
			// this.getDetail();
		},
		methods: {
			openvideo(url, lists, name) {
				console.log(url, lists, name);
			},
			//提交评价
			submit() {
				let imgList = [];
				let videoList = []
				this.fileList0.map((item) => {
					console.log(item);
					if (item.url.slice(-4) == '.png' || item.url.slice(-4) == '.jpg'){
						imgList.push(item.url);
					}
					if (item.url.slice(-4) == '.mp4'){
						videoList.push(item.url);
					}
				});
				uni.$u.http
					.post(`/api/order/comment`, {
						order_goods_id: this.orderDetail.goods[0].id,
						content: this.value1,
						score: this.value,
						image: imgList.join(","),
						video: videoList.join(","),
					})
					.then((res) => {
						uni.showToast({
							title: "评价成功",
							icon: "none",
						});
						let timer = setTimeout(() => {
							uni.navigateBack({
								delta: 1,
							});
							clearTimeout(timer);
						}, 1000);
					});
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1);
			},
			// deletePic(event) {
			// 	this.orderDetail.goods[event.name].list.splice(event.index, 1);
			// 	//触发响应式
			// 	let name = this.orderDetail.goods[event.name].goods_name + " "
			// 	this.orderDetail.goods[event.name].goods_name = name.substring(-2)
			// },
			// async afterRead(event) {
			// 	console.log(event);
			// 	var that = this
			// 	let lists = [].concat(event.file);
			// 	let imglist = [].concat(that.orderDetail.goods[event.name].list)
			// 	let fileListLen = that.orderDetail.goods[event.name].list.length;
			// 	// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
			// 	// lists.map((item) => {
			// 	// 	this.orderDetail.goods[event.name].list.push({
			// 	// 		...item,
			// 	// 		status: "uploading",
			// 	// 		message: "上传中",
			// 	// 	});
			// 	// });

			// 	that.orderDetail.goods[event.name].list.push({
			// 		...event.file[0],
			// 		status: "uploading",
			// 		message: "上传中",
			// 	});
			// 	console.log(that.orderDetail.goods[event.name].list);
			// 	// console.log()
			// 	const result = await that.uploadFilePromise(event.file[0].url);
			// 	console.log(result);
			// 	let item = that.orderDetail.goods[event.name].list[fileListLen];
			// 	that.orderDetail.goods[event.name].list.splice(
			// 		fileListLen,
			// 		1,
			// 		Object.assign(item, {
			// 			status: "success",
			// 			message: "",
			// 			url: result,
			// 		})
			// 	);
			// 	let imgs = [].concat(that.orderDetail.goods[event.name].list)
			// 	// that.$set(that.orderDetail.goods[event.name],"list",imgs)
			// 	this.$set(that.orderDetail.goods[event.name], "length", imgs.length)
			// 	that.orderDetail.goods[event.name].length = imgs.length
			// 	that.orderDetail.goods[event.name].list = imgs


			// 	//触发响应式
			// 	let name = that.orderDetail.goods[event.name].goods_name + " "
			// 	that.orderDetail.goods[event.name].goods_name = name.substring(-2)
			// },
			// 新增图片
			async afterRead(event) {
				console.log(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,
					});
				});
				console.log('list', this[`fileList${event.name}`]);
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url);
					console.log(result);
					let item = this[`fileList${event.name}`][fileListLen];
					this[`fileList${event.name}`].splice(
						fileListLen,
						1,
						Object.assign(item, {
							status: "success",
							message: "",
							url: result,
						})
					);
					fileListLen++;
					console.log('list', this[`fileList${event.name}`]);
				}
			},
			uploadFilePromise(url) {
				console.log(url);
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: `${uni.$u.http.config.baseURL}/api/upload/images`, // 仅为示例，非真实的接口地址
						filePath: url,
						name: "file",
						formData: {
							user: "test",
						},
						success: (res) => {
							setTimeout(() => {
								resolve(JSON.parse(res.data).data.filePath);
								// resolve(res.data.data);  
							}, 1000);
						},
					});
				});
			},
			getDetail() {
				uni.$u.http.get(`/api/order/read?id=${this.status}`).then((res) => {
					this.orderDetail = res;
					this.orderDetail.goods.forEach((item) => {
						item.list = []
						item.length = 0
					})
					console.log(this.orderDetail);
				});
			},
		},
		mounted() {
			uni.setNavigationBarTitle({
				title: "评价",
			});
		},
	};
</script>

<style lang="scss" scoped>
	::v-deep .u-upload__wrap__preview__image {
		width: 180rpx;
		height: 180rpx;
	}

	.content {
		background-color: #fff;
		padding: 40rpx 24rpx;

		.goods-title {
			display: flex;
			justify-content: flex-start;
			align-items: center;

			.goods-img {
				width: 52rpx;
				height: 52rpx;
				border-radius: 6rpx;
			}

			.title {
				font-size: 24rpx;
				color: rgba(51, 51, 51, 0.5);
				margin-left: 20rpx;
			}
		}

		.rate {
			display: flex;
			align-items: center;
			margin-bottom: 20rpx;

			._label {
				font-size: 32rpx;
				font-weight: 600;
				margin-right: 22rpx;
			}
		}

		._text {
			margin-top: 32rpx;
			padding-top: 26rpx;
			border-top: 2rpx solid rgba(151, 151, 151, 0.1);
		}

		.upload-photo {
			width: 180rpx;
			height: 180rpx;
			border: 2rpx dashed #979797;
			border-radius: 8rpx;
			display: flex;
			align-items: center;
			flex-direction: column;
			justify-content: center;
			font-size: 24rpx;
			color: rgba(34, 34, 34, 0.8);
		}

		::v-deep .u-textarea {
			background-color: #fff;
		}
	}

	.btn-1 {
		background-color: #f83224;
		color: #fff;
		border-radius: 40rpx;
		height: 80rpx;
		line-height: 80rpx;
		width: 90%;
		margin: 0 auto;
		margin-top: 70rpx;
		font-size: 32rpx;
	}
</style>