<template>
	<!-- 配送员已完成订单列表 -->
	<view class="page1">
		<view class="worker-order-fixed">
			<view class="title">
				<view class="title-sreach">
					<view class="back-btn" @tap="back" :style="{'top': menuButtonInfo.top +'px'}">
						<uni-icons type="left" size="28"></uni-icons>
					</view>
					<view class="title-name" :style="{'padding-top': menuButtonInfo.top +'px'}">
						已完成订单
					</view>
				</view>
			</view>

			<view class="summary-card">
				<view class="summary-label">共计送达</view>
				<view class="summary-count">{{totalCount}}<text>单</text></view>
			</view>

			<view class="search-card">
				<view class="date-picker-wrap">
					<uni-datetime-picker :clear-icon="false" v-model="range" type="daterange" @change="searchList" />
				</view>
				<view class="reset-btn" @tap="resetDateRange">重置</view>
			</view>
		</view>
		<view class="worker-order-fixed-holder"></view>

		<view class="list-wrap">
			<view class="box1" @tap="goDetail(item)" v-for="(item,index) in orderList" :key="index">
				<view class="order-head">
					<view class="type-tag" :class="'type-' + item.deliveryType">
						{{deliveryTypeText(item.deliveryType)}}
					</view>
					<view class="order-code" v-if="item.numberCode">
						#{{orderCodeText(item.numberCode)}}
					</view>
					<view class="done-tag">已送达</view>
				</view>

				<view class="time-row">
					<text v-if="item.mustFinishTime">{{item.mustFinishTime | formatHourMinute}}前送达</text>
					<text v-else>{{item.createTime | formatISOTime}}</text>
				</view>

				<view class="time-detail">
					<view class="time-item" v-if="item.acceptTime">
						<text>接单</text>{{item.acceptTime | formatISOTime}}
					</view>
					<view class="time-item" v-if="item.arriveTime">
						<text>到店</text>{{item.arriveTime | formatISOTime}}
					</view>
					<view class="time-item" v-if="item.getTime">
						<text>取货</text>{{item.getTime | formatISOTime}}
					</view>
					<view class="time-item" v-if="item.finishTime">
						<text>送达</text>{{item.finishTime | formatISOTime}}
					</view>
				</view>

				<view class="remark" v-if="item.remark != '' && item.remark != null">
					备注：{{item.remark}}
				</view>

				<view class="extra-info" v-if="item.deliveryType == 2 || item.deliveryType == 3">
					<text>{{item.allCount != null ? item.allCount : 0}}件</text>
					<text v-if="item.getCodes != null">取件码：{{item.getCodes}}</text>
					<text v-if="item.phoneNumber != null">尾号：{{item.phoneNumber}}</text>
				</view>

				<view class="route-box">
					<view class="route-line">
						<view class="route-dot get">取</view>
						<view class="route-main">
							<view class="route-name">{{item.shopName || '取货点'}}</view>
							<view class="route-address">{{item.shopAddress || '暂无取货地址'}}</view>
						</view>
					</view>
					<view class="route-line">
						<view class="route-dot send">送</view>
						<view class="route-main">
							<view class="route-name">{{item.receiverName || '收货人'}}</view>
							<view class="route-address">{{item.receiverAddress || '暂无收货地址'}}</view>
						</view>
					</view>
				</view>

				<view class="goods-row" v-if="item.goodsList && item.goodsList.length > 0" @tap.stop="productDetail(item)">
					<view class="goods-title">
						商品详情
					</view>
					<view class="goods-desc">
						共{{item.goodsNum}}件，点击查看规格和数量
					</view>
					<uni-icons type="right" size="14" color="#7a8582"></uni-icons>
				</view>

				<view class="fee-row">
					<view class="big-tag" v-if="item.isBig != null && item.isBig == 1">超大/超重</view>
					<view class="fee-text">
						配送佣金 <text>￥{{(item.deliveryFee + item.deliveryFeeMarketplace).toFixed(2)}}</text>
					</view>
					<view class="market-fee" v-if="item.deliveryFeeMarketplace != null">
						含平台￥{{item.deliveryFeeMarketplace}}
					</view>
				</view>
			</view>

			<view class="empty-box" v-if="orderList.length == 0 && loadStatus == 'nomore'">
				暂无已完成订单
			</view>
		</view>

		<uni-popup ref="productPopup" background-color="transparent">
			<view class="product-popup-content">
				<view class="product-popup-header">
					<view class="product-popup-title">商品详情</view>
					<view class="product-popup-subtitle">请核对商品规格和数量</view>
				</view>
				<view class="product-popup-list" v-if="productData != null && productData.length > 0">
					<view class="product-popup-card" v-for="(item1,index1) in productData" :key="index1">
						<view class="product-popup-img">
							<img :src="item1.productPicture" alt="">
						</view>
						<view class="product-popup-info">
							<view class="product-popup-name">
								{{item1.productName}}
							</view>
							<view class="product-popup-spec">
								<text class="product-popup-label">规格</text>
								<text class="product-popup-spec-text">{{item1.specs | delNode}}</text>
							</view>
						</view>
						<view class="product-popup-quantity">
							<view class="product-popup-quantity-num">x{{item1.quantity}}</view>
							<view class="product-popup-quantity-label">数量</view>
						</view>
					</view>
				</view>
				<view class="product-popup-empty" v-else>
					暂无商品详情
				</view>
			</view>
		</uni-popup>

		<view class="bottom-space"></view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				loadStatus: 'more',
				searchForm: {
					status: 3,
					pageNum: 1,
					pageSize: 10,
					order: '',
					regionId: JSON.parse(uni.getStorageSync('area')).id,
					deliveryType: null,
					workerId: uni.getStorageSync('worker').workerId,
					startDate: '',
					endDate: ''
				},
				range: [],
				productData: [],
				totalPages: 1,
				totalCount: 0,
				tuanzhangOrder:[],
				orderList: [],
				currentIndex: 10,
				menuButtonInfo: {}
			}
		},
		onReachBottom() {
			if (this.searchForm.pageNum >= this.totalPages) return;
			this.searchForm.pageNum++;
			this.getList();
		},
		filters: {
			formatHourMinute(value) {
				if (!value) return '';
				const date = new Date(value);
				const day = String(date.getDate()).padStart(2, '0');
				const hour = String(date.getHours()).padStart(2, '0');
				const minute = String(date.getMinutes()).padStart(2, '0');
				return `${day}日${hour}:${minute}`;
			},
			formatISOTime(isoString) {
				const date = new Date(isoString);
				const year = date.getFullYear();
				const month = (date.getMonth() + 1).toString().padStart(2, '0');
				const day = date.getDate().toString().padStart(2, '0');
				const hours = date.getHours().toString().padStart(2, '0');
				const minutes = date.getMinutes().toString().padStart(2, '0');
				const seconds = date.getSeconds().toString().padStart(2, '0');
				return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
			},
			delPlus(data){
				if (data.endsWith("+")) {
				    data = data.slice(0, -1);
				}
				return data;
			},
			delNode(data) {
				let str;
				if (typeof data === 'object' && data !== null) {
					str = JSON.stringify(data);
				} else if (typeof data === 'string') {
					str = data;
				} else {
					str = String(data);
				}
				return str.replace(/[{}"]/g, '');
			}
		},
		onShow() {
			this.menuButtonInfo = uni.getMenuButtonBoundingClientRect()
			this.searchForm.regionId = JSON.parse(uni.getStorageSync('area')).id
		},
		onLoad(option) {
			if (option.index) {
				this.searchForm.searchStatus = option.index
				this.currentIndex = option.index
			}
			if (option.today == 1) {
				const today = this.formatDate(new Date())
				this.range = [today, today]
				this.searchForm.startDate = today
				this.searchForm.endDate = today
			}
			this.getList()
		},
		methods: {
			getList() {
				this.loadStatus = 'loading'
				let that = this
				const requestForm = Object.assign({}, this.searchForm)
				delete requestForm.shopId
				that.tui.request("/mall/delivery/pagebyworkerHistory", "POST", requestForm, false, false).then((
				res) => {
					that.loadStatus = 'nomore';
					if (res.code == 200) {
						if (that.searchForm.pageNum == 1) {
							that.orderList = res.result.records;
						} else {
							that.orderList = [...that.orderList, ...res.result.records]
						}
						for(let i=0;i<that.orderList.length;i++){
							that.orderList[i].goodsNum = 0
							if (that.orderList[i].goodsList && that.orderList[i].goodsList.length) {
								for(let m=0;m<that.orderList[i].goodsList.length;m++){
									that.orderList[i].goodsNum += that.orderList[i].goodsList[m].quantity
								}
							}
						}
						that.totalPages = res.result.pages;
						that.totalCount = res.result.total || that.orderList.length;
						that.$forceUpdate();
					} else {
						that.tui.toast(res.message);
						return;
					}
					uni.hideLoading();
				}).catch((res) => {});
			},
			deliveryTypeText(type) {
				if (type == 1) return '外卖';
				if (type == 2) return '代取快递';
				if (type == 3) return '代跑腿';
				return '配送';
			},
			orderCodeText(numberCode) {
				if (!numberCode) return '';
				if (numberCode.length > 14) return '多订单';
				return numberCode.endsWith("+") ? numberCode.slice(0, -1) : numberCode;
			},
			formatDate(date) {
				const year = date.getFullYear();
				const month = String(date.getMonth() + 1).padStart(2, '0');
				const day = String(date.getDate()).padStart(2, '0');
				return `${year}-${month}-${day}`;
			},
			productDetail(item) {
				this.productData = item.goodsList || [];
				this.$refs.productPopup.open()
			},
			searchList(range) {
				const dateRange = range || this.range || [];
				this.searchForm.startDate = dateRange[0] || '';
				this.searchForm.endDate = dateRange[1] || '';
				this.searchForm.pageNum = 1;
				this.getList();
			},
			resetDateRange() {
				this.range = [];
				this.searchForm.startDate = '';
				this.searchForm.endDate = '';
				this.searchForm.pageNum = 1;
				this.getList();
			},
			tanchuang(v){
				this.tui.toast('该订单为'+(v==1?'配送':'自取')+'单')
			},
			getGroupOrders(id){
				this.tui.request("/mall/order/selectAllOrderByOrderId/"+id, "GET", {}, false, true).then((res) => {
					if (res.code == 200) {
						this.tuanzhangOrder = res.result;
						this.$refs.pintuanPopup.open('bottom')
					} else {
						that.tui.toast(res.message)
						return
					}
					uni.hideLoading()
				}).catch((res) => {})
			},
			openPopup(){
				this.$refs.groupPopup.open()
			},
			mealServing(item){
				let that = this
				uni.showModal({
					title: '提示',
					content: '确定要出餐吗？',
					success: function (res) {
						if (res.confirm) {
							let data = {}
							let url = ''
							if(item.deliveryType == 2){ //自取单
								url = '/mall/order/complete'
							}else if(item.deliveryType == 1){ //外卖单
								url = '/mall/order/shopMakeTime'
							}
							that.tui.request(url, "post", {
								orderId:item.id
							}, false, true).then((res) => {
								if (res.code == 200) {
									that.getList()
								} else {
									that.tui.toast(res.message);
									return;
								}
								uni.hideLoading();
							}).catch((res) => {});
						}
					}
				});
			},
			goDetail(item) {
				uni.navigateTo({
					url: '/package2/shop/orderDetail?type=work&id=' + item.orderId
				})
			},
			handleStatusClick(index) {
				this.searchForm.pageNum = 1
				if (this.currentIndex === index) return;
				this.currentIndex = index;
				this.searchForm.searchStatus = index
				this.getList()
			},
			back() {
				uni.navigateBack()
			},
		}
	}
</script>

<style>
	page {
		width: 100%;
		height: 100%;
		font-size: 24rpx;
		background: #F5F8F5;
		color: #00231C;
	}

	.page1 {
		width: 100%;
		height: 100%;
		font-size: 24rpx;
		position: relative;
		padding-bottom: 40rpx;
	}

	.worker-order-fixed {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		z-index: 1000;
		padding-bottom: 12rpx;
		background: #F5F8F5;
	}

	.worker-order-fixed-holder {
		height: 462rpx;
	}

	.title {
		background: url('https://jewel-shop.oss-cn-beijing.aliyuncs.com/8bc15960c2dc40268e295d6dd23aecce.png') no-repeat;
		width: 100%;
		height: 360rpx;
		background-size: 100% 100%;
	}

	.title-sreach {
		width: 100%;
		display: flex;
		height: 200rpx;
		position: relative;
	}

	.back-btn {
		position: absolute;
		bottom: 0;
		left: 0;
	}

	.title-name {
		padding-top: 110rpx;
		font-size: 36rpx;
		font-weight: 700;
		flex: 1;
		text-align: center;
	}

	.summary-card {
		width: 92%;
		height: 128rpx;
		margin: -156rpx auto 24rpx;
		padding: 24rpx 28rpx;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		border-radius: 28rpx;
		background:
			radial-gradient(circle at 92% 0, rgba(166, 255, 234, 0.78) 0, rgba(166, 255, 234, 0) 170rpx),
			#fff;
		box-shadow: 0 14rpx 34rpx rgba(0, 35, 28, 0.08);
	}

	.summary-label {
		flex: 1;
		color: #61716d;
		font-size: 26rpx;
		font-weight: 700;
	}

	.summary-count {
		color: #00231C;
		font-size: 52rpx;
		font-weight: 900;
		line-height: 64rpx;
	}

	.summary-count text {
		margin-left: 6rpx;
		color: #61716d;
		font-size: 24rpx;
		font-weight: 700;
	}

	.list-wrap {
		width: 92%;
		margin: 0 auto;
		position: relative;
		z-index: 1;
	}

	.search-card {
		width: 92%;
		margin: 0 auto 8rpx;
		padding: 16rpx 18rpx;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		border-radius: 22rpx;
		background: #fff;
		box-shadow: 0 10rpx 24rpx rgba(0, 35, 28, 0.05);
	}

	.date-picker-wrap {
		flex: 1;
		min-width: 0;
	}

	.reset-btn {
		width: 88rpx;
		height: 56rpx;
		margin-left: 14rpx;
		border-radius: 56rpx;
		background: rgba(166, 255, 234, 0.48);
		color: #126255;
		font-size: 24rpx;
		font-weight: 800;
		line-height: 56rpx;
		text-align: center;
	}

	.box1 {
		width: 100%;
		margin: 18rpx auto;
		background: #fff;
		border-radius: 24rpx;
		padding: 22rpx;
		box-sizing: border-box;
		box-shadow: 0 10rpx 28rpx rgba(0, 35, 28, 0.05);
	}

	.order-head {
		display: flex;
		align-items: center;
		min-height: 44rpx;
	}

	.type-tag {
		height: 40rpx;
		line-height: 40rpx;
		padding: 0 16rpx;
		border-radius: 40rpx;
		font-size: 22rpx;
		font-weight: 700;
	}

	.type-1 {
		background: rgba(255, 233, 89, 1);
	}

	.type-2 {
		background: rgba(255, 220, 199, 1);
	}

	.type-3 {
		background: rgba(130, 255, 130, 1);
	}

	.order-code {
		flex: 1;
		min-width: 0;
		margin-left: 14rpx;
		color: #60716c;
		font-size: 22rpx;
		height: 40rpx;
		line-height: 40rpx;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}

	.done-tag {
		height: 38rpx;
		line-height: 38rpx;
		padding: 0 14rpx;
		border-radius: 12rpx;
		background: rgba(166, 255, 234, 0.5);
		color: #0b665a;
		font-size: 22rpx;
		font-weight: 700;
	}

	.time-row {
		margin-top: 12rpx;
		color: #223b36;
		font-size: 28rpx;
		font-weight: 800;
		line-height: 38rpx;
	}

	.time-detail {
		display: grid;
		grid-template-columns: 1fr 1fr;
		gap: 8rpx 14rpx;
		margin-top: 12rpx;
		padding: 12rpx 14rpx;
		border-radius: 16rpx;
		background: rgba(247, 248, 248, 0.58);
	}

	.time-item {
		min-width: 0;
		color: #6f7c78;
		font-size: 20rpx;
		line-height: 30rpx;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}

	.time-item text {
		margin-right: 8rpx;
		color: #243d38;
		font-weight: 800;
	}

	.remark,
	.extra-info {
		margin-top: 14rpx;
		padding: 12rpx 16rpx;
		border-radius: 16rpx;
		background: rgba(247, 248, 248, 0.72);
		color: #5f6c69;
		font-size: 22rpx;
		line-height: 34rpx;
	}

	.extra-info {
		display: flex;
		flex-wrap: wrap;
		gap: 12rpx 24rpx;
	}

	.route-box {
		margin-top: 16rpx;
		padding: 14rpx 16rpx;
		border: 1px solid rgba(166, 255, 234, 0.78);
		border-radius: 20rpx;
	}

	.route-line {
		display: flex;
		padding: 8rpx 0;
	}

	.route-dot {
		width: 38rpx;
		height: 38rpx;
		line-height: 38rpx;
		flex-shrink: 0;
		margin: 3rpx 14rpx 0 0;
		text-align: center;
		border-radius: 38rpx;
		background: #00231C;
		color: #fff;
		font-size: 20rpx;
		font-weight: 700;
	}

	.route-dot.send {
		background: #8adfca;
		color: #00231C;
	}

	.route-main {
		flex: 1;
		min-width: 0;
	}

	.route-name {
		color: #152d28;
		font-size: 25rpx;
		font-weight: 800;
		line-height: 34rpx;
	}

	.route-address {
		margin-top: 4rpx;
		color: #7a8582;
		font-size: 22rpx;
		line-height: 32rpx;
		word-break: break-all;
	}

	.goods-row {
		display: flex;
		align-items: center;
		margin-top: 16rpx;
		padding: 14rpx 16rpx;
		border-radius: 18rpx;
		background: rgba(247, 248, 248, 0.72);
	}

	.goods-title {
		color: #172f2a;
		font-size: 24rpx;
		font-weight: 900;
	}

	.goods-desc {
		flex: 1;
		min-width: 0;
		margin-left: 16rpx;
		color: #7a8582;
		font-size: 22rpx;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}

	.fee-row {
		display: flex;
		align-items: center;
		margin-top: 16rpx;
		color: #64726f;
		font-size: 24rpx;
		line-height: 40rpx;
	}

	.big-tag {
		margin-right: 12rpx;
		padding: 0 10rpx;
		height: 32rpx;
		line-height: 32rpx;
		border-radius: 10rpx;
		background: rgba(255, 117, 88, 1);
		color: #fff;
		font-size: 20rpx;
		font-weight: 700;
	}

	.fee-text {
		flex: 1;
	}

	.fee-text text {
		color: #00231C;
		font-size: 30rpx;
		font-weight: 900;
	}

	.market-fee {
		color: #9aa4a1;
		font-size: 20rpx;
	}

	.empty-box {
		padding: 120rpx 0;
		color: #8b9894;
		font-size: 28rpx;
		text-align: center;
	}

	.product-popup-content {
		width: 680rpx;
		max-height: 880rpx;
		padding: 34rpx 28rpx 30rpx;
		box-sizing: border-box;
		overflow: scroll;
		border-radius: 36rpx;
		background:
			radial-gradient(circle at 92% 4%, rgba(166, 255, 234, 0.38) 0, rgba(166, 255, 234, 0) 180rpx),
			linear-gradient(180deg, #ffffff 0%, #f8fffc 100%);
		box-shadow: 0 24rpx 56rpx rgba(0, 35, 28, 0.18);
	}

	.product-popup-header {
		margin-bottom: 26rpx;
		text-align: center;
	}

	.product-popup-title {
		color: #143d35;
		font-size: 36rpx;
		font-weight: 900;
		line-height: 48rpx;
	}

	.product-popup-subtitle {
		margin-top: 8rpx;
		color: #7b8a85;
		font-size: 24rpx;
		line-height: 34rpx;
	}

	.product-popup-list {
		display: flex;
		flex-direction: column;
		gap: 20rpx;
	}

	.product-popup-card {
		display: flex;
		align-items: center;
		padding: 22rpx;
		border: 1px solid rgba(166, 255, 234, 0.58);
		border-radius: 28rpx;
		background: rgba(255, 255, 255, 0.88);
		box-shadow: 0 12rpx 28rpx rgba(0, 35, 28, 0.06);
	}

	.product-popup-img {
		width: 128rpx;
		height: 128rpx;
		flex-shrink: 0;
		overflow: hidden;
		border-radius: 22rpx;
		background: #f0f5f3;
	}

	.product-popup-img img {
		width: 100%;
		height: 100%;
		background-size: 100%;
	}

	.product-popup-info {
		flex: 1;
		min-width: 0;
		padding: 0 22rpx;
	}

	.product-popup-name {
		color: #172f2a;
		font-size: 30rpx;
		font-weight: 900;
		line-height: 42rpx;
	}

	.product-popup-spec {
		display: flex;
		align-items: flex-start;
		margin-top: 16rpx;
	}

	.product-popup-label {
		flex-shrink: 0;
		height: 34rpx;
		padding: 0 14rpx;
		border-radius: 999rpx;
		background: rgba(166, 255, 234, 0.72);
		color: #126255;
		font-size: 20rpx;
		font-weight: 800;
		line-height: 34rpx;
	}

	.product-popup-spec-text {
		flex: 1;
		margin-left: 12rpx;
		color: #65736f;
		font-size: 24rpx;
		line-height: 34rpx;
		word-break: break-all;
	}

	.product-popup-quantity {
		width: 92rpx;
		height: 92rpx;
		flex-shrink: 0;
		border-radius: 28rpx;
		background: linear-gradient(135deg, #fff4ce 0%, #a6ffea 100%);
		color: #103f36;
		text-align: center;
		box-shadow: 0 10rpx 20rpx rgba(0, 35, 28, 0.08);
	}

	.product-popup-quantity-num {
		padding-top: 16rpx;
		font-size: 30rpx;
		font-weight: 900;
		line-height: 34rpx;
	}

	.product-popup-quantity-label {
		margin-top: 2rpx;
		color: #52736b;
		font-size: 20rpx;
		line-height: 28rpx;
	}

	.product-popup-empty {
		padding: 80rpx 20rpx;
		border-radius: 28rpx;
		background: rgba(247, 255, 251, 0.9);
		color: #7b8a85;
		font-size: 28rpx;
		font-weight: 700;
		text-align: center;
	}

	.bottom-space {
		width: 100%;
		height: 60rpx;
	}
</style>