import {ref} from 'vue'
import { showToast } from 'vant';
import {_apiLogin} from '@/api/user'

export function useLogin(){
    //图形验证码src
    let picSrc = ref('')
    //图形验证码id
    let picId = ref('')

    //获取图形验证码
    async function getPicCode(){
        const res = await _apiLogin.apiGetPicCode()
        if(res.Ret!==200) return
        picId.value = res.Data.Id
        picSrc.value = res.Data.Base64Blob
    }

    async function getCode({mobile,email,picCode,areaCode},model,source){
        const res = await _apiLogin.apiGetVerifyCode({
            VerifyType:model==='mobile'?1:2,
            CaptchaId:picId.value,
            CaptchaCode:picCode,
            Mobile:mobile||'',
            Email:email||'',
            TelAreaCode:areaCode+''||'',
            Source:source
        })
        if(res.Ret!==200) return 
        showToast('验证码已发送')
        //60秒倒计时
        codeCountDown.value = 60
        countDown()
        timer = setInterval(()=>{
            countDown()
        },1000)
    }
    let timer = 0
    let codeStr = ref('获取验证码')
    let codeCountDown = ref(60)
    function countDown(){
        codeCountDown.value--
        codeStr.value = `重新获取（${codeCountDown.value}）秒`
        if(codeCountDown.value<=0){
            clearInterval(timer)
            codeStr.value = '重新获取'
        }
    }



    return {
        picId,
        picSrc,
        getPicCode,

        timer,codeStr,codeCountDown,
        countDown,
        getCode,
    }
}