<template>
  <view class="page">
    <!-- 头部搜索栏 -->
		 <view class="navbox">
        <view class="status_bar"></view>
        <view class="search-header ">
    
			<view class="back-icon" @click="goBack">
				<uni-icons type="left" size="30" color="#8B2316"></uni-icons>
			</view>
			<view class="search-input-wrapper">
				<view class="search-icon">
					<uni-icons type="search" size="20" color="#999"></uni-icons>
				</view>
				<input 
					class="search-input" 
					placeholder="搜索会议关键字" 
					v-model="searchKeyword"
					@input="onSearchInput"
					@confirm="onSearchConfirm"
					confirm-type="search"
				/>
				<view class="clear-icon" v-if="searchKeyword" @click="clearSearch">
					<uni-icons type="clear" size="20" color="#999"></uni-icons>
				</view>
			</view>
			<view class="cancel-btn" @click="goSearch">
				<text class="cancel-text">搜索</text>
			</view>
		</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,'YYYY.MM.DD')}}</view>
               <view class="text" v-if="formatDate(meeting.end_date,'YYYY.MM.DD')!=formatDate(meeting.begin_date,'YYYY.MM.DD')">{{ '-'+formatDate(meeting.end_date,'YYYY.MM.DD')}}</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>
</template>

<script setup>
import { ref, onMounted } from "vue";
import { onShow } 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 dayjs from "dayjs";
import timeImg from "@/static/play_long.png"
import empty from "@/components/empty/empty.vue";

const searchKeyword = 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 goSearch=()=>{
  if(!searchKeyword.value){
    uni.showToast({
      title: '请输入搜索内容',
      icon: 'none'
    });
    return;
  }
  currentPage.value = 1;
  hasMoreData.value = true;
  meetingList.value = [];
	loadMeetingData();
}

const goBack=()=>{
	uni.navigateBack({
		fail:()=>{
			uni.redirectTo({
				url:'/pages/index/index'
			})
		}
	});
}



// 加载会议数据
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:'',
      title: searchKeyword.value,
      month: '',
    };

    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 || [])];
      }
      console.log("会议列表数据:", meetingList.value);
      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;
  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;
  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;

.page {
  background-color: #f5f5f5;
  height: 100vh;
  display: flex;
  flex-direction: column;
}
// 头部搜索栏
.search-header {

		background-color: #eeeeee;
		
		display: flex;
		height:44px;
		align-items: center;
		padding:0 30rpx;
		gap: 20rpx;
		

		.search-input-wrapper {
			flex: 1;
			display: flex;
			align-items: center;
			background-color: #fff;
			border-radius: 20rpx;
			padding: 0 20rpx;
			height:70rpx;

			.search-icon {
				margin-right: 15rpx;
			}

			.search-input {
				flex: 1;
				height: 80rpx;
				
				font-size: 28rpx;
				color: #333;
			}

			.clear-icon {
				margin-left: 15rpx;
				padding: 10rpx;
			}
		}

		.cancel-btn {
			padding: 10rpx 20rpx;
			background:#8B2316;
			border-radius: 10rpx;

			.cancel-text {
				font-size: 28rpx;
				color: #fff;
			}
		}
	}

// 滚动内容区域
.scroll-content {
	position: fixed;
	top: calc(var(--status-bar-height) + 44px);
	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: 271rpx;
    position: relative;
    background-color: $white;
    width: 100%;
    height: calc(100vh - 275rpx);

    .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>
