<template>
	<view class="container">
		<!-- 筛选区域 -->
		<Search :typeList="typeList" :timeList="timeList" @handLeConveyData="getConveyData" />

		<!-- 列表区域 -->
		<view class="list" v-for="(item, index) in listData" :key="index">
			<view class="list-title">
				<view>{{ item.route }}</view>
				<view class="list-img" :class="item.state === 1 ? 'mr-30' : ''">
					<img v-if="item.state === 1" src="../../static/success.png" />
					<img v-if="item.state === 2" src="../../static/pass.png" />
					<img v-if="item.state === 3" src="../../static/waiting.png" />
					<img v-if="item.state === 4" src="../../static/cancel.png" />
					<img v-if="item.state === 5" src="../../static/unAppoint.png" />
				</view>
			</view>
			<view class="list-info">
				<view class="list-info-item">
					<span>预约号：</span>
					{{ item.row_num.toString().padStart(2, '0') }}
				</view>
				<view :class="['list-info-item', item.state == 1 ? 'highlight' : '']" v-if="item.state !== 3">
					<span>车牌号：</span>
					{{ item.car_number }}
				</view>
				<view class="list-info-item" v-if="item.state === 1 || item.state === 2">
					<span>容 量：</span>
					{{ item.contain }}人
				</view>
				<view class="list-info-item">
					<span>站点：</span>
					{{ item.route_end }}
				</view>
				<view class="list-info-item highlight" v-if="item.state === 1">
					<span>发车时间：</span>
					{{ item.yy_date }}
				</view>
				<view class="list-info-item highlight" v-if="item.state === 1">
					<span>扫码时间段：</span>
					{{ item.sm_time }}
				</view>
				<view class="list-info-item">
					<span>下单时间：</span>
					{{ item.yy_time }}
				</view>

				<view class="list-info-item2" v-if="item.state === 1 || item.state === 2">
					<span>变更信息：</span>
					{{ item.remark != '' ? item.remark : '无' }}
				</view>
			</view>
			<view class="list-button">
				<!-- <view class="list-button-change" v-if="item.state ===1" @click="handleChange">
					更换车次
				</view> -->
				<view class="list-button-change" v-if="item.state === 1 && item.contain == 6" @click="handleOnCar(item.id)">我已上车</view>
				<view class="list-button-cancel" v-if="item.state === 1 || item.state === 3" @click="handleCancel(item.id)">取消预约</view>
			</view>
		</view>

		<!-- 无数据时展示的区域 -->
		<view class="list-nodata" v-if="listData.length == 0">
			<img src="../../static/no-bus.png" />
			<view>暂无数据</view>
		</view>
	</view>
</template>

<script setup>
import { ref } from 'vue'

import { onLoad, onShow, onPullDownRefresh, onReachBottom } from '@dcloudio/uni-app'

import { myRequest } from '@/util/api.js'

import { isWeixin } from '@/util/isWeixin.js'

import { filterIdentity } from '@/util/filterIdentity.js'

import Search from '@/components/search'

onLoad(() => {
	if (isWeixin()) {
		filterIdentity()
		card_number.value = uni.getStorageSync('bus_card_number')
	} else {
		uni.redirectTo({
			url: '/pages/404/404?message=请在微信客户端打开链接'
		})
	}
})
onShow(() => {
	result_state.value = 0
	date_state.value = 1
	currentPage.value = 1
	listData.value = []
	getMyData()
})

// 下拉刷新回调
onPullDownRefresh(() => {
	currentPage.value = 1
	listData.value = []
	getMyData()
	setTimeout(function() {
		uni.stopPullDownRefresh()
	}, 500)
})

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

// 用户card_number
const card_number = ref('')
// 筛选条件状态列表
const typeList = ref([
	{
		text: '全部',
		value: 0
	},
	{
		text: '预约成功',
		value: 1
	},
	{
		text: '已乘车',
		value: 2
	},
	{
		text: '候补中',
		value: 3
	},
	{
		text: '已取消',
		value: 4
	},
	{
		text: '已爽约',
		value: 5
	}
])
// 筛选条件时间列表
const timeList = ref([
	{
		text: '全部',
		value: 0
	},
	{
		text: '当天',
		value: 1
	},
	{
		text: '本周',
		value: 2
	},
	{
		text: '本月',
		value: 3
	}
])

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

// 获取用户预约数据
const getMyData = async () => {
	const res = await myRequest({
		url: '/appqueryUserOrders.action',
		data: {
			result_state: result_state.value,
			date_state: date_state.value,
			card_number: card_number.value,
			rows: pageSize.value,
			page: currentPage.value
		}
	})
	// console.log(res)
	total.value = res.data.totalCount
	listData.value = [...listData.value, ...res.data.list]
}

// 更换车次按钮回调
const handleChange = () => {
	uni.switchTab({
		url: '/pages/home/home'
	})
}

// 我已上车按钮回调
const handleOnCar = id => {
	uni.showModal({
		title: '提示',
		content: '确定已经上车吗？',
		success: res => {
			if (res.confirm) {
				handleOnCarRequest(id)
			} else if (res.cancel) {
			}
		}
	})
}

// 已上车请求
const handleOnCarRequest = async id => {
	const res = await myRequest({
		url: '/appBoarding.action',
		data: {
			card_number: card_number.value,
			record_id: id
		}
	})
	// console.log(res);
	if (res) {
		uni.showToast({
			title: res.message
		})
		setTimeout(() => {
			currentPage.value = 1
			listData.value = []
			getMyData()
		}, 1500)
	}
}

// 取消预约按钮回调
const handleCancel = id => {
	uni.showModal({
		title: '提示',
		content: '确定取消预约吗？',
		success: res => {
			if (res.confirm) {
				handleCancelRequest(id)
			} else if (res.cancel) {
			}
		}
	})
}
// 取消预约请求
const handleCancelRequest = async id => {
	const res = await myRequest({
		url: '/appcancelOrder.action',
		data: {
			card_number: card_number.value,
			record_id: id
		}
	})
	// console.log(res);
	if (res) {
		uni.showToast({
			title: res.message
		})
		setTimeout(() => {
			currentPage.value = 1
			listData.value = []
			getMyData()
		}, 1500)
	}
}

// 筛选下拉框确定选择回调
const getConveyData = Obj => {
	// console.log(Obj)
	result_state.value = Obj.typeIndex
	date_state.value = Obj.timeIndex - 0 + 1
	currentPage.value = 1
	listData.value = []
	getMyData()
}
</script>

<style lang="scss" scoped>
.container {
	display: flex;
	flex-direction: column;
	background-color: #f2f2f2;

	.list {
		margin-bottom: 20rpx;
		font-size: 32rpx;
		background-color: #fff;

		.list-title {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 0 30rpx;
			height: 94rpx;
			font-weight: bold;
			font-size: 32rpx;
			border-bottom: 1rpx solid #e6e6e6;

			.list-img {
				width: 121rpx;
				height: 34rpx;

				img {
					// width: 100%;
					height: 100%;
				}
			}

			.mr-30 {
				margin-right: 30rpx;
			}
		}

		.list-info {
			display: flex;
			flex-direction: column;
			justify-content: space-evenly;
			padding: 0 30rpx;
			margin-top: 15rpx;

			.list-info-item,
			.list-info-item2 {
				line-height: 60rpx;
				font-size: 28rpx;
				color: #333333;

				span {
					display: inline-block;
					width: 230rpx;
					text-align-last: justify;
					color: #999999;
					font-size: 28rpx;
				}
			}

			.highlight {
				color: #5c60f3;
			}

			.list-info-item {
				display: flex;
				// height: 60rpx;
			}
		}

		.list-button {
			display: flex;
			justify-content: flex-end;
			align-items: center;
			font-size: 28rpx;
			text-align: center;

			.list-button-change {
				display: flex;
				justify-content: center;
				align-items: center;
				margin: 17rpx 0 40rpx 0;
				width: 170rpx;
				height: 70rpx;
				color: #fff;
				border-radius: 15rpx;
				background: linear-gradient(#8684ff, #3c50e8);
			}

			.list-button-cancel {
				display: flex;
				justify-content: center;
				align-items: center;
				margin: 17rpx 30rpx 40rpx;
				width: 170rpx;
				height: 70rpx;
				color: #fff;
				border-radius: 15rpx;
				background-color: #ffc400;
			}
		}
	}

	.list-nodata {
		margin-top: -20rpx;
		padding-top: 100rpx;
		background-color: #fff;
		text-align: center;
		color: #999999;

		img {
			width: 600rpx;
		}
	}
}
</style>
