<template>
  <div class="booking-list">
    <el-card>
      <template #header>
        <div class="card-header">
          <span class="card-title">预约管理</span>
        </div>
      </template>

      <el-form :inline="true" :model="queryForm" class="search-form">
        <el-form-item label="预约编号">
          <el-input v-model="queryForm.bookingNumber" placeholder="请输入预约编号" clearable />
        </el-form-item>
        <el-form-item label="联系人">
          <el-input v-model="queryForm.contactName" placeholder="请输入联系人" clearable />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="queryForm.status" placeholder="请选择" clearable>
            <el-option label="待确认" value="pending" />
            <el-option label="已确认" value="confirmed" />
            <el-option label="进行中" value="in_progress" />
            <el-option label="已完成" value="completed" />
            <el-option label="已取消" value="cancelled" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :icon="Search" @click="handleSearch">搜索</el-button>
          <el-button :icon="RefreshLeft" @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table :data="tableData" v-loading="loading" stripe>
        <el-table-column prop="bookingNumber" label="预约编号" width="160" />
        <el-table-column prop="contactName" label="联系人" width="100" />
        <el-table-column prop="contactPhone" label="联系电话" width="130" />
        <el-table-column prop="service.name" label="服务类型" min-width="120" />
        <el-table-column prop="address" label="地址" min-width="150" show-overflow-tooltip />
        <el-table-column prop="appointmentTime" label="预约时间" width="170">
          <template #default="{ row }">
            {{ formatDate(row.appointmentTime) }}
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="getStatusType(row.status)">{{ getStatusText(row.status) }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="220" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" link :icon="View" @click="handleView(row)">查看</el-button>
            <el-dropdown @command="(cmd) => handleStatusCommand(cmd, row)">
              <el-button type="primary" link>
                更新状态 <el-icon class="el-icon--right"><arrow-down /></el-icon>
              </el-button>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item command="confirmed">确认</el-dropdown-item>
                  <el-dropdown-item command="in_progress">进行中</el-dropdown-item>
                  <el-dropdown-item command="completed">完成</el-dropdown-item>
                  <el-dropdown-item command="cancelled">取消</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        v-model:current-page="queryForm.page"
        v-model:page-size="queryForm.limit"
        :page-sizes="[10, 20, 50, 100]"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="loadData"
        @current-change="loadData"
        style="margin-top: 20px; justify-content: center"
      />
    </el-card>

    <el-dialog v-model="detailVisible" title="预约详情" width="700px">
      <el-descriptions :column="2" border v-if="currentRow">
        <el-descriptions-item label="预约编号">{{ currentRow.bookingNumber }}</el-descriptions-item>
        <el-descriptions-item label="状态">
          <el-tag :type="getStatusType(currentRow.status)">{{ getStatusText(currentRow.status) }}</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="联系人">{{ currentRow.contactName }}</el-descriptions-item>
        <el-descriptions-item label="联系电话">{{ currentRow.contactPhone }}</el-descriptions-item>
        <el-descriptions-item label="服务类型" :span="2">{{ currentRow.service?.name }}</el-descriptions-item>
        <el-descriptions-item label="预约时间" :span="2">
          {{ formatDate(currentRow.appointmentTime) }}
        </el-descriptions-item>
        <el-descriptions-item label="服务地址" :span="2">{{ currentRow.address }}</el-descriptions-item>
        <el-descriptions-item label="问题描述" :span="2">
          {{ currentRow.description || '无' }}
        </el-descriptions-item>
        <el-descriptions-item label="图片" :span="2" v-if="currentRow.images && currentRow.images.length">
          <div style="display: flex; gap: 10px; flex-wrap: wrap">
            <el-image
              v-for="(img, index) in currentRow.images"
              :key="index"
              :src="img"
              fit="cover"
              style="width: 100px; height: 100px; border-radius: 4px"
              :preview-src-list="currentRow.images"
              :initial-index="index"
            />
          </div>
        </el-descriptions-item>
        <el-descriptions-item label="创建时间" :span="2">
          {{ formatDate(currentRow.createdAt) }}
        </el-descriptions-item>
      </el-descriptions>
      <template #footer>
        <el-button @click="detailVisible = false">关闭</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { Search, RefreshLeft, View, ArrowDown } from '@element-plus/icons-vue'
import { getBookingList, updateBooking } from '@/api/booking'
import dayjs from 'dayjs'

const loading = ref(false)
const tableData = ref<any[]>([])
const total = ref(0)

const queryForm = reactive({
  bookingNumber: '',
  contactName: '',
  status: '',
  page: 1,
  limit: 50 // 增加默认显示数量
})

const detailVisible = ref(false)
const currentRow = ref<any>(null)

const loadData = async () => {
  loading.value = true
  try {
    const params = {
      ...queryForm,
      bookingNumber: queryForm.bookingNumber || undefined,
      contactName: queryForm.contactName || undefined,
      status: queryForm.status || undefined
    }
    const res = await getBookingList(params)
    tableData.value = res.data.list || []
    total.value = res.data.total || 0
  } catch (error) {
    console.error('加载数据失败：', error)
  } finally {
    loading.value = false
  }
}

const handleSearch = () => {
  queryForm.page = 1
  loadData()
}

const handleReset = () => {
  queryForm.bookingNumber = ''
  queryForm.contactName = ''
  queryForm.status = ''
  queryForm.page = 1
  loadData()
}

const handleView = (row: any) => {
  currentRow.value = row
  detailVisible.value = true
}

const handleStatusCommand = async (command: string, row: any) => {
  try {
    await updateBooking(row.id, { status: command })
    ElMessage.success('状态更新成功')
    loadData()
  } catch (error) {
    console.error('更新失败：', error)
  }
}

const formatDate = (date: string) => {
  return dayjs(date).format('YYYY-MM-DD HH:mm:ss')
}

const getStatusType = (status: string) => {
  const map: Record<string, any> = {
    pending: 'warning',
    confirmed: 'success',
    in_progress: 'primary',
    completed: 'info',
    cancelled: 'danger'
  }
  return map[status] || 'info'
}

const getStatusText = (status: string) => {
  const map: Record<string, string> = {
    pending: '待确认',
    confirmed: '已确认',
    in_progress: '进行中',
    completed: '已完成',
    cancelled: '已取消'
  }
  return map[status] || status
}

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

<style scoped>
.booking-list {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-title {
  font-size: 18px;
  font-weight: 600;
}

.search-form {
  margin-bottom: 20px;
}
</style>
