<script setup lang="ts">
import { Search } from '@element-plus/icons-vue'
import { computed, reactive } from 'vue'

import { fetchAdminListings, type AdminListingQuery, type Listing } from '@/api/listings'
import { useAdminTable } from '@/composables/useAdminTable'
import { useMoney } from '@/composables/useMoney'
import { listingReviewStatusLabel, listingStatusLabel } from '@/utils/statusLabels'
import { formatDateTime } from '@/utils/time'

const money = useMoney()

const filters = reactive<AdminListingQuery>({
  owner_id: '',
  status: '',
  review_status: '',
  limit: 200,
})

const { loading, data: listings, load: loadListings } = useAdminTable<Listing>({
  fetchFn: () => fetchAdminListings(filters),
})

const publishedCount = computed(() => listings.value.filter((item) => item.status === 'published').length)
const rentedCount = computed(() => listings.value.filter((item) => item.status === 'rented').length)
const pendingCount = computed(() => listings.value.filter((item) => item.review_status === 'pending').length)

function resetFilters() {
  filters.owner_id = ''
  filters.status = ''
  filters.review_status = ''
  filters.limit = 200
  void loadListings()
}

function listingPrice(row: Listing) {
  return money(row.price)
}

function ownerName(row: Listing) {
  return row.owner_phone || row.owner_nickname || `号主 ${row.owner_id}`
}

function statusType(status: string) {
  if (status === 'published') return 'success'
  if (status === 'rented') return 'warning'
  if (status === 'offline') return 'info'
  return ''
}

function reviewType(status: string) {
  if (status === 'approved') return 'success'
  if (status === 'pending') return 'warning'
  if (status === 'rejected') return 'danger'
  return 'info'
}
</script>

<template>
  <section class="page">
    <div class="page-header-row">
      <div class="page-header">
        <p class="eyebrow">Listings</p>
        <h1>商品管理</h1>
        <p>查看全部租号商品、号主、区服平台、价格押金、上架状态和审核状态。</p>
      </div>
      <div class="toolbar-actions">
        <el-button @click="resetFilters">重置</el-button>
        <el-button type="primary" :icon="Search" :loading="loading" @click="loadListings">查询</el-button>
      </div>
    </div>

    <div class="metric-grid">
      <div class="metric-card">
        <span>当前结果</span>
        <strong>{{ listings.length }} 个</strong>
      </div>
      <div class="metric-card">
        <span>已上架</span>
        <strong>{{ publishedCount }} 个</strong>
      </div>
      <div class="metric-card">
        <span>已锁定</span>
        <strong>{{ rentedCount }} 个</strong>
      </div>
      <div class="metric-card">
        <span>待审核</span>
        <strong>{{ pendingCount }} 个</strong>
      </div>
    </div>

    <el-form class="filter-panel" label-position="top">
      <el-form-item label="号主 ID">
        <el-input v-model="filters.owner_id" clearable placeholder="按号主筛选" />
      </el-form-item>
      <el-form-item label="商品状态">
        <el-select v-model="filters.status" clearable placeholder="全部状态" class="full-control">
          <el-option label="草稿" value="draft" />
          <el-option label="已上架" value="published" />
          <el-option label="已锁定" value="rented" />
          <el-option label="已下架" value="offline" />
          <el-option label="异常" value="abnormal" />
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态">
        <el-select v-model="filters.review_status" clearable placeholder="全部审核状态" class="full-control">
          <el-option label="未提交" value="none" />
          <el-option label="待审核" value="pending" />
          <el-option label="已通过" value="approved" />
          <el-option label="已拒绝" value="rejected" />
        </el-select>
      </el-form-item>
      <el-form-item label="查询条数">
        <el-input-number v-model="filters.limit" :min="20" :max="500" :step="20" class="full-control" />
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" class="table-panel" :data="listings">
      <el-table-column prop="id" label="ID" width="80" />
      <el-table-column label="商品" min-width="220">
        <template #default="{ row }">
          <strong>{{ row.title }}</strong>
          <span class="table-subtext">账号 ID: {{ row.account_id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="号主" min-width="150">
        <template #default="{ row }">
          <strong>{{ ownerName(row) }}</strong>
          <span class="table-subtext">ID: {{ row.owner_id }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="server_region" label="区服" width="120" />
      <el-table-column prop="login_platform" label="平台" width="120" />
      <el-table-column prop="rank_level" label="段位" width="110" />
      <el-table-column prop="haf_coin_amount" label="哈夫币" width="110" />
      <el-table-column label="价格" width="100">
        <template #default="{ row }">{{ listingPrice(row) }}</template>
      </el-table-column>
      <el-table-column label="押金" width="100">
        <template #default="{ row }">{{ money(row.deposit_amount) }}</template>
      </el-table-column>
      <el-table-column label="商品状态" width="110">
        <template #default="{ row }">
          <el-tag :type="statusType(row.status)">{{ listingStatusLabel(row.status) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" width="110">
        <template #default="{ row }">
          <el-tag :type="reviewType(row.review_status)">{{ listingReviewStatusLabel(row.review_status) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="上架时间" min-width="180">
        <template #default="{ row }">{{ formatDateTime(row.published_at) }}</template>
      </el-table-column>
      <el-table-column label="更新时间" min-width="180">
        <template #default="{ row }">{{ formatDateTime(row.updated_at) }}</template>
      </el-table-column>
      <el-table-column label="操作" width="110">
        <template #default="{ row }">
          <RouterLink :to="`/admin/listings/${row.id}`">
            <el-button size="small">详情</el-button>
          </RouterLink>
        </template>
      </el-table-column>
    </el-table>
  </section>
</template>
