<script setup lang="ts">
import { ref, onMounted, computed, onUnmounted } from 'vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import { GlobalStore } from '@/stores/index'
import type { TabsPaneContext } from 'element-plus'
import { User_News_See } from '@/api/user/index'
import { icons } from '@/utils/common'

interface NoticeType {
  CreateTime: string
  Id: number
  T_Tag: number
  T_Title: string
  T_Url: string
  T_uuid: string
}

let tabs__content: HTMLDivElement
const router = useRouter()
const globalStore = GlobalStore()
const activeName = ref('first')
const newsList = computed<NoticeType[]>(() => {
  return globalStore.noticeList
})

const handleClick = (tab: TabsPaneContext) => {
  const { props } = tab
  globalStore.SET_NoticeList([], true)
  globalStore.SET_NoticePage(1)
  switch (props.name) {
    case 'first':
      globalStore.SET_User_News_List()
      break
    case 'second':
      globalStore.SET_User_News_List(1)
      break
  }
}

/**
 * 查看通知消息
 */
const previewNotice = async (item: NoticeType) => {
  router.push(item.T_Url)
  const res: any = await User_News_See({ User_tokey: globalStore.GET_User_tokey, Id: item.Id })
  if (res.Code) {
    ElMessage({
      type: 'success',
      message: '确认查看消息！'
    })
    globalStore.SET_NoticeList(
      newsList.value.filter((notice: NoticeType) => item.Id !== notice.Id),
      true
    )
  }
}

const ScrollHandle = (e: any) => {
  const { target } = e
  if (
    target.scrollHeight - target.scrollTop === target.clientHeight &&
    globalStore.noticeTotal !== newsList.value.length
  ) {
    globalStore.SET_NoticePage()
    globalStore.SET_User_News_List()
  }
}

onMounted(() => {
  tabs__content = document.querySelector('#home')?.getElementsByClassName('el-tabs__content')[0] as HTMLDivElement

  tabs__content?.addEventListener('scroll', ScrollHandle)
})
onUnmounted(() => {
  tabs__content.removeEventListener('scroll', ScrollHandle)
})
</script>

<template>
  <div class="home">
    <div class="notice">
      <h1>消息通知</h1>
      <el-tabs v-model="activeName" @tab-click="handleClick" id="home">
        <transition-group
          appear
          leave-active-class="animate__animated animate__fadeOutRight"
          enter-active-class="animate__animated animate__fadeInLeft"
        >
          <el-tab-pane label="未读" name="first" key="first">
            <div class="item" v-for="item in newsList" :key="item.T_uuid + 'first'" @click="previewNotice(item)">
              <div class="item-box">
                <el-badge :value="item.T_Tag === 0 ? 'new' : ''">{{ item.T_Title }}</el-badge>
              </div>
              <span>{{ item.CreateTime.split(' ')[0] }}</span>
            </div>
          </el-tab-pane>
          <el-tab-pane label="全部" name="second" key="second">
            <div v-for="item in newsList" :key="item.T_uuid + 'second'" class="item" @click="previewNotice(item)">
              <div class="item-box">
                <el-badge :value="item.T_Tag === 0 ? 'new' : ''">{{ item.T_Title }}</el-badge>
              </div>
              <span>{{ item.CreateTime.split(' ')[0] }}</span>
            </div>
          </el-tab-pane>
        </transition-group>
      </el-tabs>
    </div>
  </div>
</template>

<style scoped lang="scss">
.home {
  width: 100%;
  height: 100%;
  padding-top: 50px;
  display: flex;
  justify-content: center;
  // align-items: center;
  .notice {
    width: 50%;
    height: 60%;
    padding: 15px;
    display: flex;
    flex-direction: column;
    align-items: center;
    box-shadow: 0px 0px 6px #ccc;
    .el-tabs {
      width: 100%;
      height: calc(100% - 25px);
      :deep(.el-tabs__content) {
        padding-top: 10px;
        height: calc(100% - 60px);
        overflow-y: scroll;
      }
    }
    .item {
      width: 100%;
      display: flex;
      padding: 5px 16px;
      cursor: pointer;
      transition: transform 0.5s ease-in-out;
      justify-content: space-between;
      &:hover {
        background-color: #ecf5ff;
        color: #409eff;
        box-shadow: 0 0 6px #dfdfdf;
        transform: translateX(-2px);
      }
      .item-box {
        width: 80%;
        .el-badge {
          max-width: 100%;
          .item-content {
            overflow: hidden;
            white-space: nowrap;
            text-overflow: ellipsis;
            max-width: 100%;
            display: inline-block;
          }
        }
      }
      span {
        white-space: nowrap;
      }
    }
  }
  h1 {
    font-size: 2rem;
  }
}
</style>
