import request from '@/utils/request'
import { LoginData } from '@/api/types'
import { useAdminInfo } from '@/stores/adminInfo'
import { sm3Digest } from '@/assets/commjs/sm3.js'
import { sm2, encrypt } from '@/assets/commjs/sm2.js'

// 获取公钥
export function gongkey(params?: any) {
    if (!params) {
        const adminInfo = useAdminInfo()
        params = {
            loginName: encrypt(adminInfo.$state.loginName)
        }
    }
    return request({
        url: '/user-boot/user/generateSm2Key',
        method: 'get',
        params
    })
}

export async function pwdSm3(pwd: any, loginName?: string) {
    let publicKey = await gongkey(
        loginName
            ? {
                  loginName: encrypt(loginName)
              }
            : false
    )
    let sm3Pwd = sm3Digest(pwd) //SM3加密
    return sm2(sm3Pwd + '|' + pwd, publicKey.data, 0)
}

//登录获取token
export async function login(params: any) {
    if (params.refresh_token == undefined) {
        params.password = await pwdSm3(params.password, params.username)
    }
    params.username = encrypt(params.username)
    return request({
        url: '/pqs-auth/oauth/token',
        method: 'post',
        params
    })
}

//获取用户信息
export function getUserById() {
    const adminInfo = useAdminInfo()
    return request({
        url: '/user-boot/user/getUserById?id=' + adminInfo.userIndex,
        method: 'get'
    })
}

// 刷新token
export function refreshToken(): Promise<any> {
    const adminInfo = useAdminInfo()
    return login({
        grant_type: 'refresh_token',
        refresh_token: adminInfo.refresh_token,
        username: adminInfo.loginName
    })
}

/**
 * 获取营销用户列表
 * @returns {AxiosPromise}
 */
export const getMarketList = () => {
    return request({
        url: '/user-boot/user/getMarketList',
        method: 'post'
    })
}

export function add(data: any) {
    return request({
        url: '/user-boot/user/add',
        method: 'post',
        data: data
    })
}

export function edit(data: any) {
    return request({
        url: '/user-boot/user/update',
        method: 'put',
        data: data
    })
}

export async function passwordConfirm(pwd: string) {
    return request({
        url: '/user-boot/user/passwordConfirm?password=' + (await pwdSm3(pwd)),
        method: 'get'
    })
}

export function deluser(data: any) {
    return request({
        url: '/user-boot/user/delete',
        method: 'delete',
        params: data
    })
}

export function activateUser(data: any) {
    return request({
        url: '/user-boot/user/activateUser',
        method: 'put',
        params: data
    })
}

export async function updatePassword(params: any) {
    return request({
        url: '/user-boot/user/updatePassword',
        method: 'put',
        params: {
            id: params.id,
            newPassword: await pwdSm3(params.newPassword)
        }
    })
}

export async function updateFirstPassword(params: any) {
    return request({
        url: '/user-boot/user/updateFirstPassword',
        method: 'put',
        data: {
            name: encrypt(params.name),
            password: await pwdSm3(params.password, params.name)
        }
    })
}

export function checkUser(data: any) {
    return request({
        url: '/user-boot/user/check',
        method: 'put',
        data: data
    })
}

/**
 * 查询所有用户包括管理员
 */
export const getAllUserSimpleList = () => {
    return request({
        url: '/user-boot/user/getAllUserSimpleList',
        method: 'GET'
    })
}

export const getSysConfig = () => {
    return request({
        url: '/system-boot/config/getSysConfig',
        method: 'get'
    })
}
// 驾驶舱组件配置
export const componentTree = () => {
    return request({
        url: '/user-boot/component/componentTree',
        method: 'GET'
    })
}
