<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<?php
	$rootPath = defined('ROOTPATH') ? ROOTPATH : FCPATH;
	$logo = '';
	$logo_candidates = array(config_item('invoice_logo'), config_item('company_logo'), 'uploads/default_logo.png');
	foreach ($logo_candidates as $logo_path) {
		if (empty($logo_path)) {
			continue;
		}
		$logo_path = trim((string) $logo_path);
		$is_absolute = preg_match('/^[A-Za-z]:[\\\\\/]/', $logo_path) === 1 || (isset($logo_path[0]) && ($logo_path[0] === '/' || $logo_path[0] === '\\'));
		$local_logo = $is_absolute ? $logo_path : rtrim($rootPath, '/\\') . DIRECTORY_SEPARATOR . ltrim($logo_path, '/\\');
		if (is_file($local_logo) && is_readable($local_logo)) {
			$logo = $local_logo;
			break;
		}
	}

	$status_map = array(
		'0' => lang('pending'),
		'1' => lang('started'),
		'2' => lang('completed'),
		'3' => lang('terminated'),
	);
	$performance_map = array(
		'0' => lang('not_concluded'),
		'1' => lang('satisfactory'),
		'2' => lang('average'),
		'3' => lang('poor'),
		'4' => lang('excellent'),
	);
	$status_text = isset($status_map[(string) $training->status]) ? $status_map[(string) $training->status] : '-';
	$performance_text = isset($performance_map[(string) $training->performance]) ? $performance_map[(string) $training->performance] : '-';
	$issue_date = !empty($training->print_date) ? $training->print_date : date('Y-m-d');
	$employee_name = !empty($training->fullname) ? $training->fullname : '-';
	$company_name = config_item('company_name') ?: '';
	$company_email = config_item('company_email') ?: '';
	$company_phone = config_item('company_phone') ?: '';
	$company_address = config_item('company_address') ?: '';
	?>
	<style>
		body {
			margin: 0;
			background: #ffffff;
			color: #222222;
			font: 12px/1.55 DejaVu Sans, Arial, Helvetica, sans-serif;
		}
		.print-toolbar {
			padding: 12px;
			text-align: right;
			background: #f3f4f6;
			border-bottom: 1px solid #dddddd;
		}
		.print-toolbar button {
			border: 1px solid #333333;
			border-radius: 4px;
			background: #ffffff;
			color: #222222;
			font-weight: 600;
			padding: 8px 14px;
			cursor: pointer;
		}
		.print-frame {
			max-width: 800px;
			margin: 20px auto;
			padding: 0;
		}
		.letter {
			background: #ffffff;
			padding: 40px 46px;
		}
		table { width: 100%; border-collapse: collapse; }
		.header {
			border-bottom: 2px solid #222222;
			padding-bottom: 14px;
			margin-bottom: 28px;
		}
		.header td { vertical-align: middle; }
		.logo {
			max-width: 72px;
			max-height: 72px;
		}
		.company {
			padding-left: 16px;
		}
		.company h1 {
			margin: 0 0 5px;
			font-size: 20px;
			line-height: 1.2;
			color: #111111;
		}
		.company p {
			margin: 0;
			color: #555555;
			font-size: 10.5px;
			line-height: 1.4;
		}
		.meta {
			text-align: right;
			width: 190px;
			color: #333333;
			font-size: 11px;
			line-height: 1.5;
		}
		.title {
			margin: 0 0 24px;
			color: #111111;
			font-size: 18px;
			line-height: 1.2;
			text-align: center;
			text-decoration: underline;
		}
		.copy {
			margin: 0 0 15px;
			color: #222222;
			font-size: 12px;
		}
		.details {
			margin: 22px 0;
			border: 1px solid #cccccc;
		}
		.details td {
			padding: 8px 10px;
			border-bottom: 1px solid #dddddd;
			vertical-align: top;
		}
		.details tr:last-child td {
			border-bottom: 0;
		}
		.label {
			width: 32%;
			color: #333333;
			font-weight: 600;
			background: #f7f7f7;
		}
		.value {
			color: #222222;
		}
		.signature {
			margin-top: 48px;
		}
		.signature td {
			width: 50%;
			vertical-align: bottom;
		}
		.sign-line {
			width: 210px;
			border-top: 1px solid #222222;
			padding-top: 8px;
			color: #222222;
			font-weight: 600;
		}
		.sign-note {
			color: #666666;
			font-size: 10px;
		}
		.footer {
			margin-top: 34px;
			padding-top: 12px;
			border-top: 1px solid #dddddd;
			color: #777777;
			font-size: 9px;
			text-align: center;
		}
		@media print {
			body { background: #fff; }
			.print-toolbar { display: none; }
			.print-frame { margin: 0; box-shadow: none; max-width: none; }
		}
	</style>
</head>
<body>
<?php if (!empty($print_mode)) : ?>
	<div class="print-toolbar">
		<button type="button" onclick="window.print()">Print Training Letter</button>
	</div>
<?php endif; ?>
<div class="print-frame">
	<div class="letter">
		<div class="header">
			<table>
				<tr>
					<td>
					<table>
						<tr>
							<td style="width:80px;">
								<?php if (!empty($logo)) : ?>
									<img src="<?= $logo ?>" class="logo" alt="">
								<?php endif; ?>
							</td>
							<td class="company">
								<h1><?= html_escape($company_name) ?></h1>
								<p><?= html_escape($company_address) ?></p>
								<p><?= html_escape($company_email) ?><?= !empty($company_phone) ? ' | ' . html_escape($company_phone) : '' ?></p>
							</td>
						</tr>
					</table>
					</td>
					<td class="meta">
						<div><strong>Date:</strong> <?= display_date($issue_date) ?></div>
						<div><strong>Ref:</strong> TRN-<?= (int) $training->training_id ?></div>
					</td>
				</tr>
			</table>
		</div>

		<h2 class="title">Training Letter</h2>

		<p class="copy">Dear <?= html_escape($employee_name) ?>,</p>
		<p class="copy">
			This is to confirm your assignment to the training program <strong><?= html_escape($training->training_name) ?></strong>. Please attend the training as scheduled and complete all required sessions and related activities.
		</p>
		<p class="copy">
			The details of the training are given below for your reference.
		</p>

		<table class="details">
			<tr>
				<td class="label">Employee</td>
				<td class="value"><?= html_escape($employee_name) ?><?= !empty($training->employment_id) ? ' (' . html_escape($training->employment_id) . ')' : '' ?></td>
			</tr>
			<tr>
				<td class="label">Department</td>
				<td class="value"><?= !empty($training->deptname) ? html_escape($training->deptname) : '-' ?></td>
			</tr>
			<tr>
				<td class="label">Designation</td>
				<td class="value"><?= !empty($training->designations) ? html_escape($training->designations) : '-' ?></td>
			</tr>
			<tr>
				<td class="label">Training Name</td>
				<td class="value"><?= html_escape($training->training_name) ?></td>
			</tr>
			<tr>
				<td class="label">Training Provider</td>
				<td class="value"><?= !empty($training->vendor_name) ? html_escape($training->vendor_name) : '-' ?></td>
			</tr>
			<tr>
				<td class="label">Schedule</td>
				<td class="value"><?= display_date($training->start_date) ?> to <?= display_date($training->finish_date) ?></td>
			</tr>
			<tr>
				<td class="label">Status</td>
				<td class="value"><?= html_escape($status_text) ?></td>
			</tr>
			<tr>
				<td class="label">Performance</td>
				<td class="value"><?= html_escape($performance_text) ?></td>
			</tr>
			<tr>
				<td class="label">Assigned By</td>
				<td class="value"><?= !empty($training->assigned_by_name) ? html_escape($training->assigned_by_name) : html_escape($company_name) ?></td>
			</tr>
		</table>

		<?php if (!empty($training->remarks)) : ?>
			<p class="copy"><strong>Remarks:</strong> <?= strip_tags($training->remarks, '<br><strong><b><em><i>') ?></p>
		<?php endif; ?>

		<p class="copy">You are requested to coordinate with the concerned department and complete the training within the mentioned period.</p>

		<table class="signature">
			<tr>
				<td>
					<div class="sign-line">Human Resources</div>
					<div class="sign-note"><?= html_escape($company_name) ?></div>
				</td>
				<td style="text-align:right;">
					<div class="sign-line" style="margin-left:auto;">Employee Acknowledgement</div>
					<div class="sign-note">Signature and date</div>
				</td>
			</tr>
		</table>

		<div class="footer">
			This is a system-generated training letter. Please keep a copy for HR and employee records.
		</div>
	</div>
</div>
<?php if (!empty($print_mode)) : ?>
	<script>
		window.addEventListener('load', function () {
			window.print();
		});
	</script>
<?php endif; ?>
</body>
</html>
