<template>
  <div class="advanced-search">
    <h1 class="page-title">高级搜索</h1>
    
    <el-form :model="searchForm" label-width="100px" class="search-form">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="关键词">
            <el-input v-model="searchForm.keyword" placeholder="课程名称、描述" />
          </el-form-item>
        </el-col>
        
        <el-col :span="8">
          <el-form-item label="分类">
            <el-select v-model="searchForm.category" placeholder="选择分类" clearable>
              <el-option v-for="item in categories" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>
        
        <el-col :span="8">
          <el-form-item label="难度级别">
            <el-select v-model="searchForm.difficulty" placeholder="选择难度" clearable>
              <el-option label="初级" value="beginner" />
              <el-option label="中级" value="intermediate" />
              <el-option label="高级" value="advanced" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="课程标签">
            <el-select v-model="searchForm.tags" multiple placeholder="选择标签" clearable>
              <el-option v-for="tag in tags" :key="tag.value" :label="tag.label" :value="tag.value" />
            </el-select>
          </el-form-item>
        </el-col>
        
        <el-col :span="8">
          <el-form-item label="价格">
            <el-radio-group v-model="searchForm.isFree">
              <el-radio :label="null">全部</el-radio>
              <el-radio :label="true">免费</el-radio>
              <el-radio :label="false">付费</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        
        <el-col :span="8">
          <el-form-item label="发布日期">
            <el-date-picker
              v-model="dateRange"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
            />
          </el-form-item>
        </el-col>
      </el-row>
      
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="排序方式">
            <el-select v-model="searchForm.sortBy" placeholder="排序字段">
              <el-option label="发布日期" value="publishDate" />
              <el-option label="热度" value="popularity" />
              <el-option label="评分" value="rating" />
            </el-select>
          </el-form-item>
        </el-col>
        
        <el-col :span="8">
          <el-form-item label="排序方向">
            <el-select v-model="searchForm.sortOrder" placeholder="排序方向">
              <el-option label="升序" value="asc" />
              <el-option label="降序" value="desc" />
            </el-select>
          </el-form-item>
        </el-col>
        
        <el-col :span="8">
          <el-form-item>
            <el-button type="primary" @click="handleSearch" :loading="loading">搜索</el-button>
            <el-button @click="resetForm">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    
    <!-- 搜索结果 -->
    <div class="search-results">
      <h2>搜索结果 ({{ total }})</h2>
      
      <el-table v-loading="loading" :data="courses" style="width: 100%">
        <el-table-column prop="title" label="课程名称" min-width="200">
          <template #default="{ row }">
            <router-link :to="`/course/detail/${row.id}`">{{ row.title }}</router-link>
          </template>
        </el-table-column>
        <el-table-column prop="categoryName" label="分类" width="120" />
        <el-table-column prop="difficulty" label="难度" width="100">
          <template #default="{ row }">
            <el-tag :type="getDifficultyType(row.difficulty)">
              {{ getDifficultyLabel(row.difficulty) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="price" label="价格" width="100">
          <template #default="{ row }">
            <span v-if="row.price > 0">¥{{ row.price }}</span>
            <el-tag v-else type="success">免费</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="publishDate" label="发布日期" width="120" />
        <el-table-column prop="rating" label="评分" width="100">
          <template #default="{ row }">
            <el-rate v-model="row.rating" disabled text-color="#ff9900" />
          </template>
        </el-table-column>
      </el-table>
      
      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="searchForm.page"
          v-model:page-size="searchForm.limit"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, watch } from 'vue'
import { searchCourses } from '../../api/course'

const loading = ref(false)
const courses = ref([])
const total = ref(0)
const dateRange = ref([])

// 分类选项
const categories = [
  { value: 'test', label: '测试' },
  { value: 'business', label: '企业培训' },
  { value: 'skill', label: '职业技能' },
  { value: 'law', label: '法律法规' }
]

// 标签选项
const tags = [
  { value: 'python', label: 'Python' },
  { value: 'java', label: 'Java' },
  { value: 'javascript', label: 'JavaScript' },
  { value: 'cloud', label: '云计算' },
  { value: 'ai', label: '人工智能' },
  { value: 'security', label: '网络安全' }
]

const searchForm = ref({
  page: 1,
  limit: 10,
  keyword: '',
  category: '',
  difficulty: '',
  tags: [],
  isFree: null,
  sortBy: 'publishDate',
  sortOrder: 'desc',
  startDate: '',
  endDate: ''
})

// 监听日期范围变化，更新搜索表单
watch(dateRange, (newVal) => {
  if (newVal && newVal.length === 2) {
    searchForm.value.startDate = newVal[0]
    searchForm.value.endDate = newVal[1]
  } else {
    searchForm.value.startDate = ''
    searchForm.value.endDate = ''
  }
})

// 执行搜索
const handleSearch = async () => {
  loading.value = true
  try {
    const res = await searchCourses(searchForm.value)
    courses.value = res.data.list
    total.value = res.data.total
  } catch (error) {
    console.error('搜索课程失败:', error)
  } finally {
    loading.value = false
  }
}

// 重置表单
const resetForm = () => {
  searchForm.value = {
    page: 1,
    limit: 10,
    keyword: '',
    category: '',
    difficulty: '',
    tags: [],
    isFree: null,
    sortBy: 'publishDate',
    sortOrder: 'desc',
    startDate: '',
    endDate: ''
  }
  dateRange.value = []
  handleSearch()
}

// 处理每页数量变化
const handleSizeChange = (val) => {
  searchForm.value.limit = val
  handleSearch()
}

// 处理页码变化
const handleCurrentChange = (val) => {
  searchForm.value.page = val
  handleSearch()
}

// 获取难度标签类型
const getDifficultyType = (difficulty) => {
  switch (difficulty) {
    case 'beginner': return 'success'
    case 'intermediate': return 'warning'
    case 'advanced': return 'danger'
    default: return 'info'
  }
}

// 获取难度标签文本
const getDifficultyLabel = (difficulty) => {
  switch (difficulty) {
    case 'beginner': return '初级'
    case 'intermediate': return '中级'
    case 'advanced': return '高级'
    default: return '未知'
  }
}

// 初始加载
handleSearch()
</script>

<style lang="scss" scoped>
.advanced-search {
  max-width: 1200px;
  margin: 0 auto;
  
  .search-form {
    background: white;
    padding: 20px;
    border-radius: 8px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    margin-bottom: 20px;
  }
  
  .search-results {
    background: white;
    padding: 20px;
    border-radius: 8px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    
    h2 {
      margin-bottom: 20px;
      font-size: 18px;
    }
  }
  
  .pagination-container {
    margin-top: 20px;
    display: flex;
    justify-content: flex-end;
  }
}
</style> 