<template>
	<!-- popup -->
	<view>
		<uni-popup ref="deliveryOrderPopup" type="bottom" @change="changePopup">
			<view class="text-bold text-gray text-lg text-center left-top-sm-bar" data-popup="deliveryOrderPopup" @click="closePopup"><text
					class="cuIcon-close"></text></view>
			<view class="bg-white padding" style="padding-top: 74rpx; min-height: 1000rpx;">
				<view class="text-xxl text-center">发货类型</view>
				<radio-group class="flex padding-tb-sm flex-direction" style="gap: 10rpx">
					<label @click="form.deliveryType = 1">
						<radio class="main-color" :value="1" :checked="form.deliveryType === 1" /><text class="margin-left-sm">发快递/物流</text>
					</label>
					<view style="padding-left: 70rpx;">
						<input type="text" v-model="form.trackingNumber" class="custom-input radius-input" placeholder="请输入快递/物流单号">
					</view>
					<label @click="form.deliveryType = 2">
						<radio class="main-color" :value="2"  :checked="form.deliveryType === 2"/><text class="margin-left-sm">送货上门</text>
					</label>
					<label @click="form.deliveryType = 3">
						<radio class="main-color" :value="3"  :checked="form.deliveryType === 3"/><text class="margin-left-sm">客户自提</text>
					</label>
				</radio-group>
				<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.deliveryRemark"></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
			},
			data: {
				type: Object,
				default: () => {}
			},
			isQuicklyDelivery: {
				type: String,
				default: '0'
			}
		},
		watch: {
			show: {
				handler(newVal) {
					if(newVal) {
						this.$nextTick(() => {
							this.$refs.deliveryOrderPopup.open()
						})
					}
				},
				immediate: true
			}
		},
		data() {
			return {
				form: {
					deliveryType: 1,
					deliveryRemark: '',
					deliveryImages: '',
					trackingNumber: ''
				},
				imgList: [],
				clicked: false
			}
		},
		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() {
				if(this.clicked) return;
				this.clicked = true;
				if(this.form.deliveryType == 1 && !this.form.trackingNumber) {
					uni.showToast({
						title: '请填写物流/快递单号',
						icon: 'none'
					})
					this.clicked = false;
					return
				}
				const isAfterService = this.data.afterServiceRecordList && this.data.afterServiceRecordList.length !== 0
				let res;
				if(isAfterService && this.isQuicklyDelivery === '0') {
					const updateGoodsParams = {
						id: this.data.afterServiceRecordList[0].id,
						workerFeedbackResult: 3, // 已重发/补发
						workerResendType: this.form.deliveryType, // 1-快递/物流
						workerResendTrackingNumber: this.form.trackingNumber, // 必填
						workerResendRemark: this.form.deliveryRemark,
						workerResendImages: this.imgList.length ? this.imgList.toString() : ''
					}
					res = await this.$request.editAfterServiceGoodsRecord(updateGoodsParams);
				} else {
					// 确定方法名
					let reqFunName = "updateOrder", id, isQuicklyDelivery;
					if (this.data.orderDetailId == null) {
						id = this.data.orderMasterId;
					} else {
						reqFunName = "updateDetailOrder";
						id = this.data.orderDetailId;
						isQuicklyDelivery = 1
					}
					const updateOrderParams = {
						...this.form,
						id: id,
						deliveryImages: this.imgList.length ? this.imgList.toString() : ''
					}
					
					if(isQuicklyDelivery) {
						updateOrderParams.isQuicklyDelivery = isQuicklyDelivery;
						updateOrderParams.orderStatus = 3
					}
					
					res = await this.$request[reqFunName](updateOrderParams);
				}
				this.clicked = false;
				if (res.code === 0) {
					this.$emit('confirmFeedback')
				} else {
					uni.showToast({
						title: '操作失败',
						icon: 'none',
						duration: 2000
					})
				}
			}
		}
	}
</script>

<style>
</style>
