<template>
	<view>
		<scroll-view scroll-y class="page">
			<cu-custom bgColor="bg-blue" :isBack="true"><block slot="backText">返回</block><block slot="content">预警列表</block></cu-custom>
			
			<!-- 搜索框 -->
			<view class="search-box">
				<input
					class="search-input"
					type="text"
					v-model="searchKeyword"
					placeholder="请输入医院或科室名称"
					placeholder-class="search-placeholder"
				/>
				<text class="search-icon" v-if="!searchKeyword">🔍</text>
				<text class="clear-icon" v-else @click="clearSearch">✕</text>
			</view>
			
			<!-- 空数据提示 -->
			<view v-if="!filteredWarnListData || filteredWarnListData.length === 0" class="empty-container">
				<text class="empty-icon">🔔</text>
				<text class="empty-text">{{searchKeyword ? '未找到匹配的预警记录' : '暂无预警数据'}}</text>
			</view>
			
			<!-- 表格展示 -->
			<view v-else class="table-wrapper">
				<view class="table-container">
					<view class="table-header">
						<view class="table-cell cell-hospital">医院</view>
						<view class="table-cell cell-dept">科室</view>
						<view class="table-cell cell-type">类型</view>
						<view class="table-cell cell-time">时间</view>
						<view class="table-cell cell-status">状态</view>
					</view>
					<view class="table-row" v-for="(item, index) in currentPageData" :key="index">
						<view class="table-cell cell-hospital">{{item.hospital || '-'}}</view>
						<view class="table-cell cell-dept">{{item.department || '-'}}</view>
						<view class="table-cell cell-type">
							<text class="type-tag" :class="'type-' + getTypeClass(item.type)">{{item.type || '-'}}</text>
						</view>
						<view class="table-cell cell-time">{{formatTime(item.create_time)}}</view>
						<view class="table-cell cell-status">
							<text class="status-tag" :class="'status-' + getStatusClass(item.status)">{{getStatusText(item.status)}}</text>
						</view>
					</view>
				</view>
				
				<!-- 分页控件 -->
				<view class="pagination-wrapper">
					<view class="pagination-info">
						<text>共 {{totalItems}} 条，第 {{currentPage}}/{{totalPages}} 页</text>
					</view>
					<view class="pagination-controls">
						<button class="page-btn" :disabled="currentPage === 1" @click="changePage('prev')">上一页</button>
						<button class="page-btn" :disabled="currentPage === totalPages" @click="changePage('next')">下一页</button>
					</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				hospital: '',
				sortHospital: '',
				department: '',
				name: '',
				duty: '',
				warnListData: [],
				searchKeyword: '',
				currentPage: 1,
				pageSize: 20,
				totalItems: 0,
				totalPages: 0
			}
		},
		computed: {
			filteredWarnListData() {
				if (!this.searchKeyword) {
					return this.warnListData
				}
				const keyword = this.searchKeyword.toLowerCase()
				return this.warnListData.filter(item => {
					const hospitalMatch = item.hospital && item.hospital.toLowerCase().includes(keyword)
					const deptMatch = item.department && item.department.toLowerCase().includes(keyword)
					return hospitalMatch || deptMatch
				})
			},
			currentPageData() {
				const start = (this.currentPage - 1) * this.pageSize
				const end = start + this.pageSize
				return this.filteredWarnListData.slice(start, end)
			}
		},
		watch: {
			warnListData(newVal) {
				this.currentPage = 1
			},
			searchKeyword() {
				this.currentPage = 1
			},
			filteredWarnListData(newVal) {
				this.totalItems = newVal.length
				this.totalPages = Math.ceil(this.totalItems / this.pageSize)
				if (this.currentPage > this.totalPages && this.totalPages > 0) {
					this.currentPage = this.totalPages
				}
			}
		},
		mounted(){
			this.hospital = getApp().globalData.hospital
			this.sortHospital = getApp().globalData.sortHospital
			this.department = getApp().globalData.department
			this.name = getApp().globalData.name
			this.duty = getApp().globalData.duty
			this.getWarnListData()
		},
		methods: {
			// 获取预警列表数据
			getWarnListData(){
				let that = this;
				uni.request({
					url: 'https://lekapi.opmonitor.com/?c=app_api&a=getWarnListData',
					data: {hospital: that.hospital, sortHospital: that.sortHospital, department: that.department, duty: that.duty},
					header: {
						'Content-type': 'application/json'
					},
					success: function(res){
						that.warnListData = res.data.data || []
					},
					fail: () => {
						console.info('获取预警列表失败')
					}
				})
			},
			// 格式化时间 m-d H:i
			formatTime(timeStr) {
				if (!timeStr) return '-'
				const date = new Date(timeStr.replace(/-/g, '/'))
				const month = String(date.getMonth() + 1).padStart(2, '0')
				const day = String(date.getDate()).padStart(2, '0')
				const hours = String(date.getHours()).padStart(2, '0')
				const minutes = String(date.getMinutes()).padStart(2, '0')
				return `${month}-${day} ${hours}:${minutes}`
			},
			// 获取类型样式类
			getTypeClass(type) {
				if (!type) return 'default'
				type = type.toLowerCase()
				if (type.includes('重量') || type.includes('weight')) return 'weight'
				if (type.includes('温度') || type.includes('temp')) return 'temp'
				if (type.includes('时间') || type.includes('time')) return 'time'
				return 'default'
			},
			// 获取状态样式类
			getStatusClass(status) {
				if (!status) return 'default'
				status = String(status).toLowerCase()
				if (status === '1' || status === '已处理' || status === 'handled') return 'handled'
				if (status === '0' || status === '未处理' || status === 'pending') return 'pending'
				return 'default'
			},
			// 获取状态文本
			getStatusText(status) {
				if (!status) return '-'
				if (status === '1' || status === 'handled') return '已处理'
				if (status === '0' || status === 'pending') return '未处理'
				return status
			},
			// 切换页面
			changePage(direction) {
				if (direction === 'prev' && this.currentPage > 1) {
					this.currentPage--
				} else if (direction === 'next' && this.currentPage < this.totalPages) {
					this.currentPage++
				}
			},
			// 清空搜索
			clearSearch() {
				this.searchKeyword = ''
			}
		}
	}
</script>

<style>
	.page {
		width: 100vw;
		height: 100vh;
	}
	
	/* 搜索框 */
	.search-box {
		margin: 12rpx;
		padding: 0 24rpx;
		background-color: #fff;
		border-radius: 30rpx;
		display: flex;
		align-items: center;
		box-shadow: 0 2rpx 12rpx rgba(255, 152, 0, 0.15);
		border: 1px solid #ffe0b2;
	}
	
	.search-input {
		flex: 1;
		height: 70rpx;
		font-size: 26rpx;
		color: #333;
	}
	
	.search-placeholder {
		color: #999;
	}
	
	.search-icon, .clear-icon {
		font-size: 28rpx;
		margin-left: 10rpx;
	}
	
	.clear-icon {
		color: #999;
		padding: 10rpx;
	}
	
	/* 空数据容器 */
	.empty-container {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 200rpx 0;
	}
	
	.empty-icon {
		font-size: 120rpx;
		margin-bottom: 30rpx;
	}
	
	.empty-text {
		font-size: 32rpx;
		color: #999;
	}
	
	/* 表格包裹层 */
	.table-wrapper {
		height: calc(100vh - 88rpx);
		display: flex;
		flex-direction: column;
	}
	
	/* 表格容器 - 预警列表使用橙色系 */
	.table-container {
		flex: 1;
		overflow-y: auto;
		margin: 12rpx;
		background-color: #fff;
		border-radius: 8rpx;
		overflow-x: hidden;
		box-shadow: 0 2rpx 8rpx rgba(255, 152, 0, 0.1);
		border: 1px solid #ffe0b2;
	}
	
	/* 表头 */
	.table-header {
		display: flex;
		background: linear-gradient(90deg, #ff9800 0%, #f57c00 100%);
		color: #fff;
		font-weight: 600;
		font-size: 22rpx;
		padding: 0;
		position: sticky;
		top: 0;
		z-index: 10;
	}
	
	/* 表格行 */
	.table-row {
		display: flex;
		border-bottom: 1px solid #fff3e0;
		background-color: #fff;
		min-height: 68rpx;
	}
	
	.table-row:last-child {
		border-bottom: none;
	}
	
	.table-row:nth-child(even) {
		background-color: #fffbf5;
	}
	
	/* 表格单元格 */
	.table-cell {
		padding: 12rpx 10rpx;
		font-size: 22rpx;
		color: #333;
		display: flex;
		align-items: center;
		word-break: break-all;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}
	
	.cell-hospital {
		flex: 1.5;
		min-width: 140rpx;
	}
	
	.cell-dept {
		flex: 1.2;
		min-width: 120rpx;
	}
	
	.cell-type {
		flex: 1;
		min-width: 100rpx;
		justify-content: center;
	}
	
	.cell-time {
		flex: 1.2;
		min-width: 120rpx;
	}
	
	.cell-status {
		flex: 0.9;
		min-width: 90rpx;
		justify-content: center;
	}
	
	/* 类型标签 */
	.type-tag {
		padding: 4rpx 12rpx;
		border-radius: 8rpx;
		font-size: 18rpx;
	}
	
	.type-weight {
		background-color: #e3f2fd;
		color: #1976d2;
	}
	
	.type-temp {
		background-color: #fce4ec;
		color: #c2185b;
	}
	
	.type-time {
		background-color: #fff3e0;
		color: #e65100;
	}
	
	.type-default {
		background-color: #f5f5f5;
		color: #757575;
	}
	
	/* 状态标签 */
	.status-tag {
		padding: 4rpx 12rpx;
		border-radius: 8rpx;
		font-size: 18rpx;
	}
	
	.status-pending {
		background-color: #ffebee;
		color: #d32f2f;
	}
	
	.status-handled {
		background-color: #e8f5e9;
		color: #388e3c;
	}
	
	.status-default {
		background-color: #f5f5f5;
		color: #757575;
	}
	
	/* 分页包裹层 */
	.pagination-wrapper {
		margin: 0 12rpx 12rpx 12rpx;
		padding: 14rpx 20rpx;
		background-color: #fff;
		border-radius: 8rpx;
		box-shadow: 0 2rpx 8rpx rgba(255, 152, 0, 0.1);
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
	
	.pagination-info {
		font-size: 22rpx;
		color: #666;
	}
	
	.pagination-controls {
		display: flex;
		gap: 12rpx;
	}
	
	.page-btn {
		padding: 8rpx 24rpx;
		background: linear-gradient(135deg, #ff9800 0%, #f57c00 100%);
		color: #fff;
		border-radius: 16rpx;
		font-size: 22rpx;
		border: none;
		box-shadow: 0 1rpx 6rpx rgba(255, 152, 0, 0.25);
	}
	
	.page-btn[disabled] {
		background: #ccc;
		box-shadow: none;
		opacity: 0.5;
	}
</style>
