<?php
if (is_file(config_item('invoice_logo'))) {
	$logo = base_url() . config_item('invoice_logo');
} else {
	$logo = base_url() . 'uploads/default_logo.png';
}

$currency_user_id = isset($sales_info->user_id) ? $sales_info->user_id : null;
$currency = client_currency($currency_user_id);
$currency_symbol = is_object($currency) && !empty($currency->symbol) ? $currency->symbol : (is_string($currency) ? $currency : '');
$status_label = isset($payment_status) ? $payment_status : strip_tags($sales_info->status ?? '');
$status_value = strtolower((string) $status_label);
$status_class = 'bg-slate-100 text-slate-700 border-slate-200';
if ($status_value === strtolower(lang('fully_paid')) || $status_value === strtolower(lang('paid'))) {
	$status_class = 'bg-emerald-50 text-emerald-700 border-emerald-200';
} elseif ($status_value === strtolower(lang('draft'))) {
	$status_class = 'bg-slate-100 text-slate-700 border-slate-200';
} elseif ($status_value === strtolower(lang('partially_paid'))) {
	$status_class = 'bg-amber-50 text-amber-700 border-amber-200';
} elseif ($status_value === strtolower(lang('cancelled'))) {
	$status_class = 'bg-rose-50 text-rose-700 border-rose-200';
} elseif ($status_value === strtolower(lang('sent'))) {
	$status_class = 'bg-blue-50 text-blue-700 border-blue-200';
}

$client_heading = !empty($sales_info->company_heading) ? $sales_info->company_heading : lang('customer');
$company_name = config_item('company_legal_name');
$company_address = config_item('company_address');
$company_city = config_item('company_city');
$company_zip = config_item('company_zip_code');
$company_country = config_item('company_country');
$company_phone = config_item('company_phone');
$company_vat = config_item('company_vat');
$invoice_view = config_item('invoice_view');
$document_label = isset($document_label) && $document_label ? $document_label : lang('invoice');
$document_key = strtolower((string) $document_label);
$is_invoice_like = strpos($document_key, 'invoice') !== false;
$ref_no = $sales_info->reference_no ?? '';
$issue_date = $sales_info->invoice_date ?? $sales_info->purchase_date ?? $sales_info->return_stock_date ?? $sales_info->estimate_date ?? $sales_info->proposal_date ?? null;
$due_date = $sales_info->due_date ?? null;
$amount_due_value = isset($invoice_due) ? $invoice_due : ($sales_info->total ?? 0);
$discount_value = $sales_info->discount ?? $sales_info->discount_total ?? 0;
$discount_percent_value = $sales_info->discount_percent ?? 0;
$sub_total_value = $sales_info->sub_total ?? $sales_info->sub_total_cost ?? $sales_info->sub_total_amount ?? 0;
$sales_total = $sales_info->total ?? 0;
$tax_json = $sales_info->total_tax ?? null;
$tax_info = null;
if (!empty($tax_json)) {
	$decoded = json_decode($tax_json);
	if (json_last_error() === JSON_ERROR_NONE) {
		$tax_info = $decoded;
	}
}
?>

<?php if (!empty($sales_info->overdue_days)) { ?>
	<div class="rounded-xl border border-rose-200 bg-rose-50 px-4 py-3 text-sm text-rose-700 dark:border-rose-700/50 dark:bg-rose-900/40 dark:text-rose-300">
		<div class="flex items-start gap-3">
			<i data-lucide="alert-circle" class="h-4 w-4 mt-0.5"></i>
			<div>
				<div class="font-semibold uppercase tracking-wide"><?= lang('account_overdue') ?></div>
				<div><?= $sales_info->overdue_days ?></div>
			</div>
		</div>
	</div>
<?php } ?>

<div id="sales_details" class="space-y-6">
	<div class="rounded-2xl border border-border bg-card p-6 shadow-sm">
		<div class="flex flex-col gap-6 lg:flex-row lg:items-start lg:justify-between">
			<div class="space-y-4">
				<div class="flex items-center gap-3">
					<img src="<?= $logo ?>" alt="<?= html_escape(config_item('company_name')) ?>" class="h-10 w-10 rounded-lg object-contain">
					<div>
						<div class="text-lg font-semibold text-foreground"><?= html_escape($company_name) ?></div>
					</div>
				</div>
				<div class="space-y-1 text-sm text-muted-foreground">
					<div class="font-semibold text-foreground"><?= html_escape(config_item('company_name')) ?></div>
					<div><?= html_escape($company_address) ?></div>
					<div><?= html_escape(trim($company_city . ' ' . $company_zip)) ?></div>
					<div><?= html_escape($company_country) ?></div>
					<?php if (!empty($company_phone)) { ?>
						<div><?= lang('phone') ?>: <?= html_escape($company_phone) ?></div>
					<?php } ?>
					<?php if (!empty($company_vat)) { ?>
						<div><?= lang('vat_number') ?>: <?= html_escape($company_vat) ?></div>
					<?php } ?>
				</div>
			</div>
			<div class="min-w-[240px] text-right">
				<div class="text-2xl font-light tracking-[0.2em] text-muted-foreground"><?= strtoupper($document_label) ?></div>
				<div class="mt-3 grid gap-2 text-sm">
					<div class="flex items-center justify-between gap-4">
						<span class="text-muted-foreground"><?= $is_invoice_like ? lang('invoice_number') : lang('reference_no') ?></span>
						<span class="font-semibold text-foreground"><?= html_escape($ref_no) ?></span>
					</div>
					<div class="flex items-center justify-between gap-4">
						<span class="text-muted-foreground"><?= $is_invoice_like ? lang('date_of_issue') : lang('date') ?></span>
						<span class="font-semibold text-foreground"><?= !empty($issue_date) ? display_date($issue_date) : '-' ?></span>
					</div>
					<div class="flex items-center justify-between gap-4">
						<span class="text-muted-foreground"><?= lang('date_due') ?></span>
						<span class="font-semibold text-rose-600"><?= !empty($due_date) ? display_date($due_date) : '-' ?></span>
					</div>
					<div class="flex items-center justify-between gap-4">
						<span class="text-muted-foreground"><?= lang('amount_due') ?></span>
						<span class="text-lg font-semibold text-foreground"><?= display_money($amount_due_value, $currency_symbol) ?></span>
					</div>
				</div>
			</div>
		</div>

		<div class="mt-8 grid gap-6 lg:grid-cols-[2fr_1fr]">
			<div>
				<div class="text-xs font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('billed_to') ?></div>
				<div class="mt-3 space-y-1 text-sm text-muted-foreground">
					<div class="font-semibold text-foreground"><?= html_escape($sales_info->name ?? $sales_info->supplier_name ?? $sales_info->company_name ?? '') ?></div>
					<div><?= html_escape($sales_info->company_name ?? '') ?></div>
					<div class="italic text-muted-foreground"><?= lang('no_additional_address_details_provided') ?></div>
				</div>
			</div>
			<?php if (!empty($qrcode)) { ?>
				<div class="text-right">
					<div class="text-xs font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('payment_scan') ?></div>
					<div class="mt-3 inline-flex items-center justify-center rounded-lg border border-border p-3">
						<?= $qrcode ?>
					</div>
				</div>
			<?php } ?>
		</div>

		<div class="mt-8 overflow-hidden rounded-xl border border-border">
			<table class="min-w-full text-sm">
				<thead class="border-b border-border bg-muted/30 text-xs uppercase tracking-wide text-muted-foreground">
					<tr>
						<th class="px-4 py-3 text-left">#</th>
						<th class="px-4 py-3 text-left"><?= lang('description') ?></th>
						<th class="px-4 py-3 text-right"><?= lang('qty') ?></th>
						<th class="px-4 py-3 text-right"><?= lang('price') ?></th>
						<th class="px-4 py-3 text-right"><?= lang('total') ?></th>
					</tr>
				</thead>
				<tbody class="divide-y divide-border">
					<?php if (!empty($all_items)) : ?>
						<?php foreach ($all_items as $index => $v_item) : ?>
							<?php $item_name = $v_item->item_name ? $v_item->item_name : $v_item->item_desc; ?>
							<tr>
								<td class="px-4 py-3 text-muted-foreground"><?= $index + 1 ?></td>
								<td class="px-4 py-3">
									<div class="font-medium text-foreground"><?= html_escape($item_name) ?></div>
									<div class="text-xs text-muted-foreground"><?= nl2br($v_item->item_desc) ?></div>
								</td>
								<td class="px-4 py-3 text-right"><?= html_escape($v_item->quantity) ?></td>
								<td class="px-4 py-3 text-right"><?= display_money($v_item->unit_cost, $currency_symbol) ?></td>
								<td class="px-4 py-3 text-right font-semibold text-foreground"><?= display_money($v_item->total_cost, $currency_symbol) ?></td>
							</tr>
						<?php endforeach; ?>
					<?php else : ?>
						<tr>
							<td colspan="5" class="px-4 py-6 text-center text-muted-foreground"><?= lang('nothing_to_display') ?></td>
						</tr>
					<?php endif; ?>
				</tbody>
			</table>
		</div>

		<div class="mt-8 grid gap-6 lg:grid-cols-[2fr_1fr]">
			<div class="space-y-2 text-sm text-muted-foreground">
				<div class="text-xs font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('terms_conditions') ?></div>
				<div><?= $sales_info->notes ?? '' ?></div>
				<?php if (!empty($footer)) { ?>
					<div class="italic text-xs text-muted-foreground"><?= $footer ?></div>
				<?php } ?>
			</div>
			<div class="space-y-3 text-sm">
				<div class="flex items-center justify-between">
					<span class="text-muted-foreground"><?= lang('sub_total') ?></span>
					<span class="font-semibold text-foreground"><?= display_money($sub_total_value, $currency_symbol) ?></span>
				</div>
				<?php if (!empty($discount_value) && $discount_value > 0) { ?>
					<div class="flex items-center justify-between">
						<span class="text-muted-foreground"><?= lang('discount') ?> (<?= $discount_percent_value ?>%)</span>
						<span class="font-semibold text-rose-600">-<?= display_money($discount_value, $currency_symbol) ?></span>
					</div>
				<?php } ?>
				<?php
				if (!empty($tax_info) && !empty($tax_info->tax_name)) {
					foreach ($tax_info->tax_name as $t_key => $v_tax_info) {
						$tax = explode('|', $v_tax_info);
						$tax_value = !empty($tax_info->total_tax[$t_key]) ? $tax_info->total_tax[$t_key] : 0;
						?>
						<div class="flex items-center justify-between">
							<span class="text-muted-foreground"><?= html_escape($tax[0]) ?> (<?= html_escape($tax[1]) ?>%)</span>
							<span class="font-semibold text-foreground"><?= display_money($tax_value, $currency_symbol) ?></span>
						</div>
						<?php
					}
				}
				?>
				<?php if (!empty($sales_info->adjustment)) { ?>
					<div class="flex items-center justify-between">
						<span class="text-muted-foreground"><?= lang('adjustment') ?></span>
						<span class="font-semibold text-foreground"><?= display_money($sales_info->adjustment, $currency_symbol) ?></span>
					</div>
				<?php } ?>
				<div class="mt-3 border-t border-border pt-3">
					<div class="flex items-center justify-between text-base font-semibold text-foreground">
						<span><?= lang('total') ?></span>
						<span><?= display_money($sales_total, $currency_symbol) ?></span>
					</div>
					<?php if (isset($paid_amount)) { ?>
						<div class="mt-2 flex items-center justify-between text-sm text-muted-foreground">
							<span><?= lang('amount_paid') ?></span>
							<span><?= display_money($paid_amount, $currency_symbol) ?></span>
						</div>
					<?php } ?>
					<?php if (isset($invoice_due)) { ?>
						<div class="mt-3 border-t border-border pt-3 text-sm font-semibold text-foreground">
							<div class="flex items-center justify-between">
								<span><?= lang('amount_due') ?></span>
								<span><?= display_money($invoice_due, $currency_symbol) ?></span>
							</div>
						</div>
					<?php } ?>
				</div>
			</div>
		</div>
	</div>
</div>
