/**
 * 诊所端列表筛选条件本地缓存（对齐 PC reconciliation formCache）
 */

import {
	defaultMonthStartDate,
	defaultTodayDate,
} from './reconciliationParams.js'

const KEY_PREFIX = () => uni.getStorageSync('loginMode') || 'clinic_admin'
const KEY_ORDER = () => `${KEY_PREFIX()}.order.filter`
const KEY_RECONCILIATION = () => `${KEY_PREFIX()}.reconciliation.filter`

function readJson(key) {
	try {
		const raw = uni.getStorageSync(key)
		if (!raw) return null
		return typeof raw === 'string' ? JSON.parse(raw) : raw
	} catch (e) {
		return null
	}
}

function writeJson(key, data) {
	uni.setStorageSync(key, JSON.stringify(data))
}

export function defaultOrderFilter() {
	return {
		orderNo: '',
		statusIndex: 0,
		deliveryIndex: 0,
		prescriptionTypeIndex: 0,
		dateStart: defaultMonthStartDate(),
		dateEnd: defaultTodayDate(),
	}
}

export function loadOrderFilter() {
	const cached = readJson(KEY_ORDER())
	if (!cached || typeof cached !== 'object') return defaultOrderFilter()
	const d = defaultOrderFilter()
	return {
		orderNo: cached.orderNo != null ? String(cached.orderNo) : d.orderNo,
		statusIndex: Number(cached.statusIndex) || 0,
		deliveryIndex: Number(cached.deliveryIndex) || 0,
		prescriptionTypeIndex: Number(cached.prescriptionTypeIndex) || 0,
		dateStart: cached.dateStart || d.dateStart,
		dateEnd: cached.dateEnd || d.dateEnd,
	}
}

export function saveOrderFilter(data) {
	writeJson(KEY_ORDER(), {
		orderNo: data.orderNo || '',
		statusIndex: data.statusIndex,
		deliveryIndex: data.deliveryIndex,
		prescriptionTypeIndex: data.prescriptionTypeIndex,
		dateStart: data.dateStart,
		dateEnd: data.dateEnd,
	})
}

export function clearOrderFilter() {
	try {
		uni.removeStorageSync(KEY_ORDER())
	} catch (e) { /* ignore */ }
}

export function defaultReconciliationFilter() {
	return {
		tabIndex: 0,
		dateStart: defaultMonthStartDate(),
		dateEnd: defaultTodayDate(),
		byOrder: false,
		drugSearchQ: '',
		orderSearchQ: '',
		drugId: null,
		orderId: null,
		selectedDrugLabel: '',
		selectedOrderLabel: '',
	}
}

export function loadReconciliationFilter() {
	const cached = readJson(KEY_RECONCILIATION())
	if (!cached || typeof cached !== 'object') return defaultReconciliationFilter()
	const d = defaultReconciliationFilter()
	return {
		tabIndex: cached.tabIndex === 1 ? 1 : 0,
		dateStart: cached.dateStart || d.dateStart,
		dateEnd: cached.dateEnd || d.dateEnd,
		byOrder: !!cached.byOrder,
		drugSearchQ: cached.drugSearchQ != null ? String(cached.drugSearchQ) : '',
		orderSearchQ: cached.orderSearchQ != null ? String(cached.orderSearchQ) : '',
		drugId: cached.drugId != null && cached.drugId !== '' ? Number(cached.drugId) : null,
		orderId: cached.orderId != null && cached.orderId !== '' ? Number(cached.orderId) : null,
		selectedDrugLabel: cached.selectedDrugLabel || '',
		selectedOrderLabel: cached.selectedOrderLabel || '',
	}
}

export function saveReconciliationFilter(data) {
	writeJson(KEY_RECONCILIATION(), {
		tabIndex: data.tabIndex,
		dateStart: data.dateStart,
		dateEnd: data.dateEnd,
		byOrder: data.byOrder,
		drugSearchQ: data.drugSearchQ || '',
		orderSearchQ: data.orderSearchQ || '',
		drugId: data.drugId,
		orderId: data.orderId,
		selectedDrugLabel: data.selectedDrugLabel || '',
		selectedOrderLabel: data.selectedOrderLabel || '',
	})
}

export function clearReconciliationFilter() {
	try {
		uni.removeStorageSync(KEY_RECONCILIATION())
	} catch (e) { /* ignore */ }
}
