<template>
	<!-- popup -->
	<view>
		<uni-popup ref="reFinishPopup" type="bottom" @change="changePopup">
			<view class="text-bold text-gray text-lg text-center left-top-sm-bar" data-popup="reFinishPopup" @click="closePopup"><text
					class="cuIcon-close"></text></view>
			<view class="bg-white padding" style="padding-top: 74rpx;">
				<view class="text-xxl text-center">售后不同意</view>
				<view class="text-lg text-left">订单将提交平台，由平台介入处理纠纷！</view>
				<view>
					<div class="grid col-3 grid-square">
						<view class="bg-img" v-for="(item,index) in imgList" :key="index"
							@tap="viewImage($event, imgList)" :data-url="item">
							<image :src="item" mode="aspectFill"></image>
							<view class="cu-tag bg-red" @tap.stop="delImg($event, imgList)" :data-index="index">
								<text class='cuIcon-close'></text>
							</view>
						</view>
						<view class="solids" @tap="chooseImage" v-if="imgList.length < 6">
							<text class='cuIcon-cameraadd'></text>
						</view>
					</div>
				</view>
				<view style="margin-bottom: 10rpx;">
					<textarea style="width: 100%;box-sizing: border-box;" class="custom-input radius-input" placeholder="请输入理由或备注" cols="30" rows="10" v-model="form.redoCompleteRemark"></textarea>
				</view>
				<view class="cu-bar bg-white solid-top">
					<view class="action margin-0 flex-sub text-black" @tap="closePopup">取消</view>
					<view class="action margin-0 flex-sub text-main-color solid-left" @click="Submit">确认</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		emits: ['confirmFeedback', 'close'],
		props: {
			show: {
				type: Boolean,
				default: false
			},
			orderAfterId: {
				type: String | Number,
				default: ''
			}
		},
		watch: {
			show: {
				handler(newVal) {
					if(newVal) {
						this.$nextTick(() => {
							this.$refs.reFinishPopup.open()
						})
					}
				},
				immediate: true
			}
		},
		data() {
			return {
				form: {
					redoCompleteRemark: '',
					redoCompleteImages: '',
				},
				imgList: [],
			}
		},
		methods: {
			chooseImage(e) {
				uni.chooseMedia({
					count: 1, //默认9
					mediaType: ['image'],
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: (res) => {
						uni.showLoading({
							title: '上传中',
							mask: true
						});
						res.tempFiles.forEach((fileObj, index) => {
							this.$request.uploadFile(fileObj.tempFilePath).then((url) => {
								this.imgList.push(url);
								if (index === res.tempFiles.length - 1) {
									uni.hideLoading();
								}
							});
						});
					}
				});
			},
			viewImage(e, imgList) {
				uni.previewImage({
					urls: imgList,
					current: e.currentTarget.dataset.url
				});
			},
			delImg(e, imgList) {
				uni.showModal({
					title: '',
					content: '确定要删除这张图片吗？',
					cancelText: '取消',
					confirmText: '确定',
					success: res => {
						if (res.confirm) {
							imgList.splice(e.currentTarget.dataset.index, 1)
						}
					}
				})
			},
			changePopup(e) {
				console.log(e);
				if(!e.show) {
					this.closePopup()
				}
			},
			closePopup() {
				this.$emit('close')
			},
			changeRadio(e) {
				this.form.deliveryType = e.target.value
			},
			async Submit() {
				const updateOrderParams = {
					id: this.orderAfterId,
					customerDisagreeReason: this.form.redoCompleteRemark,
					customerDisagreeImages: this.imgList.length ? this.imgList.toString() : '',
					customerFinalCheck: 0
				}
				
				let res = await this.$request.editAfterServiceRecord(updateOrderParams)
				
				if (res.code === 0) {
					uni.showToast({
						icon: 'success',
						title: '提交成功',
						duration: 1000
					})
					this.$emit('confirmFeedback')
				} else {
					uni.showToast({
						title: '操作失败',
						icon: 'none',
						duration: 2000
					})
				}
			}
		}
	}
</script>

<style>
</style>