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

const PLATFORM_PREFIX = '/newApi/platform-admin-salesperson';
const PLATFORM_CONFIG_PREFIX = '/newApi/platform-admin-salesperson-store-config';
const CLINIC_PREFIX = '/newApi/clinic-admin-salesperson';
const CLINIC_CONFIG_PREFIX = '/newApi/clinic-admin-salesperson-store-config';

function createRequest(prefix, headerKey) {
	return 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 };
		});
	};
}

const platformRequest = createRequest(PLATFORM_PREFIX, '_platformAdmin');
const clinicRequest = createRequest(CLINIC_PREFIX, '_clinicAdmin');
const platformConfigRequest = createRequest(PLATFORM_CONFIG_PREFIX, '_platformAdmin');
const clinicConfigRequest = createRequest(CLINIC_CONFIG_PREFIX, '_clinicAdmin');

export function createSalespersonManageApi(mode) {
	const isPlatform = mode === 'platform';
	const request = isPlatform ? platformRequest : clinicRequest;
	const configRequest = isPlatform ? platformConfigRequest : clinicConfigRequest;
	const prefix = isPlatform ? PLATFORM_PREFIX : CLINIC_PREFIX;

	return {
		mode,
		prefix,

		getList(params) {
			const url = isPlatform ? `${prefix}/platform-list` : `${prefix}/list`;
			return request({ url, method: 'GET', data: params });
		},

		create(data) {
			const url = isPlatform ? `${prefix}/platform-create` : `${prefix}/create`;
			return request({ url, method: 'POST', data });
		},

		update(data) {
			return request({ url: `${prefix}/update`, method: 'POST', data });
		},

		getPlatformStoreOptions(keyword) {
			return request({
				url: `${prefix}/platform-store-options`,
				method: 'GET',
				data: { keyword: keyword || '' },
			});
		},

		openAdminAccount(data) {
			return request({ url: `${prefix}/open-admin-account`, method: 'POST', data });
		},

		getDrugCommissionList(params) {
			return request({ url: `${prefix}/drug-commission-list`, method: 'GET', data: params });
		},

		saveDrugCommission(data) {
			return request({ url: `${prefix}/save-drug-commission`, method: 'POST', data });
		},

		getDrugCommissionCopyFrom(fromSalespersonId) {
			return request({
				url: `${prefix}/drug-commission-copy-from`,
				method: 'GET',
				data: { from_salesperson_id: fromSalespersonId },
			});
		},

		getCommissionList(params) {
			return request({ url: `${prefix}/commission-list`, method: 'GET', data: params });
		},

		getCommissionOrderList(params) {
			return request({ url: `${prefix}/commission-order-list`, method: 'GET', data: params });
		},

		getSettlementPreview(params) {
			return request({ url: `${prefix}/settlement-preview`, method: 'GET', data: params });
		},

		confirmSettlement(data) {
			return request({ url: `${prefix}/settlement`, method: 'POST', data });
		},

		getSettlementList(params) {
			return request({ url: `${prefix}/settlement-list`, method: 'GET', data: params });
		},

		getSettlementDetail(params) {
			return request({ url: `${prefix}/settlement-detail`, method: 'GET', data: params });
		},

		getUserBindList(params) {
			return request({ url: `${prefix}/user-bind-list`, method: 'GET', data: params });
		},

		getSalespersonOptions(params) {
			return request({ url: `${prefix}/salesperson-options`, method: 'GET', data: params });
		},

		getInvitedSalespersonList(params) {
			return request({ url: `${prefix}/invited-salesperson-list`, method: 'GET', data: params });
		},

		toggleSeePrice(storeId) {
			const configPrefix = isPlatform ? PLATFORM_CONFIG_PREFIX : CLINIC_CONFIG_PREFIX;
			return configRequest({
				url: `${configPrefix}/toggle-see-price`,
				method: 'POST',
				data: { store_id: storeId },
			});
		},

		getSeePriceDetail(storeId) {
			const configPrefix = isPlatform ? PLATFORM_CONFIG_PREFIX : CLINIC_CONFIG_PREFIX;
			return configRequest({
				url: `${configPrefix}/detail`,
				method: 'GET',
				data: storeId ? { store_id: storeId } : {},
			});
		},
	};
}

export function getSalespersonManageApiByLoginMode() {
	const mode = uni.getStorageSync('loginMode');
	return createSalespersonManageApi(mode === 'platform_admin' ? 'platform' : 'clinic');
}
