import { HcApi } from '../request/index'
import website from '~src/config/index'
import md5 from 'js-md5'

export const userLogin = ({ tenantId, deptId, roleId, username, password, type, key, code }) => HcApi({
    url: '/api/blade-auth/oauth/token',
    method: 'post',
    headers: {
        'Tenant-Id': tenantId,
        'Dept-Id': (website.switchMode ? deptId : ''),
        'Role-Id': (website.switchMode ? roleId : ''),
        'Captcha-Key': key,
        'Captcha-Code': code,
    },
    params: {
        tenantId,
        username,
        password: md5(password),
        grant_type: (website.captchaMode ? 'captcha' : 'password'),
        scope: 'all',
        type,
    },
})

export const refreshToken = (refresh_token, tenantId, deptId, roleId) => HcApi({
    url: '/api/blade-auth/oauth/token',
    method: 'post',
    headers: {
        'Tenant-Id': tenantId,
        'Dept-Id': (website.switchMode ? deptId : ''),
        'Role-Id': (website.switchMode ? roleId : ''),
    },
    params: {
        tenantId,
        refresh_token,
        grant_type: 'refresh_token',
        scope: 'all',
    },
})

export const logout = () => HcApi({
    url: '/api/blade-auth/oauth/logout',
    method: 'get',
})

export const registerGuest = (form, oauthId) => HcApi({
    url: '/api/blade-user/register-guest',
    method: 'post',
    params: {
        tenantId: form.tenantId,
        name: form.name,
        account: form.account,
        password: form.password,
        oauthId,
    },
})

export const getProjectAndContract = () => HcApi({
    url: '/api/blade-business/userViewProjectContract/queryUserViewProjectAndContract',
    method: 'get',
})

export const getCaptcha = () => HcApi({
    url: '/api/blade-auth/oauth/captcha',
    method: 'get',
})

export const getUserInfo = () => HcApi({
    url: '/api/blade-auth/oauth/user-info',
    method: 'get',
})

export const sendLogs = (list) => HcApi({
    url: '/api/blade-auth/oauth/logout',
    method: 'post',
    data: list,
})

export const clearCache = () => HcApi({
    url: '/api/blade-auth/oauth/clear-cache',
    method: 'get',
})

export const loginByToken = (form) => HcApi({
    url: '/api/blade-user/loginByToken',
    method: 'post',
    params: form,
})

//获取租户ID
export const getTenantID = (domain) => HcApi({
    url: '/api/blade-system/tenant/info',
    method: 'get',
    params: {
        domain,
    },
})
