<template>
  <div class="matchmaker-list-container">
    <h2 class="page-title">红娘管理</h2>
    
    <!-- 工具栏 -->
    <el-card shadow="never" class="toolbar-card">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-button type="primary" icon="Plus" @click="$router.push('/matchmaker/create')">
            添加红娘
          </el-button>
          <el-button icon="Refresh" @click="loadList">刷新</el-button>
        </el-col>
        <el-col :span="18">
          <el-space wrap>
            <el-select v-model="filters.matchmakerType" placeholder="红娘类型" clearable style="width: 120px" @change="loadList">
              <el-option label="全部" :value="undefined" />
              <el-option label="兼职" :value="1" />
              <el-option label="全职" :value="2" />
            </el-select>
            
            <el-select v-model="filters.level" placeholder="等级" clearable style="width: 120px" @change="loadList">
              <el-option label="全部" :value="undefined" />
              <el-option label="青铜" :value="1" />
              <el-option label="白银" :value="2" />
              <el-option label="黄金" :value="3" />
              <el-option label="铂金" :value="4" />
              <el-option label="钻石" :value="5" />
            </el-select>
            
            <el-input v-model="filters.keyword" placeholder="搜索姓名/手机" clearable style="width: 200px" @keyup.enter="loadList">
              <template #append>
                <el-button icon="Search" @click="loadList" />
              </template>
            </el-input>
          </el-space>
        </el-col>
      </el-row>
    </el-card>
    
    <!-- 列表 -->
    <el-card shadow="never" class="table-card">
      <el-table v-loading="loading" :data="list" stripe>
        <template #empty>
          <div class="custom-empty-state">
            <el-icon class="empty-icon"><User /></el-icon>
            <div class="empty-text">暂无红娘数据</div>
            <div class="empty-hint">点击"添加红娘"按钮开始创建</div>
          </div>
        </template>
        <el-table-column type="index" label="序号" width="60" />
        <el-table-column prop="avatarUrl" label="头像" width="80">
          <template #default="{ row }">
            <el-avatar :src="row.avatar_url" />
          </template>
        </el-table-column>
        <el-table-column prop="realName" label="姓名" width="100">
          <template #default="{ row }">
            <span class="data-highlight">{{ row.real_name }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="phone" label="手机号" width="130" />
        <el-table-column prop="matchmakerType" label="类型" width="90" align="center">
          <template #default="{ row }">
            <el-tag :type="row.matchmakerType === 2 ? 'success' : 'info'" size="small" effect="light">
              {{ row.matchmakerType === 2 ? '全职' : '兼职' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="level" label="等级" width="90" align="center">
          <template #default="{ row }">
            <el-tag :type="getLevelColor(row.level)" size="small" effect="light">
              {{ getLevelText(row.level) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="successCouples" label="成功对数" width="100" align="center">
          <template #default="{ row }">
            <span class="number-emphasis">{{ row.successCouples || 0 }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="cityName" label="城市" width="120" />
        <el-table-column prop="status" label="状态" width="90" align="center">
          <template #default="{ row }">
            <el-tag 
              :class="{
                'status-success': row.status === 1,
                'status-danger': row.status === 0,
                'status-disabled': row.status === 2
              }"
              size="small"
              effect="light"
            >
              {{ row.status === 1 ? '正常' : row.status === 0 ? '禁用' : '离职' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" size="small" link @click="$router.push(`/matchmaker/edit/${row.matchmaker_id}`)">
              编辑
            </el-button>
            <el-button type="danger" size="small" link @click="handleDelete(row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :total="total"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="loadList"
          @current-change="loadList"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { User } from '@element-plus/icons-vue'
import request from '@/utils/request'
import { API_ENDPOINTS } from '@/config/api'

const loading = ref(false)
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)
const list = ref([])

const filters = reactive({
  matchmakerType: null,
  level: null,
  keyword: ''
})

const loadList = async () => {
  loading.value = true
  try {
    const response = await request.post(API_ENDPOINTS.MATCHMAKER_LIST, {
      pageNum: currentPage.value,
      pageSize: pageSize.value,
      matchmakerType: filters.matchmakerType,
      level: filters.level,
      keyword: filters.keyword
    })
    if (response.code === 200) {
      list.value = response.data.records || response.data.list || response.data || []
      total.value = response.data.total || list.value.length
    }
  } catch (error) {
    console.error('加载失败:', error)
    ElMessage.error('加载失败')
  } finally {
    loading.value = false
  }
}

const getLevelText = (level) => {
  const texts = { 1: '青铜', 2: '白银', 3: '黄金', 4: '铂金', 5: '钻石' }
  return texts[level] || '未知'
}

const getLevelColor = (level) => {
  const colors = { 1: '', 2: 'info', 3: 'warning', 4: 'success', 5: 'danger' }
  return colors[level] || ''
}

const handleDelete = async (row) => {
  try {
    await ElMessageBox.confirm('确定要删除这个红娘吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    const response = await request.delete(`${API_ENDPOINTS.MATCHMAKER_DELETE}/${row.matchmakerId}`)
    if (response.code === 200) {
      ElMessage.success('删除成功')
      loadList()
    }
  } catch (error) {
    if (error !== 'cancel') console.error('删除失败:', error)
  }
}

onMounted(() => loadList())
</script>

<style scoped>
@import '@/assets/list-common.css';

.matchmaker-list-container {
  padding: 0;
}
</style>

