<template>
	<view class="page">
		<u-navbar :title="tit" :border-bottom="false" :background="bgc" back-icon-color="#000" title-color='#000'
			title-size='36' height='44'></u-navbar>
		
		<!-- 顶部固定筛选区域 -->
		<view class="fixed-header">
			<view class="search-box">
				<view class="date-picker-group">
					<view class="date-btn" @click="btnonetime">
						<text>{{startDate}}</text>
						<u-icon name="calendar" color="#999" size="28" style="margin-left: 10rpx;"></u-icon>
					</view>
					<text class="separator">至</text>
					<view class="date-btn" @click="btnonetimes">
						<text>{{endDate}}</text>
						<u-icon name="calendar" color="#999" size="28" style="margin-left: 10rpx;"></u-icon>
					</view>
				</view>
				<view class="search-icon-btn" @click="getlist">
					<u-icon name="search" color="#fff" size="36"></u-icon>
				</view>
			</view>
		</view>

		<view class="list-container">
			<!-- 占位元素，把内容顶下来 -->
			<!-- <view style="height: 140rpx;"></view> -->
			
			<scroll-view class="list_box" @scrolltolower="handqixing" scroll-y refresher-enabled @refresherrefresh="onRefresh" :refresher-triggered="isRefreshing">
				<view class="card" v-for="(item,index) in dhlist" :key="index">
					<view class="card-header">
						<view class="header-left">
							<u-icon name="clock" color="#999" size="28" style="margin-right: 10rpx;"></u-icon>
							<text class="time">{{item.startTime}}</text>
				</view>
						<view class="status-tag" :class="item.status == 2 ? 'success' : 'processing'">
							{{item.status == 1 ? '调度中' : '已完成'}}
				</view>
			</view>
					
					<view class="divider"></view>
					
					<view class="info-grid">
						<view class="grid-item">
							<text class="label">车牌号</text>
							<text class="value highlight">{{item.deviceVehicleNum}}</text>
				</view>
						<view class="grid-item">
							<text class="label">是否有效</text>
							<text class="value" :class="item.isValid ? 'valid' : 'invalid'">
								{{item.isValid ? '有效' : '无效'}}
							</text>
				</view>
				</view>
			</view>
				
				<view class="loading-text">
					{{ dhlist.length == 0 ? '暂无调度记录' : (total > dhlist.length ? '加载中...' : '当前没有更多调度记录咯...') }}
			</view>
				<view style="height: 50rpx;"></view>
		</scroll-view>
		</view>
		
        <u-picker mode="time" v-model="show" :params="params" @confirm="confirmone"></u-picker>
		<u-picker mode="time" v-model="shows" :params="paramss" @confirm="confirmones"></u-picker>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				bgc: {
					backgroundColor: "#fff",
				},
				areaId: '',
				startDate: '', // 初始开始日期
				endDate: '' ,// 初始结束日期
			    dhlist:[],
				userId:'',
				params: {
					year: true,
					month: true,
					day: true,
					hour: false,
					minute: false,
					second: false
				},
				show: false,
				paramss: {
					year: true,
					month: true,
					day: true,
					hour: false,
					minute: false,
					second: false
				},
				shows: false,
				tit:'',
				pageNum:1,
				total:0,
				isRefreshing:false,
			}
		},
		onLoad(option) {
			this.tit = option.tit || '调度列表'
			this.startDate = this.getCurrentDate().formatted
			this.endDate = this.getCurrentDate().formatted
			this.areaId = option.areaId
			this.userId = option.userId
			this.getlist()
		},
		methods: {
			// 上拉加载更多
			handqixing() {
				if(this.dhlist.length < this.total){
					this.pageNum++
					this.getlist()
				}
				console.log(11)
			},
			// 下拉刷新列表
			onRefresh() {
				this.isRefreshing = true
				this.pageNum = 1
				this.getlist()
				setTimeout(() => {
					this.isRefreshing = false
				}, 1000)
			},
			btnonetime(){
				this.show = true
			},
			confirmone(e){
				console.log(e);
				this.startDate = e.year + '-' + e.month + '-' + e.day
			},
			btnonetimes(){
				this.shows = true
			},
			confirmones(e){
				console.log(e);
				this.endDate = e.year + '-' + e.month + '-' + e.day
			},
			// 请求调度记录
			getlist(){
				this.$u.get(`/bst/dispatchLog/list?areaId=${this.areaId}&endDateRange=${this.startDate + ',' + this.endDate}&userId=${this.userId}&pageNum=${this.pageNum}&pageSize=20`).then(res =>{
					if(res.code == 200){
						this.total = res.total
						if(this.pageNum == 1){
							this.dhlist = res.rows
						}else{
						    this.dhlist = this.dhlist.concat(res.rows)
						}
					}
				})
			},
			// 点击跳转到调度列表
			btnitem(){
				// uni.navigateTo({
				// 	url:'/page_shanghu/gongzuotai/diaodulist'
				// })
			},
			getCurrentDate() {
			  const date = new Date(); // 获取当前时间对象
			  // 获取年份（4位数字）
			  const year = date.getFullYear();
			  // 获取月份（注意：月份从 0 开始，需 +1 转换为实际月份）
			  const month = date.getMonth() + 1;
			  // 获取日期（当月的第几天）
			  const day = date.getDate();
			  // 格式化：补零（可选，例如将 3 月转为 "03"）
			  const formattedMonth = month.toString().padStart(2, '0');
			  const formattedDay = day.toString().padStart(2, '0');
			  // 组合结果
			  const currentDate = {
			    year,               // 年（如：2025）
			    month,              // 月（如：11，未补零）
			    day,                // 日（如：6，未补零）
			    formatted: `${year}-${formattedMonth}-${formattedDay}` // 格式化字符串（如：2025-11-06）
			  };
			  console.log('当天年月日：', currentDate);
			  return currentDate;
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: #F7F7F7;
	}
	
	.page {
		min-height: 100vh;
	}

	.fixed-header {
		// position: fixed;
		// top: 0;
		// margin-top: calc(44px + var(--status-bar-height));
		// left: 0;
		width: 100%;
		z-index: 10;
		background-color: #fff;
		box-shadow: 0 2rpx 10rpx rgba(0,0,0,0.05);
	}
	
	.search-box {
		padding: 20rpx 30rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		
		.date-picker-group {
			flex: 1;
			display: flex;
			align-items: center;
			justify-content: space-between;
			background: #F5F7FA;
			padding: 10rpx;
			border-radius: 40rpx;
			margin-right: 20rpx;
			
			.date-btn {
				flex: 1;
				display: flex;
				align-items: center;
				justify-content: center;
				height: 60rpx;
				font-size: 26rpx;
				color: #333;
				background: #fff;
				border-radius: 30rpx;
				margin: 0 10rpx;
				box-shadow: 0 2rpx 4rpx rgba(0,0,0,0.03);
			}
			
			.separator {
				color: #999;
				font-size: 24rpx;
				padding: 0 6rpx;
			}
		}
		
		.search-icon-btn {
			width: 80rpx;
			height: 80rpx;
			background: #4C97E7;
			border-radius: 50%;
			display: flex;
			align-items: center;
			justify-content: center;
			box-shadow: 0 4rpx 10rpx rgba(76, 151, 231, 0.3);
			
			&:active {
				opacity: 0.9;
			}
		}
	}
	
	.list-container {
		padding: 0 24rpx;
		box-sizing: border-box;
		padding-top: 24rpx;
		height: 81vh;
	}
	
	.list_box {
		height: 100%;
	}
	
	.card {
		background: #FFFFFF;
		border-radius: 20rpx;
		padding: 30rpx;
		margin-bottom: 24rpx;
		box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.03);
		
		.card-header {
		display: flex;
		justify-content: space-between;
			align-items: center;
			padding-bottom: 20rpx;
			
			.header-left {
				display: flex;
				align-items: center;
				font-size: 28rpx;
				color: #666;
	}

			.status-tag {
				padding: 4rpx 16rpx;
				border-radius: 8rpx;
				font-size: 24rpx;
				
				&.processing {
					background: #EBF4FF;
					color: #4C97E7;
				}
				
				&.success {
					background: #F0F9EB;
					color: #67C23A;
				}
			}
		}
		
		.divider {
			height: 1rpx;
			background: #f5f5f5;
			margin-bottom: 24rpx;
		}
		
		.info-grid {
			display: flex;
			justify-content: space-between;
			
			.grid-item {
		display: flex;
				flex-direction: column;
				
				.label {
					font-size: 24rpx;
					color: #999;
					margin-bottom: 8rpx;
				}
				
				.value {
					font-size: 30rpx;
					color: #333;
					font-weight: 500;
					
					&.highlight {
						font-weight: 600;
					}
					
					&.valid {
						color: #67C23A;
		}
					
					&.invalid {
						color: #FF4D4F;
					}
				}
				
				&:last-child {
					align-items: flex-end;
				}
			}
		}
	}

	.loading-text {
		text-align: center;
		color: #999;
		font-size: 24rpx;
		padding: 30rpx 0;
	}
</style>