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

		<!-- 列表区域 -->
		<view class="list" v-for="(item, index) in listData" :key="index" @click="handleLookDetail(item)">
			<view class="list-title">
				<view class="title-number">车牌：{{ item.car_number != 0 ? item.car_number : '无' }}</view>
				<view class="title-icon"><img src="../../static/right.png" /></view>
			</view>
			<view class="list-item">
				状态：
				<span :class="item.state_str == '预约进行中' || item.state_str == '候补中' ? 'list-item-type' : ''">{{ item.state_str }}</span>
			</view>
			<view class="list-item">
				发车日期：
				<span>{{ item.yy_date }}</span>
			</view>
			<view class="list-item" v-if="item.state_str == '预约进行中' || item.state_str == '已截止'">
				发车时间：
				<span>{{ item.ci_time != 0 ? item.ci_time : '' }}</span>
			</view>
			<view class="list-item">
				扫码时间：
				<span>{{ item.sm_start + '-' + item.sm_end }}</span>
			</view>
			<view class="list-item">
				线路：
				<span>{{ item.route }}</span>
			</view>
			<view class="list-item">
				站点：
				<span>{{ item.route_end }}</span>
			</view>
			<view class="list-item">
				人数：
				<view class="list-item-progress" v-if="item.state_str == '预约进行中' || item.state_str == '已截止'">
					<progress activeColor="#3C50E8" stroke-width="9" :percent="item.percent ? item.percent : 0" />
				</view>
				<span v-if="item.state_str == '预约进行中' || item.state_str == '已截止'">{{ item.user_num + '/' + item.contain }}</span>
				<span v-else>{{ item.contain }}</span>
			</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, onPullDownRefresh } 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()
		getBusList()
		getData()
	} else {
		uni.redirectTo({
			url: '/pages/404/404?message=请在微信客户端打开链接'
		})
	}
})

onPullDownRefresh(() => {
	getBusList()
	getData()
	setTimeout(function() {
		uni.stopPullDownRefresh()
	}, 500)
})

// 预约状态  全部：0  预约进行中：1  已截止：2  已分配：3   候补中：4
const result_state = ref(0)

// 预约状态数组
const typeList = ref([
	{
		text: '全部',
		value: 0
	},
	{
		text: '预约进行中',
		value: 1
	},
	{
		text: '已截止',
		value: 2
	},
	{
		text: '已分配',
		value: 3
	},
	{
		text: '候补中',
		value: 4
	}
])

// 当前选择车牌号 默认为全部
const busNumber = ref('全部')

// 车牌列表数据
const busList = ref([
	{
		text: '全部',
		value: 0
	}
])

// 预约记录数据
const listData = ref([])

// 获取预约记录数据
const getData = async () => {
	const res = await myRequest({
		url: '/appcarCaptainManage.action',
		data: {
			result_state: result_state.value,
			car_number: busNumber.value
		}
	})
	// console.log(res.data)
	if (res.data.length) {
		res.data.forEach(item => {
			item.percent = Math.ceil((parseInt(item.user_num) / parseInt(item.contain)) * 100)
		})
	}
	listData.value = res.data
}

// 获取车牌号数组请求
const getBusList = async () => {
	const res = await myRequest({
		url: '/appqueryCarInfos.action'
	})
	// console.log(res)
	if (res.data.length) {
		let temList = [
			{
				text: '全部',
				value: 0
			}
		]
		res.data.forEach((item, index) => {
			temList.push({
				text: item.car_number,
				value: index + 1
			})
		})
		busList.value = temList
	}
}

// 点击每一条记录回调
const handleLookDetail = item => {
	const info = JSON.stringify(item)
	uni.navigateTo({
		url: `/pages/detailDriver/detailDriver?info=${info}`
	})
}

// 条件筛选框选择回调
const getConveyData = Obj => {
	// console.log(Obj)
	result_state.value = Obj.typeIndex
	busNumber.value = busList.value[Obj.timeIndex].text
	getData()
}
</script>

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

	.list {
		margin-bottom: 20rpx;
		padding-bottom: 24rpx;
		background-color: #fff;

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

			.title-icon {
				margin-top: -20rpx;
				width: 15rpx;
				height: 25rpx;

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

		.list-item {
			display: flex;
			align-items: center;
			padding: 0 30rpx;
			margin-bottom: 14rpx;

			color: #999999;
			font-size: 28rpx;

			span {
				color: #333333;
			}

			.list-item-type {
				color: #3d51e8;
			}

			.list-item-progress {
				margin-right: 36rpx;
				width: 437rpx;
				height: 22rpx;

				:deep(.uni-progress-bar),
				:deep(.uni-progress-inner-bar) {
					border-radius: 85rpx;
				}
			}
		}
	}

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

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