<template>
	<view class="cu-modal" :class="show?'show':''">
		<view class="cu-dialog bg-white">
			<view class="cu-bar">
				<view class="content">拒绝售后</view>
				<view class="action" data-modal="rejectAfterSale" @click="hideModal">
					<text class="cuIcon-close text-red"></text>
				</view>
			</view>
			<view class="padding text-left">
				<view>
					<view class="flex justify-start">
						<view><text class="text-red">*</text>原因选择：</view>
						<radio-group @change="changeReasonRadio">
							<label class="radio margin-right-sm">
								<radio style="transform:scale(0.7)" class="main-color" value="1" :checked="data.reasonType === 1"/>
								<text>客户原因</text>
							</label>
							<label class="radio">
								<radio style="transform:scale(0.7)" class="main-color" value="2" :checked="data.reasonType === 2"/>
								<text>师傅原因</text>
							</label>
							<label class="radio">
								<radio style="transform:scale(0.7)" class="main-color" value="3" :checked="data.reasonType === 3"/>
								<text>其他</text>
							</label>
						</radio-group>
					</view>
					<view class="margin-top">
						<textarea style="width: 100%; height: 200rpx;" class="solid radius text-left padding-sm"
							v-model="remark" maxlength="-1"
							placeholder="拒绝原因：发生纠纷，建议了解清楚客户诉求，积极协商和平处理（让客户撤单更佳）。否则升级重大客诉可能被平台处罚！确实属客户问题且无法协商请输入具体情况！"></textarea>
					</view>
				</view>
				<!-- 上传图片 -->
				<view class="padding-top">
					<view class="grid col-3 grid-square flex-sub margin-top-sm">
						<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" @click.stop="chooseImgList(e, imgList)" v-if="imgList.length < 3">
							<text class='cuIcon-cameraadd'></text>
						</view>
					</view>
				</view>
			</view>
			<view class="cu-bar solid-top">
				<view class="action margin-0 flex-sub text-black" data-modal="rejectAfterSale" @click="hideModal">取消</view>
				<view class="action margin-0 flex-sub text-main-color solid-left" data-modal="rejectAfterSale"
					@click="submit">确认</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'rejectAfterSale',
		props: {
			show: {
				type: Boolean,
				default: false
			},
			data: {
				type: Object,
				default: () => {}
			}
		},
		data() {
			return {
				reasonType: null,
				remark: null,
				imgList: []
			}
		},
		methods: {
			hideModal(e) {
				this.resetData();
				this.$emit('close', e);
			},
			resetData() {
				this.data = null;
				this.reasonType = null;
				this.remark = null;
			},
			changeReasonRadio(e) {
				this.reasonType = e.detail.value;
			},
			chooseImgList(e, imgList) {
				uni.chooseMedia({
					count: 3 - imgList.length, //默认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) => {
								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)
						}
					}
				})
			},
			async submit(e) {
				if(!this.remark) {
					uni.showToast({
						icon: 'none',
						duration: 3000,
						title: '请输入拒绝原因'
					})
					return
				}
				let res = await this.$request.editAfterServiceRecord({
					id: this.data.afterServiceRecordList[0].id,
					workerFeedbackReasonType: this.reasonType,
					workerFeedbackReason: this.remark,
					workerFeedbackResult: 0,
					workerFeedbackImages: this.imgList.length ? this.imgList.toString() : '',
					updateBy: 2
				});
				if (res && res.code === 0) {
					uni.showToast({
						icon: 'success',
						duration: 1000
					})
					this.hideModal(e);
					this.$emit('confirmFeedback');
					return;
				}
				uni.showToast({
					icon: 'error',
					duration: 1000
				})
			}
		},
	}
</script>

<style scoped>
	.inline-input {
		flex-basis: 25%;
	}
</style>
