<template>
  <div class="flex-1 flex flex-col" v-loading="loading">
    <!-- List Container -->
    <div class="flex-1 p-4 pt-1">
      <div class="relative">
        <el-tabs v-model="searchParams.status" @tab-change="toggleTab">
          <el-tab-pane
            v-for="tab in auditTypeListOptions"
            :key="tab.value"
            :label="tab.label"
            :name="tab.value"
          />
        </el-tabs>
        <div class="absolute right-0 top-2.5 z-1000">
          <el-icon
            size="15"
            class="cursor-pointer hover:rotate-180 transition-all duration-300"
            @click="refresh"
            ><IEpRefresh
          /></el-icon>
        </div>
      </div>
      <div v-if="!list.length" class="flex flex-col items-center justify-center h-64">
        <div class="w-16 h-16 bg-gray-100 rounded-full flex items-center justify-center mb-4">
          <el-icon class="text-2xl text-gray-300"><IEpDocument /></el-icon>
        </div>
        <div class="text-gray-500 text-lg mb-2">暂无内容</div>
      </div>

      <div v-else class="space-y-4">
        <div
          v-for="item in list"
          :key="item.id"
          class="flex items-center p-2 rounded-lg hover:bg-gray-100 transition-colors cursor-pointer"
        >
          <!-- Content -->
          <div class="flex-1 min-w-0">
            <!-- 头像 + 内容区 -->
            <div class="flex gap-3">
              <el-image :src="item.showAvatar" class="w-12 h-12 rounded-full flex-shrink-0" />

              <div class="flex-1 min-w-0 flex flex-col justify-center">
                <!-- 第一行:用户名 + 时间 + 标签 -->
                <div class="flex items-center gap-2 mb-1.5">
                  <span class="text-gray-900 text-sm flex-shrink-0">{{ item.createName }}</span>
                  <span
                    class="text-gray-400 text-sm flex-shrink-0 whitespace-nowrap flex items-center"
                  >
                    {{ dayjs(item.createTime * 1000).format('YYYY-MM-DD HH:mm:ss') }}
                    <span class="flex items-center px-2"> 举报帖子 </span>
                    <el-link
                      type="primary"
                      :underline="false"
                      @click="jumpToArticleDetailPage({ type: item.type, id: item.articleId })"
                    >
                      {{ item.title }}
                    </el-link>
                  </span>
                  <!-- <div class="flex flex-wrap gap-1.5 flex-1 min-w-0">
                    <el-tag size="small">
                      {{ articleTypeListOptions.find((i) => i.value === item.type)?.label }}
                    </el-tag>
                  </div> -->
                </div>

                <div class="text-gray-700 font-medium truncate line-clamp-1">
                  举报理由：{{ item.reason }}
                </div>
              </div>
            </div>
          </div>

          <div
            v-if="searchParams.status === AuditStatusEnum.UNAUDITED"
            class="flex items-center text-gray-400 text-sm ml-4"
          >
            <el-button
              type="primary"
              link
              @click="handleAudit({ id: item.id, status: AuditStatusEnum.AGREED })"
            >
              同意
            </el-button>
            <el-button
              type="danger"
              link
              @click="
                handleAudit({
                  id: item.id,
                  status: AuditStatusEnum.REJECTED,
                })
              "
            >
              拒绝
            </el-button>
          </div>
        </div>
      </div>
    </div>

    <div
      v-if="list.length"
      class="flex items-center justify-end px-6 py-4 border-t border-gray-200"
    >
      <div class="pagination-wrapper bg-white rounded-lg shadow-sm border border-gray-100 p-2">
        <el-pagination
          v-model:current-page="searchParams.current"
          v-model:page-size="searchParams.size"
          @size-change="changePageSize"
          @current-change="goToPage"
          :page-sizes="[10, 20, 30, 40]"
          layout="prev, pager, next, jumper, total"
          :total="total"
          class="custom-pagination"
        />
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { getComplaintList, auditComplaint } from '@/api'
import { usePageSearch } from '@/hooks'
import { auditTypeListOptions } from '@/constants/options'
import dayjs from 'dayjs'
import { AuditStatusEnum } from '@/constants'
import type { AuditComplaintDto } from '@/api'
import type { TabPaneName } from 'element-plus'
import { useNavigation } from '@/hooks'
import { push } from 'notivue'
import { useMessageBox } from '@/hooks'

const { prompt } = useMessageBox()
const toggleTab = (key: TabPaneName) => {
  searchParams.value.status = key as AuditStatusEnum
  refresh()
}

// State
const { list, loading, searchParams, total, refresh, goToPage, changePageSize } = usePageSearch(
  getComplaintList,
  {
    defaultParams: {
      status: AuditStatusEnum.UNAUDITED,
    },
  },
)
const { jumpToArticleDetailPage } = useNavigation()
const handleAudit = async (data: AuditComplaintDto) => {
  if (data.status === AuditStatusEnum.REJECTED) {
    const reason = await prompt({
      title: '请输入拒绝理由',
      type: 'warning',
    })

    data.remark = reason
  }

  await auditComplaint(data)
  push.success('审核成功')

  refresh()
}
</script>
