import { request } from '@/utils/request';

export interface AccountLoginParams {
    account: string;
    password: string;
    /** 老师登录传 'MANAGE'，学生登录默认 'PC' */
    clientType?: string;
    companyId?: string;
}

/** 账号密码登录 */
export const psdLogin = (data: AccountLoginParams) => {
    const payload: Record<string, any> = {
        clientType: 'PC',
        model: 'web',
        ...data,
    };
    // 学生端登录不传 simSerialNumber（接口要求）
    if (payload.clientType === 'MANAGE') {
        payload.simSerialNumber = 'web';
    }
    return request<string>({
        url: '/login',
        method: 'POST',
        headers: { Authorization: '' },
        data: payload,
    });
};

export interface ParentSmsLoginParams {
    phone: string;
    verifyCode: string;
    openid: string;
}

export interface ParentMpLoginResp {
    accessToken?: string | null;
    account?: string | null;
    phone?: string | null;
    wxUser?: {
        openid?: string;
        unionId?: string;
        nickname?: string | null;
        [key: string]: any;
    };
}

/** 家长端 - 小程序 code 登录（查 openId & 绑定关系） */
export const parentMpLogin = (code: string) =>
    request<ParentMpLoginResp>({
        url: '/mpLogin',
        method: 'POST',
        headers: { Authorization: '' },
        silent: true,
        data: { code },
    });

/** 家长端 - 发送登录验证码（form-urlencoded 请求体，非 URL 查询参数） */
export const sendParentLoginCode = (phone: string) =>
    request({
        url: '/sms/sendMessage',
        method: 'POST',
        headers: {
            Authorization: '',
            'Content-Type': 'application/x-www-form-urlencoded',
        },
        silent: true,
        data: `phoneNumbers=${encodeURIComponent(phone)}`,
    });

/** 家长端 - 手机号验证码登录（绑定微信 openid），成功时 data 为 JWT 字符串 */
export const parentSmsLogin = (data: ParentSmsLoginParams) =>
    request<string>({
        url: '/smsLogin',
        method: 'POST',
        headers: {
            Authorization: '',
            'X-Tenant-Id': '1966391196339204098',
        },
        data: {
            bindMaOpenid: true,
            adminType: 16,
            clientType: 'PARENT',
            phone: data.phone,
            verifyCode: data.verifyCode,
            wxUser: {
                openid: data.openid,
            },
        },
    });

/** 退出登录 */
export const logout = () =>
    request({
        url: '/logout',
        method: 'GET',
        silent: true,
    });

export interface SysUserInfo {
    id?: string | number;
    account?: string;
    phone?: string;
    nickName?: string;
    name?: string;
    avatar?: string;
    avatarUrl?: string;
    [key: string]: any;
}

/** 获取当前登录用户信息（家长端登录后使用） */
export const selectUser = () =>
    request<SysUserInfo>({
        url: '/sysUser/selectUser',
        method: 'GET',
    });

/** 获取学生用户首页信息 */
export const getUserInfo = () =>
    request<any>({
        url: '/personal/getHomePage',
        method: 'GET',
    });

/** 老师登录用户 */
export const getLoginUser = () =>
    request<any>({
        url: '/getLoginUser',
        method: 'GET',
    });
