<template>
  <a-card
    class="general-card"
    title="项目动态"
    :header-style="{ paddingBottom: '0' }"
    :body-style="{ padding: '15px 20px 13px 20px' }"
  >
    <div style="height: 253px">
      <a-collapse :bordered="false" accordion>
        <a-collapse-item
          v-for="item in prjLogs"
          :key="item.id"
          :header="item.prj_name"
        >
          <template #extra>
            <span class="extra"> {{ item.ts }}</span>
          </template>
          <div class="item">
            <div v-html="item.content"></div>
            <a-link
              style="font-size: 12px"
              @click="redirectProjectDetailPage(item.prj_id)"
              >查看</a-link
            >
          </div>
        </a-collapse-item>
      </a-collapse>
    </div>
  </a-card>
</template>

<script lang="ts" setup>
import { getProjectLogs } from '@/api/project/info'
import { useWindows } from '@/hooks/useWindow'
import { ProjectLog } from '@/types/project'
import { onMounted, ref } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()
const expandId = ref()
const prjLogs = ref<ProjectLog[]>([])

// 获取项目动态日志
const getLogs = async () => {
  const { list } = await getProjectLogs({
    page_no: 1,
    page_size: 6
  })
  prjLogs.value = list
  if (list.length > 0) {
    expandId.value = list[0].id
  }
}
const { addWindow } = useWindows([])
// 跳转到详情页面
const redirectProjectDetailPage = (id: string) => {
  const routeData = router.resolve({
    name: 'ProjectDetail',
    params: { prjId: id }
  })
  const myWindow = window.open(routeData.href, id)
  const windowSubset = {
    close: window.close.bind(myWindow)
  }
  // 将这个新对象转换为字符串
  const windowSubsetString = JSON.stringify(windowSubset, (key, value) => {
    // 如果值是函数，转换为字符串表示的函数（例如："function() {...}"）
    if (typeof value === 'function') {
      return value.toString()
    }
    return value
  })
  addWindow({ id, value: JSON.stringify(windowSubsetString)! })
}

onMounted(() => {
  getLogs()
})
</script>

<style scoped lang="less">
.item {
  display: flex;
  flex-direction: row;
  font-size: 12px;
  justify-content: space-between;
  width: 100%;
  .item-content {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    margin-left: 4px;
    color: var(--color-text-2);
    text-decoration: none;
    font-size: 12px;
    cursor: pointer;
  }
}
.extra {
  font-size: 12px;
  color: var(--color-text-3);
}
:deep(.arco-list-item-action) {
  font-size: 12px;
  color: var(--color-text-3);
}
:deep(.arco-collapse-item-header) {
  font-size: 13px;
  color: var(--color-text-2);
  padding-top: 4px;
  padding-bottom: 4px;
}
</style>
