<?php
$company_name = config_item('company_legal_name') ?: config_item('company_name');
$company_name = $company_name ?: 'Ultimate Project Manager CRM PRO';
$company_address = config_item('company_address') ?: '123 Corporate Avenue, XYZ Street';
$company_city_line = trim((string) config_item('company_city'));
if (config_item('company_zip_code')) {
	$company_city_line = trim($company_city_line . ' ' . config_item('company_zip_code'));
}
if (config_item('company_country')) {
	$company_city_line = trim($company_city_line . ', ' . config_item('company_country'), ', ');
}
$company_city_line = $company_city_line ?: 'London SE1 7PB, United Kingdom';
$company_phone = config_item('company_phone') ?: '+44 234 2432';
$company_email = config_item('company_email') ?: 'HR@ultimatecrm.pro';
$company_logo = config_item('company_logo');
$logo_path = '';
if (!empty($company_logo)) {
	$local_logo = FCPATH . ltrim($company_logo, '/');
	$logo_path = is_file($local_logo) ? $local_logo : base_url($company_logo);
}

$employee_name = !empty($resignation->fullname) ? $resignation->fullname : '-';
$employee_id = !empty($resignation->employment_id) ? $resignation->employment_id : '-';
$department = !empty($resignation->deptname) ? $resignation->deptname : '-';
$designation = !empty($resignation->designations) ? $resignation->designations : '-';
$notice_date = !empty($resignation->notice_date) ? date('m.d.Y', safe_strtotime($resignation->notice_date)) : '-';
$resignation_date = !empty($resignation->resignation_date) ? date('m.d.Y', safe_strtotime($resignation->resignation_date)) : '-';
$issue_date = date('m.d.Y');
$reference = 'RESG-' . str_pad((string) $resignation->id, 5, '0', STR_PAD_LEFT);
$remarks = !empty($resignation->description) ? trim(strip_tags($resignation->description)) : '';
$company_parts = preg_split('/\s+/', trim($company_name));
$brand_main = strtoupper(implode(' ', array_slice($company_parts, 0, 3)));
$brand_sub = strtoupper(trim(implode(' ', array_slice($company_parts, 3))));
if ($brand_sub === '') {
	$brand_sub = 'HR DOCUMENT';
}
?>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>Resignation Letter</title>
	<style>
		@page { margin: 14mm 15mm 12mm; }
		body { margin: 0; background: #f5f5f5; color: #1b1b1b; font-family: DejaVu Sans, Arial, sans-serif; font-size: 11px; line-height: 1.38; }
		.page { background: #fff; }
		.print-frame { max-width: 980px; margin: 24px auto; background: #fff; padding: 20px 34px 28px; box-shadow: 0 20px 55px rgba(0, 0, 0, .14); }
		.print-toolbar { position: sticky; top: 0; z-index: 20; background: #111; padding: 12px 18px; text-align: right; }
		.print-toolbar button { border: 0; border-radius: 4px; background: #111; color: #fff; font-weight: 700; padding: 9px 16px; box-shadow: inset 0 0 0 1px #555; cursor: pointer; }
		.header-table, .meta-table, .details-table, .signature-table { width: 100%; border-collapse: collapse; }
		.header-table { margin-bottom: 18px; }
		.logo-box { width: 56px; height: 56px; background: #171717; color: #fff; text-align: center; font-size: 25px; font-weight: 800; line-height: 56px; }
		.logo-img { width: 56px; max-height: 56px; object-fit: contain; }
		.brand-title { margin: 0; font-size: 21px; line-height: 1.05; font-weight: 900; letter-spacing: -.4px; text-transform: uppercase; }
		.brand-sub { margin-top: 3px; font-size: 13px; font-weight: 800; letter-spacing: 2.5px; color: #4f4f4f; text-transform: uppercase; }
		.brand-address { margin-top: 7px; font-size: 10px; line-height: 1.28; color: #6a6a6a; }
		.doc-kicker { font-size: 9px; font-weight: 800; letter-spacing: 3px; color: #777; text-transform: uppercase; text-align: right; }
		.doc-title { margin-top: 7px; font-size: 23px; font-weight: 300; letter-spacing: 4px; text-transform: uppercase; text-align: right; }
		.rule { height: 2px; background: #1f1f1f; margin: 18px 0 26px; }
		.meta-table { margin-bottom: 30px; }
		.meta-table td { vertical-align: top; font-size: 11.5px; line-height: 1.48; }
		.meta-right { text-align: right; }
		.label { font-weight: 800; color: #555; }
		.subject { margin: 0 0 18px; padding-bottom: 8px; border-bottom: 1px solid #ddd; font-size: 12.5px; font-weight: 900; letter-spacing: 1px; text-transform: uppercase; }
		.letter-body { font-family: DejaVu Sans, Arial, Helvetica, sans-serif; font-size: 12px; line-height: 1.52; }
		.letter-body p { margin: 0 0 12px; }
		.letter-body strong { font-weight: 900; }
		.details-table { margin: 15px 0 16px; font-family: DejaVu Sans, Arial, sans-serif; font-size: 11px; border: 1px solid #d5d5d5; }
		.details-table th { padding: 7px 15px; border-right: 1px solid #d5d5d5; border-bottom: 1px solid #d5d5d5; background: #fafafa; color: #444; font-size: 9.5px; font-weight: 900; letter-spacing: 1.8px; text-align: left; text-transform: uppercase; }
		.details-table td { padding: 8px 15px; border-right: 1px solid #d5d5d5; border-bottom: 1px solid #d5d5d5; font-size: 11px; font-weight: 800; }
		.details-table tr:last-child td { border-bottom: 0; }
		.details-table td:last-child, .details-table th:last-child { border-right: 0; }
		.notice-box { margin: 15px 0 16px; padding: 12px 14px; border-left: 3px solid #92400e; background: #fafafa; font-family: DejaVu Sans, Arial, sans-serif; font-size: 10.5px; line-height: 1.55; }
		.remarks { margin: 15px 0 16px; padding-left: 14px; border-left: 2px solid #8c8c8c; font-family: DejaVu Sans, Arial, sans-serif; }
		.remarks-title { font-size: 10px; font-weight: 900; letter-spacing: 2px; text-transform: uppercase; }
		.remarks-text { margin-top: 4px; color: #555; font-size: 11px; font-style: italic; font-weight: 700; }
		.signature-table { margin-top: 54px; font-family: DejaVu Sans, Arial, sans-serif; }
		.signature-line { border-top: 1px solid #111; width: 290px; padding-top: 7px; font-size: 11.5px; font-weight: 900; }
		.signature-sub { margin-top: 4px; color: #555; font-size: 9.5px; line-height: 1.45; }
		.footer { margin-top: 36px; padding-top: 12px; border-top: 1px solid #ddd; color: #777; font-size: 8px; letter-spacing: 3px; text-align: center; text-transform: uppercase; }
		@media print { body { background: #fff; } .print-toolbar { display: none; } .print-frame { margin: 0; padding: 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 Letter</button></div>
	<div class="print-frame">
<?php endif; ?>
<div class="page">
	<table class="header-table">
		<tr>
			<td style="width: 72px; vertical-align: top;">
				<?php if (!empty($logo_path)) : ?>
					<img class="logo-img" src="<?= html_escape($logo_path) ?>" alt="<?= html_escape($company_name) ?>">
				<?php else : ?>
					<div class="logo-box"><?= html_escape(strtoupper(substr($company_name, 0, 1))) ?></div>
				<?php endif; ?>
			</td>
			<td style="vertical-align: top;">
				<div class="brand-title"><?= html_escape($brand_main) ?></div>
				<div class="brand-sub"><?= html_escape($brand_sub) ?></div>
				<div class="brand-address">
					<?= html_escape($company_address) ?><br>
					<?= html_escape($company_city_line) ?><br>
					Tel: <?= html_escape($company_phone) ?> | <?= html_escape($company_email) ?>
				</div>
			</td>
			<td style="width: 360px; vertical-align: top;">
				<div class="doc-kicker">Official HR Document</div>
				<div class="doc-title">Resignation Letter</div>
			</td>
		</tr>
	</table>

	<div class="rule"></div>

	<table class="meta-table">
		<tr>
			<td style="width: 56%;">
				<div><span class="label">To:</span> <strong><?= html_escape($employee_name) ?></strong></div>
				<div><span class="label">Employee ID:</span> <?= html_escape($employee_id) ?></div>
				<div><span class="label">Department:</span> <?= html_escape($department) ?></div>
				<div><span class="label">Position:</span> <?= html_escape($designation) ?></div>
			</td>
			<td class="meta-right">
				<div><span class="label">Date of Issue:</span> <?= html_escape($issue_date) ?></div>
				<div><span class="label">Reference:</span> <?= html_escape($reference) ?></div>
			</td>
		</tr>
	</table>

	<div class="subject">Subject: Official Resignation Acknowledgement</div>

	<div class="letter-body">
		<p>Dear <strong><?= html_escape($employee_name) ?></strong>,</p>
		<p>This letter formally acknowledges receipt and acceptance of your resignation from <?= html_escape($company_name) ?>. Your resignation notice was recorded on <strong><?= html_escape($notice_date) ?></strong>, with your final working date confirmed as <strong><?= html_escape($resignation_date) ?></strong>.</p>
		<p>We request that you complete all assigned handover responsibilities, return company property, and coordinate with Human Resources for clearance and final settlement before your final working day.</p>
	</div>

	<table class="details-table">
		<tr><th>Details</th><th>Employment Information</th></tr>
		<tr><td><span class="label">Notice Date</span></td><td><?= html_escape($notice_date) ?></td></tr>
		<tr><td><span class="label">Last Working Date</span></td><td><?= html_escape($resignation_date) ?></td></tr>
		<tr><td><span class="label">Current Position</span></td><td><?= html_escape($designation) ?> / <?= html_escape($department) ?></td></tr>
		<tr><td><span class="label">Employee ID</span></td><td><?= html_escape($employee_id) ?></td></tr>
	</table>

	<div class="notice-box">
		Final salary, benefits, deductions, and any outstanding settlement will be processed according to company policy and the approved payroll schedule after completion of the exit clearance process.
	</div>

	<?php if ($remarks !== '') : ?>
		<div class="remarks">
			<div class="remarks-title">Employee Remarks:</div>
			<div class="remarks-text">"<?= html_escape($remarks) ?>"</div>
		</div>
	<?php endif; ?>

	<div class="letter-body">
		<p>We appreciate your contributions during your time with <?= html_escape($company_name) ?> and wish you success in your future endeavors.</p>
		<p>Sincerely,</p>
	</div>

	<table class="signature-table">
		<tr>
			<td style="width: 50%; vertical-align: top;">
				<div class="signature-line">Authorized Signatory</div>
				<div class="signature-sub">Human Resources Department<br><?= html_escape($company_name) ?></div>
			</td>
			<td style="width: 50%; vertical-align: top; padding-left: 70px;">
				<div class="signature-line">Employee Acknowledgment</div>
				<div class="signature-sub">Name: <?= html_escape($employee_name) ?><br>Date: ____________________</div>
			</td>
		</tr>
	</table>

	<div class="footer">System Generated Document. Validated via ZiscoERP HR Module.</div>
</div>
<?php if (!empty($print_mode)) : ?>
	</div>
	<script>
		window.addEventListener('load', function() {
			setTimeout(function() {
				window.print();
			}, 300);
		});
	</script>
<?php endif; ?>
</body>
</html>
