<template>
	<view style="border-top: 1rpx solid #f6f6f6;">
		<view class="wrap">
			<view class="u-tabs-box">
				<u-tabs-swiper activeColor="#f29100" ref="swiperTabs" :list="list" :current="current" @change="change"
					:is-scroll="false" swiperWidth="750"></u-tabs-swiper>
			</view>
			<swiper class="swiper-box" :current="swiperCurrent" @transition="transition"
				@animationfinish="animationfinish">
				<swiper-item v-for="(s, i) in list.length" :key="i" class="swiper-item">
					<scroll-view v-if="orderData.length !== 0" scroll-y style="height: 100%;width: 100%;"
						@scrolltolower="reachBottom">
						<view class="page-box">
							<view class="order" v-for="(res, index) in orderData" :key="res.id">
								<view class="top">
									<view class="left">
										<u-icon name="home" :size="30" color="rgb(94,94,94)"></u-icon>
										<view class="store">订单编号:{{ res.orderSn }}</view>
										<u-icon name="arrow-right" color="rgb(203,203,203)" :size="26"></u-icon>
									</view>
									<view class="right">{{ res.orderStatusText }}</view>
								</view>
								<view class="item" v-for="(item, index) in res.goodsList" :key="index">
									<view class="left">
										<image :src="
											item.picUrl.split('http').length > 1
												? item.picUrl
												: baseUrl + item.picUrl
										" mode="aspectFill"></image>
									</view>
									<view class="content">
										<view class="title u-line-2">{{ item.goodsName }}</view>
										<view class="type">{{ item.type }}</view>
										<view class="content-item">
											<view v-for="(item1, index1) in item.specifications
											.length" :key="index1" class="delivery-time">
												{{ item.specifications[index1] }}
											</view>
										</view>
									</view>
									<view class="right">
										<view class="price">
											￥{{ priceInt(item.price) }}
											<text class="decimal">
												.{{ priceDecimal(item.price) }}
											</text>
										</view>
										<view class="number">x{{ item.number }}</view>
									</view>
								</view>
								<view class="total">
									共{{ totalNum(res.goodsList) }}件商品 合计:
									<text class="total-price">
										￥{{ priceInt(totalPrice(res.goodsList)) }}.
										<text class="decimal">
											{{ priceDecimal(totalPrice(res.goodsList)) }}
										</text>
									</text>
								</view>
								<view class="bottom">
									<view />
									<view style="display: flex; flex-direction: row;">
										<view class="evaluate1 btn1" @tap="addCart(res.id)">
											加入购物车
										</view>
										<view v-if="res.orderStatusText === '待推送'" class="evaluate btn"
											@tap="orderQuotation(res.id, res.type)">
											生成报价
										</view>
										<view v-if="res.orderStatusText === '待推送'" class="evaluate btn"
											@tap="orderPush(res.id)">
											推送订单
										</view>
										<view v-if="res.orderStatusText === '待推送'" class="evaluate btn"
											@tap="orderDel(res.id)">
											删除订单
										</view>
									</view>
								</view>
							</view>
							<u-loadmore :status="status" bgColor="#f2f2f2" style="padding: 20rpx 0;"></u-loadmore>
						</view>
					</scroll-view>

					<!-- 无订单 -->
					<scroll-view v-else scroll-y style="height: 100%;width: 100%;">
						<view class="page-box">
							<view>
								<view class="centre">
									<image src="https://cdn.uviewui.com/uview/template/taobao-order.png" mode="">
									</image>
									<view class="explain">
										您还没有相关的订单
										<view class="tips">可以去看看有那些想买的</view>
									</view>
									<!-- <view class="btn">随便逛逛</view> -->
								</view>
							</view>
						</view>
					</scroll-view>
				</swiper-item>
			</swiper>
		</view>
	</view>

	<u-modal v-model="show" :content="content" show-cancel-button @confirm="onConfirm"></u-modal>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import { getOrderData, orderDelete, pushOrder } from '@/api/index'
import { quotationExist, orderCartAgain } from '@/api/cart'
import { onLoad } from '@dcloudio/uni-app'
import { useCurrentStore } from '@/store/useStore'
import config from '@/utils/config';

let baseUrl = config.baseUrl;

let currentStore = useCurrentStore()
let show = ref(false)
let content = ref('确认删除该订单?')
let listQuery = ref({
	showType: 0,
	page: 1,
	limit: 10
})
let value1 = ref(1)
let options1 = ref([{
	label: '默认排序',
	value: 1,
},
{
	label: '距离优先',
	value: 2,
},
{
	label: '价格优先',
	value: 3,
}
])
let swiperTabs = ref()
let orderData = ref([] as any)
let total = ref(0)
let list = ref([
	{ name: '全部' },
	{ name: '待推送' },
	{ name: '待生产' },
	{ name: '待发货' },
	{ name: '已完成' }// count: 12
])
let current = ref(0)
let swiperCurrent = ref(0)
let status = ref('loadmore')
let orderIds = ref(0) //删除的订单id

onLoad((options?: any) => {
	if (options) {
		current.value = options.showType
		swiperCurrent.value = options.showType
		listQuery.value.showType = options.showType
	}
	getOrderList()
})

// 价格小数
let priceDecimal = computed(() => {
	return (val: any) => {
		if (val !== parseInt(val)) return val.slice(-2);
		else return '00';
	};
})
// 价格整数
let priceInt = computed(() => {
	return (val: any) => {
		if (val !== parseInt(val)) return val.split('.')[0];
		else return val;
	};
})

let reachBottom = () => {
	// 判断是否还有下一页数据
	if (listQuery.value.page * listQuery.value.limit >= total.value) return (status.value = 'nomore');
	// // 判断是否正在请求其它数据，如果是，则不发起额外的请求
	if (status.value === 'loading') return;
	listQuery.value.page += 1;
	getOrderList();
}

// 总价
let totalPrice = (item: any) => {
	let price = 0;
	item.map((val: any) => {
		price += parseFloat(val.price);
	});
	return price.toFixed(2);
}
// 总件数
let totalNum = (item: any) => {
	let num = 0;
	item.map((val: any) => {
		num += val.number;
	});
	return num;
}
// tab栏切换
let change = (index: any) => {
	if (swiperCurrent.value === index) return
	swiperCurrent.value = index;
	listQuery.value.showType = index
	listQuery.value.page = 1
	listQuery.value.limit = 10
	orderData.value = []
	getOrderList();
}
// swiper-item 的位置发生改变时会触发
let transition = (data: any) => {
	let dx = data.detail.dx
	swiperTabs.value?.setDx(dx);
}
// 动画结束时会触发 animationfinish 事件
let animationfinish = (data: any) => {
	let currentNum = data.detail.current
	swiperTabs.value?.setFinishCurrent(currentNum);
	swiperCurrent.value = currentNum;
	current.value = currentNum;
}

// 加入购物车
let addCart = (orderId: number) => {
	orderIds.value = orderId
	content.value = '确定加入购物车?'
	show.value = true
}

// 删除订单
let orderDel = (orderId: number) => {
	show.value = true
	content.value = '确认删除该订单?'
	orderIds.value = orderId
}

// 生成报价 type为0是普通订单， 2是生产线订单
let orderQuotation = (orderId: number, type: number) => {
	orderIds.value = orderId
	if (type == 0) {
		quotationExist({ orderId }).then((res: any) => {
			if (res.data.code === 200) {
				if (res.data.data) {
					uni.navigateTo({
						url: '../order/editQuotation?orderId=' + orderId
					})
				} else toDownPdf(orderId)
			}
		})
	} else {
		uni.navigateTo({
			url: '../productLine/productLineDetails?orderId=' + orderId
		})
	}
}

// 跳转到订单详情
let toDownPdf = (orderId: number) => {
	uni.navigateTo({
		url: `/pages/order/downPdf?orderId=${orderId}&type=profile`,
		success() {
			currentStore.$patch({
				current: current.value
			})
		}
	});
}

// 推送订单
let orderPush = (orderId: number) => {
	show.value = true
	content.value = '确认将状态推送到待生产？'
	orderIds.value = orderId
}


//  确认操作
let onConfirm = () => {
	if (content.value == '确认删除该订单?') {
		orderDelete({ orderId: orderIds.value }).then((res: any) => {
			if (res.data.code === 200) {
				orderData.value = []
				listQuery.value.page = 1
				getOrderList()
				uni.showToast({
					title: '删除成功',
					icon: 'none'
				})
			}
		})
	} else if (content.value == '确定加入购物车?') {
		orderCartAgain({ orderId: orderIds.value }).then((res: any) => {
			if (res.data.code === 200) {
				uni.showToast({
					title: '加入成功',
					icon: 'none',
					mask: true
				})
			}
		})
	} else {
		pushOrder({ orderId: orderIds.value }).then((res: any) => {
			if (res.data.code === 200) {
				orderData.value = []
				listQuery.value.page = 1
				getOrderList()
				uni.showToast({
					title: '推送成功',
					icon: 'none'
				})
			}
		})
	}
}

// 页面数据
let getOrderList = () => {
	status.value = 'loading';
	getOrderData(listQuery.value).then((res: any) => {
		if (res.data.code === 200) {
			if (res.data.data.total > 10) status.value = 'loadmore';
			else status.value = 'nomore';
			total.value = res.data.data.total
			orderData.value = [...orderData.value, ...res.data.data.list];
		}
	})
}
</script>

<style>
/* #ifndef H5 */
page {
	/* height: 100%; */
	/* background-color: #f2f2f2; */
}

/* #endif */
</style>

<style lang="scss" scoped>
.order {
	width: 710rpx;
	background-color: #ffffff;
	margin: 20rpx auto;
	border-radius: 20rpx;
	box-sizing: border-box;
	padding: 20rpx;
	font-size: 28rpx;

	.top {
		display: flex;
		justify-content: space-between;

		.left {
			display: flex;
			align-items: center;

			.store {
				margin: 0 10rpx;
				font-size: 26rpx;
				font-weight: bold;
			}
		}

		.right {
			color: $u-type-warning-dark;
		}
	}

	.item {
		display: flex;
		margin: 20rpx 0 0;

		.left {
			margin-right: 20rpx;

			image {
				width: 200rpx;
				height: 200rpx;
				border-radius: 10rpx;
			}
		}

		.content {
			width: 45%;

			.title {
				font-size: 28rpx;
				line-height: 72rpx;
			}

			.type {
				margin: 10rpx 0;
				font-size: 24rpx;
				color: $u-tips-color;
			}

			.content-item {
				display: flex;
				flex-direction: row;
				justify-content: flex-start;
				flex-wrap: wrap;

				.delivery-time {
					color: #e5d001;
					font-size: 24rpx;
					margin-right: 8rpx;
				}
			}
		}

		.right {
			margin-left: 10rpx;
			padding-top: 20rpx;
			text-align: right;

			.decimal {
				font-size: 24rpx;
				margin-top: 4rpx;
			}

			.number {
				color: $u-tips-color;
				font-size: 24rpx;
			}
		}
	}

	.total {
		margin-top: 20rpx;
		text-align: right;
		font-size: 24rpx;

		.total-price {
			font-size: 32rpx;
		}
	}

	.bottom {
		display: flex;
		margin-top: 40rpx;
		padding: 0 10rpx;
		justify-content: space-between;
		align-items: center;

		.btn {
			line-height: 52rpx;
			width: 140rpx;
			border-radius: 26rpx;
			border: 2rpx solid $u-border-color;
			font-size: 24rpx;
			text-align: center;
			color: $u-type-info-dark;
		}

		.btn1 {
			line-height: 52rpx;
			width: 150rpx;
			border-radius: 26rpx;
			border: 2rpx solid $u-border-color;
			font-size: 24rpx;
			text-align: center;
			color: $u-type-info-dark;
		}

		.evaluate {
			color: $u-type-warning-dark;
			border-color: $u-type-warning-dark;
			margin-right: 8rpx;
		}

		.evaluate1 {
			color: #000;
			border-color: rgba(204, 204, 204, 0.621);
			margin-right: 8rpx;
		}
	}
}

.centre {
	text-align: center;
	margin: 200rpx auto;
	font-size: 32rpx;

	image {
		width: 164rpx;
		height: 164rpx;
		border-radius: 50%;
		margin-bottom: 20rpx;
	}

	.tips {
		font-size: 24rpx;
		color: #999999;
		margin-top: 20rpx;
	}

	.btn {
		margin: 80rpx auto;
		width: 200rpx;
		border-radius: 32rpx;
		line-height: 64rpx;
		color: #ffffff;
		font-size: 26rpx;
		background: linear-gradient(270deg, rgba(249, 116, 90, 1) 0%, rgba(255, 158, 1, 1) 100%);
	}
}

.wrap {
	display: flex;
	flex-direction: column;
	height: calc(100vh - var(--window-top));
	width: 100%;
}

.swiper-box {
	flex: 1;
}

.swiper-item {
	height: 100%;
}
</style>
