// composables/useSendCode.js
import { ref, onUnmounted } from 'vue'

export function useSendCode() {
  const disabled = ref(false)
  const text = ref('发送验证码')
  let timer = null
  let count = 60

  const sendCode = () => {
    if (disabled.value) return

    disabled.value = true
    count = 60
    text.value = `剩余 ${count}s`

    timer = setInterval(() => {
      count--
      if (count < 0) {
        clearInterval(timer)
        disabled.value = false
        text.value = '重新获取'
        return
      }
      text.value = `剩余 ${count}s`
    }, 1000)
  }

  onUnmounted(() => {
    if (timer) clearInterval(timer)
  })

  return {
    disabled,
    text,
    sendCode
  }
}
