import { req } from '@/common/js/index.js';
import { unwrapClinicRes } from '@/api/clinicAdmin.js';

/**
 * 按前缀生成业务 API（订单/对账/提现/仓库等）
 */
export function createBusinessApi(prefix, headerFlag) {
	const headerKey = headerFlag || '_businessAdmin';
	function bizRequest(options) {
		return req.request({
			...options,
			header: {
				...(options.header || {}),
				[headerKey]: '1',
			},
		}).then((res) => {
			const data = unwrapClinicRes(res);
			const ok = res && (res.code === 0 || (res.errcode != null && Number(res.errcode) !== -1));
			return { res, data, ok };
		});
	}

	return {
		prefix,
		getMyInfo() {
			return bizRequest({ url: `${prefix}-auth/my-info`, method: 'GET' });
		},
		getInputStats() {
			return bizRequest({ url: `${prefix}-auth/input-stats`, method: 'GET' });
		},
		getMyBalance() {
			return bizRequest({ url: `${prefix}-account/my-balance`, method: 'GET' });
		},
		getMyCard() {
			return bizRequest({ url: `${prefix}-account/my-card`, method: 'GET' });
		},
		saveMyCard(data) {
			return bizRequest({ url: `${prefix}-account/save-card`, method: 'POST', data });
		},
		getLedgerLogList(params) {
			return bizRequest({ url: `${prefix}-settlement/ledger-log-list`, method: 'GET', data: params });
		},
		getLedgerDetail(params) {
			return bizRequest({ url: `${prefix}-settlement/ledger-detail`, method: 'GET', data: params });
		},
		getAccountChangeLogList(params) {
			return bizRequest({ url: `${prefix}-account-change-log/list`, method: 'GET', data: params });
		},
		getWithdrawalList(params) {
			return bizRequest({ url: `${prefix}-withdrawal/list`, method: 'GET', data: params });
		},
		getWithdrawalAuditList(params) {
			return bizRequest({ url: `${prefix}-withdrawal/audit-list`, method: 'GET', data: params });
		},
		getWithdrawalDetail(id) {
			return bizRequest({ url: `${prefix}-withdrawal/detail`, method: 'GET', data: { id } });
		},
		submitWithdrawal(data) {
			return bizRequest({ url: `${prefix}-withdrawal/withdrawal`, method: 'POST', data });
		},
		passWithdrawal(data) {
			return bizRequest({ url: `${prefix}-withdrawal/pass-application`, method: 'POST', data });
		},
		refuseWithdrawal(data) {
			return bizRequest({ url: `${prefix}-withdrawal/refuse-application`, method: 'POST', data });
		},
		getReconciliationList(params) {
			return bizRequest({ url: `${prefix}-reconciliation/list`, method: 'GET', data: params });
		},
		getReconciliationDrugList(params) {
			return bizRequest({ url: `${prefix}-reconciliation/drug-list`, method: 'GET', data: params });
		},
		getReconciliationDrugOptions(params) {
			return bizRequest({ url: `${prefix}-reconciliation/drug-options`, method: 'GET', data: params });
		},
		getReconciliationOrderOptions(params) {
			return bizRequest({ url: `${prefix}-reconciliation/order-options`, method: 'GET', data: params });
		},
		getOrderSaleAmount(params) {
			return bizRequest({ url: `${prefix}-order/sale-amount`, method: 'GET', data: params });
		},
		getPrescriptionDetail(id) {
			return bizRequest({ url: `${prefix}-prescription/detail`, method: 'GET', data: { id } });
		},
		getOrderList(params) {
			return bizRequest({ url: `${prefix}-order/list`, method: 'GET', data: params });
		},
		getOrderStatusOption() {
			return bizRequest({ url: `${prefix}-order/status-option`, method: 'GET' });
		},
		getOrderDetail(id) {
			return bizRequest({ url: `${prefix}-order/detail`, method: 'GET', data: { id } });
		},
		getOrderTrace(params) {
			return bizRequest({ url: `${prefix}-order/trace`, method: 'GET', data: params });
		},
		getOrderLedgerDetail(params) {
			return bizRequest({ url: `${prefix}-order/ledger-detail`, method: 'GET', data: params });
		},
		getOrderReconciliationDetail(params) {
			return bizRequest({ url: `${prefix}-order/reconciliation-detail`, method: 'GET', data: params });
		},
		refundOrder(data) {
			return bizRequest({ url: `${prefix}-order/refund`, method: 'POST', data });
		},
		wareSendOrder(data) {
			return bizRequest({ url: `${prefix}-order/ware-send`, method: 'POST', data });
		},
		getExpressDetailByOrderId(orderId) {
			return bizRequest({
				url: `${prefix}-express-detail/detail-by-order`,
				method: 'POST',
				data: { order_id: orderId },
			});
		},
		getExpressCompaniesOption() {
			return bizRequest({ url: `${prefix}-express-companies/option`, method: 'GET' });
		},
		getChinaErpSyncLogs(params) {
			return bizRequest({ url: `${prefix}-order/china-erp-sync-logs`, method: 'GET', data: params });
		},
		syncChinaOrderToErp(data) {
			return bizRequest({ url: `${prefix}-order/sync-china-erp`, method: 'POST', data });
		},
		getPriceAdjustConfig(storeId) {
			return bizRequest({ url: `${prefix}-order/price-adjust-config`, method: 'GET', data: storeId ? { store_id: storeId } : {} });
		},
		adjustOrderPercent(data) {
			return bizRequest({ url: `${prefix}-order/adjust-order-percent`, method: 'POST', data });
		},
		getWarehouseList(params) {
			return bizRequest({ url: `${prefix}-warehouse/list`, method: 'GET', data: params });
		},
		getWarehouseDetail(id) {
			return bizRequest({ url: `${prefix}-warehouse/detail`, method: 'GET', data: { id } });
		},
		updateWarehouseStatus(id) {
			return bizRequest({ url: `${prefix}-warehouse/update-status`, method: 'GET', data: { id } });
		},
		updateWarehousePrice(data) {
			return bizRequest({ url: `${prefix}-warehouse/update`, method: 'POST', data });
		},
		getWarehouseProductTypeOptions() {
			return bizRequest({ url: `${prefix}-warehouse/product-type-options`, method: 'GET' });
		},
		getStoreOption() {
			return bizRequest({ url: `${prefix}-store/option`, method: 'GET' });
		},
		getStoreInputList(params) {
			return bizRequest({ url: `${prefix}-store-input/list`, method: 'GET', data: params });
		},
		getStoreInputDetail(id) {
			return bizRequest({ url: `${prefix}-store-input/detail`, method: 'GET', data: { id } });
		},
		getPromoterOptions() {
			return bizRequest({ url: `${prefix}-store-input/promoter-options`, method: 'GET' });
		},
		getPromoterDetail(params) {
			return bizRequest({ url: `${prefix}-store-input/promoter-detail`, method: 'GET', data: params });
		},
		getStoreInputOption() {
			return bizRequest({ url: `${prefix}-store-input/option`, method: 'GET' });
		},
		createStoreInput(data) {
			return bizRequest({ url: `${prefix}-store-input/create`, method: 'POST', data });
		},
		updateStoreInput(data) {
			return bizRequest({ url: `${prefix}-store-input/update`, method: 'POST', data });
		},
		auditStoreInput(data) {
			return bizRequest({ url: `${prefix}-store-input/audit`, method: 'POST', data });
		},
		getDoctorInputList(params) {
			return bizRequest({ url: `${prefix}-doctor-input/list`, method: 'GET', data: params });
		},
		getDoctorInputDetail(id) {
			return bizRequest({ url: `${prefix}-doctor-input/detail`, method: 'GET', data: { id } });
		},
		createDoctorInput(data) {
			return bizRequest({ url: `${prefix}-doctor-input/create`, method: 'POST', data });
		},
		updateDoctorInput(data) {
			return bizRequest({ url: `${prefix}-doctor-input/update`, method: 'POST', data });
		},
		auditDoctorInput(data) {
			return bizRequest({ url: `${prefix}-doctor-input/audit`, method: 'POST', data });
		},
		getDoctorTitleOption() {
			return bizRequest({
				url: `${prefix}-pharmacist/title-option`,
				method: 'GET',
				data: { type: 1 },
			});
		},
		getDepartOption() {
			return bizRequest({ url: `${prefix}-doctor/depart-option`, method: 'GET' });
		},
	};
}

export const platformAdminApi = createBusinessApi('/newApi/platform-admin', '_platformAdmin');
export const salespersonApi = createBusinessApi('/newApi/salesperson', '_salesperson');

export function getActiveBusinessApi() {
	const mode = uni.getStorageSync('loginMode');
	if (mode === 'platform_admin') return platformAdminApi;
	if (mode === 'salesperson') return salespersonApi;
	return null;
}
