<template>
  <view class="message-page">
    <BasicListLayout @register="register">
      <template #top>
        <view class="tabs-container">
          <view
              class="tab-item"
              :class="{ active: currentTab === 'A' }"
              @click="changeTab('A')"
          >
            待办
          </view>
          <view
              class="tab-item"
              :class="{ active: currentTab === 'B' }"
              @click="changeTab('B')"
          >
            已办
          </view>
        </view>
      </template>
      <template #default="{ data }">
        <view class="white-bg-color r-md p-15 mb-15 flex-row" @click="goToDetail(data)">
          <view class="card-left">
            <view class="card-title">{{ data.dbBt }}</view>
            <view class="card-desc" v-html="data.dbZy"></view>
            <view class="card-meta">
              <text>{{ data.createdTime }}</text>
              <text>{{ getTimeAgo(data.createdTime) }}</text>
            </view>
          </view>
          <view class="card-right" v-if="dbLxMap[data.dbLx]">
            <view class="tag" :class="dbLxMap[data.dbLx].className">{{ dbLxMap[data.dbLx].label }}</view>
          </view>
        </view>
      </template>
    </BasicListLayout>
  </view>
</template>

<script lang="ts" setup>
import {ref, onMounted} from "vue";
import { useLayout } from "@/components/BasicListLayout/hooks/useLayout";
import { dbListApi } from "@/api/base/server";
import { getTimeAgo } from "@/utils/dateUtils";
import { useUserStore } from "@/store/modules/user";
import { useDataStore } from "@/store/modules/data";
import { useDicStore } from "@/store/modules/dic";
const { findByPid } = useDicStore();
const { setDb } = useDataStore();
const { getUser } = useUserStore();

const dbLxMap = ref<any>({});

// 获取待办类型
const fetchDbLxMap = async () => {
  try {
    // 待办类型
    const res = await findByPid({pid: 186148807});
    if (res && res.result) {
      // 将res.result的list转换成对象，key为dictionaryValue，value为dictionary
      dbLxMap.value = res.result.reduce((acc: any, item: any) => {
        acc[item.dictionaryCode] = {
          value: item.dictionaryCode,
          label: item.dictionaryValue,
          className: item.remark
        };
        return acc;
      }, {});
    }
  } catch (error) {
    console.error("获取状态选项失败", error);
    // 使用默认状态
    dbLxMap.value = {};
  }
};

const [register, {reload, setParam}] = useLayout({
  api: dbListApi,
  componentProps: {
    auto: false
  },
});

const currentTab = ref('A'); // 0: 待办, 1: 已办

const fetchListData = async (tabIndex: string) => {
  setParam({
    dbZt: tabIndex,
    userId: getUser.id
  });
  reload();
};

const changeTab = (tabIndex: string) => {
  if (currentTab.value !== tabIndex) {
    currentTab.value = tabIndex;
    fetchListData(tabIndex);
  }
};

// 组件加载完成后，获取初始数据 (待办)
onMounted(() => {
  fetchDbLxMap();
  fetchListData(currentTab.value);
});

const goToDetail = (data: any) => {
  if (data && data.id) {
    setDb(data);
    uni.navigateTo({
      url: data.mobileUrl
    });
  }
};
</script>

<style scoped lang="scss">
.message-page {
  display: flex;
  flex-direction: column;
  height: 100vh; /* 或 calc(100vh - var(--window-top)) 如果需要精确计算 */
  background-color: #f4f5f7;
}

// 自定义导航栏
.custom-navbar {
  display: flex;
  align-items: center;
  justify-content: center; // 标题居中
  height: 44px; // 标准导航栏高度
  padding: 0 15px;
  padding-top: var(--status-bar-height); // 适配状态栏
  background-color: #ffffff;
  position: relative;
  border-bottom: 1px solid #eee;

  .navbar-title {
    font-size: 17px;
    font-weight: bold;
    color: #333;
  }

  .navbar-actions {
    position: absolute;
    right: 15px;
    top: var(--status-bar-height);
    height: 44px;
    display: flex;
    align-items: center;
    font-size: 18px; // ... 图标大小
    color: #666;
  }
}

// Tab 切换
.tabs-container {
  display: flex;
  background-color: #ffffff;
  height: 45px;
  border-bottom: 1px solid #eee;
  flex-shrink: 0; // 防止被压缩

  .tab-item {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 15px;
    color: #666;
    position: relative;
    cursor: pointer;

    &.active {
      color: #447ade;
      font-weight: bold;

      &::after {
        content: "";
        position: absolute;
        bottom: 0;
        left: 50%;
        transform: translateX(-50%);
        width: 30px;
        height: 3px;
        background-color: #447ade;
        border-radius: 2px;
      }
    }
  }
}

// 内容滚动区域
.content-scroll {
  flex: 1;
  overflow-y: auto;
  padding: 15px;
  box-sizing: border-box;
}

.loading-placeholder,
.empty-placeholder {
  padding: 40px 0;
  text-align: center;
  color: #999;
  font-size: 14px;
}

// 消息卡片
.card-left {
  flex: 1;
  margin-right: 15px;
  // 防止内容溢出，特别是标题和描述
  min-width: 0;

  .card-title {
    font-size: 16px;
    font-weight: bold;
    color: #333;
    margin-bottom: 5px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .card-desc {
    font-size: 13px;
    color: #666;
    margin-bottom: 10px;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    line-clamp: 2;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }

  .card-meta {
    font-size: 12px;
    color: #999;
    display: flex;
    justify-content: space-between;
  }
}

.card-right {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0; // 防止被压缩
  flex: 0 0 75px;

  .tag {
    border-radius: 4px;
    font-size: 13px;
    font-weight: bold;
    color: #ffffff;
    white-space: nowrap;
    display: flex;
    align-items: center;
    justify-content: center;
    flex: 1 0 1px;
    width: 100%;

    &.db-xs-qj {
      background-color: #447ade;
    }

    &.db-js-qj {
      background-color: #19be6b;
    }

    &.db-task {
      background-color: #ff9f0a; // 橙色
    }

    &.submit {
      background-color: #8e8e93; // 灰色
    }
  }

  .stats {
    display: flex;
    align-items: center;
    font-size: 12px;
    color: #999;

    .icon {
      margin-left: 8px;
      display: flex;
      align-items: center;
    }
  }
}
</style>
