<template>
	<view class="content">
		<cu-custom :isBack="true" bgColor="bg-white">
			<block slot="content">
				评价
			</block>
		</cu-custom>
		<view class="">
			<view class="bg-ff margin-xs padding" style="border-radius: 20rpx;">
				<u-form :model="form" ref="myForm" :label-style="labelStyle">
					<u-form-item :label="item.dimensionName" required label-width="140"
						v-for="item in commentDimensionList.cateDimensionVoList" :key="item.id">
						<u-rate :count="count" v-model="item.dimensionScore" active-color="#FC6035 " size="38"
							gutter="40">
						</u-rate>
						<view class="color-66">
							{{item.dimensionScore}} <text>分</text>
						</view>
					</u-form-item>
					<view class="">
						<u-form-item style="position: relative;">
							<u-input v-model="form.intro" placeholder="请输入评价内容" height="220"
								style="background-color: #FAFAFA;padding: 20rpx;border-radius: 10rpx;font-size: 30rpx;color:#333;"
								maxlength="100" type="textarea" :clearable="false" />
							<view class="" style="position: absolute;bottom: 20rpx;right: 40rpx;">
								<text>{{form.intro.length}}</text><text class="color-99">/100</text>
							</view>
						</u-form-item>
						<view class="color-99 font-26">
							评价内容满<text style="color: #FC6035;">10</text> 个字得<text
								style="color: #FC6035;">{{commentDimensionList.commentCoin}}</text>优金豆
						</view>
					</view>
					<view class="" style="margin-top: 84rpx;">
						<view class="flex align-center">
							<text class="color-11 text-bold " style="font-size: 32rpx;">添加视频/图片</text>
							<text class="color-99 margin-left">(最多9张)</text>
						</view>
						<view class="margin-top">
							<view class="flex flex-wrap">
								<view class="justify-between" @click="changePhoto" v-if="imgArr.length<1"
									style="border:1rpx dashed #999;width: 150rpx;height:150rpx;flex-direction: column;border-radius: 10rpx;margin-right: 17rpx;">
									<view class="" style="width: 64rpx;height: 64rpx;margin:24rpx auto;">
										<image src="../../static/video_icon.png" mode="widthFix" style=""></image>
									</view>
									<view class=""
										style="background-color: #EEE;border-radius: 10rpx;height: 39rpx;text-align: center;padding: 0 10rpx;">
										15s
									</view>
								</view>
								<view class="bg-f8 flex align-center justify-center margin-bottom-xs"
									v-for="(item,index) in imgArr" :key="index" @tap.stop="$videoList(index,imgArr,'resourceType')"
									style="position: relative;width: 150rpx;height: 150rpx;"
									:style="{'margin-right':  (index%3==0 &&index!=0)?'0rpx':'17rpx'}">
									<video :src="item.url" v-if="item.kind=='video'" :controls="false"
										style="position: relative;width: 150rpx;height: 150rpx;"></video>
									<image :src="item.url" v-else-if="item.kind=='image'"
										style="position: relative;width: 150rpx;height: 150rpx;">
									</image>
									<image src="../../static/close-r_icon.png"
										style="width: 40rpx;height: 40rpx;position: absolute;top: 10rpx;right: 10rpx;"
										@tap.stop="delImg(index)"></image>
								</view>
								<view class="flex align-center  justify-center  margin-bottom-xs" v-if="imgArr.length<9"
									@click="changePhoto"
									style="border:1rpx dashed #999;width: 150rpx;height: 150rpx;border-radius: 10rpx;margin-right: 17rpx;">
									<image src="../../static/camera_icon.png" mode="widthFix"
										style="width: 64rpx;height: 64rpx;"></image>
								</view>
							</view>

						</view>
					</view>
					<view class="color-99 font-26">
						上传视频或图片得<text style="color: #FC6035;">{{commentDimensionList.videoImageCoin}}</text>优金豆
					</view>
					<view class="flex align-center margin-top">
						<view class="bg-f8 " :style="{'background-color':form.chooseFlag?'#FC6035':'' }"
							style="border-radius: 50%;padding: 5rpx;border: 1rpx solid #CCCCCC;width: 40rpx;height: 40rpx;"
							@click="form.chooseFlag=!form.chooseFlag">
							<u-icon name="checkbox-mark" color="#fff" v-if="form.chooseFlag"></u-icon>
						</view>
						<view class="margin-left-sm">
							匿名评价
						</view>
					</view>
				</u-form>
			</view>
		</view>
		<view class="submit" @click="save()" :style="{'background-color':formFalg?'#FC6035':'#E0E0E0'}">提交</view>
		<com-select-picture :show="actionSheetShow" :length="maxLength-imgArr.length"
			@addProperties="chooseImageSuccess" @cancelSheet="cancelSheetClick"></com-select-picture>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				switchVal: '',
				maxResourceList: false,
				maxLength: 9,
				imgArr: [],
				actionSheetShow: false,
				labelStyle: {
					fontSize: '28rpx',
					color: '#111',
					fontWeight: 'bold'
				},
				count: 5,
				value: 2,
				form: {
					rate1: '',
					rate2: '',
					rate3: '',
					value: '',
					intro: '',
					resourceList: [],
					chooseFlag: false,
					isAnonymous: 0
				},
				chooseFlag: false,
				categoryId: '24',
				goodsId: '1',
				commentDimensionList: [],
			}
		},
		onReady() {},
		computed: {
			formFalg() {
				let flag = false
				if (this.commentDimensionList.cateDimensionVoList) {
					let flag = false
					for (var i = 0; i < this.commentDimensionList.cateDimensionVoList.length; i++) {
						if (this.commentDimensionList.cateDimensionVoList[i].dimensionScore < 1) {
							flag = false
							return
						} else {
							flag = true
						}
					}
					return flag
				}
			}
		},
		onLoad() {
			this.getCommentDimension()
		},
		methods: {
			// 获取评价纬度
			getCommentDimension() {
				const param = {
					categoryId: this.categoryId,
					goodsId: this.goodsId
				}
				this.$u.api.getCommentDimensionInfo(param).then(res => {
					console.log(res)
					if (res.code === 200) {
						this.commentDimensionList = res.data
					}
				})
			},
			// 提交评论
			submit() {
				console.log('可以提交评论')
				const dimensionList = this.commentDimensionList.cateDimensionVoList.map(item => {
					return {
						'dimensionId': item.dimensionId,
						'score': item.dimensionScore
					}

				})
				const resourceList = this.imgArr.map(item => {
					return {
						'type': item.type,
						'url': item.url,
						'faceImg': item.url
					}
				})
				const param = {
					memberId: uni.getStorageSync('userInfo').memberId, //会员id
					serverId: uni.getStorageSync('tempAppraise').serverId, //服务人员id
					orderId:  uni.getStorageSync('tempAppraise').orderId, //订单id
					merchantId:  uni.getStorageSync('tempAppraise').merchantId, //商户id
					goodsId:  uni.getStorageSync('tempAppraise').goodsId, //商品id
					content: this.form.intro, //评价内容
					goodsCommentResourceList: resourceList,
					goodsCommentDimensionList: dimensionList
				}
				//是否匿名
				if (this.form.chooseFlag) {
					param.isAnonymous = 1
				} else {
					param.isAnonymous = 0
				}
				console.log(param)
				this.$u.api.saveMemberGoodsComment(param).then(res => {
					console.log(res)
					if (res.code === 200) {
						// this.$api.msg('评论成功')
						uni.removeStorageSync('tempAppraise')
						setTimeout(() => {
							uni.navigateBack({
								delta: 1
							})
						}, 1000)


					}
				})
			},
			// 验证条件是否满足
			save() {
				this.commentDimensionList.cateDimensionVoList.every(item => {
					if (item.dimensionScore < 1) {
						this.$api.msg(`请给${item.dimensionName}打分`)
					} else {
						this.submit()
					}
				})
			},

			changePhoto() {
				this.actionSheetShow = true;
			},
			//上传成功
			chooseImageSuccess(url) {
				this.imgArr.push(url)
				this.$api.msg("上传成功")
				if (this.form.resourceList.length > this.maxLength) {
					this.maxResourceList = true
				}
			},
			delImg(index) {
				this.imgArr.splice(index, 1)
				if (this.form.resourceList.length <= this.maxLength) {
					this.maxResourceList = false
				}
			},
			//取消上传
			cancelSheetClick() {


				this.actionSheetShow = false;
			},

		}
	}
</script>

<style scoped lang="scss">
	.active {
		background-color: #FC6035;
	}

	.submit {
		color: #fff;
		margin: 50rpx auto;
		text-align: center;
		line-height: 90rpx;
		width: 600rpx;
		height: 90rpx;

		border-radius: 45rpx 45rpx 45rpx 45rpx;
	}
</style>
