<template>
	<view class="mg-lr-24">
		<view class="white-bg-wrap mg-bot-26">
			<view class="light-black fs24 mg-bot-20">商品信息</view>
			<view class="u-f u-f-jcfs u-f-aic mg-bot-24">
				<view style="flex: 1">
					<image class="border-radius-9 mg-right-10" :src="shopSureBillInfo.goodsImage"
						style="width: 128rpx;height: 128rpx;" :mode="'widthFix'"></image>
				</view>
				<view class="u-f u-f-fdc" style="flex: 3">
					<view class="fs28 light-black fw400 mg-bot-10 two-line-ellipsis">{{shopSureBillInfo.goodsName}}
					</view>
					<view class="mg-bot-10 u-f u-f-aic u-f-jcfs">
						<text class="light-gray fs24 mg-right-8">{{shopSureBillInfo.goodsSpecification}}</text>
					</view>
					<view class="u-f u-f-aic u-f-jcsb">
						<view class="u-f u-f-jcfs u-f-aic" style="margin-right: 96rpx;">
							<view class="mg-right-10">
								<text class="fs24 main-color"></text>
								<text
									class="fs32 main-color fw600">{{shopSureBillInfo.goodsUnitPrice || shopSureBillInfo.goodsPrice}}</text>
							</view>
						</view>
						<view>
							<uni-number :min="1" :disabled="true" :value="shopSureBillInfo.goodsNum"></uni-number>
						</view>
					</view>
				</view>
			</view>
		</view>

		<common-title :msg="'退款信息'"></common-title>

		<view class="white-bg-wrap mg-top-20">
			<view class="u-f u-f-jcsb u-f-aic fs28 mg-bot-30">
				<text class="light-gray">商品件数</text>
				<text class="light-black">{{shopSureBillInfo.goodsNum}}件</text>
			</view>
			<view class="u-f u-f-jcsb u-f-aic fs28 mg-bot-30">
				<text class="light-gray">退款原因</text>
				<view class="u-f u-f-aic u-f-jcfe" @click="handleShowPopup">
					<text class="light-gray mg-right-10">{{refundType}}</text>
					<image class="sg-gray-arrow" src="../.././static/common/sg_right_arrow.png"></image>
				</view>
			</view>
			<view class="u-f u-f-jcsb u-f-aic fs28 mg-bot-8 mg-bot-50">
				<text class="light-gray">退款金额</text>
				<text class="light-black">￥{{shopSureBillInfo.paymentAmount || shopSureBillInfo.actualAmount}}</text>
			</view>
			<view class="light-gray fs24 mg-bot-24" style="text-align: right;">最终退款金额以审核后为准</view>
			<view class="mg-bot-18">
				<view class="mg-bot-20">问题描述</view>
				<textarea
					style="width: 100%;min-height: 230rpx;background-color: #f4f5f7;padding: 20rpx 24rpx;box-sizing: border-box;"
					auto-height placeholder-style="color:#999999;font-size: 24rpx;" placeholder="请描述申请退款的具体原因..."
					v-model="applyContent" />
			</view>
			<view class="u-f u-f-aic u-f-jcc mg-bot-250">
				<u-upload width="150rpx" height="150rpx" :fileList="fileList" @afterRead="afterRead" @delete="deletePic"
					name="1" multiple :maxCount="10" :previewFullImage="true"></u-upload>
			</view>
		</view>

		<!-- <view class="fix-bot u-f u-f-aic u-f-jcc" @click="goToRefundPage"> -->
		<view class="fix-bot u-f u-f-aic u-f-jcc" @click="submitFeedback">
			<view class="line-gradient-btn" style="width: calc(100% - 48rpx)">提交</view>
		</view>

		<uni-popup :position="'bottom'" class="updatePopup" :show="showBotPopup" @hidePopup="handleHidePopup">
			<view class="sys-mes-nofity">

				<view class="u-f u-f-jcsb u-f-aic mg-bot-40" style="padding: 0 34rpx;">
					<text class="fs36 light-black fw600">选择退款原因</text>
					<!-- 					<image src="../../static/common/gray_close.png" style="width: 44rpx;height:44rpx;"
						@click="handleHidePopup"></image> -->
					<view class="closeBtn" @click="handleHidePopup" style="width: 44rpx;height:44rpx;">
						<image src="/static/common/guanbi.png" mode="" style="width: 44rpx;height:44rpx;"></image>
					</view>
				</view>
				<view style="padding: 0 34rpx;">
					<view class="u-f u-f-jcsb u-f-aic mg-bot-50" v-for="(item, index) in refundTypeList" :key="index"
						@click="radioChange(index)">
						<view class="fs32 fw400 light-black">{{item.name}}</view>
						<!-- <view :class="[current === index ? 'select-radio' : 'unselect-radio']"> -->
						<view :class="[current === index ? 'unselect-radio1' : 'unselect-radio']">
							<!-- <view class="unselect-radio"> -->
							<image v-if="current === index" src="../../static/common/xuanzhong.png" mode=""
								style="width: 100%; height: 100%;"></image>
						</view>
					</view>
				</view>
				<!-- </view> -->
				<view class="bot u-f u-f-jcc u-f-aic" @click="comfirmRefundType"><text class="confirm">确定</text></view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import uniNumber from "../.././components/uni-number-box/uni-number-box.vue"
	import commonTitle from '../../components/common-title/common-title.vue'
	export default {
		components: {
			commonTitle,
			uniNumber
		},
		data() {
			return {
				applyContent: '',
				afterSalesType: '',
				shopSureBillInfo: {},
				fileList: [],
				refundTypeList: [],
				showBotPopup: false,
				current: 0,
				refundType: '请选择'
			}
		},
		onLoad(options) {
			let data = options.data ? JSON.parse(decodeURIComponent(options.data)) : ""
			this.shopSureBillInfo = data || {}
			console.log("shopSureBillInfo", this.shopSureBillInfo)
			this.getRefundType()
		},
		methods: {
			// goToRefundPage() {
			// 	this.User.navigate(
			// 		{ url: '/pages/refund/refund' }
			// 	)
			// },
			// 删除图片
			deletePic(event) {
				this[`fileList`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList`].length
				lists.map((item) => {
					this[`fileList`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					console.log('LMG', result)
					let item = this[`fileList`][fileListLen]
					this[`fileList`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			// 退款类型
			async getRefundType(event) {
				this.$http.get('/api/client/order/refundType').then(res => {
					let resData = res.data.data
					console.log('resData', resData)
					this.refundTypeList = resData.map(item => {
						return {
							value: item.value,
							name: item.label
						}
					}) || []
					console.log('refundTypeList', this.refundTypeList)
				})
			},
			uploadFilePromise(url) {
				let baseUrl = this.$config.webUrl
				return new Promise((resolve, reject) => {
					uni.uploadFile({
						url: baseUrl + '/api/client/upload',
						filePath: url,
						header: {
							'Content-Type': 'application/json;charset=UTF-8',
							Authorization: uni.getStorageSync('token')
						},
						name: 'file',
						success: (res) => {
							try {
								// 解析返回的数据
								const data = JSON.parse(res.data);
								if (data.data.url != null) {
									resolve(data.data.url);
								} else {
									reject(new Error('Invalid response format'));
								}
							} catch (error) {
								reject(new Error('Failed to parse response data'));
							}
						},
						fail: (error) => {
							// 处理上传失败的情况
							reject(error);
						}
					});
				});
			},
			//提交退款申请
			submitFeedback() {
				let feedbackImage = []
				this.fileList.map(item => {
					feedbackImage.push(item.url)
				})

				// if (this.refundType=='请选择') {
				// 	uni.showToast({
				// 		title: '请选择退款原因',
				// 		icon: 'none'
				// 	})
				// 	return
				// }

				// if (this.fileList.length==0) {
				// 	uni.showToast({
				// 		title: '请上传凭证',
				// 		icon: 'none'
				// 	})
				// 	return
				// }

				// if (this.applyContent.length==0) {
				// 	uni.showToast({
				// 		title: '请输入具体退款原因',
				// 		icon: 'none'
				// 	})
				// 	return
				// }

				let data = {
					storeId: this.shopSureBillInfo.storeId,
					goodsId: this.shopSureBillInfo.goodsId,
					orderId: this.shopSureBillInfo.orderId,
					type: 1, // 类型（1-退款 2-售后）
					applyContent: this.applyContent,
					requestRefundPictureList: feedbackImage
				}

				if (this.refundType !== '请选择') {
					data = Object.assign(data, {
						requestRefundType: this.refundType
					})
				}
				uni.showLoading()
				console.log('LMG', data)
				this.$http.post('/api/client/order/requestRefund', data).then(res => {
					uni.showToast({
						icon: 'none',
						title: res.data.msg
					})
					uni.hideLoading()
					if (res.data.code == 200) {
						uni.navigateTo({
							url: `/pagesB/home-shouhou-result/home-shouhou-result?data=${encodeURIComponent(JSON.stringify(this.shopSureBillInfo))}&status=${1}`
						})
					}
				})
			},
			handleShowPopup(item) {
				if (this.current == 0) {
					this.current = 0
					this.refundType = this.refundTypeList[this.current].value
				}
				this.showBotPopup = true
			},
			handleHidePopup() {
				this.showBotPopup = false
			},
			radioChange(index) {
				this.current = index
			},
			comfirmRefundType() {
				this.handleHidePopup()
				this.refundType = this.refundTypeList[this.current].value
			}
		}
	}
</script>


<style>
	page {
		background-color: #FAFBFF;
	}
</style>

<style lang="scss" scoped>
	.upload-pic {
		width: 224rpx;
		height: 224rpx;
		background: #F6F6F6;
		border-radius: 8rpx;
	}

	.sg-gray-arrow {
		width: 11rpx;
		height: 21rpx;
		margin-top: 3rpx;
	}

	.camera {
		width: 66rpx;
		height: 66rpx;
		background: url(../.././static/common/camera.png) no-repeat center center;
		background-size: 100%;
	}

	.fix-bot {
		width: 100%;
		position: fixed;
		bottom: 28rpx;
		left: 0;
		padding: 10rpx 0;
		background-color: #ffffff;
	}

	.sys-mes-nofity {
		position: relative;
		width: 100%;
		border-radius: 14rpx;
		padding-top: 40rpx;

		.bot {
			margin-top: 72rpx;
			padding-bottom: 20rpx;

			.confirm {
				padding: 10rpx 60rpx;
				border-radius: 30rpx;
				color: #ffffff;
				background-color: #FF4502;
			}
		}
	}

	.updatePopup :deep(.uni-popup-bottom) {
		border-radius: 40rpx 40rpx 0 0;
	}

	.updatePopup :deep(.uni-list-cell) {
		justify-content: flex-start;
		padding-left: 24rpx;
		padding-right: 24rpx;
	}

	.unselect-radio {
		width: 38rpx;
		height: 38rpx;
		background: #fff;
		border: 1rpx solid #979797;
		border-radius: 50%;
		box-sizing: border-box;
	}

	.unselect-radio1 {
		width: 38rpx;
		height: 38rpx;
		// background: #fff;
		// border: 1rpx solid #979797;
		// border-radius: 50%;
	}

	.select-radio {
		width: 44rpx;
		height: 44rpx;
		background: #FB5024;
		border: 1rpx solid #FB5024;
		border-radius: 50%;
		position: relative;

		&:before {
			content: '';
			width: 11px;
			height: 7px;
			position: absolute;
			right: 4px;
			top: 4px;
			border: 1px solid #fff;
			border-top-color: transparent;
			border-right-color: transparent;
			transform: rotate(-45deg);
		}
	}
</style>