import { defineStore } from 'pinia'
import { ref } from 'vue'
import type { Flower, FlowerQueryParams, FlowerSale } from '@/api/flower'

interface FlowerState {
  flowers: Flower[]
  currentFlower: Flower | null
  sales: FlowerSale[]
  loading: boolean
  salesLoading: boolean
  totalCount: number
  salesTotalCount: number
  queryParams: FlowerQueryParams
}

export const useFlowerStore = defineStore('flower', () => {
  // 状态
  const flowers = ref<Flower[]>([])
  const currentFlower = ref<Flower | null>(null)
  const sales = ref<FlowerSale[]>([])
  const loading = ref(false)
  const salesLoading = ref(false)
  const totalCount = ref(0)
  const salesTotalCount = ref(0)
  
  const queryParams = ref<FlowerQueryParams>({
    page: 1,
    limit: 20,
    keyword: '',
    type: undefined,
    status: undefined,
    merchantId: undefined
  })

  // 获取花卉列表
  const fetchFlowers = async (params?: FlowerQueryParams) => {
    loading.value = true
    try {
      if (params) {
        Object.assign(queryParams.value, params)
      }
      
      const response = await import('@/api/flower').then(m => m.getFlowers(queryParams.value))
      if (response.data.success) {
        flowers.value = response.data.data.flowers
        totalCount.value = response.data.data.pagination.total
      }
    } catch (error) {
      console.error('获取花卉列表失败:', error)
      throw error
    } finally {
      loading.value = false
    }
  }

  // 获取花卉详情
  const fetchFlower = async (id: number) => {
    try {
      const response = await import('@/api/flower').then(m => m.getFlower(id))
      if (response.data.success) {
        currentFlower.value = response.data.data.flower
        return response.data.data.flower
      }
    } catch (error) {
      console.error('获取花卉详情失败:', error)
      throw error
    }
  }

  // 创建花卉
  const createFlower = async (data: any) => {
    try {
      const response = await import('@/api/flower').then(m => m.createFlower(data))
      if (response.data.success) {
        return response.data.data.flower
      }
    } catch (error) {
      console.error('创建花卉失败:', error)
      throw error
    }
  }

  // 更新花卉
  const updateFlower = async (id: number, data: any) => {
    try {
      const response = await import('@/api/flower').then(m => m.updateFlower(id, data))
      if (response.data.success) {
        return response.data.data.flower
      }
    } catch (error) {
      console.error('更新花卉失败:', error)
      throw error
    }
  }

  // 删除花卉
  const deleteFlower = async (id: number) => {
    try {
      const response = await import('@/api/flower').then(m => m.deleteFlower(id))
      if (response.data.success) {
        return true
      }
    } catch (error) {
      console.error('删除花卉失败:', error)
      throw error
    }
  }

  // 获取销售记录
  const fetchSales = async (params?: any) => {
    salesLoading.value = true
    try {
      const response = await import('@/api/flower').then(m => m.getFlowerSales(params))
      if (response.data.success) {
        sales.value = response.data.data.sales
        salesTotalCount.value = response.data.data.pagination.total
      }
    } catch (error) {
      console.error('获取销售记录失败:', error)
      throw error
    } finally {
      salesLoading.value = false
    }
  }

  // 重置状态
  const reset = () => {
    flowers.value = []
    currentFlower.value = null
    sales.value = []
    loading.value = false
    salesLoading.value = false
    totalCount.value = 0
    salesTotalCount.value = 0
    Object.assign(queryParams.value, {
      page: 1,
      limit: 20,
      keyword: '',
      type: undefined,
      status: undefined,
      merchantId: undefined
    })
  }

  return {
    flowers,
    currentFlower,
    sales,
    loading,
    salesLoading,
    totalCount,
    salesTotalCount,
    queryParams,
    
    fetchFlowers,
    fetchFlower,
    createFlower,
    updateFlower,
    deleteFlower,
    fetchSales,
    reset
  }
})