import request from '@/utils/user_center_request';
import request2 from '@/utils/request2';
import md5 from 'js-md5';
import {UserUISettings} from '@/types/user';

export const loginService = (loginData) => {
    const params = new URLSearchParams();
    loginData = loginData._value;
    for (let key in loginData) {
        if (key === "username") {
            //婵犳鍠楃换鎰緤娴犲鍨傛い鏍ㄧ矋閸嬫﹢鏌曟繛鍨鐟滄妸鍛亾濞堝灝鏋涚紒缁樺姍瀹曡绂掔€ｎ亝顥濆銈嗘尪閸ㄨ绂掕箛鏇犵＜闁绘瑥鎳忕€氾拷
            if (RegExp(/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/).test(loginData[key])) {
                loginData['email'] = loginData[key]
                loginData[key] = ''
            } 
        }
        // if (key === 'password') {
        //     loginData[key] = md5(loginData[key])
        // }
        params.append(key, loginData[key])
    }
    return request.post('/user/login', params)
}

export const registerService = (registerData) => {
    const params = new URLSearchParams();
    registerData = registerData._value;
    for (let key in registerData) {
        params.append(key, registerData[key])
    }
    console.log("registerdata:",registerData)
    return request.post('/user/register', params)
}

export const sendLoginCode = (Data) => {
    return request.post('/user/login_code', Data, {
        headers: {
            'token': Data.token, 
            "Content-Type": "application/json"
        }
    });
}

export const loginByCode = (Data) => {
    return request.post('/user/login_by_code', Data, {
        headers: {
            'token': Data.token, 
            "Content-Type": "application/json"
        }
    });
}


export const getUUIDService = (registerData) => {
    const params = new URLSearchParams();
    for (let key in registerData) {
        params.append(key, registerData[key])
    }
    return request.post('/user/uuid', params)
}

export const getAllDefaultUsers = () => {
    return request.get('/user/get_all_default_users')
}

export const addGroupRequestService  = (Data) => {
    const params = new URLSearchParams();
    for (let key in Data) {
        params.append(key, Data[key])
    }
    return request.post('/im/send_message', params,{
        headers: {
            'token': Data.token,
        }
    })
}

export const AdminAddUserRequestService  = (Data) => {
    const params = new URLSearchParams();
    for (let key in Data) {
        params.append(key, Data[key])
    }
    let token = localStorage.getItem("token") || "";
    return request.post('/user/admin_add_user', params,{
        headers: {
            'token': token,
        }
    })
}

export const AdminAddUserGroupRequestService  = (Data) => {
    const params = new URLSearchParams();
    for (let key in Data) {
        params.append(key, Data[key])
    }
    let token = localStorage.getItem("token") || "";
    return request.post('/user/add_group', params,{
        headers: {
            'token': token,
        }
    })
}

export const AdminDeleteUserRequestService  = (Data) => {
    const params = new URLSearchParams();
    for (let key in Data) {
        params.append(key, Data[key])
    }
    let token = localStorage.getItem("token") || "";
    return request.post('/user/delete', params,{
        headers: {
            'token': token,
        }
    })
}

export const GetUserTokenService  = (Data) => {
    const params = new URLSearchParams();
    for (let key in Data) {
        params.append(key, Data[key])
    }
    let token = localStorage.getItem("token") || "";
    return request.post('/user/gen_user_token', params,{
        headers: {
            'token': token,
        }
    })
}


export const UpdateUserCatalogueService  = (Data) => {
    const params = new URLSearchParams();
    for (let key in Data) {
        params.append(key, Data[key])
    }
    let token = localStorage.getItem("token") || "";
    return request.post('/user/catalogue', params,{
        headers: {
            'token': token,
        }
    })
}

export const getFriendReqService = (Data) => {
    const params = new URLSearchParams();
    for (let key in Data) {
        params.append(key, Data[key])
    }
    return request.post('/im/get_friend_request', params, {
        headers: {
            'token': Data.token,
        }
    })
}

export const GetUserInfoService = (Data) => {
    const params = new URLSearchParams();
    for (let key in Data) {
        params.append(key, Data[key])
    }
    return request.post('/user/info', params, {
        headers: {
            'token': Data.token,
        }
    })
}


export const DelFGService =(Data) =>{
    const params = new URLSearchParams();
    for (let key in Data) {
        params.append(key, Data[key])
    }
    return request.post('/im/del_friend_or_group', params, {
        headers: {
            'token': Data.token,
        }
    })
}

export const updateUserInfoService = (Data) => {
    const params = new URLSearchParams();
    for (let key in Data) {
        params.append(key, Data[key])
    }
    return request.post('/user/update', params, {
        headers: {
            'token': Data.token,  
        }
    })
}

export const acceptInviteService =(Data)=> {
    const params = new URLSearchParams();
    for (let key in Data) {
        params.append(key, Data[key])
    }
    return request.post('/im/accept_invite', params, {
        headers: {
            'token': Data.token,
        }
    })
}
export const rejectInviteService =(Data)=> {
    const params = new URLSearchParams();
    for (let key in Data) {
        params.append(key, Data[key])
    }
    return request.post('/im/reject_invite', params, {
        headers: {
            'token': Data.token,
        }
    })
}

export const SearchUserService = (Data) => {
    const params = new URLSearchParams();
    for (let key in Data) {
        params.append(key, Data[key])
    }
    return request.post('/user/search', params,{
        headers: {
            'token': Data.token,
        }
    })
}

export const GetUserStatisticService = (Data) => {
    const params = new URLSearchParams();
    for (let key in Data) {
        params.append(key, Data[key])
    }
    return request2.post('/tool/statistic', params,{
        headers: {
            'token': Data.token,   
        }
    })
}


export const getQRService = (qrData) => {
    const params = new URLSearchParams();
    //console.log("qrdata=",qrData);
    for (let key in qrData) {
        params.append(key, qrData[key])
    }
    return request.post('/user/gqr', params)
}

export const genResetPassword = (Data) => {
    const params = new URLSearchParams();
    //console.log("qrdata=",qrData);
    for (let key in Data) {
        params.append(key, Data[key])
    }
    return request.post('/user/reset', params)
}

//获取第三方登录uuid
export const getThirdPartyUUID = (data) => {
    let url ='/user/oAuth_uuid' + "?type=" + data["type"]
    return request.get(url)
}

//获取第三方登录的url
export const getThirdPartyLoginUrl = (Data) => {
    let url ='/tool/get_auth_url' + "?uuid=" + Data.uuid + "&type=" + Data["type"] + "&platform=" + Data["platform"] + "&fingerprint=" + Data["fingerprint"]
    if (Data["type"] === "add" ) {
        url += "&token=" + Data["token"]
    }
    return request.get(url)
    
}

//获取绑定的第三方登录信息
export const getThirdPartyInfo = (Data) => {
    const params = new URLSearchParams();
    //console.log("qrdata=",qrData);
    // for (let key in Data) {
    //     params.append(key, Data[key])
    // }
    return request.post('/user/third_party_login_list', params ,{
        headers: {
            'token': Data.token,   
        }
    })
}
export const deleteThirdPartyInfo = (Data) => {
    let url ='/user/delete_third_party_login' + "?id=" + Data.id
    return request.delete(url, {
        headers: {
            'token': Data.token,   
        }
    })
}

export const getRigisterEmailCode = (Data) => {
    const params = new URLSearchParams();
    //console.log("qrdata=",qrData);
    for (let key in Data) {
        params.append(key, Data[key])
    }
    return request.post('/user/register_code' , params)
}

//获取qq登录的url
export const getQQLoginUrl = (Data) => {
    let url ='/tool/qq_auth' + "?uuid=" + Data.uuid + "&type=" + Data["type"]
    return request.get(url)
}
//获取github登录的url
export const getGithubLoginUrl = (Data) => {
    let url ='/tool/github_auth' + "?uuid=" + Data.uuid + "&type=" + Data["type"]
    return request.get(url)
}

//获取第三方登录状态
export const getThirdPartyLoginStatus = (Data) => {
    let url ='/user/oAuth' + "?uuid=" + Data.uuid + "&host_id=" + Data.host_id
    return request.get(url)
}

//获取用户前端配置
export const getUserUIconfigInfoService = (Data) => {
    let url ='/tool/get_user_ui_config'
    return request2.get(url,{
        headers: {
            'token': Data.token,   
        }
    })
}

export const updateUserUIconfigInfoService = (Data: UserUISettings, token:string) => {
    return request2.post('/tool/set_user_ui_config' , Data, {
        headers: {
            'token': token,   
            'Content-Type': 'application/json' // 设置请求头为 JSON 格式
        }
    })
}

export const getUserTokenCode = (Data) => {
    return request.get('/user/get_token_code', {
        headers: {
            'Authorization': `Bearer ${Data.token}`, // 使用 Bearer 认证方式
        }
    })
}

export const getTokenByCode = (Data) => {
    let url = '/user/get_token_by_code' + "?code=" + Data.code
    return request.get(url, {
        headers: {
            'Authorization': `Bearer ${Data.token}`, // 使用 Bearer 认证方式
        }
    })
}

//生成totp密钥
export const genTOTPSecret = (Data) => {
    return request.get('/tool/gen_totp_secret', {
        headers: {
            'token': Data.token,
        }
    })
}
export const getTOTPSecretInfo = (Data) => {
    return request.get('/tool/get_totp_secret_info', {
        headers: {
            'token': Data.token,
        }
    })
}

export const unBindTOTPSecretService = (Data) => {
    return request.delete('/tool/del_totp_secret', {
        headers: {
            'token': Data.token,
        }
    })
}

export const secondAuthLoginService = (Data) => {
    let token = Data.token
    delete Data.token
    return request.post('/user/second_auth' , Data, {
        headers: {
            'token': token,   
            'Content-Type': 'application/json' // 设置请求头为 JSON 格式
        }
    })
}

export const SetTokenCodeStatusService = (code, token) => {
    let url = '/user/set_client_token_status' + "?code=" + code
    return request.get(url, {
        headers: {
            'token': token,
        }
    })
}
export const fetchUserData = () => {
    return {
        "list": [
            {
                "id": 1,
                "name": "张三",
                "password": "123",
                "email": "123@qq.com",
                "phone": "12345678944",
                "date": "2024-01-01",
                "role": "管理员"
            },
            {
                "id": 2,
                "name": "李四",
                "password": "123",
                "email": "1234@qq.com",
                "phone": "12345678945",
                "date": "2024-01-01",
                "role": "普通用户"
            }
        ],
        "pageTotal": 2
    }
};