<template>
	<view class="order">
		<view class="order-search">
			<u-search v-model="queryParams.name" :animation="true" :showAction="false" bgColor="#f6f6f6"
				placeholder="请输入门店" @confirm="onSearch"></u-search>
			<view class="order-search-txt" @click.native="onSearch">搜索</view>
		</view>

		<view class="order-tab">
			<view class="order-tab-item" @click="onTabChange(1)" :class="{'order-tab-active':queryParams.status==1}">
				待配送({{wCount.no_send}})</view>
			<view class="order-tab-item" @click="onTabChange(2)" :class="{'order-tab-active':queryParams.status==2}">
				已配送({{wCount.send}})</view>
		</view>

		<view class="order-list">
			<scroll-view style="height: 100%;" :scroll-y="true" @scrolltolower="onScrollToLower">
				<view class="order-list-wrap">
					<block v-for="(item,indx) in wiriteList" :key="indx">
						<view class="order-list-item" @click="onClickToDetail(item)" v-if="queryParams.status == 1">
							<view class="order-list-item-order_num">订单编号:{{item.order_id}}</view>
							<view class="order-list-item-trans">
								<view class="order-list-item-trans-right">
									<view class="order-type">{{item.shipping_type == 1?'配送订单':'自提订单'}} </view>
									<view class="order-reciver">收货人：{{item.real_name}}</view>
									<view class="order-verification-time">商品信息：{{item.goods_list[0].store_name}}</view>
								</view>

								<view class="order-list-item-trans-left" @click.stop="onClickToWrite(item)">
									<!-- <up-image width="76rpx" height="76rpx" src="../images/scan.png" /> -->
									<image src="../images/scan.png"></image>
								</view>
							</view>
						</view>

						<!-- 已经核销 -->
						<view class="order-list-item" @click="onClickToDetail(item)" v-if="queryParams.status == 2">
							<view class="order-list-item-order_num">订单编号:{{item.order_id}}</view>
							<view class="order-list-item-trans">
								<view class="order-list-item-trans-left" @click.stop="onClickToWrite(item)">
									<u-image width="76rpx" height="76rpx" src="../images/write_done.jpg" />
								</view>

								<view class="order-list-item-trans-right">
									<view class="order-type">{{item.shipping_type == 1?'配送订单':'自提订单'}} </view>
									<view class="order-reciver">收货人：{{item.real_name}}</view>
									<view class="order-verification-time">商品信息：{{item.goods_list[0].store_name}}</view>
								</view>
							</view>
						</view>
					</block>
				</view>

				<view class="load-wrap">
					<up-loadmore :status="status" />
				</view>
			</scroll-view>

			<view class="batch-write" @click="onPatchWrite">
				<image class="btn-img" src="../images/scan.png"></image>
				<view class="btn-txt">扫码核销</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		onLoad,
		onShow,
	} from "@dcloudio/uni-app";
	import {
		reactive,
		ref
	} from "vue";
	import {
		writeListApi,
		writeOrderApi,
		writeCountApi
	} from "@/api/order.js";

	// 搜索条件
	const onSearch = () => {
		queryParams.value.page_no = 1;
		status.value = '';
		wiriteList.value = [];
		getList();
	}

	// tab切换
	const onTabChange = (e) => {
		queryParams.value.status = e;
		queryParams.value.page_no = 1;
		status.value = '';
		wiriteList.value = [];
		getList();
	}

	// 详情页面
	const onClickToDetail = (item) => {
		uni.navigateTo({
			url: "/multipleShop/verificationOrder/detail",
			success(res) {
				res.eventChannel.emit('transferDataToDetail', item)
			}
		})
	}

	// 切换到核销
	const onClickToWrite = (item) => {
		uni.scanCode({
			success(resp) {
				writeOrderApi({
					verify_code: resp.result
				}).then(res => {
					if (res.code == 1) {
						uni.showToast({
							title: '核销成功',
							icon: 'none',
							success() {
								uni.$u.sleep(1500).then(rrr => {
									status.value = '';
									queryParams.value.page_no = 1;
									wiriteList.value = [];
									getList();
								})
							}
						})
					} else {
						uni.showToast({
							title: '核销失败',
							icon: 'none'
						})
					}
				})
			}
		})
	}

	// 输入核销码核销
	const onPatchWrite = () => {
		uni.navigateTo({
			url: "/multipleShop/orderVerification/index"
		})
	}

	// 滚动到底部
	const onScrollToLower = (e) => {
		getList();
	}

	// 核销数量统计
	const wCount = ref({
		no_send: 0,
		send: 0
	});
	const writeCount = () => {
		writeCountApi(queryParams.value).then(res => {
			let {
				no_send,
				send
			} = res.data;
			wCount.value.no_send = no_send;
			wCount.value.send = send;
		})
	}

	// 加载状态
	const status = ref('');
	const wiriteList = ref([]);

	// 获取列表
	const queryParams = ref({
		status: 1,
		name: '', //查询字段
		page_no: 1,
		page_size: 10
	})
	const getList = () => {
		if (status.value == 'loading') return;
		if (status.value == 'nomore') return;

		status.value = 'loading';
		writeListApi(queryParams.value).then(res => {
			let len = res.data.list;
			status.value = queryParams.value.page_size > len.length ? 'nomore' : 'loadmore';
			wiriteList.value = wiriteList.value.concat(len);
			if (status.value == 'loadmore') queryParams.value.page_no += 1;
		});

		// 核销记录统计
		writeCount();
	}

	onShow(() => {
		//清空选择 刷新页面
		onSearch();

		getList();

		// 隐藏home键返回主页
		uni.hideHomeButton();
	})
</script>

<style lang="scss">
	page {
		background: #FAFAFA;
	}

	.order {
		padding-bottom: 30rpx;

		.order-search {
			position: sticky;
			top: 0;
			z-index: 10;
			display: flex;
			height: 90rpx;
			background-color: #fff;
			padding: 0 24rpx;
			box-sizing: border-box;

			.order-search-txt {
				position: absolute;
				right: 28rpx;
				top: 50%;
				z-index: 10;
				transform: translateY(-50%);
				width: 110rpx;
				height: 52rpx;
				line-height: 52rpx;
				text-align: center;
				background: #FFFFFF;
				border-radius: 30rpx 30rpx 30rpx 30rpx;
				font-size: 28rpx;
				color: #20B128;
			}
		}

		.order-list {
			position: relative;
			padding-top: 24rpx;
			height: calc(100vh - 90rpx - 24rpx - 30rpx);

			.order-list-item {
				width: 710rpx;
				background: #FFFFFF;
				border-radius: 12rpx;
				margin: 0 auto 24rpx;
				padding: 0 30rpx;
				box-sizing: border-box;

				.order-list-item-order_num {
					height: 74rpx;
					line-height: 74rpx;
					font-weight: 600;
					font-size: 26rpx;
					color: #333333;
					border-bottom: 2rpx solid #F1F1F1;
				}

				.order-list-item-trans {
					display: flex;
					justify-content: space-between;
					padding: 16rpx 0 24rpx 0;

					.order-list-item-trans-left {
						display: flex;
						justify-content: center;
						align-items: center;
						margin-right: 38rpx;

						image {
							width: 76rpx;
							height: 76rpx;
						}
					}

					.order-list-item-trans-right {
						.order-type {
							margin-bottom: 10rpx;
							font-weight: 600;
							font-size: 32rpx;
							color: #444444;
						}

						.order-reciver {
							margin-bottom: 10rpx;
							font-size: 26rpx;
							color: #777777;
						}

						.order-verification-time {
							width: 500rpx;
							font-size: 26rpx;
							color: #777777;
							overflow: hidden;
							white-space: nowrap;
							text-overflow: ellipsis;
						}
					}
				}
			}

			.load-wrap {
				padding: 20rpx 0 400rpx 0;
			}

			.batch-write {
				display: flex;
				justify-content: center;
				align-items: center;
				position: absolute;
				bottom: 100rpx;
				left: 50%;
				transform: translateX(-50%);
				width: 670rpx;
				height: 90rpx;
				background: #20B128;
				border-radius: 20rpx;

				.btn-img {
					width: 60rpx;
					height: 60rpx;
				}

				.btn-txt {
					font-size: 32rpx;
					color: #FFFFFF;
					margin-left: 8rpx;
				}
			}
		}

		.order-tab {
			display: flex;
			align-items: center;
			justify-content: space-around;
			width: 100%;
			height: 102rpx;
			background-color: #fff;

			.order-tab-item {
				font-size: 32rpx;
				color: #444444;
			}

			.order-tab-active {
				position: relative;
				font-weight: bold;
				font-size: 32rpx;
				color: #20B128;

				&::after {
					content: "";
					display: block;
					position: absolute;
					width: 52rpx;
					height: 6rpx;
					border-radius: 6rpx;
					background-color: #20B128;
					bottom: -20rpx;
					left: 50%;
					transform: translateX(-50%);
				}
			}
		}
	}
</style>