<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">
      <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 pointer-events-none">calendar_today</span>
          <input ref="dateInput" v-model="searchForm.createTime" type="date" @click="openDatePicker"
            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 cursor-pointer" />
        </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-blue-600 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 flex flex-col overflow-hidden relative" style="padding-left: 20px;">
      <div v-if="loading" class="absolute inset-0 bg-white/50 z-30 flex items-center justify-center">
        <span class="material-icons-round animate-spin text-primary text-4xl">sync</span>
      </div>

      <!-- Table with Fixed Layout -->
      <div class="flex-1 overflow-auto">
        <table class="w-full text-left" style="table-layout: fixed; min-width: 1000px;">
          <colgroup>
            <col style="width: 12%;">
            <col style="width: 5%;">
            <col style="width: 8%;">
            <col style="width: 8%;">
            <col style="width: 6%;">
            <col style="width: 10%;">
            <col style="width: 8%;">
            <col style="width: 8%;">
            <col style="width: 7%;">
            <col style="width: 7%;">
            <col style="width: 7%;">
            <col style="width: 7%;">
          </colgroup>
          <thead class="bg-white sticky top-0 z-10">
            <tr class="text-slate-400 text-xs font-semibold uppercase tracking-wider border-b border-slate-100">
              <th class="py-3 px-2 font-semibold bg-white">{{ $t('hostList.hostId') }}</th>
              <th class="py-3 px-2 font-semibold bg-white">{{ $t('hostList.grade') }}</th>
              <th class="py-3 px-2 font-semibold bg-white">{{ $t('hostList.invitationType') }}</th>
              <th class="py-3 px-2 font-semibold bg-white">{{ $t('hostList.liveSessions') }}</th>
              <th class="py-3 px-2 font-semibold bg-white">{{ $t('hostList.country') }}</th>
              <th class="py-3 px-2 font-semibold bg-white">{{ $t('hostList.creationTime') }}</th>
              <th class="py-3 px-2 font-semibold bg-white">{{ $t('hostList.anchorcoins') }}</th>
              <th class="py-3 px-2 font-semibold bg-white">{{ $t('hostList.yesterdayGoldCoins') }}</th>
              <th class="py-3 px-2 font-semibold bg-white">{{ $t('hostList.fansNum') }}</th>
              <th class="py-3 px-2 font-semibold bg-white">{{ $t('hostList.followersNum') }}</th>
              <th class="py-3 px-2 font-semibold bg-white">{{ $t('hostList.onlineFans') }}</th>
              <th class="py-3 px-2 font-semibold bg-white">{{ $t('hostList.anchorType') }}</th>
            </tr>
          </thead>
          <tbody class="text-sm text-slate-600">
            <tr v-if="tableData.length === 0" class="text-center">
              <td colspan="14" 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">


              <!-- Host ID -->
              <td class="py-4 px-2 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-blue-600">
                  {{ row.hostId }}
                </span>
              </td>

              <!-- Level -->
              <td class="py-4 px-2 border-b border-slate-50 group-last:border-none">
                {{ row.hostlevel }}
              </td>

              <!-- Invitation Type -->
              <td class="py-4 px-2 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-4 px-2 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>
                </div>
             </td>

              <!-- Country -->
              <td class="py-4 px-2 border-b border-slate-50 group-last:border-none">
                {{ row.country }}
              </td>

              <!-- Time -->
              <td class="py-4 px-2 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-4 px-2 border-b border-slate-50 group-last:border-none font-semibold text-slate-900">
                {{ row.hostsCoins }}
              </td>

              <!-- Yesterday Coins -->
              <td class="py-4 px-2 border-b border-slate-50 group-last:border-none">
                {{ row.yesterdayCoins }}
              </td>

              <!-- Fans -->
              <td class="py-4 px-2 border-b border-slate-50 group-last:border-none">
                {{ row.fans }}
              </td>

              <!-- Followers -->
              <td class="py-4 px-2 border-b border-slate-50 group-last:border-none">
                {{ row.fllowernum }}
              </td>

              <!-- Online Fans -->
              <td class="py-4 px-2 border-b border-slate-50 group-last:border-none">
                {{ row.onlineFans }}
              </td>

              <!-- Host Kind -->
              <td class="py-4 px-2 border-b border-slate-50 group-last:border-none">
                {{ row.hostsKind }}
              </td>


            </tr>
          </tbody>
        </table>
      </div>
    </div>

    <!-- Footer / Pagination -->
    <footer
      class="px-8 py-6 border-t border-slate-100 dark:border-slate-200/60 bg-white 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-blue-600">{{ 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>

        <!-- Page Numbers -->
        <template v-for="(p, index) in paginationPages" :key="index">
          <span v-if="p === '...'" class="w-8 h-8 flex items-center justify-center text-slate-400 text-sm">...</span>
          <button v-else @click="changePage(p)" class="w-8 h-8 rounded-lg text-xs font-bold transition-all"
            :class="p === page ? 'bg-slate-900 text-white shadow-md' : 'text-slate-600 hover:bg-slate-100'">
            {{ p }}
          </button>
        </template>

        <button @click="changePage(page + 1)" :disabled="page >= totalPages"
          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-blue-600">
            {{ $t('hostList.sure') }}
          </el-button>
        </span>
      </template>
    </el-dialog>

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

  </div>
</template>

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

const { t } = useI18n()
const loading = ref(false)
const dateInput = ref(null)
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 filterdialogVisible = ref(false)
let liveDetailDialogVisible = ref(false)
let liveDetailRecords = ref([])

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 openDatePicker(event) {
  const input = event.target;
  if (input && typeof input.showPicker === 'function') {
    try {
      input.showPicker();
    } catch (e) {
      input.focus();
    }
  }
}

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

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

// 计算总页数
const totalPages = computed(() => Math.ceil(total.value / pageSize.value))

// 生成分页页码数组
const paginationPages = computed(() => {
  const current = page.value
  const totalP = totalPages.value
  const pages = []

  if (totalP <= 7) {
    for (let i = 1; i <= totalP; i++) {
      pages.push(i)
    }
  } else {
    pages.push(1)
    if (current > 4) {
      pages.push('...')
    }
    let start = Math.max(2, current - 2)
    let end = Math.min(totalP - 1, current + 2)
    if (current <= 4) end = Math.min(5, totalP - 1)
    if (current >= totalP - 3) start = Math.max(totalP - 4, 2)
    for (let i = start; i <= end; i++) {
      pages.push(i)
    }
    if (current < totalP - 3) {
      pages.push('...')
    }
    if (totalP > 1) {
      pages.push(totalP)
    }
  }
  return pages
})


const getlist = () => {
  loading.value = true
  // Fix: Check if userInfo is valid before call
  const tenantId = userInfo.value ? Number(userInfo.value.tenantId) : 0
  
  tkhostdata({
    tenantId: 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 openHTML(id) {
  givePyAnchorId(id)
}

function getCountry() {
  getCountryinfo({}).then(res => {
    if(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] || ''
}
</script>

<style scoped>
/* Scoped styles mainly for Element Plus overrides if needed */
</style>
