import { Image, Input, Text, View } from "@tarojs/components"
import back from '@/images/back.png'
import checked from '@/images/checked.png'
import eyeClose from '@/images/eyeClose.png'
import eye from '@/images/eye.png'
import './index.scss'
import { Button } from "@nutui/nutui-react-taro"
import { useState } from "react"
import Taro from "@tarojs/taro"
import { pwdManage, sendCode } from "../../utils/api"
import { closeLoading, errorNotice, loading, successNotice } from "../../utils/utils"

const activeEye = eye

const Login = () => {

    const [account, setAccount] = useState('')
    const [pwd, setPwd] = useState()
    const [confirmPwd, setConfirmPwd] = useState()
    const [showPwd, setShowPwd] = useState(false)
    const [showConfirmPwd, setShowConfirmPwd] = useState(false)

    const [mobile, setMobile] = useState('')
    const [smsCode, setSmsCode] = useState('')
    const [interval, setIntervalTime] = useState(0)


    // 返回页面
    const backFn = () => {
        Taro.getCurrentPages().length > 0 && Taro.navigateBack()
    }


    // 去登陆
    const loginFn = () => {
        Taro.redirectTo({
            url: '/pages/login/index'
        })
    }

    // 倒计时
    const countDown = async () => {
        if (!account) {
            return
        }
        setIntervalTime(60)
        if (interval > 0) {
            return
        }
        let start = 60
        const timer = setInterval(() => {
            if (start > 0) {
                start--
                if (start <= 0) {
                    clearInterval(timer)
                }
                setIntervalTime(start)
            }
        }, 1000)
        const re = await sendCode(account)
        if (!re) return
        Taro.showToast({ title: '验证码发送成功', icon: 'success' })
    }

    const submit = async () => {
        if (!account || !smsCode || !pwd || !confirmPwd) {
            errorNotice('请完善信息')
            return
        }
        if (pwd !== confirmPwd) {
            errorNotice("两次密码不一致")
            return
        }
        loading('密码重置中，请稍后～')
        const re = await pwdManage('login_password', {
            confirm_password: confirmPwd,
            password: pwd,
            phone: account,
            verification_code: smsCode
        })
        closeLoading()
        if (!re) return
        successNotice('密码重置成功，请重新登录')
        setTimeout(() => {
            backFn()
        }, 2000)
    }

    return <View className="login-frame bg-slate-50 h-screen text-base">
        <View className="login-header flex justify-center items-center text-lg font-bold">
            <Image src={back} className="square-35 absolute left-7" onClick={backFn} />
            <View>新起点</View>
        </View>
        <View className="forgot-container relative">
            <View className="relative  font-bold text-lg block h-6 ">忘记密码</View>
            <View>
                <View className="form-item mt-58">
                    <View className="form-label">手机号</View>
                    <View className="form-control relative">
                        <Input className="form-input" placeholder="请输入手机号" onInput={(v) => {
                            setAccount(v.detail.value)
                        }} />
                        {
                            account && <Image className="w-6 h-6 absolute right-0 bottom-16" src={checked} />
                        }

                    </View>
                </View>
                <View className="form-item mt-22">
                    <View className="form-label">验证码</View>
                    <View className="form-control relative">
                        <Input className="form-input" maxlength={6} placeholder="请输入验证码" onInput={(v) => {
                            setSmsCode(v.detail.value)
                        }} />
                        <Button className="code-btn" disabled={interval > 0} onClick={countDown}>{interval ? interval + 's' : '获取验证码'}</Button>
                    </View>
                </View>
                <View className="form-item mt-22">
                    <View className="form-label">密码</View>
                    <View className="form-control relative">
                        <Input className="form-input" type={showPwd ? 'text' : 'password'} placeholder="设置新密码" onInput={(v) => {
                            setPwd(v.detail.value)
                        }} />
                        <Image className="w-6 h-6 absolute right-0 bottom-16" src={showPwd ? activeEye : eyeClose} onClick={() => {
                            setShowPwd(v => !v)
                        }} />
                    </View>
                </View>
                <View className="form-item mt-22">
                    <View className="form-label">确认密码</View>
                    <View className="form-control relative">
                        <Input className="form-input" type={showConfirmPwd ? 'text' : 'password'} placeholder="再次确认新密码" onInput={(v) => {
                            setConfirmPwd(v.detail.value)
                        }} />
                        <Image className="w-6 h-6 absolute right-0 bottom-16" src={showConfirmPwd ? activeEye : eyeClose} onClick={() => {
                            setShowConfirmPwd(v => !v)
                        }} />
                    </View>
                </View>
                <View className="forgot-form-helper">
                    <View className="forgot-password" onClick={loginFn}>已有账号，去登陆</View>
                </View>
                <View className="login-footer flex flex-col justify-center">
                    <Button className="login-btn" onClick={submit}>找回密码</Button>
                </View>
            </View>

        </View>

    </View>
}


export default Login