﻿<template>
  <div class="bg-white dark:bg-slate-50 rounded-3xl shadow-2xl flex flex-col overflow-hidden h-full">
    <!-- Header -->
    <header class="px-8 py-6 border-b border-slate-100 dark:border-slate-200/60 bg-white/50 backdrop-blur-md">
      <div class="flex flex-wrap items-center gap-4">
        <div class="relative flex-1 min-w-[200px]">
          <select 
            v-model="searchForm.country" 
            class="w-full bg-slate-50 border-none rounded-xl py-3 pl-4 pr-10 text-sm text-slate-600 appearance-none focus:ring-2 focus:ring-primary/20 shadow-soft-inner outline-none"
          >
            <option value="">{{ $t('hostList.selectAll') }}</option>
            <option v-for="item in options" :key="item.value" :value="item.value">{{ item.label }}</option>
          </select>
          <span class="material-icons-round absolute right-3 top-1/2 -translate-y-1/2 text-slate-400 pointer-events-none">expand_more</span>
        </div>
        
        <div class="relative flex-1 min-w-[200px]">
          <span class="material-icons-round absolute left-3 top-1/2 -translate-y-1/2 text-slate-400 text-sm">calendar_today</span>
          <input 
            v-model="searchForm.createTime"
            type="date"
            class="w-full bg-slate-50 border-none rounded-xl py-3 pl-10 pr-4 text-sm text-slate-600 focus:ring-2 focus:ring-primary/20 shadow-soft-inner outline-none" 
          />
        </div>
        
        <div class="relative flex-[1.5] min-w-[240px]">
          <input 
            v-model="searchForm.hostsId"
            class="w-full bg-slate-50 border-none rounded-xl py-3 px-4 text-sm text-slate-600 focus:ring-2 focus:ring-primary/20 shadow-soft-inner outline-none" 
            :placeholder="$t('hostList.placeHostId')" 
          />
        </div>
        
        <div class="flex items-center gap-2 ml-auto">
          <button 
            @click="serch"
            class="bg-primary hover:bg-blue-700 text-white px-8 py-3 rounded-xl font-medium text-sm transition-all shadow-lg shadow-primary/20 flex items-center gap-2"
          >
            <span class="material-icons-round text-sm">search</span>
            {{ $t('hostList.query') }}
          </button>
          <button 
            @click="filterdialogVisible = true"
            class="bg-slate-100 hover:bg-slate-200 text-slate-600 p-3 rounded-xl transition-all"
          >
            <span class="material-icons-round">tune</span>
          </button>
        </div>
      </div>
    </header>

    <!-- Table -->
    <div class="flex-1 overflow-auto p-8 relative">
      <div v-if="loading" class="absolute inset-0 bg-white/50 z-10 flex items-center justify-center">
         <span class="material-icons-round animate-spin text-primary text-4xl">sync</span>
      </div>
      <table class="w-full text-left border-separate border-spacing-0">
        <thead>
          <tr class="text-slate-400 text-xs font-semibold uppercase tracking-wider sticky top-0 bg-white z-0">
             <!-- Selection Header -->
             <th class="pb-4 pl-4 font-normal bg-white">
               <!-- Select All Logic not full implemented yet, just visual for design match -->
               <input type="checkbox" class="rounded border-slate-300 text-primary focus:ring-primary/20" />
             </th>
             <th class="pb-4 font-semibold bg-white">{{ $t('hostList.hostId') }}</th>
             <th class="pb-4 font-semibold bg-white">{{ $t('hostList.grade') }}</th>
             <th class="pb-4 font-semibold bg-white">{{ $t('hostList.invitationType') }}</th>
             <th class="pb-4 font-semibold bg-white">{{ $t('hostList.liveSessions') }} / {{ $t('hostList.liveRevenue') }}</th>
             <th class="pb-4 font-semibold bg-white">{{ $t('hostList.country') }}</th>
             <th class="pb-4 font-semibold bg-white">{{ $t('hostList.creationTime') }}</th>
             <th class="pb-4 font-semibold bg-white">{{ $t('hostList.anchorcoins') }}</th>
             <th class="pb-4 font-semibold text-right pr-4 bg-white">操作</th>
          </tr>
        </thead>
        <tbody class="text-sm text-slate-600">
           <tr v-if="tableData.length === 0" class="text-center">
             <td colspan="9" class="py-10 text-slate-400">暂无数据</td>
           </tr>
           <tr v-for="row in tableData" :key="row.hostId" class="group hover:bg-slate-50/80 transition-colors">
             <!-- Checkbox -->
             <td class="py-5 pl-4 border-b border-slate-50 group-last:border-none">
                <input 
                  type="checkbox" 
                  class="rounded border-slate-300 text-primary focus:ring-primary/20"
                  :value="row.hostId"
                  v-model="selectHostList"
                />
             </td>
             
             <!-- Host ID -->
             <td class="py-5 border-b border-slate-50 group-last:border-none">
               <span 
                 @click="openHTML(row.hostId)"
                 class="font-medium text-slate-900 border-b border-transparent group-hover:border-primary/30 transition-all cursor-pointer hover:text-primary"
               >
                 {{ row.hostId }}
               </span>
             </td>
             
             <!-- Level -->
             <td class="py-5 border-b border-slate-50 group-last:border-none">
               {{ row.hostlevel }}
             </td>
             
             <!-- Invitation Type -->
             <td class="py-5 border-b border-slate-50 group-last:border-none">
               <span 
                 class="px-3 py-1 text-[10px] font-bold uppercase rounded-full"
                 :class="row.invitationType == 1 ? 'bg-green-50 text-green-600' : 'bg-amber-50 text-amber-600'"
               >
                 {{ row.invitationType == 1 ? $t('hostList.invitationType1') : $t('hostList.invitationType2') }}
               </span>
             </td>
             
             <!-- Data Buttons -->
             <td class="py-5 border-b border-slate-50 group-last:border-none">
                <div class="flex gap-2">
                  <button 
                    @click="getliveHost(row.hostId)"
                    class="px-3 py-1.5 bg-blue-50 text-blue-600 hover:bg-blue-600 hover:text-white rounded-lg text-xs font-medium transition-all"
                  >
                    {{ $t('hostList.viewSessions') }}
                  </button>
                  <button 
                    @click="getRevenueStats(row.hostId)"
                    class="px-3 py-1.5 bg-sky-50 text-sky-600 hover:bg-sky-600 hover:text-white rounded-lg text-xs font-medium transition-all"
                  >
                    {{ $t('hostList.viewRevenue') }}
                  </button>
                </div>
             </td>
             
             <!-- Country -->
             <td class="py-5 border-b border-slate-50 group-last:border-none">
               {{ row.country }}
             </td>
             
             <!-- Time -->
             <td class="py-5 border-b border-slate-50 group-last:border-none">
               <div class="flex flex-col">
                 <span>{{ formatTimeOnlyDate(row.createTime) }}</span>
                 <span class="text-[10px] text-slate-400">{{ formatTimeOnlyTime(row.createTime) }}</span>
               </div>
             </td>
             
             <!-- Coins -->
             <td class="py-5 border-b border-slate-50 group-last:border-none font-semibold text-slate-900">
               {{ row.hostsCoins }}
             </td>
             
             <!-- Actions -->
             <td class="py-5 border-b border-slate-50 group-last:border-none text-right pr-4">
                <button class="p-1 hover:bg-slate-200 rounded transition-colors text-slate-400">
                  <span class="material-icons-round text-lg">more_horiz</span>
                </button>
             </td>
           </tr>
        </tbody>
      </table>
    </div>
    
    <!-- Footer / Pagination -->
    <footer class="px-8 py-6 border-t border-slate-100 dark:border-slate-200/60 bg-white/50 backdrop-blur-md flex flex-wrap items-center justify-between gap-4">
      <div class="flex items-center gap-4">
        <div class="relative">
          <select 
            v-model="pageSize"
            @change="handleSizeChange"
            class="bg-slate-50 border-none rounded-lg py-2 pl-4 pr-10 text-sm text-slate-600 appearance-none focus:ring-2 focus:ring-primary/20 shadow-soft-inner outline-none"
          >
            <option :value="10">10/page</option>
            <option :value="20">20/page</option>
            <option :value="50">50/page</option>
            <option :value="100">100/page</option>
          </select>
          <span class="material-icons-round absolute right-2 top-1/2 -translate-y-1/2 text-slate-400 text-sm pointer-events-none">expand_more</span>
        </div>
        <span class="text-xs text-slate-400 font-medium">总条数: <span class="text-primary">{{ total }}</span></span>
      </div>
      
      <div class="flex items-center gap-1">
        <button 
          @click="changePage(page - 1)"
          :disabled="page <= 1"
          class="p-2 text-slate-400 hover:bg-slate-100 rounded-lg transition-colors disabled:opacity-50"
        >
          <span class="material-icons-round text-lg">chevron_left</span>
        </button>
        
        <!-- Simplified Pagination Logic -->
        <button class="w-8 h-8 rounded-lg bg-slate-900 text-white text-xs font-bold transition-all shadow-md">{{ page }}</button>
        
        <button 
          @click="changePage(page + 1)"
          :disabled="page * pageSize >= total"
          class="p-2 text-slate-400 hover:bg-slate-100 rounded-lg transition-colors disabled:opacity-50"
        >
          <span class="material-icons-round text-lg">chevron_right</span>
        </button>
      </div>
    </footer>

    <!-- Filter Dialog (Preserved Element Plus) -->
    <el-dialog v-model="filterdialogVisible" width="800px" :before-close="handleClose">
      <el-row v-for="(field, index) in fields" :key="index" :gutter="20" style="margin-bottom: 10px">
        <el-col :span="4">
          <div style="height: 100%; padding-top: 10px" class="flex items-center justify-center">
            {{ field.label }}
          </div>
        </el-col>
        <el-col :span="10">
          <div><label class="text-sm text-slate-500 mb-1 block">{{ $t('hostList.min') }}</label></div>
          <el-input type="number" :oninput="'if(value.length>9)value=value.slice(0,9)'"
            v-model.number="searchForm[field.minModel]" :placeholder="$t('hostList.placeMin')" />
        </el-col>
        <el-col :span="10">
          <div><label class="text-sm text-slate-500 mb-1 block">{{ $t('hostList.max') }}</label></div>
          <el-input type="number" :oninput="'if(value.length>9)value=value.slice(0,9)'"
            v-model.number="searchForm[field.maxModel]" :placeholder="$t('hostList.placeMax')" />
        </el-col>
      </el-row>

      <el-row :gutter="20" class="mt-4">
        <el-col :span="4">
          <div style="height: 100%;padding-top: 10px;" class="flex items-center justify-center">
            {{ $t('hostList.sort') }}
          </div>
        </el-col>
        <el-col :span="10">
          <div><label class="text-sm text-slate-500 mb-1 block">{{ $t('hostList.sortType') }}</label></div>
          <el-select v-model="sortData.sortType" filterable :placeholder="$t('hostList.selectPlaceholder')"
            class="w-full">
            <el-option v-for="item in sortNameOptions" :key="item.type" :label="item.label" :value="item.type" />
          </el-select>
        </el-col>
        <el-col :span="10">
          <div><label class="text-sm text-slate-500 mb-1 block">{{ $t('hostList.ascending') }}/{{ $t('hostList.descending') }}</label></div>
          <el-select v-model="sortData.sortForm" filterable :placeholder="$t('hostList.selectPlaceholder')"
            class="w-full">
            <el-option
              v-for="item in [{ label: $t('hostList.ascending'), value: 'asc' }, { label: $t('hostList.descending'), value: 'desc' }]"
              :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-col>
      </el-row>

      <template #footer>
        <span class="dialog-footer">
          <el-button @click="reset">
            {{ $t('hostList.reset') }}
          </el-button>
          <el-button type="primary" @click="handelClick" class="bg-primary">
            {{ $t('hostList.sure') }}
          </el-button>
        </span>
      </template>
    </el-dialog>

    <!-- Dialogs -->
    <LiveRecordDialog v-model:modelValue="liveDetailDialogVisible" :rows="liveDetailRecords" @select="handleLiveSelect" />

    <el-dialog v-model="revenueDialogVisible" :title="$t('hostList.liveRevenue')" width="80vw" top="6vh"
      :close-on-click-modal="false" destroy-on-close>
      <el-table :data="revenueRecords" border height="62vh" style="width: 100%" v-loading="revenueLoading"
        table-layout="auto">
        <el-table-column prop="displayId" :label="$t('hostList.revenueHost')" />
        <el-table-column prop="todayRevenue" :label="$t('hostList.todayRevenueUsd')" />
        <el-table-column prop="totalRevenue" :label="$t('hostList.totalRevenueUsd')" />
        <el-table-column prop="lastDaysCount" :label="$t('hostList.liveDays')" />
        <el-table-column prop="history" :label="$t('hostList.historyRevenueUsd')">
          <template #default="{ row }">
            <el-tooltip v-if="hasHistory(row.history)" effect="dark" placement="top">
              <template #content>{{ buildHistoryTitle(row.history) }}</template>
              <div class="history-sparkline-wrap">
                <div class="history-sparkline-top">
                  <span class="history-sparkline-min-top">
                    {{ $t('hostList.revenueLow') }}: {{ formatRevenueValue(getHistoryMin(row.history)) }}
                  </span>
                  <span class="history-sparkline-max">
                    {{ $t('hostList.revenueHigh') }}: {{ formatRevenueValue(getHistoryMax(row.history)) }}
                  </span>
                </div>
                <svg class="history-sparkline" viewBox="0 0 180 48" preserveAspectRatio="none">
                  <line x1="2" y1="46" x2="178" y2="46" stroke="#e6eef7" stroke-width="1" />
                  <polyline :points="buildSparklinePoints(row.history)" fill="none" stroke="#45a1ff" stroke-width="2" />
                </svg>
                <div class="history-sparkline-bottom">
                  <div class="history-sparkline-dates">
                    <span class="history-sparkline-date">{{ getHistoryStartDate(row.history) }}</span>
                    <span class="history-sparkline-date">{{ getHistoryEndDate(row.history) }}</span>
                  </div>
                </div>
              </div>
            </el-tooltip>
            <span v-else>-</span>
          </template>
        </el-table-column>
        <el-table-column prop="createdAt" :label="$t('hostList.revenueTime')">
          <template #default="scope">
            {{ formatTimestamp(scope.row.createdAt) }}
          </template>
        </el-table-column>
      </el-table>
      <template #footer>
        <el-button @click="revenueDialogVisible = false">{{ $t('hostList.close') }}</el-button>
      </template>
    </el-dialog>

  </div>
</template>

<script setup>
import { tkhostdata, getCountryinfo, liveHostDetail, revenueStats } from '@/api/account';
import { usePythonBridge } from '@/utils/pythonBridge'
import { getUser } from '@/utils/storage'
import { ref, reactive, onMounted } from 'vue';
import { useI18n } from 'vue-i18n'
import LiveRecordDialog from '@/components/LiveRecordDialog.vue'

const { t } = useI18n()
const loading = ref(false)
const { givePyAnchorId, exportToExcel } = usePythonBridge();

const userInfo = ref(getUser())

const tableData = ref([])
const searchForm = ref({
  country: '',
  createTime: '',
  hostsId: '',
  fansMin: null, fansMax: null,
  onlineFansMin: null, onlineFansMax: null,
  hostsCoinsMin: null, hostsCoinsMax: null,
  fllowernumMin: null, fllowernumMax: null
})

const fields = [
  { label: t('hostList.fansNum'), minModel: 'fansMin', maxModel: 'fansMax' },
  { label: t('hostList.onlineFans'), minModel: 'onlineFansMin', maxModel: 'onlineFansMax' },
  { label: t('hostList.anchorcoins'), minModel: 'hostsCoinsMin', maxModel: 'hostsCoinsMax' },
  { label: t('hostList.followersNum'), minModel: 'fllowernumMin', maxModel: 'fllowernumMax' },
]

let sortData = ref({ sortForm: 'desc', sortType: "createTime" })
let sortNameOptions = ref([
  { label: t('hostList.creationTime'), type: 'createTime' },
  { label: t('hostList.anchorcoins'), type: 'hostsCoins' },
  { label: t('hostList.fansNum'), type: 'fans' },
  { label: t('hostList.yesterdayGoldCoins'), type: 'yesterdayCoins' },
  { label: t('hostList.onlineFans'), type: 'onlineFans' },
  { label: t('hostList.followersNum'), type: 'fllowernum' },
])

let selectHostList = ref([])
let filterdialogVisible = ref(false)
let liveDetailDialogVisible = ref(false)
let liveDetailRecords = ref([])
let revenueDialogVisible = ref(false)
let revenueRecords = ref([])
let revenueLoading = ref(false)

let pageSize = ref(10)
let page = ref(1)
let total = ref(0)
let options = ref([])

onMounted(() => {
  getCountry();
  getlist();
})

function serch() {
  page.value = 1
  getlist();
}

function handleSizeChange() {
  page.value = 1
  getlist();
}

function changePage(newPage) {
  if (newPage < 1 || (newPage - 1) * pageSize.value >= total.value) return
  page.value = newPage
  getlist()
}


const getlist = () => {
  loading.value = true
  tkhostdata({
    tenantId: Number(userInfo.value.tenantId),
    sort: sortData.value.sortForm,
    sortName: sortData.value.sortType,
    "current": page.value,
    "pageSize": pageSize.value,
    ...searchForm.value,
  }).then(res => {
    loading.value = false
    if (res) {
      console.log('主播列表', res)
      total.value = Number(res.total)
      tableData.value = res.records.map(item => ({
        hostId: item.hostsId,
        hostlevel: item.hostsLevel,
        country: item.country,
        createTime: item.createTime,
        fans: item.fans,
        fllowernum: item.fllowernum,
        hostsCoins: item.hostsCoins,
        hostsKind: item.hostsKind,
        onlineFans: item.onlineFans,
        yesterdayCoins: item.yesterdayCoins,
        belongBy: item.belongBy,
        useable: item.useable,
        invitationType: item.invitationType,
      }));
    }
  }).catch(() => {
    loading.value = false
  })
}

function handelClick() {
  filterdialogVisible.value = false
  getlist()
}

function reset() {
  searchForm.value.fansMin = null
  searchForm.value.fansMax = null
  searchForm.value.onlineFansMin = null
  searchForm.value.onlineFansMax = null
  searchForm.value.hostsCoinsMin = null
  searchForm.value.hostsCoinsMax = null
  searchForm.value.fllowernumMin = null
  searchForm.value.fllowernumMax = null
}

function handleClose(done) {
  done()
}

function getliveHost(hostId) {
  liveHostDetail({
      "hostsId": hostId,
      "tenantId": userInfo.value.tenantId
    }).then(res => {
    const detailList = Array.isArray(res) ? res : (res?.records || [])
    liveDetailRecords.value = detailList
    liveDetailDialogVisible.value = true
  })
}

function handleLiveSelect(row) {
  liveDetailDialogVisible.value = false
}

function getRevenueStats(hostId) {
  revenueLoading.value = true
  revenueStats(hostId).then(res => {
    const detailList = Array.isArray(res) ? res : (res?.records || [])
    revenueRecords.value = detailList
    revenueDialogVisible.value = true
  }).finally(() => {
    revenueLoading.value = false
  })
}

function openHTML(id) {
  givePyAnchorId(id)
}

function getCountry() {
  getCountryinfo({}).then(res => {
    res.forEach(item => {
      if (item.countryGroupName) {
        options.value.push({ value: item.countryGroupName, label: item.countryGroupName })
      }
    })
  })
}

function formatTimeOnlyDate(val) {
   if(!val) return ''
   return val.split(' ')[0] || val
}

function formatTimeOnlyTime(val) {
   if(!val) return ''
   return val.split(' ')[1] || ''
}

// History parsing helpers (preserved from original)
function parseHistoryItems(history) {
  if (!history) return []
  let arr = history
  if (typeof history === 'string') {
    try { arr = JSON.parse(history) } catch { return [] }
  }
  if (!Array.isArray(arr)) return []
  return arr.map((item, index) => {
      if (typeof item === 'number') return { date: `Day ${index + 1}`, value: item }
      if (item && typeof item === 'object') return { date: item.date ? String(item.date) : '', value: Number(item.value ?? 0) }
      return null
  }).filter(Boolean)
}
function hasHistory(history) { return parseHistoryItems(history).length > 0 }
function getHistoryStats(history) {
  const items = parseHistoryItems(history)
  if (items.length === 0) return { min: 0, max: 0, start: '', end: '' }
  const values = items.map(item => item.value)
  return { min: Math.min(...values), max: Math.max(...values), start: items[0].date || '', end: items[items.length - 1].date || '' }
}
function getHistoryMin(history) { return getHistoryStats(history).min }
function getHistoryMax(history) { return getHistoryStats(history).max }
function getHistoryStartDate(history) { return getHistoryStats(history).start }
function getHistoryEndDate(history) { return getHistoryStats(history).end }
function formatRevenueValue(value) {
  const num = Number(value)
  if (!Number.isFinite(num)) return ''
  return num.toFixed(3).replace(/\.0+$/, '').replace(/(\.\d*?)0+$/, '$1')
}
function buildHistoryTitle(history) {
  const items = parseHistoryItems(history)
  return items.length ? items.map(item => `${item.date}: ${formatRevenueValue(item.value)}`).join('\n') : '-'
}
function buildSparklinePoints(history) {
  const items = parseHistoryItems(history)
  if (items.length === 0) return ''
  const values = items.map(item => item.value)
  const min = Math.min(...values)
  const max = Math.max(...values)
  const range = max - min || 1
  const width = 180, height = 48, padding = 2
  const step = items.length > 1 ? (width - padding * 2) / (items.length - 1) : 0
  return items.map((item, index) => {
    const x = padding + index * step
    const y = padding + (height - padding * 2) * (1 - (item.value - min) / range)
    return `${x},${y}`
  }).join(' ')
}
function formatTimestamp(value) {
  if (!value) return ''
  const date = new Date(value)
  return date.toLocaleString()
}
</script>
<style scoped>
/* Only keep essential overrides that Tailwind can't easily handle, like svg styling */
.history-sparkline-wrap {
  display: flex;
  flex-direction: column;
  background-color: #f8fafc;
  border: 1px solid #e2e8f0;
  border-radius: 4px;
  padding: 4px;
  width: 190px;
}
.history-sparkline-top,
.history-sparkline-bottom {
  display: flex;
  justify-content: space-between;
  font-size: 10px;
  color: #64748b;
  margin-bottom: 2px;
}
.history-sparkline {
  background-color: #fff;
  border-radius: 2px;
  height: 48px;
  width: 100%;
}
</style>
