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 backNav from '@/images/backNav.png'
import { Picker } from "@nutui/nutui-react-taro"
import { profitToScore, userDetail } from "../../utils/api"
import { useEffect } from "react"
import { closeLoading, errorNotice, loading, successNotice } from "../../utils/utils"

const activeEye = eye

const Login = () => {

    const [account, setAccount] = useState('')
    const [user, setUser] = useState({})
    const [visible, setVisible] = useState(false)
    const [bank, setBank] = useState({ value: 0, text: '' })

    useEffect(() => {
        userDetail().then(rs => {
            if (!rs) return
            setUser(rs)
        })
    }, [])

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

    const submit = async () => {
        if (!account) {
            errorNotice("请输入转换金额")
            return
        }
        if (user.profit < 1) {
            errorNotice("余额不足")
            return
        }
        loading('转换中...')
        const re = await profitToScore(account)
        closeLoading()
        if (!re) return
        successNotice('余额转换成功')
        setTimeout(() => {
            backFn()
        }, 1000)
    }


    return <View className="conversion-frame bg-slate-50 h-screen text-base">
        <View className='addr-detail-title'>
            <Image src={backNav} className="square-35 absolute left-10 nav-icon" onClick={backFn} />
            立即转换
        </View>
        <View className="conversion-container relative">
            <View className="conversion-amount flex-col justify-center">
                <Text>当前收益</Text>
                <Text className="conversion-amount-price">{user.profit}</Text>
            </View>
            <View>
                <View className="form-item mt-22">
                    <View className="form-label">输入转换金额</View>
                    <View className="form-control relative">
                        <Input className="form-input" type="digit" 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="conversion-footer ">
                        <Button className="conversion-btn" onClick={submit}>立即转换</Button>
                    </View>
                }
            </View>

        </View>
    </View>
}


export default Login