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 { closeLoading, errorNotice, loading, successNotice } from "../../utils/utils"
import { modifyUser, userDetail } from "../../utils/api"
import { useEffect } from "react"

const activeEye = eye

const uinfo = () => {

    const [account, setAccount] = useState('')
    const [user, setUser] = useState({})


    useEffect(() => {
        userDetail().then(rs => {
            if (!rs) return
            setUser(rs)
            setAccount(rs.nick_name)
        })

    }, [])


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

    const submit = async () => {
        if (!account) {
            errorNotice("请输入要修改的昵称")
            return
        }
        loading('昵称修改中...')
        const re = await modifyUser('nick_name', account)
        closeLoading()
        if (!re) return
        successNotice('昵称修改成功')
        setTimeout(() => {
            backFn()
        }, 1000)
    }


    return <View className="uinfo-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="uinfo-container bg-slate-50 relative">
            <View>
                {/* <View className="form-item mt-22">
                    <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" type='text' placeholder="请输入昵称" value={account} onInput={(v) => {
                            setAccount(v.detail.value)
                        }} />
                        {
                            account && <Image className="w-6 h-6 absolute right-0 bottom-16" src={checked} />
                        }
                    </View>
                </View>
                <View className="uinfo-footer flex flex-col justify-center">
                    <Button className="uinfo-btn" onClick={submit}>保存</Button>

                </View>
            </View>

        </View>

    </View>
}


export default uinfo