/**
 * 门店管理 API：按 loginMode 切换平台超管 / 业务员前缀
 * 业务员与超管复用同一套门店列表/详情页
 */
import { req } from '@/common/js/index.js';
import { unwrapClinicRes } from '@/api/clinicAdmin.js';

function getStoreContext() {
	const mode = uni.getStorageSync('loginMode');
	if (mode === 'salesperson') {
		return {
			prefix: '/newApi/salesperson-store',
			configPrefix: '',
			headerKey: '_salesperson',
			isSalesperson: true,
		};
	}
	return {
		prefix: '/newApi/platform-admin-store',
		configPrefix: '/newApi/platform-admin-salesperson-store-config',
		headerKey: '_platformAdmin',
		isSalesperson: false,
	};
}

function storeRequest(options) {
	const ctx = getStoreContext();
	return req.request({
		...options,
		header: {
			...(options.header || {}),
			[ctx.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 };
	});
}

/** 业务员是否允许编辑门店（系统配置开关） */
export function getSalespersonStoreEditPermission() {
	return storeRequest({
		url: '/newApi/salesperson-store/edit-permission',
		method: 'GET',
	});
}

export function getStoreList(params) {
	const { prefix } = getStoreContext();
	return storeRequest({ url: `${prefix}/list`, method: 'GET', data: params });
}

export function getStoreDetail(id) {
	const { prefix } = getStoreContext();
	return storeRequest({ url: `${prefix}/detail`, method: 'GET', data: { id } });
}

export function updateStore(data) {
	const { prefix } = getStoreContext();
	return storeRequest({ url: `${prefix}/update`, method: 'POST', data });
}

export function openStorePcWindows(id) {
	const { prefix } = getStoreContext();
	return storeRequest({ url: `${prefix}/open-pc-windows`, method: 'POST', data: { id } });
}

export function openStoreQrCode(id) {
	const { prefix } = getStoreContext();
	return storeRequest({ url: `${prefix}/open-qr-code`, method: 'POST', data: { id } });
}

export function updateStoreShippingFree(id) {
	const { prefix } = getStoreContext();
	return storeRequest({ url: `${prefix}/update-shipping-free`, method: 'POST', data: { id } });
}

export function updateStoreSubscribeStatus(id) {
	const { prefix } = getStoreContext();
	return storeRequest({ url: `${prefix}/update-subscribe-status`, method: 'POST', data: { id } });
}

export function updateStoreSeeRate(id) {
	const { prefix } = getStoreContext();
	return storeRequest({ url: `${prefix}/update-see-rate`, method: 'POST', data: { id } });
}

export function updateStoreAllowInsurance(id) {
	const { prefix } = getStoreContext();
	return storeRequest({ url: `${prefix}/update-allow-insurance-category`, method: 'POST', data: { id } });
}

export function updateStoreClinicType(id, clinicType) {
	const { prefix } = getStoreContext();
	return storeRequest({
		url: `${prefix}/update-clinic-type`,
		method: 'POST',
		data: { id, clinic_type: clinicType },
	});
}

export function toggleSalespersonSeePrice(storeId) {
	const { configPrefix, isSalesperson } = getStoreContext();
	if (isSalesperson || !configPrefix) {
		return Promise.resolve({ ok: false, data: null, res: null });
	}
	return storeRequest({
		url: `${configPrefix}/toggle-see-price`,
		method: 'POST',
		data: { store_id: storeId },
	});
}

export function getStoreDrugsByType(storeId, type) {
	const { prefix } = getStoreContext();
	return storeRequest({
		url: `${prefix}/get-store-drugs-by-type`,
		method: 'GET',
		data: { store_id: storeId, type },
	});
}

export function updateStoreDrugPrices(data) {
	const { prefix } = getStoreContext();
	return storeRequest({ url: `${prefix}/update-store-drug-prices`, method: 'POST', data });
}

export function getDoctorOption(storeId) {
	const { prefix } = getStoreContext();
	return storeRequest({
		url: `${prefix}/doctor-option`,
		method: 'GET',
		data: storeId ? { store_id: storeId } : {},
	});
}

export function getInternetMedicalStoreOption() {
	const { prefix } = getStoreContext();
	return storeRequest({ url: `${prefix}/internet-medical-store-option`, method: 'GET' });
}

export function bindOnlineConsultation(data) {
	const { prefix } = getStoreContext();
	return storeRequest({ url: `${prefix}/bind-online-consultation`, method: 'POST', data });
}

export function isSalespersonStoreMode() {
	return uni.getStorageSync('loginMode') === 'salesperson';
}

export { getStoreContext };
