import { defineStore } from 'pinia'
import { ref } from 'vue'
import axios from 'axios'

export const useCourseStore = defineStore('course', () => {
  const courses = ref([])
  const loading = ref(false)
  const error = ref(null)

  const fetchCourses = async () => {
    loading.value = true
    error.value = null
    
    try {
      // 在实际应用中，这里应该调用后端API
      // 这里使用模拟数据
      const mockCourses = [
        {
          id: 1,
          title: '保密法律法规学习',
          category: 'law',
          categoryName: '法律法规',
          coverImg: 'https://picsum.photos/300/200?random=1',
          price: 0,
          isFavorite: false
        },
        {
          id: 2,
          title: '深度学习在气象云图识别中的应用',
          category: 'test',
          categoryName: '测试',
          coverImg: 'https://picsum.photos/300/200?random=2',
          price: 0,
          isFavorite: false
        },
        {
          id: 3,
          title: '天气雷达与云图识别基础',
          category: 'test',
          categoryName: '测试',
          coverImg: 'https://picsum.photos/300/200?random=3',
          price: 0,
          isFavorite: false
        },
        {
          id: 4,
          title: '气象卫星云图分析与天气预测',
          category: 'test',
          categoryName: '测试',
          coverImg: 'https://picsum.photos/300/200?random=4',
          price: 0,
          isFavorite: false
        },
        {
          id: 5,
          title: '【云帆演示】问答演示',
          category: 'skill',
          categoryName: '职业技能',
          coverImg: 'https://picsum.photos/300/200?random=5',
          price: 0,
          isFavorite: false
        },
        {
          id: 6,
          title: '【云帆课程】直播课程-不在线调联系客服',
          category: 'business',
          categoryName: '企业培训',
          coverImg: 'https://picsum.photos/300/200?random=6',
          price: 0,
          isFavorite: false
        },
        {
          id: 7,
          title: '【云帆演示】站外直播+课件学习',
          category: 'business',
          categoryName: '企业培训',
          coverImg: 'https://picsum.photos/300/200?random=7',
          price: 0,
          isFavorite: false
        },
        {
          id: 8,
          title: '【云帆课程】综合课程-包含视频课程及文档类课程',
          category: 'skill',
          categoryName: '职业技能',
          coverImg: 'https://picsum.photos/300/200?random=8',
          price: 0,
          isFavorite: false
        }
      ]
      
      courses.value = mockCourses
    } catch (err) {
      error.value = err.message || '获取课程失败'
    } finally {
      loading.value = false
    }
  }

  const toggleFavorite = (courseId) => {
    const course = courses.value.find(c => c.id === courseId)
    if (course) {
      course.isFavorite = !course.isFavorite
    }
  }

  return {
    courses,
    loading,
    error,
    fetchCourses,
    toggleFavorite
  }
}) 