<template>

  <!-- 顶部Tabs栏 -->
  <view class="fixed-tabs" >
    <uv-tabs :list="topTabs" @click="click"></uv-tabs>
  </view>

  <!-- 内容区域 -->
  <view class="content-wrapper" :style="{ paddingTop: (  tabsHeight) + 'px' }">
    <!-- 日期条 -->
    <DateBar :days="weekDays" v-model="selectedDate" />

    <view>
      <uv-calendar ref="calendar"  mode="single" @change="handleConfirm" closeOnClickConfirm="false"></uv-calendar>
      <button @click="openCalendar">选择日期</button>
    </view>

    <!-- 时间轴表格 -->
    <TimeTable :hours="hours" :events="eventsInDay" />
  </view>

  <!-- 悬浮新建按钮 -->
  <FabPlus @click="showAdd = true" />

  <!-- 新建日程弹窗 -->
  <AddEventModal :show="showAdd" @ok="addEvent" @cancel="showAdd = false" />

  <!-- 底部导航 -->
  <uv-tabbar :value="value" @change="index=>value = index">
    <uv-tabbar-item text="首页" icon="home"></uv-tabbar-item>
    <uv-tabbar-item text="放映厅" icon="photo"></uv-tabbar-item>
    <uv-tabbar-item text="直播" icon="play-right"></uv-tabbar-item>
    <uv-tabbar-item text="我的" icon="account"></uv-tabbar-item>
  </uv-tabbar>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue';

import DateBar from '@/components/DateBar.vue';
import TimeTable from '@/components/TimeTable.vue';
import FabPlus from '@/components/FabPlus.vue';

import AddEventModal from '@/components/AddEventModal.vue';

// 获取状态栏高度

const tabsHeight = ref(44); // tabs 高度，单位：px


// 顶部tabs选项
const topTabs = [
  { name: '日程编辑', value: 0 },
  { name: '内容看板', value: 1 },
  { name: '待办事项', value: 2 },
  { name: '消息内容', value: 3 }
];
const topTabValue = ref(0);

function click(item) {
  topTabValue.value = item.value;
  console.log('切换tab:', item.name);
}

// 生成一周日期
function getWeekDays() {
  const days = [];
  const base = new Date();
  base.setHours(0,0,0,0);
  for(let i=-2; i<=4; i++) {
    const d = new Date(base);
    d.setDate(base.getDate() + i);
    const ymd = d.toISOString().slice(0,10);
    days.push({
      date: ymd,
      weekStr: '日一二三四五六'[d.getDay()],
      day: d.getDate(),
      tip: '',
      isHoliday: d.getDay() === 0 || d.getDay() === 6,
      isWeekend: d.getDay() === 6 || d.getDay() === 0,
    });
  }
  return days;
}
const weekDays = ref(getWeekDays());
const selectedDate = ref(weekDays.value.find(i=>i.date === new Date().toISOString().slice(0,10))?.date || weekDays.value[2].date);

// 小时段
const hours = Array.from({length: 13}, (_,i)=>i+8); // 8~20点
// 示例日程
const allEvents = ref([
  {id:1,title:'日程标题',startHour:15,startMin:30,color:'#e3fae6',date:weekDays.value[3].date},
  {id:2,title:'日程标题',startHour:16,startMin:0,color:'#fae1e1',date:weekDays.value[3].date},
  {id:3,title:'日程标题',startHour:23,startMin:0,color:'#e3fae6',date:weekDays.value[3].date},
]);

// 根据当前选择日期过滤
const eventsInDay = computed(() =>
  allEvents.value.filter(e=>e.date===selectedDate.value)
);

const calendar = ref(null)
// 打开日历方法
const openCalendar = () => {
  if (calendar.value) {
    calendar.value.open()
  }
}
const handleConfirm = (e) => {
  console.log('日历选择：', e)
}

// 悬浮按钮/弹窗控制
const showAdd = ref(false);
function addEvent(e) {
  // e是{title, startHour, startMin, color}，补充date
  allEvents.value.push({
    ...e, date: selectedDate.value,
    id: Date.now()
  });
  showAdd.value = false;
}
const value=ref(0);

// 底部导航
const tabbarItems = [
  { label: '任务列表', value: 0, icon: 'list' },
  { label: '首页', value: 1, icon: 'home' },
  { label: '工作台', value: 2, icon: 'calendar' },
  { label: '月度考核', value: 3, icon: 'integral' },
  { label: '管理', value: 4, icon: 'account' }
];
const tabbarVal = ref(1);
</script>

<style lang="scss" scoped>

.status_bar {
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 1000;
}

.fixed-tabs {
  position: fixed;
  left: 0;
  right: 0;
  width: 100%;
  background: #fff;
  z-index: 999;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.06);
}

.content-wrapper {
  min-height: 100vh;
}

:deep(.bottom-tabbar) { z-index: 1000 !important; }

.schedule-timeline {
  padding-bottom: 130rpx !important;
}
</style>