/** 还车应结款-新：任务阶段、角色与 mock 数据 */

export var ROLE_OPTIONS = [
	{ value: 'biz', label: '业务管理组' },
	{ value: 'energy', label: '能源部' },
	{ value: 'ops', label: '运维部' },
	{ value: 'safety', label: '安全部' },
	{ value: 'finance', label: '财务部' }
];

export var TASK_STAGE_META = {
	pending_dept: { label: '待部门填报', tone: 'amber' },
	pending_main_approval: { label: '待发起审批', tone: 'blue' },
	approving: { label: '审批中', tone: 'blue' },
	pending_reconcile: { label: '待对账', tone: 'violet' },
	pending_payment: { label: '待收付款', tone: 'amber' },
	archived: { label: '已归档', tone: 'green' },
	rejected: { label: '审批驳回', tone: 'red' },
	withdrawn: { label: '已撤回', tone: 'gray' }
};

export var PIPELINE_STEPS = [
	{ key: 'return_archived', label: '还车归档' },
	{ key: 'dept_fill', label: '部门填报' },
	{ key: 'main_approval', label: '主审批' },
	{ key: 'reconcile', label: '账单对账' },
	{ key: 'payment', label: '收付款' },
	{ key: 'done', label: '归档完成' }
];

export var DEPT_KEYS = [
	{ key: 'biz', field: 'submitBusiness', label: '业务管理组' },
	{ key: 'energy', field: 'submitEnergy', label: '能源部' },
	{ key: 'ops', field: 'submitOperation', label: '运维部' },
	{ key: 'safety', field: 'submitSafety', label: '安全部' }
];

export function readRole() {
	try {
		var role = new URLSearchParams(window.location.search).get('role') || 'biz';
		if (ROLE_OPTIONS.some(function (o) { return o.value === role; })) return role;
	} catch (e) { /* noop */ }
	return 'biz';
}

export function writeRole(role) {
	try {
		var url = new URL(window.location.href);
		url.searchParams.set('role', role);
		window.history.replaceState({}, '', url.toString());
	} catch (e) { /* noop */ }
}

export function normalizeDept(cell) {
	if (!cell) return { status: '待提交', name: '' };
	if (cell.status) return { status: cell.status, name: cell.name || '' };
	return { status: cell.ok ? '已提交' : '待提交', name: cell.name || '' };
}

export function isDeptSubmitted(row, deptKey) {
	var item = DEPT_KEYS.find(function (d) { return d.key === deptKey; });
	if (!item) return false;
	var cell = normalizeDept(row[item.field]);
	return cell.status === '已提交' || cell.status === '审批完成' || cell.status === '审批中';
}

export function deptProgress(row) {
	return DEPT_KEYS.map(function (d) {
		var cell = normalizeDept(row[d.field]);
		var done = cell.status === '已提交' || cell.status === '审批完成';
		var active = cell.status === '审批中';
		return {
			key: d.key,
			label: d.label,
			status: cell.status,
			name: cell.name,
			done: done,
			active: active
		};
	});
}

export function pipelineIndex(stage) {
	switch (stage) {
		case 'pending_dept': return 1;
		case 'pending_main_approval': return 2;
		case 'approving': return 3;
		case 'pending_reconcile': return 3;
		case 'pending_payment': return 4;
		case 'archived': return 5;
		case 'rejected': return 2;
		case 'withdrawn': return 2;
		default: return 0;
	}
}

export function settlementLabel(row) {
	if (!row || !row.settlementDirection) return '待核算';
	if (row.settlementDirection === 'refund') return '应退 ' + fmtMoney(row.settlementAmount) + ' 元';
	if (row.settlementDirection === 'pay') return '应补 ' + fmtMoney(row.settlementAmount) + ' 元';
	return '待核算';
}

export function canSeeSettlement(role) {
	return role === 'biz' || role === 'finance';
}

export function fmtMoney(v) {
	var n = typeof v === 'number' ? v : parseFloat(v);
	if (isNaN(n)) return '0.00';
	return n.toFixed(2);
}

export function fmtYMDHM(v) {
	if (!v) return '—';
	var s = String(v).trim();
	if (/^\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}/.test(s)) return s.slice(0, 16);
	if (/^\d{4}-\d{2}-\d{2}$/.test(s)) return s + ' 00:00';
	return s;
}

/** 当前角色是否在此任务上有待办 */
export function hasPendingForRole(row, role) {
	var stage = row.taskStage;
	if (stage === 'archived' || stage === 'withdrawn') return false;

	if (role === 'biz') {
		if (stage === 'pending_dept' && !isDeptSubmitted(row, 'biz')) return true;
		if (stage === 'pending_main_approval') return true;
		if (stage === 'pending_reconcile') return true;
		if (stage === 'approving' && row.canRevoke) return true;
		if (stage === 'rejected') return true;
		return false;
	}
	if (role === 'energy' && stage === 'pending_dept' && !isDeptSubmitted(row, 'energy')) return true;
	if (role === 'ops' && stage === 'pending_dept' && !isDeptSubmitted(row, 'ops')) return true;
	if (role === 'safety' && stage === 'pending_dept' && !isDeptSubmitted(row, 'safety')) return true;
	if (role === 'finance' && stage === 'approving' && row.financePending) return true;
	if (role === 'finance' && stage === 'pending_payment') return true;
	return false;
}

export function pendingHint(row, role) {
	if (!hasPendingForRole(row, role)) return '—';
	if (role === 'biz') {
		if (row.taskStage === 'pending_dept' && !isDeptSubmitted(row, 'biz')) return '待您提交业务费用';
		if (row.taskStage === 'pending_main_approval') return '待您发起总审批';
		if (row.taskStage === 'pending_reconcile') return '待您生成账单并对账';
		if (row.taskStage === 'approving') return '审批进行中，可撤回';
		if (row.taskStage === 'rejected') return '审批已驳回，待处理';
	}
	if (role === 'energy') return '待您提交能源费用';
	if (role === 'ops') return '待您提交运维费用';
	if (role === 'safety') return '待您提交安全费用';
	if (role === 'finance' && row.taskStage === 'approving') return '待您财务终审';
	if (role === 'finance' && row.taskStage === 'pending_payment') return '待登记收/付款';
	return '待处理';
}

export function getPrimaryAction(row, role) {
	var stage = row.taskStage;
	if (hasPendingForRole(row, role)) {
		if (role === 'biz' && stage === 'pending_main_approval') return { label: '发起审批', action: 'approve' };
		if (role === 'biz' && stage === 'pending_reconcile') return { label: '生成账单', action: 'bill' };
		if (role === 'finance' && stage === 'approving') return { label: '去审批', action: 'finance_approve' };
		if (role === 'finance' && stage === 'pending_payment') return { label: '登记收付款', action: 'payment' };
		return { label: '去处理', action: 'handle' };
	}
	if (stage === 'archived' || stage === 'pending_payment' || stage === 'approving') {
		return { label: '详情', action: 'view' };
	}
	return { label: '详情', action: 'view' };
}

export var SEED_ROWS = [
	{
		key: 'r1',
		contractCode: 'LNZLHT20251106001',
		submitSafety: { status: '已提交', name: '罗日可' },
		submitBusiness: { status: '已提交', name: '何苗苗' },
		submitOperation: { status: '审批中', name: '邱陈佳' },
		submitEnergy: { status: '待提交', name: '' },
		taskStage: 'pending_dept',
		customerName: '嘉兴某某物流有限公司',
		projectName: '嘉兴腾4.5T租赁',
		plateNo: '粤AGP5621',
		businessDept: '业务一部',
		businessOwner: '张经理',
		deliveryTime: '2026-02-01 09:30',
		returnTime: '2026-02-27 16:20',
		returnPerson: '陈还车',
		returnArchived: true,
		esignStatus: '已归档',
		settlementDirection: null,
		settlementAmount: null,
		deposit: 10000,
		pendingTotal: null,
		canRevoke: false,
		financePending: false
	},
	{
		key: 'r2',
		contractCode: 'LNZLHT20251201002',
		submitSafety: { status: '已提交', name: '罗日可' },
		submitBusiness: { status: '已提交', name: '何苗苗' },
		submitOperation: { status: '已提交', name: '邱陈佳' },
		submitEnergy: { status: '已提交', name: '许诗琪' },
		taskStage: 'pending_main_approval',
		customerName: '上海馨想事成物流有限公司',
		projectName: '上海干线运输项目',
		plateNo: '浙F03218F',
		businessDept: '业务二部',
		businessOwner: '李主管',
		deliveryTime: '2026-01-10 10:00',
		returnTime: '2026-02-20 18:05',
		returnPerson: '周还车',
		returnArchived: true,
		esignStatus: '已归档',
		settlementDirection: 'refund',
		settlementAmount: 5200,
		deposit: 10000,
		pendingTotal: 4800,
		canRevoke: false,
		financePending: false,
		slaReady: true
	},
	{
		key: 'r7',
		contractCode: 'LNZLHT20260108007',
		submitSafety: { status: '已提交', name: '罗日可' },
		submitBusiness: { status: '待提交', name: '' },
		submitOperation: { status: '待提交', name: '' },
		submitEnergy: { status: '待提交', name: '' },
		taskStage: 'pending_dept',
		customerName: '合肥某某物流有限公司',
		projectName: '合肥城配项目',
		plateNo: '皖A7K3P8',
		businessDept: '业务一部',
		businessOwner: '张经理',
		deliveryTime: '2026-02-10',
		returnTime: '2026-02-28',
		returnPerson: '许还车',
		returnArchived: true,
		esignStatus: '已归档',
		settlementDirection: null,
		settlementAmount: null,
		deposit: 8000,
		pendingTotal: null,
		canRevoke: false,
		financePending: false
	},
	{
		key: 'r4',
		contractCode: 'LNZLHT20251106004',
		submitSafety: { status: '已提交', name: '罗日可' },
		submitBusiness: { status: '审批中', name: '何苗苗' },
		submitOperation: { status: '已提交', name: '邱陈佳' },
		submitEnergy: { status: '审批中', name: '许诗琪' },
		taskStage: 'pending_dept',
		customerName: '苏州某某快运有限公司',
		projectName: '苏州城配项目',
		plateNo: '苏E8K2P1',
		businessDept: '业务一部',
		businessOwner: '张经理',
		deliveryTime: '2026-02-15 13:40',
		returnTime: '2026-02-26 09:10',
		returnPerson: '孙还车',
		returnArchived: true,
		esignStatus: '已归档',
		settlementDirection: null,
		settlementAmount: null,
		deposit: 10000,
		pendingTotal: null,
		canRevoke: false,
		financePending: false
	},
	{
		key: 'r9',
		contractCode: 'LNZLHT20251130009',
		submitSafety: { status: '已提交', name: '罗日可' },
		submitBusiness: { status: '已提交', name: '何苗苗' },
		submitOperation: { status: '已提交', name: '邱陈佳' },
		submitEnergy: { status: '已提交', name: '许诗琪' },
		taskStage: 'approving',
		customerName: '天津某某运输有限公司',
		projectName: '津冀干线项目',
		plateNo: '津A5R8L1',
		businessDept: '业务三部',
		businessOwner: '王总',
		deliveryTime: '2026-02-02',
		returnTime: '2026-02-27',
		returnPerson: '韩还车',
		returnArchived: true,
		esignStatus: '已归档',
		settlementDirection: 'refund',
		settlementAmount: 6786.66,
		deposit: 10000,
		pendingTotal: 3213.34,
		canRevoke: true,
		financePending: true
	},
	{
		key: 'r8',
		contractCode: 'ZYYGHT20260112008',
		submitSafety: { status: '已提交', name: '罗日可' },
		submitBusiness: { status: '已提交', name: '何苗苗' },
		submitOperation: { status: '已提交', name: '邱陈佳' },
		submitEnergy: { status: '已提交', name: '许诗琪' },
		taskStage: 'pending_reconcile',
		customerName: '深圳某某快运有限公司',
		projectName: '深莞干线项目',
		plateNo: '粤B2P6M9',
		businessDept: '业务二部',
		businessOwner: '李主管',
		deliveryTime: '2026-01-20',
		returnTime: '2026-02-22',
		returnPerson: '彭还车',
		returnArchived: true,
		esignStatus: '已归档',
		settlementDirection: 'pay',
		settlementAmount: 1200,
		deposit: 5000,
		pendingTotal: 6200,
		canRevoke: false,
		financePending: false
	},
	{
		key: 'r6',
		contractCode: 'LNZLHT20251215006',
		submitSafety: { status: '已提交', name: '罗日可' },
		submitBusiness: { status: '已提交', name: '何苗苗' },
		submitOperation: { status: '已提交', name: '邱陈佳' },
		submitEnergy: { status: '已提交', name: '许诗琪' },
		taskStage: 'pending_payment',
		customerName: '宁波某某供应链有限公司',
		projectName: '宁波港区支线项目',
		plateNo: '浙B6H9Q2',
		businessDept: '业务三部',
		businessOwner: '周经理',
		deliveryTime: '2026-01-05',
		returnTime: '2026-02-18',
		returnPerson: '郑还车',
		returnArchived: true,
		esignStatus: '已归档',
		settlementDirection: 'refund',
		settlementAmount: 4500,
		deposit: 10000,
		pendingTotal: 5500,
		canRevoke: false,
		financePending: false,
		billNo: 'ZD-20260218-006'
	},
	{
		key: 'r10',
		contractCode: 'LNZLHT20251118010',
		submitSafety: { status: '已提交', name: '罗日可' },
		submitBusiness: { status: '已提交', name: '何苗苗' },
		submitOperation: { status: '审批驳回', name: '邱陈佳' },
		submitEnergy: { status: '审批中', name: '许诗琪' },
		taskStage: 'rejected',
		customerName: '武汉某某物流有限公司',
		projectName: '武汉园区项目',
		plateNo: '鄂A8D1Q6',
		businessDept: '业务一部',
		businessOwner: '周经理',
		deliveryTime: '2026-01-15',
		returnTime: '2026-02-16',
		returnPerson: '蒋还车',
		returnArchived: true,
		esignStatus: '已归档',
		settlementDirection: null,
		settlementAmount: null,
		deposit: 10000,
		pendingTotal: null,
		canRevoke: false,
		financePending: false
	},
	{
		key: 'r5',
		contractCode: 'LNZLHT20251106005',
		submitSafety: { status: '已提交', name: '罗日可' },
		submitBusiness: { status: '已提交', name: '何苗苗' },
		submitOperation: { status: '已提交', name: '邱陈佳' },
		submitEnergy: { status: '审批中', name: '许诗琪' },
		taskStage: 'withdrawn',
		customerName: '杭州某某物流有限公司',
		projectName: '杭州港区项目',
		plateNo: '浙A3M8Q7',
		businessDept: '业务二部',
		businessOwner: '李主管',
		deliveryTime: '2026-02-05',
		returnTime: '2026-02-25',
		returnPerson: '吴还车',
		returnArchived: true,
		esignStatus: '已归档',
		settlementDirection: null,
		settlementAmount: null,
		deposit: 10000,
		pendingTotal: null,
		canRevoke: false,
		financePending: false
	},
	{
		key: 'r11',
		contractCode: 'LNZLHT2025082502',
		submitSafety: { status: '已提交', name: '罗日可' },
		submitBusiness: { status: '已提交', name: '何苗苗' },
		submitOperation: { status: '已提交', name: '邱陈佳' },
		submitEnergy: { status: '已提交', name: '许诗琪' },
		taskStage: 'archived',
		customerName: '荣达餐饮(广东)集团有限公司',
		projectName: '荣达餐饮试-租赁帕力安4.5T',
		plateNo: '粤AGP3628',
		businessDept: '业务一部',
		businessOwner: '张经理',
		deliveryTime: '2025-08-26 15:56',
		returnTime: '2026-07-09 09:38',
		returnPerson: '运维员A',
		returnArchived: true,
		esignStatus: '已归档',
		settlementDirection: 'refund',
		settlementAmount: 6786.66,
		deposit: 10000,
		pendingTotal: 3213.34,
		canRevoke: false,
		financePending: false,
		billNo: 'ZD-20260709-001',
		paymentRef: 'FK-20260715-088'
	}
];

export function buildDeptFeeDetail() {
	return {
		biz: {
			total: '1005.00',
			submitter: '何苗苗',
			status: '审批中',
			rows: [
				{ key: 'c1', seq: 1, feeItem: 'ETC-客户未缴费用', amount: '0.00', remark: '', updatedAt: '2026-07-09 10:06:50', source: '自动' },
				{ key: 'c2', seq: 2, feeItem: '租金', amount: '1005.00', remark: '', updatedAt: '2026-07-09 10:06:50', source: '手动' }
			]
		},
		energy: {
			total: '34.94',
			submitter: '许诗琪',
			status: '待提交',
			fields: [
				{ label: '氢量差补缴金额', value: '34.94', source: '还车记录' },
				{ label: '交车氢量', value: '85.20 MPa', source: '交车' },
				{ label: '还车氢量', value: '72.10 MPa', source: '还车' },
				{ label: '能源账户退费金额', value: '0.00', source: '手动' }
			],
			prepayBalance: '13523.20'
		},
		ops: {
			total: '2173.40',
			submitter: '邱陈佳',
			status: '待提交',
			rows: [
				{ key: 'o1', seq: 1, feeItem: '未结算保养', amount: '372.50', discount: '0.00', updatedAt: '2026-07-09 10:04:40', source: '维保台账' },
				{ key: 'o2', seq: 2, feeItem: '轮胎磨损费用', amount: '1800.90', discount: '0.00', updatedAt: '2026-07-09 10:04:40', source: '还车OCR' }
			]
		},
		safety: {
			total: '0.00',
			submitter: '罗日可',
			status: '待提交',
			feeRows: [
				{ key: 's1', seq: 1, feeItem: '违章处理违约金', amount: '0.00', updatedAt: '2026-07-09 10:04:40', source: '违章管理' }
			],
			violations: [
				{ key: 'v1', code: 'WZ-2026061511180585', plateNo: '粤AGP3628', act: '超速20%-50%', time: '2025-12-18 06:31:00', fine: '150.00', status: '已缴费', handled: '已处理' }
			]
		}
	};
}
