<template>
  <div class="inspection-list-container">
    <div class="page-header">
      <h2>巡店管理</h2>
      <el-button type="primary">新建巡店</el-button>
    </div>

    <el-card>
      <el-table :data="tableData" v-loading="loading" style="width: 100%">
        <el-table-column prop="storeName" label="门店名称" width="200" />
        <el-table-column prop="templateName" label="模板名称" min-width="200" />
        <el-table-column label="巡店人员" width="200">
          <template #default="{ row }">
            {{ row.inspectors?.join(', ') }}
          </template>
        </el-table-column>
        <el-table-column prop="date" label="巡店时间" width="300" />
        <el-table-column prop="scoreRate" label="得分率" width="100">
          <template #default="{ row }">
            {{ row.scoreRate }}%
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150" fixed="right">
          <template #default="{ row }">
            <el-button link type="primary" @click="handleDetail(row)">查看报告</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import axios from 'axios'
import { ElMessage } from 'element-plus'

const router = useRouter()
const loading = ref(false)
const tableData = ref([])

const fetchList = async () => {
  loading.value = true
  try {
    const res = await axios.get('/api/inspections')
    if (res.data.code === 0) {
      tableData.value = res.data.data
    }
  } catch (error) {
    ElMessage.error('Failed to load inspections')
  } finally {
    loading.value = false
  }
}

const handleDetail = (row: any) => {
  router.push(`/store-inspection/detail/${row.id}`)
}

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

<style scoped>
.inspection-list-container {
  padding: 20px;
}
.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}
</style>
