/**
 * 合同模板占位符变量 — 供合同模板管理与租赁合同创建页共用
 */

export var DEFAULT_LESSOR_COMPANIES = [
	{
		id: 'jx',
		shortName: '嘉兴羚牛',
		enabled: true,
		legalName: '浙江羚牛氢能科技有限公司',
		creditCode: '91330481MA2JXXXXX',
		address: '浙江省嘉兴市平湖市乍浦镇杭州湾大道1号',
		contact: '法务部',
		phone: '0573-88888888',
		email: 'legal@lingniu-jx.com',
		accountName: '浙江羚牛氢能科技有限公司',
		bankName: '招商银行嘉兴分行',
		bankAccount: '120924165110301',
		hotline: '400-021-1773',
	},
	{
		id: 'sh',
		shortName: '上海羚牛',
		enabled: true,
		legalName: '羚牛氢能科技（上海）有限公司',
		creditCode: '91310115MA1HXXXXX',
		address: '上海市浦东新区张江高科技园区科苑路88号',
		contact: '法务部',
		phone: '021-88888888',
		email: 'legal@lingniu-sh.com',
		accountName: '羚牛氢能科技（上海）有限公司',
		bankName: '招商银行上海张江支行',
		bankAccount: '120924165110401',
		hotline: '400-021-1773',
	},
	{
		id: 'gd',
		shortName: '广东羚牛',
		enabled: true,
		legalName: '羚牛氢能科技（广东）有限公司',
		creditCode: '91440101MA5CXXXXX',
		address: '广东省广州市黄埔区科学城开源大道11号',
		contact: '法务部',
		phone: '020-88888888',
		email: 'legal@lingniu-gd.com',
		accountName: '羚牛氢能科技（广东）有限公司',
		bankName: '招商银行广州萝岗支行',
		bankAccount: '120924165110201',
		hotline: '400-021-1773',
	},
];

export var MOCK_LEASE_CUSTOMERS = [
	{
		id: '1',
		name: '嘉兴某某物流有限公司',
		creditCode: '91330400MA2XXXXX1',
		address: '浙江省嘉兴市南湖区科技大道1号',
		contact: '张三',
		phone: '13800138001',
		email: 'zhangsan@example.com',
		companyName: '嘉兴某某物流有限公司',
		companyPhone: '0573-88888888',
		mailingAddress: '浙江省嘉兴市南湖区科技大道1号',
		bank: '中国工商银行嘉兴分行',
		bankAccount: '6222021234567890123',
		taxId: '91330400MA2XXXXX1',
		attachments: {
			businessLicense: { name: '嘉兴某某物流-营业执照.pdf' },
			legalIdFront: { name: '法人身份证-正面.jpg' },
			legalIdBack: { name: '法人身份证-反面.jpg' },
			roadTransportPermit: { name: '道路运输经营许可证.pdf' },
		},
	},
	{
		id: '2',
		name: '上海某某运输公司',
		creditCode: '91310000MA2XXXXX2',
		address: '上海市浦东新区张江高科技园区',
		contact: '李四',
		phone: '13800138002',
		email: 'lisi@example.com',
		companyName: '上海某某运输公司',
		companyPhone: '021-66666666',
		mailingAddress: '上海市浦东新区张江高科技园区碧波路88号',
		bank: '中国建设银行上海分行',
		bankAccount: '6217001234567890123',
		taxId: '91310000MA2XXXXX2',
		attachments: {
			businessLicense: { name: '上海某某运输-营业执照.pdf' },
			legalIdFront: { name: '法人身份证-正面.jpg' },
			legalIdBack: { name: '法人身份证-反面.jpg' },
			roadTransportPermit: { name: '道路运输经营许可证.pdf' },
		},
	},
	{
		id: '3',
		name: '杭州某某租赁有限公司',
		creditCode: '91330100MA2XXXXX3',
		address: '浙江省杭州市余杭区未来科技城',
		contact: '王五',
		phone: '13800138003',
		email: 'wangwu@example.com',
		companyName: '杭州某某租赁有限公司',
		companyPhone: '0571-99999999',
		mailingAddress: '浙江省杭州市余杭区文一西路998号',
		bank: '中国农业银行杭州分行',
		bankAccount: '6228481234567890123',
		taxId: '91330100MA2XXXXX3',
		attachments: {
			businessLicense: { name: '杭州某某租赁-营业执照.pdf' },
			legalIdFront: { name: '法人身份证-正面.jpg' },
			legalIdBack: { name: '法人身份证-反面.jpg' },
			roadTransportPermit: { name: '道路运输经营许可证.pdf' },
		},
	},
];

export var CUSTOMER_CREDENTIAL_ITEMS = [
	{ key: 'businessLicense', label: '客户营业执照' },
	{ key: 'legalIdFront', label: '法人身份证（正面）' },
	{ key: 'legalIdBack', label: '法人身份证（反面）' },
	{ key: 'roadTransportPermit', label: '道路运输许可证' },
];

var ATTACHMENT_PREVIEW_COLORS = {
	businessLicense: { bg: '#ecfdf5', accent: '#32a06e' },
	legalIdFront: { bg: '#eff6ff', accent: '#3b82f6' },
	legalIdBack: { bg: '#eef2ff', accent: '#6366f1' },
	roadTransportPermit: { bg: '#fff7ed', accent: '#ea580c' },
};

function escapeSvgText(text) {
	return String(text || '')
		.replace(/&/g, '&amp;')
		.replace(/</g, '&lt;')
		.replace(/>/g, '&gt;')
		.replace(/"/g, '&quot;');
}

/** 客户表证照预览图（原型用 SVG 占位，模拟客户档案附件） */
export function getCustomerAttachmentPreviewUrl(customer, key, label) {
	if (!customer) return '';
	var file = customer.attachments && customer.attachments[key];
	if (file && file.previewUrl) return file.previewUrl;
	var colors = ATTACHMENT_PREVIEW_COLORS[key] || { bg: '#f8fafc', accent: '#64748b' };
	var svg = '<svg xmlns="http://www.w3.org/2000/svg" width="640" height="480" viewBox="0 0 640 480">'
		+ '<rect width="640" height="480" fill="' + colors.bg + '"/>'
		+ '<rect x="32" y="32" width="576" height="416" rx="12" fill="#ffffff" stroke="' + colors.accent + '" stroke-width="2"/>'
		+ '<text x="320" y="232" text-anchor="middle" font-family="PingFang SC,Microsoft YaHei,sans-serif" font-size="22" fill="#18181b">'
		+ escapeSvgText(label)
		+ '</text>'
		+ '<text x="320" y="272" text-anchor="middle" font-family="PingFang SC,Microsoft YaHei,sans-serif" font-size="14" fill="#64748b">'
		+ escapeSvgText(customer.name || '')
		+ '</text>'
		+ '</svg>';
	return 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(svg);
}

export var DEFAULT_MILEAGE_STANDARD = {
	hasRequirement: false,
	period: 'month',
	targetKm: 6000,
	reductionYuan: 2000,
	validUntil: '2026-12-31',
};

export var MILEAGE_PERIOD_OPTIONS = [
	{ value: 'month', labelBefore: '每月完成', labelAfter: '公里' },
	{ value: 'quarter', labelBefore: '每季度完成', labelAfter: '公里' },
	{ value: 'year', labelBefore: '每年度完成', labelAfter: '公里' },
];

export var CONTRACT_CODE_PREFIX = 'LNZLHT';

export var DEFAULT_FEE_INFO = {
	paymentPeriod: 1,
	hydrogenPaymentMethod: 'self',
	prepayAmount: null,
	payAheadWorkdays: 5,
	returnHydrogenDiffUnitPrice: null,
};

export var PAYMENT_PERIOD_OPTIONS = [
	{ value: 1, label: '1个月' },
	{ value: 3, label: '3个月' },
	{ value: 6, label: '6个月' },
	{ value: 12, label: '12个月' },
];

export var HYDROGEN_PAYMENT_METHOD_OPTIONS = [
	{ value: 'self', label: '自行解决' },
	{ value: 'prepay', label: '预付款' },
];

export function getLessorCompanies() {
	if (typeof window !== 'undefined' && window.ONEOS_LESSOR_COMPANIES && window.ONEOS_LESSOR_COMPANIES.length) {
		return window.ONEOS_LESSOR_COMPANIES;
	}
	return DEFAULT_LESSOR_COMPANIES;
}

export function getLessorCompanyById(id) {
	if (!id) return null;
	var list = getLessorCompanies();
	return list.find(function (c) { return c.id === id; })
		|| list.find(function (c) { return c.shortName === id; })
		|| null;
}

export function getLeaseCustomerById(id) {
	if (!id) return null;
	return MOCK_LEASE_CUSTOMERS.find(function (c) { return c.id === id; }) || null;
}

export function buildLessorVars(company) {
	if (!company) return {};
	return {
		lessorName: company.legalName || '',
		lessorShortName: company.shortName || '',
		lessorAddress: company.address || '',
		lessorContact: company.contact || '',
		lessorPhone: company.phone || '',
		lessorEmail: company.email || '',
		lessorAccountName: company.accountName || company.legalName || '',
		lessorBankName: company.bankName || '',
		lessorBankAccount: company.bankAccount || '',
		lessorCreditCode: company.creditCode || '',
	};
}

export function applyTemplateVars(html, vars) {
	var out = String(html || '');
	Object.keys(vars || {}).forEach(function (key) {
		var val = vars[key] != null ? String(vars[key]) : '';
		out = out.split('{{' + key + '}}').join(val);
	});
	return out;
}

/** 将左侧表单值映射为 V26.6 合同 HTML 占位符 */
export function buildLeasePreviewVars(form) {
	var lessor = getLessorCompanyById(form && form.lessorId);
	var customer = getLeaseCustomerById(form && form.customerId);
	var vars = {
		contractCode: (form && form.contractCode) ? String(form.contractCode) : '',
		customerName: customer ? customer.name : '',
		customerAddress: customer ? customer.address : '',
		customerContact: customer ? customer.contact : '',
		customerPhone: customer ? customer.phone : '',
		creditCode: customer ? customer.creditCode : '',
	};
	return Object.assign(vars, buildLessorVars(lessor));
}

export function getCustomerInvoicePreview(customer) {
	if (!customer) {
		return {
			companyName: '',
			bank: '',
			bankAccount: '',
			taxId: '',
			mailingAddress: '',
			companyPhone: '',
		};
	}
	return {
		companyName: customer.companyName || customer.name || '',
		bank: customer.bank || '',
		bankAccount: customer.bankAccount || '',
		taxId: customer.taxId || customer.creditCode || '',
		mailingAddress: customer.mailingAddress || customer.address || '',
		companyPhone: customer.companyPhone || '',
	};
}

export function getLessorAccountPreview(company) {
	if (!company) {
		return {
			accountName: '',
			bankName: '',
			bankAccount: '',
		};
	}
	return {
		accountName: company.accountName || company.legalName || '',
		bankName: company.bankName || '',
		bankAccount: company.bankAccount || '',
	};
}

export function getLessorContactPreview(company) {
	if (!company) {
		return {
			mailAddress: '',
			contactName: '',
			contactPhone: '',
			email: '',
		};
	}
	return {
		mailAddress: company.address || '',
		contactName: company.contact || '',
		contactPhone: company.phone || '',
		email: company.email || '',
	};
}

export function getCustomerContactPreview(customer) {
	if (!customer) {
		return {
			mailAddress: '',
			contactName: '',
			contactPhone: '',
			email: '',
		};
	}
	return {
		mailAddress: customer.address || customer.mailingAddress || '',
		contactName: customer.contact || '',
		contactPhone: customer.phone || '',
		email: customer.email || '',
	};
}
