/**
 * 网卡管理 Composable
 */

import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { CardService } from '@/api/modules'
import type { Card, CardQueryParams, CardOperationParams } from '@/types/api'
import { getErrorMessage } from '@/utils/business'
import { usePagination } from './usePagination'
import { useTableSelection } from './useTableSelection'

export function useCardManagement() {
  // 加载状态
  const loading = ref(false)

  // 表格数据
  const tableData = ref<Card[]>([])

  // 分页
  const { pagination, setTotal, handleCurrentChange, handleSizeChange } = usePagination()

  // 表格选择
  const { selectedRows, selectedIds, hasSelected, handleSelectionChange, clearSelection } =
    useTableSelection<Card>()

  /**
   * 获取网卡列表
   * @param params 查询参数
   */
  const fetchCardList = async (params?: CardQueryParams) => {
    loading.value = true
    try {
      const res = await CardService.getCards({
        current: pagination.current,
        size: pagination.size,
        ...params
      })

      tableData.value = res.data.items
      setTotal(res.data.total)
    } catch (error) {
      console.error('获取网卡列表失败:', error)
      console.log('获取网卡列表失败')
    } finally {
      loading.value = false
    }
  }

  /**
   * 根据ICCID获取单卡信息
   * @param iccid ICCID
   */
  const fetchCardByIccid = async (iccid: string) => {
    loading.value = true
    try {
      const res = await CardService.getCardByIccid(iccid)
      return res.data
    } catch (error) {
      console.error('获取单卡信息失败:', error)
      console.log('获取单卡信息失败')
      return null
    } finally {
      loading.value = false
    }
  }

  /**
   * 网卡操作
   * @param params 操作参数
   */
  const cardOperation = async (params: CardOperationParams) => {
    loading.value = true
    try {
      await CardService.cardOperation(params)
      ElMessage.success('操作成功')
      return true
    } catch (error) {
      console.error('操作失败:', error)
      console.log('操作失败')
      return false
    } finally {
      loading.value = false
    }
  }

  /**
   * 停机
   * @param iccid ICCID
   * @param remark 备注
   */
  const suspendCard = async (iccid: string, remark?: string) => {
    loading.value = true
    try {
      await CardService.suspend(iccid, remark)
      ElMessage.success('停机成功')
      return true
    } catch (error) {
      console.error('停机失败:', error)
      console.log('停机失败')
      return false
    } finally {
      loading.value = false
    }
  }

  /**
   * 复机
   * @param iccid ICCID
   * @param remark 备注
   */
  const resumeCard = async (iccid: string, remark?: string) => {
    loading.value = true
    try {
      await CardService.resume(iccid, remark)
      ElMessage.success('复机成功')
      return true
    } catch (error) {
      console.error('复机失败:', error)
      ElMessage.error(getErrorMessage(error, '复机失败'))
      return false
    } finally {
      loading.value = false
    }
  }

  /**
   * 批量导入网卡
   * @param file Excel文件
   * @param params 额外参数
   */
  const importCards = async (file: File, params?: Record<string, any>) => {
    loading.value = true
    try {
      await CardService.importCards(file, params)
      ElMessage.success('导入成功')
      return true
    } catch (error) {
      console.error('导入失败:', error)
      console.log('导入失败')
      return false
    } finally {
      loading.value = false
    }
  }

  /**
   * 刷新列表
   */
  const refresh = () => {
    fetchCardList()
  }

  return {
    loading,
    tableData,
    pagination,
    selectedRows,
    selectedIds,
    hasSelected,
    handleSelectionChange,
    handleCurrentChange,
    handleSizeChange,
    clearSelection,
    fetchCardList,
    fetchCardByIccid,
    cardOperation,
    suspendCard,
    resumeCard,
    importCards,
    refresh
  }
}
