<template>
	<view class="container">
		<!-- 标题区域 -->
		<view class="title" :style="{ top: `${paddingTop * 2}rpx` }">我的出行</view>

		<wd-tabs v-model="tabIndex" auto-line-width custom-class="custom_class_tab" @click="handleChange">
			<block v-for="tab in tabs" :key="tab">
				<wd-tab :title="`${tab}`" :name="tab">
					<scroll-view scroll-y class="content" @scrolltolower="scrolltolower">
						<!-- 每一个订单区域 -->
						<view v-for="item in listData" :key="item.id" class="order_box">
							<view class="box_top">
								<view class="top_left">
									<image class="left_img" src="@/static/images/mine/riding.png" mode="aspectFill" />
									墨轩出行
								</view>
								<view class="top_status red">待支付</view>
							</view>

							<view class="box_center">
								<image class="center_img" src="@/static/images/home/line.png" mode="aspectFill" />

								<view class="center_text">
									<view class="text_start">南昌交通学院墨轩湖校区</view>

									<view class="text_end">南昌火车站</view>
								</view>

								<view class="center_price">
									¥
									<text class="text">{{ item.payAmount }}</text>
								</view>
							</view>

							<view class="box_bottom">
								<view class="bottom_time">出行时间：{{ item.ciDate }}</view>
								<view class="bottom_btns">
									<view class="btn_item" @click="handleDetail">查看详情</view>
									<view class="btn_item pay">立即支付</view>
								</view>
							</view>
						</view>

						<view v-if="!listData.length" style="padding-top: 100rpx">
							<wd-status-tip image="content" tip="暂无数据" />
						</view>
					</scroll-view>
				</wd-tab>
			</block>
		</wd-tabs>
	</view>
</template>

<script setup>
import { onMounted, ref } from 'vue'
import { myRequest } from '@/utils/api.ts'
import debounce from 'lodash/debounce'

// 用户信息
const userInfo = uni.getStorageSync('carUserInfo')

// 胶囊按钮距离页面顶部的距离
const paddingTop = ref(0)

const tabIndex = ref(0)

// 预约状态 0:全部 1:预约成功 2:已乘车 3:候补中 4:已取消 5:已爽约
const result_state = ref(0)
// 时间状态 1:全部 2:当天 3:本周 4:本月
const date_state = ref(1)
// 每页多少条数据
const pageSize = ref(6)
// 当前是第几页
const currentPage = ref(1)
// 总共多少条数据
const total = ref(0)
// 预约记录数据
const listData = ref([])

const tabs = ref(['全部', '未乘车', '已乘车', '已取消', '待支付'])

// 获取用户预约数据
async function getMyData() {
	const res = await myRequest({
		url: '/tAppqueryUserOrders.action',
		data: {
			result_state: result_state.value,
			date_state: date_state.value,
			mobile: userInfo?.phone || '18320846714',
			rows: pageSize.value,
			page: currentPage.value
		}
	})
	// console.log(res)

	const result = JSON.parse(res.data.data)
	console.log(result)

	total.value = result.totalCount

	// 处理数据
	result.list.forEach((ele) => {
		// 如果是待支付状态,算出剩余支付时间
		if (ele.state === 1 && ele.payState === 0) {
			const temLockTime = 5
			// 兼容ios部分系统转换时间格式
			const createTime = ele.createTime.slice(0, 19).replace(/-/g, '/')
			ele.countDownTime = new Date(createTime).getTime() + temLockTime * 60 * 1000 - new Date().getTime()
		}
	})
	listData.value = [...listData.value, ...result.list]
}

onMounted(() => {
	console.log(userInfo)
	paddingTop.value = uni.getMenuButtonBoundingClientRect().top
	console.log(paddingTop.value)
	getMyData()
})

// 页面拉到底部触发函数
function scrolltolower() {
	if (listData.value.length < total.value) {
		currentPage.value++
		getMyData()
	} else {
		uni.showToast({
			title: '没有更多数据了',
			icon: 'none'
		})
	}
}

function handleChange(e) {
	console.log(e)
	result_state.value = e.index
	listData.value = []
	currentPage.value = 1
	total.value = 0
	getMyData()
}

function handleDetail() {
	uni.navigateTo({
		url: '/pages/orderDetail/orderDetail'
	})
}
</script>

<style lang="scss" scoped>
.container {
	position: relative;
	display: flex;
	box-sizing: border-box;
	padding-top: 162rpx;
	height: 100%;
	color: #001713;
	background-color: #fff;
	overflow: hidden;

	.title {
		position: absolute;
		left: 314rpx;
		font-size: 40rpx;
	}

	.content {
		box-sizing: border-box;
		padding: 30rpx;
		height: calc(100vh - 246rpx);
		color: #001713;
		overflow-y: auto;

		.order_box {
			margin-bottom: 20rpx;
			box-sizing: border-box;
			padding: 40rpx 20rpx;
			box-shadow: 0px 3px 6px #ccc;

			.box_top {
				display: flex;
				align-items: center;
				justify-content: space-between;
				font-size: 28rpx;
				color: #ff8205;

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

					.left_img {
						margin-right: 12rpx;
						width: 32rpx;
						height: 32rpx;
					}
				}

				.top_status {
					font-size: 32rpx;
				}

				.red {
					color: #dc2626;
				}

				.gray {
					color: #707070;
				}
			}

			.box_center {
				display: flex;
				align-items: center;
				margin-top: 40rpx;
				font-weight: bold;

				.center_img {
					width: 14rpx;
					height: 88rpx;
				}

				.center_text {
					margin-left: 16rpx;

					.text_start {
						margin-top: -8rpx;
						width: 480rpx;
						text-overflow: ellipsis;
						white-space: nowrap;
						overflow: hidden;
					}

					.text_end {
						margin-top: 30rpx;
						width: 480rpx;
						text-overflow: ellipsis;
						white-space: nowrap;
						overflow: hidden;
					}
				}

				.center_price {
					margin-left: auto;
					font-size: 24rpx;
					color: #dc2626;

					.text {
						font-size: 36rpx;
					}
				}
			}

			.box_bottom {
				display: flex;
				align-items: flex-end;
				justify-content: space-between;
				margin-top: 8rpx;
				font-size: 24rpx;
				color: #aba6a6;

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

					.btn_item {
						display: flex;
						align-items: center;
						justify-content: center;
						padding: 0 15rpx;
						margin-left: 10rpx;
						height: 50rpx;
						font-size: 24rpx;
						border-radius: 32rpx;
						border: 2rpx solid #aba6a6;
					}

					.pay {
						color: #ff8205;
						border: 2rpx solid #ff8205;
					}

					.cancel {
						color: #fff;
						background-color: #ff8205;
					}
				}
			}
		}
	}
}
</style>
