<template>
  <a-spin style="display: block" :loading="loading">
    <a-tabs v-model:activeKey="messageType" type="rounded" destroy-on-hide>
      <a-tab-pane v-for="item in tabList" :key="item.key">
        <template #title>
          <span> {{ item.title }}{{ formatUnreadLength(item.key) }} </span>
        </template>
        <a-result v-if="!renderList.length" status="404">
          <template #subtitle> 内容 </template>
        </a-result>
        <List :render-list="renderList" />
      </a-tab-pane>
      <template #extra>
        <a-button type="text" @click="refresh"> 刷新 </a-button>
      </template>
    </a-tabs>
  </a-spin>
</template>

<script lang="ts" setup>
import { ref, reactive, toRefs, computed } from 'vue'
import useLoading from '@/hooks/loading'
import { Work } from '@/types/work'
import { getWorkList } from '@/api/project/work'
import List from './list.vue'

interface TabItem {
  key: number
  title: string
  avatar?: string
}

const props = defineProps({
  prjId: {
    type: String,
    required: true
  }
})
const { loading, setLoading } = useLoading(true)
const messageType = ref(0)
const messageData = reactive<{
  renderList: Work[]
  messageList: Work[]
  taskList: Work[]
}>({
  renderList: [],
  messageList: [],
  taskList: []
})
toRefs(messageData)
const tabList: TabItem[] = [
  {
    key: 0,
    title: '进行中'
  },
  {
    key: 1,
    title: '已过期'
  },
  {
    key: 2,
    title: '已完成'
  }
]

async function fetchSourceData() {
  setLoading(true)
  try {
    const { list } = await getWorkList({
      page_no: 1,
      page_size: 9999,
      prj_id: props.prjId
    })
    messageData.messageList = list
  } catch (err) {
    // you can report use errorHandler or other
  } finally {
    setLoading(false)
  }
}

const renderList = computed(() => {
  return messageData.messageList.filter(
    item => messageType.value === item.status
  )
})

const getUnreadList = (type: number) => {
  const list = messageData.messageList.filter(
    item => item.type === type && !item.status
  )
  return list
}
const formatUnreadLength = (type: number) => {
  const list = getUnreadList(type)
  return list.length ? `(${list.length})` : ``
}

const refresh = () => {
  messageData.messageList = []
  fetchSourceData()
}

fetchSourceData()
</script>

<style scoped lang="less">
:deep(.arco-popover-popup-content) {
  padding: 0;
}

:deep(.arco-list-item-meta) {
  align-items: flex-start;
}
:deep(.arco-tabs-nav) {
  padding-bottom: 16px;
  border-bottom: 1px solid var(--color-neutral-3);
}
:deep(.arco-tabs-content) {
  padding-top: 0;
  .arco-result-subtitle {
    color: rgb(var(--gray-6));
  }
}
</style>
