<template>
  <div class="rooms-page">
    <h2 class="page-title">房间管理</h2>
    <el-card>
      <el-form inline class="filter-form">
        <el-form-item label="房间号">
          <el-input v-model="filter.roomId" placeholder="房间号" clearable style="width: 140px" />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="filter.status" placeholder="全部" clearable style="width: 120px">
            <el-option label="进行中" value="playing" />
            <el-option label="已结束" value="finished" />
            <el-option label="等待中" value="waiting" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">搜索</el-button>
          <el-button @click="resetFilter">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table v-if="filteredList.length > 0" :data="filteredList" stripe>
        <el-table-column prop="roomId" label="房间号" width="120" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }">
            <StatusTag :status="row.status" />
          </template>
        </el-table-column>
        <el-table-column prop="currentScore" label="当前分数" width="120" />
        <el-table-column label="在线时长" width="120">
          <template #default="{ row }">
            {{ formatRoomDuration(row.status, row.createdAt, row.updatedAt) }}
          </template>
        </el-table-column>
        <el-table-column label="创建时间" min-width="160">
          <template #default="{ row }">
            {{ formatDateTime(row.createdAt) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" link @click="goDetail(row.roomId)">详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-empty v-else description="暂无房间数据" />
    </el-card>
  </div>
</template>

<script setup>
import { computed, onMounted, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { useRoomsStore } from '../stores/rooms'
import StatusTag from '../components/common/StatusTag.vue'
import { formatDateTime, formatRoomDuration } from '../utils/format'

const router = useRouter()
const roomsStore = useRoomsStore()
const filter = reactive({
  roomId: '',
  status: '',
})

const filteredList = computed(() => {
  let list = roomsStore.list
  if (filter.roomId) {
    list = list.filter((r) => String(r.roomId).includes(filter.roomId))
  }
  if (filter.status) {
    list = list.filter((r) => r.status === filter.status)
  }
  return list
})

function search() {
  roomsStore.fetchRooms().catch(() => {})
}

function resetFilter() {
  filter.roomId = ''
  filter.status = ''
  search()
}

function goDetail(roomId) {
  router.push({ name: 'RoomDetail', params: { roomId } })
}

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

<style scoped>
.page-title {
  margin-bottom: 20px;
  font-size: 20px;
}
.filter-form {
  margin-bottom: 16px;
}
</style>
