import {
	request
} from "./http.js";
import {
	get
} from "../cache/storage.js";
// 公共API 
const proName = 'plug'
const userId = get('userId')
const apiPath = {
	mineInvoiceOrderList: '/api/invoicerecharge/recharge-listpage/{userId}/{status}', //可以开具发票的订单
	mineInvoiceList: '/api/invoice-config/listpage/{userId}', //我的开票信息
	saveInvoiceInfo: '/api/invoice-config/save/{userId}', //保存开票信息
	updateInvoiceInfo: '/api/invoice-config/update/{invoiceId}', //修改开票信息
	deleteInvoiceInfo: '/api/invoice-config/remove/{ids}', //删除开票信息
	mineInvoiceRecordList: '/api/invoicerecharge/listpage/{userId}', //我的开票记录
	saveInvoiceRecord: '/api/invoicerecharge/save/{userId}', // 保存开票申请
	updateInvoiceRecord: '/api/invoicerecharge/update/{invoiceRechargeId}', //修改开票申请
	cancelInvoiceRecord: '/api/invoicerecharge/cancel/{invoiceRechargeId}', //取消开票申请
	dicByPId: '/api/data/listbyparentid/{pId}', //数据字典
	minePackageUseDetail: '/app/packageorderitem/listpagerelease/{id}', //我的套餐包使用详情
}

class InvoiceApi {
	// 通用路径参数替换方法
	static #replacePathParams(path, params) {
		return Object.entries(params).reduce(
			(acc, [key, value]) => acc.replace(`{${key}}`, value),
			path
		)
	}

	// 通用请求方法
	static requestHandler(endpoint, method, data = null, pathParams = {}) {
		const path = Object.keys(pathParams).length ?
			this.#replacePathParams(endpoint, pathParams) :
			endpoint
		return request(path, method, data, proName)
	}
	//获取可以开具发票的订单
	static doGetMineInvoiceOrderList(data, status) {
		return this.requestHandler(apiPath.mineInvoiceOrderList, "GET", data, {
			userId: userId,
			status: status
		})
	}
	// 获取字典列表
	static doGetDicListByPId(id) {
		const path = apiPath.dicByPId.replace('{pId}', id)
		return request(path, "GET")
	}

	// 我的开票信息
	static doGetMineInvoiceList(data) {
		return this.requestHandler(apiPath.mineInvoiceList, "GET", data, {
			userId: get('userId')
		});
	}

	// 保存我的开票信息
	static doSaveMineInvoiceInfo(data) {
		return this.requestHandler(apiPath.saveInvoiceInfo, "POST", data, {
			userId: get('userId')
		});
	}

	// 编辑开票信息
	static doUpdateMineInvoiceInfo(id, data) {
		return this.requestHandler(apiPath.updateInvoiceInfo, "PUT", data, {
			invoiceId: id
		});
	}

	// 删除开票信息
	static doDelMineInvoiceInfo(id) {
		return this.requestHandler(apiPath.deleteInvoiceInfo, "DELETE", null, {
			ids: id
		});
	}

	// 开票申请列表（已修改，保持原有结构）
	static doGetInvoiceRecordList(data) {
		return this.requestHandler(apiPath.mineInvoiceRecordList, "GET", data, {
			userId: get('userId')
		});
	}

	// 取消开票申请
	static doCancelInvoiceRecord(id) {
		return this.requestHandler(apiPath.cancelInvoiceRecord, "PUT", null, {
			invoiceRechargeId: id
		});
	}

	// 提交开票申请
	static doSaveInvoiceRecord(data) {
		return this.requestHandler(apiPath.saveInvoiceRecord, "POST", data, {
			userId: get('userId')
		});
	}

	// 修改开票申请
	static doUpdateInvoiceRecord(id, data) {
		return this.requestHandler(apiPath.updateInvoiceRecord, "PUT", data, {
			invoiceRechargeId: id
		});
	}
	//套餐包使用详情
	static doGetMinePackageUseDetail(id, data) {
		return this.requestHandler(apiPath.minePackageUseDetail, "GET", data, {
			id: id
		})
	}
}

export default InvoiceApi;