import request from '@/utils/requestAI'
/**
 * 公共工具函数：获取并校验登录凭证（guidInfo 和 AIToken）
 * @returns {Object} { loginMark, token } - 校验后的凭证
 * @throws {Error} 缺少凭证或格式错误时抛出异常
 */
const getAuthCredentials = () => {
    const guidInfoStr = window.localStorage.getItem('guidInfo');
    const AIToken = window.localStorage.getItem('AIToken');
    // 校验凭证是否存在
    if (!guidInfoStr) throw new Error('未获取到 guidInfo，请先登录');
    // 解析并校验 guidInfo 格式
    let guidInfo;
    try {
        guidInfo = JSON.parse(guidInfoStr);
    } catch (error) {
        throw new Error('guidInfo 格式错误，请重新登录');
    }
    // 校验 guid 是否存在
    if (!guidInfo && !guidInfo.guid) throw new Error('guidInfo 中缺少 guid 参数，请重新登录');
    // 返回校验后的凭证
    return {
        loginMark: guidInfo.guid,
        token: AIToken
    };
};

// 企微认证
export function wecomAuth(query) {
    const { loginMark } = getAuthCredentials();
    query.append('loginMark', loginMark);
    // 遍历打印
    query.forEach((value, key) => {
        console.log(`${key}=${value}`);
    });
    return request({
        url: '/aidesign/wecom/auth',
        method: 'post',
        data: query
    })
}

// 2．随身邦用户认证
export function doWxidLogin(query){
    const { loginMark } = getAuthCredentials();
    query.append('loginMark', loginMark);
    // 遍历打印
    query.forEach((value, key) => {
        console.log(`${key}=${value}`);
    });
    return request({
        url: '/aidesign/wecom/ssb',
        method: 'post',
        data: query
    })
}


// 提交设计
export function CreateDesign(query) {
    const auth = getAuthCredentials();
    query.append('loginMark', auth.loginMark);
    query.append('token', auth.token);
    const userWecomType = sessionStorage.getItem("userWecomType") ? Number(sessionStorage.getItem("userWecomType")) : 5;
    query.append('wecomType', userWecomType);
    return request({
        url: '/aidesign/outside/CreateDesign',
        method: 'post',
        data: query
    })
}
// 查询列表
export function GetDesignList(query) {
    const auth = getAuthCredentials();
    query.append('loginMark', auth.loginMark);
    query.append('token', auth.token);
    return request({
        url: '/aidesign/outside/GetDesignList',
        method: 'post',
        data: query
    })
}
//删除实体
export function DeleteEntity(query) {
    const auth = getAuthCredentials();
    query.append('loginMark', auth.loginMark);
    query.append('token', auth.token);
    return request({
        url: '/aidesign/outside/DeleteEntity',
        method: 'post',
        data: query
    })
}
// 获取实体
export function GetEntity(query) {
    const auth = getAuthCredentials();
    query.append('loginMark', auth.loginMark);
    query.append('token', auth.token);
    return request({
        url: '/aidesign/outside/GetEntity',
        method: 'post',
        data: query
    })
}
// 获取未读状态
export function GetReadState(query) {
    const auth = getAuthCredentials();
    query.append('loginMark', auth.loginMark);
    query.append('token', auth.token);
    return request({
        url: '/aidesign/outside/GetReadState',
        method: 'post',
        data: query
    })
}
// 设置生图历史已读状态

export function UpdateReadState(query) {
    const auth = getAuthCredentials();
    query.append('loginMark', auth.loginMark);
    query.append('token', auth.token);
    return request({
        url: '/aidesign/outside/UpdateReadState',
        method: 'post',
        data: query
    })
}

//获取关联项目列表
export function GetProjectlist(query) {
    const auth = getAuthCredentials();
    query.append('loginMark', auth.loginMark);
    query.append('token', auth.token);
    return request({
        url: '/aidesign/outside/GetProjectlist',
        method: 'post',
        data: query
    })
}

//获取风格选项列表
export function GetDictList(query) {
    const auth = getAuthCredentials();
    query.append('loginMark', auth.loginMark);
    query.append('token', auth.token);
    const userWecomType = sessionStorage.getItem("userWecomType") ? Number(sessionStorage.getItem("userWecomType")) : 5;
    query.append('wecomType', userWecomType);
    return request({
        url: '/aidesign/GetBaseInfo',
        method: 'post',
        data: query
    })
}

/**
 * 内墙部分
 */
// 内墙提交设计
export function insideCreateDesign(query) {
    const auth = getAuthCredentials();
    query.append('loginMark', auth.loginMark);
    query.append('token', auth.token);
    const userWecomType = sessionStorage.getItem("userWecomType") ? Number(sessionStorage.getItem("userWecomType")) : 5;
    query.append('wecomType', userWecomType);
    return request({
        url: '/aidesign/inside/CreateDesign',
        method: 'post',
        data: query
    })
}
// 内墙查询列表
export function insideGetDesignList(query) {
    const auth = getAuthCredentials();
    query.append('loginMark', auth.loginMark);
    query.append('token', auth.token);
    return request({
        url: '/aidesign/inside/GetDesignList',
        method: 'post',
        data: query
    })
}

//内墙删除实体
export function insideDeleteEntity(query) {
    const auth = getAuthCredentials();
    query.append('loginMark', auth.loginMark);
    query.append('token', auth.token);
    return request({
        url: '/aidesign/inside/DeleteEntity',
        method: 'post',
        data: query
    })
}
// 内墙获取实体
export function insideGetEntity(query) {
    const auth = getAuthCredentials();
    query.append('loginMark', auth.loginMark);
    query.append('token', auth.token);
    return request({
        url: '/aidesign/inside/GetEntity',
        method: 'post',
        data: query
    })
}

// 内墙获取未读状态
export function insideGetReadState(query) {
    const auth = getAuthCredentials();
    query.append('loginMark', auth.loginMark);
    query.append('token', auth.token);
    return request({
        url: '/aidesign/inside/GetReadState',
        method: 'post',
        data: query
    })
}

// 内墙设置生图历史已读状态
export function insideUpdateReadState(query) {
    const auth = getAuthCredentials();
    query.append('loginMark', auth.loginMark);
    query.append('token', auth.token);
    return request({
        url: '/aidesign/inside/UpdateReadState',
        method: 'post',
        data: query
    })
}



// 一键诊断-提交设计
export function diagCreateDesign(query) {
    const auth = getAuthCredentials();
    query.append('loginMark', auth.loginMark);
    query.append('token', auth.token);
    const userWecomType = sessionStorage.getItem("userWecomType") ? Number(sessionStorage.getItem("userWecomType")) : 5;
    query.append('wecomType', userWecomType);
    return request({
        url: '/aidesign/diag/CreateDesign',
        method: 'post',
        data: query
    })
}


// 一键诊断-查询列表
export function diagGetDesignList(query) {
    const auth = getAuthCredentials();
    query.append('loginMark', auth.loginMark);
    query.append('token', auth.token);
    return request({
        url: '/aidesign/diag/GetDesignList',
        method: 'post',
        data: query
    })
}
//一键诊断-删除实体
export function diagDeleteEntity(query) {
    const auth = getAuthCredentials();
    query.append('loginMark', auth.loginMark);
    query.append('token', auth.token);
    return request({
        url: '/aidesign/diag/DeleteEntity',
        method: 'post',
        data: query
    })
}
// 一键诊断-获取实体
export function diagGetEntity(query) {
    const auth = getAuthCredentials();
    query.append('loginMark', auth.loginMark);
    query.append('token', auth.token);
    return request({
        url: '/aidesign/diag/GetEntity',
        method: 'post',
        data: query
    })
}
// 一键诊断-获取未读状态
export function diagGetReadState(query) {
    const auth = getAuthCredentials();
    query.append('loginMark', auth.loginMark);
    query.append('token', auth.token);
    return request({
        url: '/aidesign/diag/GetReadState',
        method: 'post',
        data: query
    })
}
// 一键诊断-设置生图历史已读状态

export function diagUpdateReadState(query) {
    const auth = getAuthCredentials();
    query.append('loginMark', auth.loginMark);
    query.append('token', auth.token);
    return request({
        url: '/aidesign/diag/UpdateReadState',
        method: 'post',
        data: query
    })
}

// 获取ticket
export function wecomTicket(query) {
    const auth = getAuthCredentials();
    query.append('loginMark', auth.loginMark);
    query.append('token', auth.token);
    return request({
        url: '/aidesign/wecom/ticket',
        method: 'post',
        data: query
    })
}

// 埋点
export function AddTrackEvent(query) {
    const auth = getAuthCredentials();
    query.append('loginMark', auth.loginMark);
    query.append('token', auth.token);
    return request({
        url: '/aidesign/AddTrackEvent',
        method: 'post',
        data: query
    })
}

// 获取产品列表
export function GetProductInfo(query) {
    return request({
        url: '/aidesign/outside/GetProductInfo',
        method: 'post',
        data: query
    })
}

// 获取生成图片剩余积分
export function GetPoints(query) {
    return request({
        url: '/aidesign/outside/GetPoints',
        method: 'post',
        data:query
    })
}

//获取确认下单地址
export function GetOrderAddress(formData) {
    const auth = getAuthCredentials();
    formData.append('loginMark', auth.loginMark);
    formData.append('token', auth.token);
    const weChat = window.localStorage.getItem("weChat");
    formData.append('WXuserid', weChat);
    return request({
        url: '/aidesign/outside/GetOrderAddress',
        method: 'post',
        data: formData
    })
}
