<template>
  <div class="email-container">
    <transition
      enter-active-class="animate__animated animate__fadeInRight"
      leave-active-class="animate__animated animate__fadeOutRight"
      :duration="300"
    >
      <email-detail v-if="showDetail" @to-detail="handleToDetail" />
    </transition>
    <div v-show="!showDetail" style="height: 100%">
      <!-- 顶部导航栏 -->
      <van-nav-bar
        left-arrow
        @click-left="onClickLeft"
        @click-right="onClickRight"
        :title="'yukai@gat.jl'"
        :border="false"
      >
        <template #right>
          <img
            :src="getIconUrl('btn_search_black_24')"
            alt=""
            class="top-icon"
            style="margin-right: 5px"
          />
          <img :src="getIconUrl('btn_plus_black_24')" alt="" class="top-icon" />
        </template>
      </van-nav-bar>

      <!-- 顶部切换区域 -->
      <van-row :class="['top-title', `active-${activeIndex}`]">
        <van-col
          :class="{ active: activeIndex == index }"
          span="8"
          v-for="(item, index) in menu"
          :key="index"
          @click="activeIndex = index"
        >
          <img
            class="content-icon"
            :src="getIconUrl(activeIndex == index ? item.activeIcon : item.icon)"
            alt=""
          />
          <span>{{ item.text }}</span>
        </van-col>
      </van-row>

      <!-- 内容区域 -->
      <van-pull-refresh
        class="email-content"
        v-model="refreshLoading"
        @refresh="onRefresh"
        :disabled="!isAtTop"
      >
        <van-list
          class="email-list"
          v-model:loading="listLoading"
          :finished="finished"
          finished-text="没有更多了"
          @scroll="onScroll"
        >
          <div
            :class="['item', { read: index == 2 }]"
            v-for="(item, index) in 6"
            :key="index"
            @click="handleEmailClick"
            :data="item"
          >
            <div class="left">
              <van-badge dot :offset="[5, 10]">
                <img src="@/assets/icons/icon_my_head_64@2x.png" alt="" />
              </van-badge>
            </div>
            <div class="right">
              <div class="top">
                <span class="name">吉林省人才部</span>
                <span class="time">{{ formatDate(new Date()) }}</span>
              </div>
              <div class="title">吉林省优化人才管理政策</div>
              <div class="content">
                为深入实施人才强省战略,贯彻落实省委人才领导小组办公室《关于以人才工作驱动产业高质量...222
              </div>
            </div>
          </div>
        </van-list>
      </van-pull-refresh>
    </div>
  </div>
</template>

<script setup>
  import { getIconUrl, formatDate } from '@/utils/utils.js'
  import EmailDetail from './EmailDetail.vue'
  import { ref } from 'vue'
  import 'animate.css'
  import { useRouter } from 'vue-router'
  const router = useRouter()
  const onClickLeft = () => {
    router.push('/home')
  }
  const onClickRight = () => {}
  const activeIndex = ref(0)
  const menu = [
    { text: '收件箱', icon: 'icon_draft_gray_16', activeIcon: 'icon_draft_black_16' },
    { text: '已发送', icon: 'icon_send_gray_16', activeIcon: 'icon_send_black_16' },
    { text: '草稿箱', icon: 'icon_mailbox_gray_16', activeIcon: 'icon_mailbox_black_16' }
  ]

  const showDetail = ref(false)
  const refreshLoading = ref(false)
  const onRefresh = () => {
    setTimeout(() => {
      refreshLoading.value = false
    }, 1000)
  }

  const listLoading = ref(false)
  const finished = ref(true)

  const isAtTop = ref(true)

  const onScroll = ({ target: { scrollTop } }) => {
    isAtTop.value = scrollTop < 10
  }

  const handleEmailClick = () => {
    showDetail.value = true
  }

  const handleToDetail = value => {
    showDetail.value = value
  }
</script>

<style lang="scss" scoped>
  .email-container {
    width: 100%;
    height: 100vh;
    position: relative;
    overflow: hidden;

    .top-title {
      width: 100%;
      background-color: #fff;
      position: relative;

      // 添加一个公共的指示线元素
      &::after {
        content: '';
        position: absolute;
        bottom: 0;
        left: calc(100% / 6); // 初始位置在第一个选项中间
        transform: translateX(-50%);
        width: 20px;
        height: 5px;
        background: url('@/assets/icons/nav_line.png') no-repeat;
        background-size: 100% 100%;
        border-radius: 3px;
        bottom: 3px;
        transition: left 0.3s ease; // 只对left属性添加过渡效果
      }

      // 根据激活的选项调整指示线位置
      &.active-0::after {
        left: calc(100% / 6);
      }

      &.active-1::after {
        left: calc(100% / 2);
      }

      &.active-2::after {
        left: calc(5 * 100% / 6);
      }

      .van-col {
        height: 40px;
        display: flex;
        justify-content: center;
        align-items: center;

        &.active {
          span {
            color: #000;
          }
        }

        span {
          color: #7b7b7b;
          margin-left: 5px;
        }
      }
    }

    .email-content {
      width: 100%;
      height: calc(100% - 80px);
      background-color: rgba(248, 248, 248, 1);
      box-sizing: border-box;
      padding: 3px 16px 13px 16px;
      .email-list {
        width: 100%;
        height: 100%;
        overflow-y: auto;
        overflow-x: hidden;
        scrollbar-width: none; /* Firefox */
        -ms-overflow-style: none; /* IE and Edge */
        &::-webkit-scrollbar {
          display: none; /* Chrome, Safari and Opera */
        }
        .item {
          display: flex;
          padding: 16px;
          width: 100%;
          border-radius: 12px;
          background-color: #fff;
          margin-top: 12px;
          &:last-child {
            margin-bottom: 12px;
          }

          .left {
            img {
              width: 40px;
              height: 40px;
              border-radius: 50%;
              background: #e8f2fe;
            }
            :deep(.van-badge--dot) {
              width: 6px;
              height: 6px;
            }
          }

          .right {
            flex: 1;
            margin-left: 12px;

            .top {
              display: flex;
              justify-content: space-between;
              align-items: center;
              margin-bottom: 8px;

              .name {
                font-size: 16px;
                font-weight: 500;
                color: #333;
              }

              .time {
                font-size: 12px;
                color: #999;
              }
            }

            .title {
              font-size: 14px;
              color: #333;
              margin-bottom: 4px;
            }

            .content {
              font-size: 12px;
              /* 设置字体大小为12像素 */
              color: #999;
              /* 设置字体颜色为浅灰色 */
              overflow: hidden;
              /* 溢出内容隐藏 */
              text-overflow: ellipsis;
              /* 文本溢出时显示省略号 */
              display: -webkit-box;
              /* 将容器设置为弹性盒子 */
              -webkit-line-clamp: 2;
              /* 限制文本显示为2行 */
              -webkit-box-orient: vertical;
              /* 设置文本垂直排列 */
            }
          }

          &.read {
            .right {
              .name,
              .title {
                color: #666;
                font-weight: normal;
              }

              .content {
                color: #aaa;
              }
            }

            // 已读状态下隐藏红点
            .left :deep(.van-badge--dot) {
              display: none;
            }
            backdrop-filter: blur(5px);
          }
        }
      }
    }
  }

  /* 导航栏图标颜色覆盖 */
  :deep(.van-nav-bar .van-icon) {
    color: #333;
    font-size: 24px;
  }
</style>
