import { shallowRef, reactive } from 'vue'
import { useLoginStore } from '@/stores'
import { useDataTable } from '@/hooks/datatable'
import { investorDel, modifyPassword, queryWrDraftUserInfo, requestAddAuth } from '@/services/api/account'
import cryptojs from 'crypto-js'

// 查询用户实名认证信息
export function getWrDraftUserInfo(userstatus?: number) {
    const { getUserId } = useLoginStore()
    const { dataList } = useDataTable<Model.UserInfo>()
    const loading = shallowRef(false)
   
    const formData = reactive<Model.WrDraftUserInfoReq>({
        userstatus: userstatus,
        userid: getUserId()
    })

    const getWrDraftUserInfoList = () => {
        const {userstatus, userid} = formData
        loading.value = true

        return queryWrDraftUserInfo({
            data: {
                userid: userid,
                userstatus: userstatus
            },
            success: (res) => {
                dataList.value = res.data
            },
            complete: () => {
                loading.value = false
            }
        })
    }

    return {
        loading, 
        formData, 
        dataList, 
        getWrDraftUserInfoList
    }
}

// 用户实名认证
export function addAuthReq() {
    const { getUserId } = useLoginStore()
    const loading = shallowRef(false)
   
    const formData = reactive<Partial<Model.AddAuthReq>>({
        userid: getUserId(),
        cardtype: 0
    })

    const formSubmit = () => {
        loading.value = true

        return requestAddAuth({
            data: {
                ...formData,
                username: formData.username,
                cardtype: formData.cardtype,
                cardbackphotourl: formData.cardbackphotourl,
                cardfrontphotourl: formData.cardfrontphotourl,
                cardnum: formData.cardnum
            },
            success: (res) => {
                console.log(res)
            },
            complete: () => {
                loading.value = false
            }
        })
    }

    return {
        loading, 
        formData, 
        formSubmit
    }
}

// 修改密码
export function useAccountPassword(ModifyPwdType: 0 | 1 | 2) {
    const { getLoginId, getFirstAccountId } = useLoginStore()
    const loading = shallowRef(false)

    const formData = reactive<Proto.ModifyPwdReq>({
        ModifyPwdType, // 修改密码类型1：登陆密码2：资金账户密码
        ModifyPwdID: ModifyPwdType === 1 ? getLoginId() : getFirstAccountId(), // 被修改的ID(ModifyType=1:LoginIDModifyType=2:AccountID)
        OldPwd: '', // 旧资金密码
        NewPwd: '' // 新资金密码
    })

    const formSubmit = () => {
        const { ModifyPwdType, ModifyPwdID, OldPwd, NewPwd } = formData
        loading.value = true

        return modifyPassword({
            data: {
                ModifyPwdType,
                ModifyPwdID,
                OldPwd: cryptojs.SHA256(ModifyPwdID + OldPwd).toString(),
                NewPwd: cryptojs.SHA256(ModifyPwdID + NewPwd).toString(),
            },
            complete: () => {
                loading.value = false
            }
        })
    }

    return {
        loading,
        formData,
        formSubmit
    }
}

// 账户注销
export function useAccountCancellation() {
    const { getUserId } = useLoginStore()
    const loading = shallowRef(false)

    const formSubmit = () => {
        loading.value = true
        return investorDel({
            data: {
                UserID: getUserId()
            },
            complete: () => {
                loading.value = false
            }
        })
    }

    return {
        loading,
        formSubmit,
    }
}