import { defineStore } from 'pinia'

export const useCenterControlStore = defineStore(
  'centerControl',
  () => {
    const isOnline = ref(-1) // 是否在线 -1未连接 0离线 1在线
    const codeType = ref<'car' | 'sn'>('car')
    const queryByBike = reactive({
      ecuSn: '',
      bikeCode: '',
    })

    // 设置code类型
    function setCode(type: 'car' | 'sn', code: string) {
      codeType.value = type
      queryByBike.bikeCode = ''
      queryByBike.ecuSn = ''
      if (type === 'car') {
        queryByBike.bikeCode = code
      }
      else {
        queryByBike.ecuSn = code
      }
    }

    // 设置在线状态
    function setOnline(status: number) {
      isOnline.value = status
    }

    // 初始化状态
    onMounted(() => {
      setOnline(-1)
    })

    return {
      isOnline,
      queryByBike,
      codeType,

      setOnline,
      setCode,
    }
  },
)
