<template>
	<view class="page">
		<u-navbar title="审核管理" :border-bottom="false" :background="bgc" title-color='#000' title-size='36' back-icon-color="#000"
			height='44'></u-navbar>
		<scroll-view class="list-container" @scrolltolower="handleScrollToLower" scroll-y>
			<view class="card" v-for="(item,index) in list " :key="index">
				<view class="card-header">
					<view class="header-left">
						<image src="https://api.ccttiot.com/smartmeter/img/static/uvEJsaNT3zNm414Zqwk3" mode="aspectFit" style="width: 44rpx; height: 44rpx; margin-right: 16rpx;"></image>
						<view class="vehicle-info">
							<text class="code">编号: {{item.vehicleCode}}</text>
						</view>
					</view>
					<view class="status-tag" 
						:class="{
							'pending': item.appealStatus==1,
							'rejected': item.appealStatus==0,
							'repairing': item.appealStatus==2,
							'completed': item.appealStatus==3
						}">
						{{ getStatusText(item.appealStatus) }}
					</view>
				</view>
				
				<view class="divider"></view>

				<view class="card-body">
					<view class="info-row">
						<text class="label">故障描述</text>
						<text class="value">{{item.faultDetail || '无描述'}}</text>
					</view>
					
					<view class="info-row">
						<text class="label">车牌号</text>
						<text class="value">{{item.vehicleNum || '--'}}</text>
					</view>
					
					<view class="info-row">
						<text class="label">提交时间</text>
						<text class="value">{{item.createTime}}</text>
					</view>

					<view class="pic-list" v-if="item.picture">
						<view class="img-wrapper" v-for="(pic, idx) in parsePictures(item.picture)" :key="idx"
							@click="preview(pic, item)">
							<image :src="pic" mode="aspectFill"></image>
						</view>
					</view>
				</view>

				<view class="card-footer" v-if="item.appealStatus==1 || item.appealStatus==2">
					<view class="divider" style="margin-bottom: 24rpx;"></view>
					<view class="action-btns">
						<view class="btn secondary" v-if="item.appealStatus==1" @click="unpass(item,0)">
							忽略
						</view>
						<view class="btn primary" v-if="item.appealStatus==1" @click="unpass(item,1)">
							处理
						</view>
						<view class="btn success" v-if="item.appealStatus==2" @click="btnwc(item)">
							维修完成
						</view>
					</view>
				</view>
			</view>

			<view class="loading-text">
				{{ list.length == 0 ? '暂无审核记录' : (total > list.length ? '加载中...' : '当前没有更多申报审核咯...') }}
			</view>
			<view style="height: 50rpx;"></view>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				bgc: {
					backgroundColor: '#F7F7F7'
				},
				sn: '',
				pageNum: 1, // 当前页码
				pageSize: 20, // 每页显示条数
				list: [] ,// 数据列表
				total:0,
				areaId:''
			};
		},
		onLoad(option) {
			this.areaId = option.areaId
		},
		onShow() {
			this.pageNum = 1
			this.getlist();
		},
		methods: {
			getStatusText(status) {
				const map = {
					0: '已驳回',
					1: '待处理',
					2: '维修中',
					3: '已完成'
				}
				return map[status] || '未知'
			},
			handleScrollToLower() {
				console.log(this.total,this.list.length);
			   if(this.total > this.list.length){
				  this.getlist()
			   }
			},
			// 点击完成
			btnwc(item){
				let data = {
					id:item.id,
					pass:true
				}
				this.$u.put('/bst/fault/complete', data).then((res) => {
					if (res.code === 200) {
						uni.showToast({
							title: '操作成功',
							icon: 'success',
							duration: 2000
						});
						this.pageNum = 1;
						this.list = []; // 清空当前列表
						this.getlist();
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 2000
						});
					}
				})
			},
            // 点击处理和忽略
			unpass(item,num){
				let passflag = ''
				if(num == 0){
					passflag = false
				}else if(num == 1){
					passflag = true
				}
				uni.showLoading({
					title:'加载中...'
				})
				let data = {
					id:item.id,
					pass:passflag
				}
				this.$u.put('/bst/fault/handle', data).then((res) => {
					uni.hideLoading()
					if (res.code === 200) {
						uni.showToast({
							title: '操作成功',
							icon: 'success',
							duration: 2000
						});
						this.pageNum = 1;
						this.list = []; // 清空当前列表
						this.getlist();
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 2000
						});
					}
				})
			},
			parsePictures(pictureString) {
				if (pictureString) {
					return pictureString.split(',')
				}
				return [];
			},
			preview(pic, item) {
				uni.previewImage({
					current: pic, // 当前显示图片的链接
					urls: this.parsePictures(item.picture) // 需要预览的图片链接列表
				})
			},
			getlist() {
				let data = {
					sn: this.sn,
					pageSize: this.pageSize,
					pageNum: this.pageNum
				};
				this.$u.get(`/bst/fault/list?pageNum=${this.pageNum}&pageSize=10&areaId=${this.areaId}`).then((res) => {
					if (res.code === 200) {
						this.total = res.total
						if(this.pageNum == 1){
							this.list = res.rows
						}else{
							this.list = this.list.concat(res.rows)
						}
						this.pageNum ++
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 2000
						})
					}
				}).catch(error => {
					console.error("Error fetching area data:", error);
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.page {
		min-height: 100vh;
		background: #F7F7F7;
	}
	
	.list-container {
		height: 89vh;
		padding: 24rpx;
		box-sizing: border-box;
		overflow: scroll;
	}

	.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;
				
				.vehicle-info {
					.code {
						font-size: 32rpx;
						font-weight: 600;
						color: #333;
					}
				}
			}
			
			.status-tag {
				padding: 6rpx 20rpx;
				border-radius: 30rpx;
				font-size: 24rpx;
				font-weight: 500;
				
				&.pending {
					background: #FFF0F0;
					color: #FF4D4F;
				}
				
				&.rejected {
					background: #F5F5F5;
					color: #999;
				}
				
				&.repairing {
					background: #EBF4FF;
					color: #4C97E7;
				}
				
				&.completed {
					background: #F0F9EB;
					color: #67C23A;
				}
			}
		}
		
		.divider {
			height: 1rpx;
			background: #f5f5f5;
			margin-bottom: 24rpx;
		}
		
		.card-body {
			.info-row {
				display: flex;
				justify-content: space-between;
				align-items: flex-start;
				margin-bottom: 16rpx;
				
				.label {
					font-size: 26rpx;
					color: #999;
					min-width: 140rpx;
				}
				
				.value {
					font-size: 28rpx;
					color: #333;
					flex: 1;
					text-align: right;
					line-height: 1.4;
				}
			}
			
			.pic-list {
				display: flex;
				flex-wrap: wrap;
				margin-top: 24rpx;
				gap: 16rpx;
				
				.img-wrapper {
					width: 140rpx;
					height: 140rpx;
					border-radius: 12rpx;
					overflow: hidden;
					
					image {
						width: 100%;
						height: 100%;
					}
				}
			}
		}
		
		.card-footer {
			.action-btns {
				display: flex;
				justify-content: flex-end;
				gap: 20rpx;
				
				.btn {
					min-width: 140rpx;
					height: 60rpx;
					border-radius: 30rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					font-size: 26rpx;
					
					&.secondary {
						border: 1rpx solid #ccc;
						color: #666;
						background: #fff;
					}
					
					&.primary {
						background: #4C97E7;
						color: #fff;
						box-shadow: 0 4rpx 10rpx rgba(76, 151, 231, 0.3);
					}
					
					&.success {
						background: #67C23A;
						color: #fff;
						box-shadow: 0 4rpx 10rpx rgba(103, 194, 58, 0.3);
					}
					
					&:active {
						opacity: 0.9;
					}
				}
			}
		}
	}

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