import request from "@/api/request.js";

// 涉华法案领域分布
/**
 * @param {year} 
 */
export function getBillIndustry(params) {
    return request({
        method: 'GET',
        url: `/api/BillOverview/billIndustry/${params.year}`,
        params,
    })
}

// 涉华法案统计
export function getBillCount(params) {
    return request({
        method: 'GET',
        url: `/api/BillOverview/billCount`,
        params
    })
}

// 获取关键条款
export function getBillOverviewKeyTK() {
    return request({
        method: 'GET',
        url: `/api/BillOverview/keyTk`,

    })
}

// 获取热门法案列表
export function getHotBills() {
    return request({
        method: 'GET',
        url: '/api/BillOverview/hotBills',
    })
}

// 获取法案风险信号
/**
 * @param {moduleId} 
 */
export function getBillRiskSignal(params) {
    return request({
        method: 'GET',
        // 这个是之前的接口地址，现在接口地址换成新的了所以使用新地址
        // url: '/api/BillOverview/riskSignal',
        url: `/api/commonFeature/riskSignal/${params.moduleId}`,
    })
}

// 获取新闻资讯
/**
 * @param {moduleId} 
 */
export function getNews(params) {
    return request({
        method: 'GET',
        url: `/api/commonFeature/news/${params.moduleId}`,
    })
}

// 获取社交媒体
/**
 * @param {moduleId} 
 */
export function getRemarks(params) {
    return request({
        method: 'GET',
        url: `/api/commonFeature/remarks/${params.moduleId}`,
    })
}

// 根据法案类型获取法案列表
/**
 * @param {type} 
 */
export function getBillsByType(params) {
    return request({
        method: 'GET',
        url: '/api/BillOverview/bills',
        params,
    })
}

// 根据行业领域字典列表
export function getHylyList() {
    return request({
        method: 'GET',
        url: `/api/billImpactAnalysis/industry/hylyList`,
    })
}

// 获取法案提出部门
/**
 * @param {year} 
 */
export function getBillPostOrg(params) {
    return request({
        method: 'GET',
        url: `/api/BillOverview/billPostOrg/${params.year}`,
    })
}

// 获取关键议员提案
/**
 * @param {year} 
 */
export function getMemberProposal(params) {
    return request({
        method: 'GET',
        url: `/api/BillOverview/memberProposal/${params.year}`,
    })
}

// 获取资源库
export function getBills(params, signal) {
    return request({
        method: 'GET',
        url: `/api/BillOverview/bills`,
        params,
        signal
    })
}

// 获取提出部门列表
export function getPostOrgList() {
    return request({
        method: 'GET',
        url: `/api/BillDict/department`,
    })
}

// 获取提出议员列表
export function getPostMemberList() {
    return request({
        method: 'GET',
        url: `/api/BillDict/member`,
    })
}




/**
 * 获取筛选项配置 - 行业列表
 * GET /api/billImpactAnalysis/industry/hylyList
 */
export async function getIndustryKeyList() {
    return request('/api/billImpactAnalysis/industry/hylyList', {
        method: 'GET',
    })
}

/**
 * 获取进度阶段配置
 * GET /api/commonDict/bill/stage
 */
export async function getBillStageConfig() {
    return request('/api/commonDict/bill/stage', {
        method: 'GET',
    })
}

/**
 * 获取法案列表
 * GET /api/personHomepage/historyBill/{personId}
 * @param {string} personId - 人物ID
 * @param {Object} params - 查询参数（不包含分页参数）
 */
export async function getHistoryBillList(personId, params = {}) {
    const queryString = Object.entries(params)
        .filter(([, value]) => value !== undefined && value !== null && value !== '')
        .map(([key, value]) => {
            if (Array.isArray(value)) {
                return value.map(v => `${encodeURIComponent(key)}=${encodeURIComponent(v)}`).join('&')
            }
            return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`
        })
        .join('&')

    const url = queryString
        ? `/api/personHomepage/historyBill/${personId}?${queryString}`
        : `/api/personHomepage/historyBill/${personId}`

    return request(url, {
        method: 'GET',
    })
}

/**
 * 获取法案列表（含阶段字典）
 * @param {string} personId - 人物ID
 * @param {Object} params - 查询参数
 */
export async function getHistoryBillListWithStage(personId, params = {}) {
    const stageRes = await getBillStageConfig()
    const billRes = await getHistoryBillList(personId, params)

    return {
        stageConfig: stageRes,
        billList: billRes,
    }
}

// /**
//  * 获取潜在提案举措分析
//  * GET /api/personHomepage/historyBill/clause/{personId}
//  * @param {string} personId - 人物ID
//  * @param {Object} params - 查询参数
//  */
// export async function getPotentialClauseAnalysis(personId, params = {}) {
//     const queryString = Object.entries(params)
//         .filter(([, value]) => value !== undefined && value !== null && value !== '')
//         .map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
//         .join('&')

//     const url = queryString
//         ? `/api/personHomepage/historyBill/clause/${personId}?${queryString}`
//         : `/api/personHomepage/historyBill/clause/${personId}`

//     return request(url, {
//         method: 'GET',
//     })
// }

/**
 * 获取排序选项
 */
export function getSortOptions() {
    return Promise.resolve({
        code: 200,
        data: [
            { value: 'latestMotionTimeDesc', label: '最新修议时间倒序' },
            { value: 'latestMotionTimeAsc', label: '最新修议时间正序' },
        ],
    })
}

export async function getPotentialNewsList(personId, params = {}) {
    const queryString = Object.entries(params)
        .filter(([, value]) => value !== undefined && value !== null && value !== '')
        .map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
        .join('&')

    const url = queryString
        ? `/api/personHomepage/historyBill/clause/${personId}?${queryString}`
        : `/api/personHomepage/historyBill/clause/${personId}`

    return request(url, {
        method: 'GET',
    })
}

export async function getPotentialNewsKeywords(personId) {
    return request(`/api/personHomepage/historyBill/clauseKeyword/${personId}`, {
        method: 'GET',
    })
}

 