<template>
   <view class="navbox">
		<view class="status_bar"></view>
    <uni-nav-bar
			left-icon="left"
			title="过往会议"
			@clickLeft="goBack"
			color="#8B2316"
			:border="false"
			backgroundColor="#eee"
		>
			<template #right>
				<view class="nav-right">
					<uni-icons type="search" size="24" color="#8B2316" @click="searchMeetHistroy"></uni-icons>
				</view>
			</template>
		</uni-nav-bar>
	</view>
  <view class="page">
    <!-- 筛选标签栏 -->
    <view class="filter-bar">
      <view class="filter-item" :class="{active:monthName}" @click="showTimePopup">
        <text>{{ monthName?monthName:'会议年份' }}</text>
        <up-image :src="monthName?selectOn:select" width="26rpx" height="26rpx"></up-image>
      </view>
      <view class="filter-divider"></view>
      <view class="filter-item" :class="{active:typeName}" @click="showLocationPopup">
        <text>{{ typeName?typeName:'会议月份' }}</text>
        <up-image :src="typeName?selectOn:select" width="26rpx" height="26rpx"></up-image>
      </view>
    </view>

    <!-- 可滚动内容区域 -->
    <scroll-view
      class="scroll-content"
      scroll-y="true"
      refresher-enabled="true"
      :refresher-triggered="isRefreshing"
      :refresher-threshold="100"
      @refresherrefresh="onRefresh"
      @scrolltolower="onLoadMore"
      :lower-threshold="100"
    >
      <!-- 会议列表 -->
      <view class="meeting-list" v-if="meetingList.length > 0">
        <view
          class="meeting-item" @click="goDetail(meeting)"
          v-for="(meeting, index) in meetingList"
          :key="meeting.id || index"
        >
          <view class="meeting-content">
            <text class="meeting-title">{{
              meeting.title || meeting.name
            }}</text>
            <view class="meeting-info">
              <view class="meeting-time">
                <view class="timebox">
                  <up-image
                    :src="timeImg"
                    width="24rpx"
                    height="24rpx"
                  ></up-image>
                </view>
               <view class="text"> {{ formatDate(meeting.begin_date) }}</view>
              </view>
              <view class="meeting-location">
				<uni-icons type="location" size="14" color="#999"></uni-icons>
				{{ meeting.location }}
			</view>
            </view>
          </view>
        </view>
      </view>

      <!-- 空状态 -->
      <view class="empty-state" v-else-if="!isRefreshing">
        <empty></empty>
      </view>

      <!-- 加载更多提示 -->
      <view class="load-more" v-if="showLoadMore">
        <view class="load-more-content" v-if="isLoadingMore">
          <uni-icons type="spinner-cycle" size="20" color="#999"></uni-icons>
          <text class="load-more-text">加载中...</text>
        </view>
        <view
          class="load-more-content"
          v-else-if="hasMoreData && meetingList.length > 0"
        >
          <text class="load-more-text">上拉加载更多</text>
        </view>
        <view
          class="load-more-content"
          v-else-if="meetingList.length > 0 && !isLoadingMore"
        >
          <text class="load-more-text">没有更多数据了</text>
        </view>
      </view>
    </scroll-view>

    <!-- 时间选择弹窗 -->
    <view class="time-popup" v-if="isTimePopupShow" @click="hideTimePopup">
      <view class="popup-mask"></view>
      <view class="time-popup-content" @click.stop>
        <view class="time-list">
          <view
            class="time-item"
            v-for="(month, index) in monthList"
            :key="index"
            :class="{ active: selectedMonth === month.value }"
            @click="selectMonth(month)"
          >
            <text>{{ month.label }}</text>
          </view>
        </view>
      </view>
    </view>

    <!-- 地区选择弹窗 -->
    <view
      class="time-popup"
      v-if="isLocationPopupShow"
      @click="hideLocationPopup"
    >
      <view class="popup-mask"></view>
      <view class="time-popup-content" @click.stop>
        <view class="time-list">
          <view
            class="time-item"
            v-for="(location, index) in locationList"
            :key="index"
            :class="{ active: selectedType === location.code }"
            @click="selectLocation(location)"
          >
            <text>{{ location.name }}</text>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, onMounted } from "vue";
import { onShow,onLoad,onHide} from "@dcloudio/uni-app";
import api from "@/api/api.js";
import navBar from "@/components/navBar/navBar.vue";
import select from "@/static/triangle_normal.png";
import selectOn from "@/static/triangle_green_theme.png"
import dayjs from "dayjs";
import timeImg from "@/static/play_long.png"
import navTo from "@/utils/navTo";
import empty from '@/components/empty/empty.vue';
// 弹窗状态
const isTimePopupShow = ref(false);
const isLocationPopupShow = ref(false);
const selectedMonth = ref("");
const monthName = ref("");
const selectedType = ref("");
const typeName = ref("");
// 下拉刷新和上拉加载状态
const isRefreshing = ref(false);
const isLoadingMore = ref(false);
const hasMoreData = ref(true);
const showLoadMore = ref(true);
const currentPage = ref(1);
const pageSize = ref(10);

// 会议列表数据
const meetingList = ref([]);
const goBack=()=>{
	uni.navigateBack({
		fail:()=>{
			uni.redirectTo({
				url:'/pages/index/index'
			})
		}
	});
}
onShow(() => {
	
});
onHide(() => {
	
});
// 年份数据
const monthList = ref([]);
const generateYearList = () => {
  const years = [];
  const currentDate = new Date();
  const currentYear = currentDate.getFullYear();
  // 从今年到过去10年
  for (let i = 0; i < 10; i++) {
    const year = currentYear - i;
    years.push({
      value: year.toString(),
      label: `${year}年`,
      year: year,
    });
  }
  monthList.value = years;
  console.log("生成年份列表:", monthList.value);
};

// 月份数据
const locationList = ref([]);

// 生成月份列表
const generateMonthList = () => {
  const months = [];
  const currentDate = new Date();
  const currentYear = currentDate.getFullYear();
  const currentMonth = currentDate.getMonth() + 1; // 当前月份（1-12）

  // 如果选择的是今年，只显示到当前月份
  if (selectedMonth.value && selectedMonth.value === currentYear.toString()) {
    for (let i = 1; i <= currentMonth; i++) {
      months.push({
        code: i,
        name: `${i}月`,
      });
    }
  } else {
    // 其他年份显示完整的12个月
    for (let i = 1; i <= 12; i++) {
      months.push({
        code: i,
        name: `${i}月`,
      });
    }
  }

  locationList.value = months;
  console.log("生成月份列表:", locationList.value);
};

// 页面加载
onMounted(() => {
  generateYearList();
  generateMonthList();
});
const searchMeetHistroy=()=>{
   navTo({	
	 url:'/pages_app/meetHistroySearch/meetHistroySearch'
   })
}
onLoad(() => {
  currentPage.value = 1;
  hasMoreData.value = true;
  loadMeetingData();
});

// 加载会议数据
const loadMeetingData = async (isRefresh = false) => {
  if (isLoadingMore.value && !isRefresh) return;

  if (isRefresh) {
    isRefreshing.value = true;
  } else {
    isLoadingMore.value = true;
  }

  try {
    const params = {
      page: currentPage.value,
      pageSize: pageSize.value,
      year: selectedMonth.value,
      title: "",
      month: selectedType.value,
    };

    console.log("获取会议列表参数:", params);
    const response = await api.meetingHistoryList(params);

    if (response && response.code === 200) {
      const { list, totalPage, pageNumber } = response.data || {};

      if (isRefresh) {
        meetingList.value = list || [];
        currentPage.value = 1;
      } else {
        meetingList.value = [...meetingList.value, ...(list || [])];
      }

      hasMoreData.value = totalPage > pageNumber;
      showLoadMore.value = meetingList.value.length > 0;
    }
  } catch (error) {
    console.error("加载会议数据失败:", error);
  } finally {
    isRefreshing.value = false;
    isLoadingMore.value = false;
  }
};

// 下拉刷新
const onRefresh = () => {
  currentPage.value = 1;
  hasMoreData.value = true;
  loadMeetingData(true);
};

// 上拉加载更多
const onLoadMore = () => {
  if (!hasMoreData.value || isLoadingMore.value) return;
  currentPage.value++;
  loadMeetingData();
};

// 显示时间选择弹窗
const showTimePopup = () => {
  isTimePopupShow.value = !isTimePopupShow.value;
};

// 隐藏时间选择弹窗
const hideTimePopup = () => {
  isTimePopupShow.value = false;
};

// 选择年份
const selectMonth = (month) => {
  selectedMonth.value = month.value;
  monthName.value = month.label;
  console.log("选择年份:", month.label);

  // 选择年份后重新生成月份列表
  generateMonthList();

  // 重置月份选择
  selectedType.value = "";

  // 选择年份后重新加载数据
  loadMeetingData(true);
  hideTimePopup();
};

// 显示地区选择弹窗
const showLocationPopup = () => {
  if (selectedMonth.value === "") {
    uni.showToast({
      title: "请先选择年份",
      icon: "none",
    });
    return;
  }
  isLocationPopupShow.value = !isLocationPopupShow.value;
};

// 隐藏地区选择弹窗
const hideLocationPopup = () => {
  isLocationPopupShow.value = false;
};

// 选择月份
const selectLocation = (location) => {
  selectedType.value = location.code;
  typeName.value = location.name;
  console.log("选择月份:", location.name);
  // 选择月份后重新加载数据
  loadMeetingData(true);
  hideLocationPopup();
};

// 格式化日期
const formatDate = (dateStr) => {
  if (!dateStr) return "";
  return dayjs(dateStr).format("YYYY.MM.DD");
};
const goDetail=(item)=>{
		const encoded = encodeURIComponent(item.path);
    let imgPath=encodeURIComponent('https://doc.igandan.com/app/html/img/2016/20160714132557.png');
		uni.navigateTo({
			url: `/pages_app/webview/webview?url=${encoded}&sharetitle=${item.title}&bg=1&type=live_old&imgPath=${imgPath}&share=1&title=会议详情`
		});
	}
</script>

<style scoped lang="scss">
$white: #fff;
$gray: #999;
$gray-dark: #666;
$gray-light: #eee;
$border-color: #e0e0e0;
$theme-color: #8b2316;
// .nav-right{
// 	margin-top: 50rpx;
// }
.page {
  background-color: #f5f5f5;
  height: 100vh;
  display: flex;
  flex-direction: column;
}

// 筛选标签栏
.filter-bar {
  position: fixed;
  top: calc(var(--status-bar-height) + 44px);
  width: 100%;
  z-index: 2;
  background-color: $white;
  height: 88rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 30rpx;
  border-bottom: 2rpx solid $gray-light;

  .filter-item {
    display: flex;
    align-items: center;
	justify-content: center;
    padding: 0 20rpx;
    font-size: 28rpx;
    color:$gray-dark;

    text {
      margin-right: 10rpx;
    }

    &.active {
		color: #8B2316;
    }
  }

  .filter-divider {
    width: 2rpx;
    height: 32rpx;
    background-color: $border-color;
    margin: 0 60rpx;
  }
}

// 滚动内容区域
.scroll-content {
	position: fixed;
	top: calc(var(--status-bar-height) + 44px + 88rpx);
	left: 0;
	right: 0;
	bottom: 0;

  background-color: $white;
  
}

// 会议列表
.meeting-list {
  padding: 0 30rpx;
}

.meeting-item {
  padding: 30rpx 0;
  border-bottom: 1rpx solid #f0f0f0;

  &:last-child {
    border-bottom: none;
  }
}

.meeting-content {
  .meeting-title {
    font-size: 32rpx;
    color: #333;
    line-height: 1.4;
    margin-bottom: 16rpx;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    line-clamp: 2;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .meeting-info {
    display: flex;
    justify-content: space-between;
    gap: 8rpx;

    .meeting-time,
    .meeting-location {
		.timebox{
			margin-top: -4rpx;
			margin-right: 8rpx;
		}
		display: flex;
		align-items: center;
		
      font-size: 24rpx;
      color: #999;
    }
  }
}

// 空状态
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 100rpx 0;

  .empty-icon {
    margin-bottom: 20rpx;
  }

  .empty-text {
    color: #999;
    font-size: 28rpx;
  }
}

// 加载更多
.load-more {
  padding: 30rpx 0;
  display: flex;
  justify-content: center;

  .load-more-content {
    display: flex;
    align-items: center;
    gap: 10rpx;

    .load-more-text {
      font-size: 24rpx;
      color: #999;
    }
  }
}

// 时间选择弹窗
.time-popup {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 1000;
  display: flex;

  .popup-mask {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
  }

  .time-popup-content {
    margin-top: calc(var(--status-bar-height) + 44px + 90rpx);;
    position: relative;
    background-color: $white;
    width: 100%;
    height: calc(100vh - calc(var(--status-bar-height) - 44px - 88rpx));

    .time-list {
      padding: 0;
      height: 100%;
      overflow-y: auto;

      .time-item {
        padding: 25rpx 60rpx;
        font-size: 32rpx;
        color: #666;
        border-bottom: 2rpx solid #f0f0f0;
        cursor: pointer;
        transition: all 0.3s ease;
        display: flex;
        align-items: center;

        &:hover {
          background-color: #f8f8f8;
        }

        &.active {
          background-color: #f8f8f8;
          color: $theme-color;
        }
      }
    }
  }
}
</style>
