import { getSN, getManufacturer } from '@/utils'
import loginApi from '@/api/login'
import { userStore } from '@/stores'
import { storeToRefs } from 'pinia'

export const useBindQrCode = () => {
  const bindQrCode = ref('')
  const loadingQrCode = ref(true)
  const { userInfo } = storeToRefs(userStore())

  const initQrCode = async () => {
    loadingQrCode.value = true
    try {
      const SN = getSN()
      const model = getManufacturer()
      const {
        data: { data: res },
      } = await loginApi.getBindQrCode({
        qrCodeFileName: `code_${userInfo.value.account}_${SN}`,
        qrCodeInfo: JSON.stringify({
          account: userInfo.value.account,
          userId: userInfo.value.userId,
          simSerialNumber: SN,
          model: model,
        }),
      })
      bindQrCode.value = res.filePath
    } finally {
      loadingQrCode.value = false
    }
  }

  return {
    bindQrCode,
    loadingQrCode,
    initQrCode,
  }
}
