/** 提现模块假数据与工具，接口就绪后替换 */

export const SERVICE_FEE = 1.0

export const MOCK_BANK_CARDS = [
	{
		bankId: '1',
		bankName: '招商银行',
		bankCardNum: '6225882251001234',
		openingBank: '招商银行太原分行',
	},
	{
		bankId: '2',
		bankName: '交通银行',
		bankCardNum: '6222608800123456',
		openingBank: '交通银行太原支行',
	},
]

/** status: pending | success | failed */
export const MOCK_WITHDRAW_LIST = [
	{
		id: 'w1',
		bankName: '招商银行',
		bankTail: '2251',
		accountName: '**玉',
		amount: 1256.57,
		createTime: '2025-12-14 12:02:45',
		arriveTime: '',
		status: 'pending',
	},
	{
		id: 'w2',
		bankName: '招商银行',
		bankTail: '2251',
		accountName: '**玉',
		amount: 25.62,
		createTime: '2025-12-13 09:15:30',
		arriveTime: '',
		status: 'pending',
	},
	{
		id: 'w3',
		bankName: '交通银行',
		bankTail: '8890',
		accountName: '**明',
		amount: 962.25,
		createTime: '2025-12-10 18:30:00',
		arriveTime: '2025-12-11 10:22:00',
		status: 'success',
	},
	{
		id: 'w4',
		bankName: '招商银行',
		bankTail: '2251',
		accountName: '**玉',
		amount: 500.0,
		createTime: '2025-11-28 16:45:33',
		arriveTime: '',
		status: 'failed',
		rejectReason: '银行卡信息异常，请核对后重试',
	},
	{
		id: 'w5',
		bankName: '交通银行',
		bankTail: '8890',
		accountName: '**明',
		amount: 368.88,
		createTime: '2025-11-25 11:20:18',
		arriveTime: '2025-11-26 08:00:00',
		status: 'success',
	},
	{
		id: 'w6',
		bankName: '招商银行',
		bankTail: '2251',
		accountName: '**玉',
		amount: 100.0,
		createTime: '2025-11-20 14:00:00',
		arriveTime: '',
		status: 'pending',
	},
]

export function getWithdrawById(id) {
	return MOCK_WITHDRAW_LIST.find(item => item.id === id) || null
}

export function formatAmount(amount) {
	const num = parseFloat(amount) || 0
	const fixed = num.toFixed(2)
	const [intPart, decPart] = fixed.split('.')
	const withComma = intPart.replace(/\B(?=(\d{3})+(?!\d))/g, ',')
	return decPart ? `${withComma}.${decPart}` : withComma
}

export function formatDateYmd(date) {
	const d = date instanceof Date ? date : new Date(date)
	const y = d.getFullYear()
	const m = String(d.getMonth() + 1).padStart(2, '0')
	const day = String(d.getDate()).padStart(2, '0')
	return `${y}.${m}.${day}`
}

export function formatDateTime(timeStr) {
	if (!timeStr) return '--'
	return timeStr.slice(0, 19)
}

export function getMonthRange(year, month) {
	const start = new Date(year, month - 1, 1)
	const end = new Date(year, month, 0, 23, 59, 59)
	return {
		start: formatDateYmd(start),
		end: formatDateYmd(end),
		startTs: start.getTime(),
		endTs: end.getTime(),
	}
}

/** 演示默认：2025年11月 */
export function getMockMonthRange() {
	return getMonthRange(2025, 11)
}

export function parseTimeStr(str) {
	return new Date(String(str).replace(/-/g, '/')).getTime()
}

const WEEK_LABELS = ['日', '一', '二', '三', '四', '五', '六']

function pad(n) {
	return String(n).padStart(2, '0')
}

function getWeekKey(date) {
	const d = new Date(date.getFullYear(), date.getMonth(), date.getDate())
	d.setDate(d.getDate() + 4 - (d.getDay() || 7))
	const yearStart = new Date(d.getFullYear(), 0, 1)
	return `${d.getFullYear()}-${Math.ceil(((d - yearStart) / 86400000 + 1) / 7)}`
}

/**
 * 列表时间显示规则（与原型流程图一致）
 */
export function formatListTime(timeStr) {
	if (!timeStr) return ''
	const date = new Date(String(timeStr).replace(/-/g, '/'))
	const now = new Date()
	const hhmm = `${pad(date.getHours())}:${pad(date.getMinutes())}`

	if (date.getFullYear() !== now.getFullYear()) {
		return `${date.getFullYear()}.${pad(date.getMonth() + 1)}.${pad(date.getDate())}`
	}

	const todayStart = new Date(now.getFullYear(), now.getMonth(), now.getDate()).getTime()
	const eventStart = new Date(date.getFullYear(), date.getMonth(), date.getDate()).getTime()
	const dayDiff = Math.floor((todayStart - eventStart) / 86400000)

	if (dayDiff === 0) return hhmm
	if (dayDiff === 1) return `昨天 ${hhmm}`
	if (dayDiff === 2) return `前天 ${hhmm}`
	if (getWeekKey(date) === getWeekKey(now)) {
		return `周${WEEK_LABELS[date.getDay()]} ${hhmm}`
	}
	return `${pad(date.getMonth() + 1)}.${pad(date.getDate())}`
}

export function maskAccount(bankName, tail, accountName) {
	return `${bankName} (${tail}) ${accountName || ''}`
}

export function calcArriveAmount(amount, fee = SERVICE_FEE) {
	return Math.max(0, Number(amount) - Number(fee))
}

export const STATUS_LABEL = {
	pending: '提现审核中',
	success: '提现成功',
	failed: '提现失败',
}

export function getTimeline(item) {
	const submitTime = formatDateTime(item.createTime)
	if (item.status === 'pending') {
		return [
			{ title: '提交提现申请', time: submitTime, state: 'done' },
			{ title: '系统处理中', time: '', state: 'active' },
		]
	}
	if (item.status === 'success') {
		return [
			{ title: '提交提现申请', time: submitTime, state: 'done' },
			{ title: '提现成功', time: formatDateTime(item.arriveTime), state: 'done' },
		]
	}
	return [
		{ title: '提交提现申请', time: submitTime, state: 'done' },
		{ title: '提现失败，请重试', time: formatDateTime(item.arriveTime) || submitTime, state: 'fail' },
	]
}
