<template>
  <view class="warn-container">
    <!-- 顶部导航 -->
    <view class="warn-navbar">
      <image src="/static/ic_back.png" class="warn-back" @click="goBack" />
      <text class="warn-title">预警处理</text>
      <text class="warn-right-txt" @click="goStatistics">预警统计</text>
    </view>

    <!-- tab栏 -->
    <view class="warn-tabs">
      <view v-for="(tab, idx) in tabs" :key="idx" :class="['warn-tab', { active: idx === activeTab }]"
        @click="changeTab(idx)">
        {{ tab }}
        <view v-if="idx === activeTab" class="tab-underline"></view>
      </view>
    </view>

    <!-- 为每个标签页创建独立的scroll-view -->
    <view class="warn-list-container">
      <scroll-view 
        v-for="(tab, idx) in tabs" 
        :key="idx"
        v-show="idx === activeTab"
        class="warn-list" 
        scroll-y
        :refresher-enabled="true"
        refresher-background="#f7f7f7"
        :refresher-triggered="refresherTriggered" 
        @refresherrefresh="onRefresh" 
        @scrolltolower="loadMore"
        :lower-threshold="50">
        <view v-for="(item, index) in tabData[idx]" :key="index" class="warn-card" @click="goDetail2(item)">
          <view class="warn-row">
            <view class="warn-no">{{item.smallTypeName}}</view>
            <view class="warn-status">{{item.levelName}}</view>
          </view>
          <image class="warn-line-image" src="/static/ic_my_repair_03.png" />
          <view class="warn-info">预警内容：{{ item.description }}</view>
          <view class="warn-info">预警位置：{{ item.deviceName }}</view>
          <view class="warn-info">预警时间：{{ item.reportTime }}</view>
          <view class="warn-info">预警设备：{{ item.deviceName }}</view>
          <view v-if="[20, 30, 31, 32].includes(item.state)" class="warn-eval-wrap">
            <view class="warn-eval-btn">去处理</view>
          </view>
        </view>

        <!-- 底部加载提示 -->
        <view v-if="loading" style="text-align:center;color:#999;font-size:26rpx;padding:20rpx;">
          加载中...
        </view>
        <view v-if="noMore[idx]" style="text-align:center;color:#999;font-size:26rpx;padding:20rpx;">
          没有更多数据了
        </view>

      </scroll-view>
    </view>
  </view>
</template>

<script>
	export default {
		data() {
			return {
				tabs: ["待处理", "全部"],
				activeTab: 0,

				tabData: [
					[],
					[]
				], // 每个 tab 的数据
				pageNum: [1, 1], // 每个 tab 当前页码
				pageSize: 10,
				noMore: [false, false], // 每个 tab 是否没有更多数据
				tabLoaded: [false, false], // tab 是否加载过
				loading: false,
				refresherTriggered: false
			};
		},
		computed: {
			list() {
				return this.tabData[this.activeTab];
			}
		},
		created() {
			this.loadAllTabsData()
		},
		onShow() {
			uni.$once('refreshData', () => {
				this.loadAllTabsData()
			});
		},
		methods: {
			goBack() {
				uni.navigateBack();
			},
			// 切换 tab
			async changeTab(idx) {
				this.activeTab = idx;
				// 移除切换标签时的加载逻辑，避免重复调用接口
				/* 
				if (!this.tabLoaded[idx]) {
					this.onRefresh()
				}
				*/
			},
			// 下拉刷新
			async onRefresh() {
				this.refresherTriggered = true;
				this.pageNum[this.activeTab] = 1;
				this.noMore[this.activeTab] = false;
				this.tabData[this.activeTab] = [];
				await this.loadTabData(this.activeTab);
				this.refresherTriggered = false;
			},
			// 滚动加载更多
			async loadMore() {
				if (this.loading || this.noMore[this.activeTab]) return;
				this.pageNum[this.activeTab]++;
				await this.loadTabData(this.activeTab);
			},
			// 请求数据
			async loadTabData(idx) {
				this.loading = true;
				let params = {
					pageNum: this.pageNum[idx],
					pageSize: this.pageSize
				};
				// 待处理
				if (idx === 0) {
					params.states = [20, 30, 31, 32];
				}
				let res = await this.$u.api.getWarns(params);
				if (res.code == "200") {
					let rows = res.rows || [];
					if (rows.length < this.pageSize) {
						this.noMore[idx] = true;
					}
					this.$set(this.tabData, idx, [...this.tabData[idx], ...rows]);
				}
				this.tabLoaded[idx] = true;
				this.loading = false;
			},
			goDetail2(item) {
				const itemStr = encodeURIComponent(JSON.stringify(item));
				if([20, 30, 31, 32].includes(item.state)){
					uni.navigateTo({
						url: "/pages/sys/workbench/earlyWarning/warnDetail?item=" + itemStr,
					});
				}else{
					uni.navigateTo({
						url: "/pages/sys/workbench/earlyWarning/warnDetail?item=" + itemStr + "&pageType=detail",
					});
				}
			},
			goStatistics() {
				uni.navigateTo({
					url: "/pages/sys/workbench/earlyWarning/warnStatistics"
				});
			},
			
			// 添加预加载所有标签页数据的方法
			async loadAllTabsData() {
			  // 并行加载所有标签页数据，提高加载速度
			  const loadPromises = [0, 1].map((index) => {
			    return this.loadTabData(index);
			  });
			  
			  await Promise.all(loadPromises);
			  
			  // 标记所有标签页已加载
			  this.tabLoaded = [true, true];
			}
		}
	};
</script>

<style scoped>
	.warn-container {
		height: 100vh;
		background: #fff;
		display: flex;
		flex-direction: column;
		overflow: hidden;
	}

	.warn-navbar {
		width: 100%;
		height: 100rpx;
		display: flex;
		align-items: center;
		justify-content: stretch;
		position: relative;
		margin-top: 50rpx;
		flex-shrink: 0;
	}

	.warn-back {
		width: 18rpx;
		height: 32rpx;
		margin-left: 24rpx;
		margin-right: 78rpx;
	}

	.warn-right-txt {
		font-size: 24rpx;
		color: #0090FF;
		margin-right: 24rpx;
	}

	.warn-title {
		font-size: 36rpx;
		color: #000;
		margin-left: auto;
		margin-right: auto;
	}

	.warn-tabs {
		display: flex;
		align-items: center;
		justify-content: space-around;
		background: #fff;
		height: 80rpx;
		border-bottom: 1px solid #f0f0f0;
		flex-shrink: 0;
	}

	.warn-tab {
		flex: 1;
		text-align: center;
		font-size: 30rpx;
		color: #888;
		position: relative;
		font-weight: 500;
		padding: 0 0 10rpx 0;
		cursor: pointer;
	}

	.warn-tab.active {
		color: #2186ff;
		font-weight: bold;
	}

	.tab-underline {
		width: 60rpx;
		height: 6rpx;
		background: #2186ff;
		border-radius: 3rpx;
		margin: 0 auto;
		margin-top: 8rpx;
	}

	.warn-list-container {
		flex: 1;
		position: relative;
		background: #f7f7f7;
	}

	.warn-list {
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		padding: 0 24rpx;
		padding-bottom: 30rpx;
		height: calc(100vh - 80rpx - 32rpx - 100rpx - 50rpx); /* 减去顶部导航和tab区域高度 */
		box-sizing: border-box;
	}

	.warn-card {
		background: #fff;
		border-radius: 12rpx;
		margin-top: 24rpx;
		box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.03);
		padding-top: 25rpx;
		padding-bottom: 32rpx;
	}

	.warn-eval-wrap {
		display: flex;
		justify-content: center;
		width: 100%;
		margin-top: 20rpx;
	}

	.warn-eval-btn {
		width: 240rpx;
		height: 60rpx;
		text-align: center;
		line-height: 60rpx;
		background: #0090FF;
		border-radius: 40rpx;
		font-size: 24rpx;
		color: white;
	}

	.warn-row {
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-bottom: 12rpx;
		margin-top: 25rpx;
		margin-left: 19rpx;
		margin-right: 50rpx;
	}

	.warn-no {
		font-size: 24rpx;
		color: #0b0b0b;
		font-weight: 500;
	}

	.warn-status {
		font-size: 24rpx;
		font-weight: 500;
	}

	.warn-line-image {
		margin-left: 29rpx;
		margin-right: 39rpx;
		height: 2rpx;
		margin-bottom: 29rpx;
	}



	.warn-info {
		font-size: 24rpx;
		color: #888;
		margin-bottom: 30rpx;
		margin-left: 47rpx;
	}
</style>