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

const PREFIX = '/newApi/clinic-admin';

/** 解析 Laravel jok 或 Yii 响应 */
export function unwrapClinicRes(res) {
	if (!res) return null;
	// code 可能是 0 / "0"
	if (Number(res.code) === 0 && res.result !== undefined) return res.result;
	if (res.errcode != null && Number(res.errcode) !== -1) return res.data;
	if (res.result !== undefined) return res.result;
	return res.data != null ? res.data : res;
}

function clinicRequest(options) {
	return req.request({
		...options,
		header: {
			...(options.header || {}),
			_clinicAdmin: '1',
		},
	}).then((res) => {
		const data = unwrapClinicRes(res);
		const ok = !!(res && (
			Number(res.code) === 0
			|| (res.errcode != null && Number(res.errcode) !== -1)
		));
		return { res, data, ok };
	});
}

export function sendClinicAdminCode(phone) {
	return req.request({
		url: '/newApi/clinic-admin-auth/send-verification-code',
		method: 'POST',
		data: { account: phone },
		header: { isToken: false, _clinicAdmin: '1' },
	});
}

export function clinicAdminLogin(phone, password, code) {
	return req.request({
		url: '/newApi/clinic-admin-auth/login',
		method: 'POST',
		data: { account: phone, password, code },
		header: { isToken: false, _clinicAdmin: '1' },
	});
}

export function getClinicAdminMyInfo() {
	return clinicRequest({ url: `${PREFIX}-auth/my-info`, method: 'GET' });
}

export function getClinicAdminStoreQr() {
	return clinicRequest({ url: `${PREFIX}-auth/store-qr`, method: 'GET' });
}

export function getClinicAdminBalance() {
	return clinicRequest({ url: `${PREFIX}-account/my-balance`, method: 'GET' });
}

export function getClinicAdminCard() {
	return clinicRequest({ url: `${PREFIX}-account/my-card`, method: 'GET' });
}

export function saveClinicAdminCard(data) {
	return clinicRequest({ url: `${PREFIX}-account/save-card`, method: 'POST', data });
}

export const getMyBalance = getClinicAdminBalance;
export const getMyCard = getClinicAdminCard;
export const saveMyCard = saveClinicAdminCard;

export function getLedgerLogList(params) {
	return clinicRequest({ url: `${PREFIX}-settlement/ledger-log-list`, method: 'GET', data: params });
}

export function getLedgerDetail(params) {
	return clinicRequest({ url: `${PREFIX}-settlement/ledger-detail`, method: 'GET', data: params });
}

export function getAccountChangeLogList(params) {
	return clinicRequest({ url: `${PREFIX}-account-change-log/list`, method: 'GET', data: params });
}

export function getWithdrawalList(params) {
	return clinicRequest({ url: `${PREFIX}-withdrawal/list`, method: 'GET', data: params });
}

export function getWithdrawalDetail(id) {
	return clinicRequest({ url: `${PREFIX}-withdrawal/detail`, method: 'GET', data: { id } });
}

export function submitWithdrawal(data) {
	return clinicRequest({ url: `${PREFIX}-withdrawal/withdrawal`, method: 'POST', data });
}

export function getReconciliationList(params) {
	return clinicRequest({ url: `${PREFIX}-reconciliation/list`, method: 'GET', data: params });
}

export function getReconciliationDrugList(params) {
	return clinicRequest({ url: `${PREFIX}-reconciliation/drug-list`, method: 'GET', data: params });
}

export function getReconciliationDrugOptions(params) {
	return clinicRequest({ url: `${PREFIX}-reconciliation/drug-options`, method: 'GET', data: params });
}

export function getReconciliationOrderOptions(params) {
	return clinicRequest({ url: `${PREFIX}-reconciliation/order-options`, method: 'GET', data: params });
}

export function getOrderSaleAmount(params) {
	return clinicRequest({ url: `${PREFIX}-order/sale-amount`, method: 'GET', data: params });
}

export function getPrescriptionDetail(id) {
	return clinicRequest({ url: `${PREFIX}-prescription/detail`, method: 'GET', data: { id } });
}

export function getOrderList(params) {
	return clinicRequest({ url: `${PREFIX}-order/list`, method: 'GET', data: params });
}

export function getOrderStatusOption() {
	return clinicRequest({ url: `${PREFIX}-order/status-option`, method: 'GET' });
}

export function getOrderDetail(id) {
	return clinicRequest({ url: `${PREFIX}-order/detail`, method: 'GET', data: { id } });
}

export function getOrderTrace(params) {
	return clinicRequest({ url: `${PREFIX}-order/trace`, method: 'GET', data: params });
}

export function getOrderLedgerDetail(params) {
	return clinicRequest({ url: `${PREFIX}-order/ledger-detail`, method: 'GET', data: params });
}

export function getOrderReconciliationDetail(params) {
	return clinicRequest({ url: `${PREFIX}-order/reconciliation-detail`, method: 'GET', data: params });
}

export function refundOrder(data) {
	return clinicRequest({ url: `${PREFIX}-order/refund`, method: 'POST', data });
}

export function wareSendOrder(data) {
	return clinicRequest({ url: `${PREFIX}-order/ware-send`, method: 'POST', data });
}

export function getExpressDetailByOrderId(orderId) {
	return clinicRequest({
		url: `${PREFIX}-express-detail/detail-by-order`,
		method: 'POST',
		data: { order_id: orderId },
	});
}

export function getPriceAdjustConfig(storeId) {
	return clinicRequest({
		url: `${PREFIX}-order/price-adjust-config`,
		method: 'GET',
		data: storeId ? { store_id: storeId } : {},
	});
}


export function adjustOrderPercent(data) {
	return clinicRequest({ url: `${PREFIX}-order/adjust-order-percent`, method: 'POST', data });
}

export function getWarehouseList(params) {
	return clinicRequest({ url: `${PREFIX}-warehouse/list`, method: 'GET', data: params });
}

export function getWarehouseDetail(id) {
	return clinicRequest({ url: `${PREFIX}-warehouse/detail`, method: 'GET', data: { id } });
}

export function updateWarehouseStatus(id) {
	return clinicRequest({ url: `${PREFIX}-warehouse/update-status`, method: 'GET', data: { id } });
}

export function updateWarehousePrice(data) {
	return clinicRequest({ url: `${PREFIX}-warehouse/update`, method: 'POST', data });
}

export function getWarehouseProductTypeOptions() {
	return clinicRequest({ url: `${PREFIX}-warehouse/product-type-options`, method: 'GET' });
}
