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

export interface ParentBindChildInfo {
    account?: string;
    avatar?: string;
    id?: number;
    name?: string;
    nickname?: string;
    phone?: string;
    [key: string]: any;
}

export interface ParentBindChildStudent {
    account?: string;
    avatar?: string;
    gradeId?: number;
    gradeName?: string;
    id?: number;
    name?: string;
    nickname?: string;
    [key: string]: any;
}

export interface ParentBindChildItem {
    id?: number;
    bindFlag?: number;
    bindTime?: string;
    identityType?: number;
    child?: ParentBindChildStudent;
    parent?: ParentBindChildInfo;
    [key: string]: any;
}

export interface PageResult<T> {
    current?: number;
    rows?: T[];
    size?: number;
    totalPage?: number;
    totalRows?: number;
    [key: string]: any;
}

/** 家长绑定孩子列表（当前家长已绑定） */
export const getParentBindChildList = (params: {
    parentId: string | number;
    current?: number;
    size?: number;
}) =>
    request<PageResult<ParentBindChildItem>>({
        url: '/parentBindChild/list',
        method: 'GET',
        params: {
            parentId: params.parentId,
            current: params.current ?? 1,
            size: params.size ?? 100,
            bindFlag: 1,
        },
    });

/** 按孩子 ID 查询绑定家长（扫码绑定前校验） */
export const getBindParentsByChildId = (childId: string | number) =>
    request<PageResult<ParentBindChildItem>>({
        url: '/parentBindChild/list',
        method: 'GET',
        params: {
            childId,
            size: -1,
        },
    });

/** 按设备序列号查询绑定家长 */
export const getBindParentsByDevice = (simSerialNumber: string) =>
    request<PageResult<any>>({
        url: '/parentBindDevice/list',
        method: 'GET',
        params: {
            simSerialNumber,
            size: -1,
        },
    });

/** 手动输入孩子账号绑定 */
export const manualBindChild = (childAcc: string) =>
    request<number>({
        url: '/parentBind/familyTeacherBindAcc',
        method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
        },
        data: `childAcc=${encodeURIComponent(childAcc.trim())}`,
    });

/** 扫码后申请绑定孩子/设备 */
export const parentBindApply = (params: {
    childId?: string | number;
    simSerialNumber?: string;
    identityType: number;
}) => {
    const childDto = params.childId
        ? { childId: params.childId, identityType: params.identityType }
        : undefined;
    const deviceDto = params.simSerialNumber
        ? { simSerialNumber: params.simSerialNumber, identityType: params.identityType }
        : undefined;
    return request({
        url: '/parentBind/apply',
        method: 'POST',
        data: { childDto, deviceDto },
    });
};
