<template>
	<view>
		<use-tabbar :tabbar="false" />
		<view class="margin-bottom-xl" v-if="fetch && fetch.info">
			<view class="padding margin-lr margin-tb-sm bg-main border-radius">
				<view class="goods-area"  v-for="(item, index) in fetch.info.detail"
				:key="index">
					<view class="dflex pos-r">
						<view class="img" @click="Dever.pic.preview([item.pic], item.pic)">
							<image :src="item.pic" mode="aspectFit"></image>
						</view>
						<view class="margin-left-sm"><text class="clamp-2">{{ item.name }}</text>
							<view class="ft-dark fs-xs padding-top-xs"><text class="margin-right">× {{item.num}}</text>{{ item.sku_name || '&nbsp;&nbsp;' }}</view>
							<view class="margin-top-sm"><text class="price">{{ item.cash_text }}</text></view>
						</view>
					</view>
				</view>
			</view>
			<view class="evaluate-kps">
				<view class="padding margin-lr margin-tb-sm bg-main border-radius dflex-b">
					<view><text>宝贝评分</text><text
							class="margin-left ft-base fs-xs">{{rate_name}}</text>
					</view>
					<view class="use-rate dflex">
						<u-rate count="5" v-model="rate" @change="setRate"></u-rate>
					</view>
				</view>
			</view>
			<view class="evaluate-area">
				<view class="padding margin-lr margin-tb-sm bg-main border-radius">
					<u--textarea :focus="true" :height="120" v-model="content" border="none"
						placeholder="请输入评价内容"></u--textarea>
						<!--
					<view class="use-upload dgrid dgrid-col-3 pos-r">
						<u-upload :fileList="pic" @afterRead="afterRead" @delete="deletePic" name="1" multiple
							:maxCount="5"></u-upload>
					</view>-->
				</view>
			</view>
			<view>
				<view class="padding margin-lr margin-tb-sm bg-main border-radius dflex-b">
					<text>公开显示您的头像、昵称</text>
					<view>
						<u-switch v-model="open" @change="setOpen" activeColor="#f56c6c"></u-switch>
					</view>
				</view>
			</view>
			<view class="padding w-full margin-top">
				<view class="dflex-b border-radius-big">
					<view class="tac padding-tb-sm flex1 bg-base" @click="submit">提交评价</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				fetch: {},
				id: 0,
				open: true,
				rate: 5,
				rate_name: '好评',
				content: '',
				pic: [],
			}
		},
		onLoad(options) {
			this.id = options.id;
			if (!this.id) {
				return Dever.goHome()
			}
			this.loadData();
		},
		methods: {
			loadData() {
				this.DeverApi.get(this, 'order.info', {
					id: this.id
				});
			},
			setRate(e) {
				if (e > 4) {
					this.rate_name = '好评';
				} else if (e > 1) {
					this.rate_name = '中评';
				} else {
					this.rate_name = '差评';
				}
			},
			setOpen(e) {

			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1);
			},
			// 新增图片
			async afterRead(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,
						})
					);
					fileListLen++;
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: "http://192.168.2.21:7001/upload", // 仅为示例，非真实的接口地址
						filePath: url,
						name: "file",
						formData: {
							user: "test",
						},
						success: (res) => {
							setTimeout(() => {
								resolve(res.data.data);
							}, 1000);
						},
					});
				});
			},
			submit() {
				if (!this.content) {
					return this.Dever.alert('请输入评价内容');
				}
				//const pic = this.pic.join(',');
				const pic = '';
				this.DeverApi.post('order.review', {content:this.content,pic:pic,open:this.open,rate:this.rate,id:this.id}, res => {
					this.Dever.success('评价成功', () => {
						this.Dever.location('order/info?id=' + this.id, 'go');
					});
				});
			},
		}
	}
</script>

<style lang="scss">
	.goods-area:last-child {
		margin-bottom: 0;
	}

	.goods-area image {
		width: 180rpx;
		height: 180rpx;
	}

	textarea {
		width: 300px;
		height: 150px;
		display: block;
		position: relative;
		font-size: 16px;
		line-height: normal;
		white-space: pre-wrap;
		word-break: break-all;
	}

	.dgrid-col-3 {
		grid-gap: 9px 9px;
		grid-template-columns: repeat(3, 1fr);
	}

	.use-upload .item {
		width: 200rpx;
		height: 200rpx;
		border: 1px solid #f0f0f0;
	}
</style>