<?php
$chart_year = ($this->session->userdata('chart_year')) ? $this->session->userdata('chart_year') : date('Y');
$cur = default_currency();
?>

<div class="space-y-4">
	<?php
	if (!empty($filterBy)) {
		if (empty($status)) {
			$status = 'all';
		}
		if (empty($range)) {
			$range = array('0', '0');
		}
	}
	if (!empty($filterBy)) {
		$id = $filterBy;
	} else {
		$id = 'sales_report';
	}
	?>
	<div class="rounded-2xl border border-border bg-card shadow-sm">
		<div id="<?= $id ?>" class="p-6">
			<div class="show_print">
				<div class="flex items-center gap-3 border-b border-slate-200 pb-3 dark:border-slate-700">
					<img src="<?= base_url() . config_item('company_logo') ?>" alt="" class="h-12 w-24 rounded-full" />
					<p class="text-sm font-semibold text-foreground"><?= config_item('company_name') ?></p>
				</div>
				<div class="h-3"></div>
			</div>
			<div class="overflow-x-auto">
				<?php
				if (!empty($type)) {
					if ($type == 'trial_balance') {
						$this->load->view('admin/accounting/reports/trial_balance');
					} elseif ($type == 'balance_sheet') {
						$this->load->view('admin/accounting/reports/balance_sheet');
					} elseif ($type == 'ledger_report') {
						$this->load->view('admin/accounting/reports/ledger_report');
					}
				} else {
					?>
					<div class="grid grid-cols-1 gap-4 lg:grid-cols-3">
						<div class="space-y-3">
							<?php
							if (!empty($all_data)) {
								foreach ($all_data as $account_type => $all_sub_type) {
									?>
									<div class="rounded-xl border border-border bg-muted/30 p-4 text-sm text-muted-foreground flex items-center justify-between">
										<span class="text-foreground"><?= $account_type ?></span>
										<span class="text-xs text-muted-foreground text-right">
											<?= lang('debit') . ': ' . display_money($all_sub_type['debit'], default_currency()) ?>
											<br/>
											<?= lang('credit') . ': ' . display_money($all_sub_type['credit'], default_currency()) ?>
										</span>
									</div>
								<?php }
							}
							?>
						</div>
						<div class="lg:col-span-2">
							<?php
							$left_side_account = array();
							$right_side_account = array();
							$total_debit = 0;
							$total_credit = 0;
							if (!empty($all_data)) {
								foreach ($all_data as $account_type => $all_sub_type) {
									$all_sub_type = $all_sub_type['sub'];
									$count = count($all_sub_type);
									$half = ceil($count / 2);
									$left_side_account[] = array_slice($all_sub_type, 0, $half);
									$right_side_account[] = array_slice($all_sub_type, $half);
								}
							}
							?>
							<div class="grid grid-cols-1 gap-4 md:grid-cols-2">
								<div>
									<table class="min-w-full overflow-hidden rounded-xl border border-slate-200 text-sm dark:border-slate-700">
										<thead class="bg-slate-200 dark:bg-slate-700">
										<tr>
											<th class="px-3 py-2 text-left font-semibold text-slate-600 dark:text-slate-300"><?= lang('account_sub_type') ?></th>
											<th class="px-3 py-2 text-right font-semibold text-slate-600 dark:text-slate-300"><?= lang('debit') ?></th>
											<th class="px-3 py-2 text-right font-semibold text-slate-600 dark:text-slate-300"><?= lang('credit') ?></th>
										</tr>
										</thead>
										<tbody class="divide-y divide-slate-100 dark:divide-slate-800">
										<?php
										foreach ($left_side_account as $all_value) {
											foreach ($all_value as $value) {
												if (empty($value['name'])) {
													continue;
												}
												$total_debit += $value['debit'];
												$total_credit += $value['credit'];
												?>
												<tr>
													<td class="px-3 py-2 text-slate-700 dark:text-slate-200"><?= $value['name'] ?></td>
													<td class="px-3 py-2 text-right text-slate-700 dark:text-slate-200"><?= display_money($value['debit'], default_currency()) ?></td>
													<td class="px-3 py-2 text-right text-slate-700 dark:text-slate-200"><?= display_money($value['credit'], default_currency()) ?></td>
												</tr>
											<?php }
										}
										?>
										</tbody>
									</table>
								</div>
								<div>
									<table class="min-w-full overflow-hidden rounded-xl border border-slate-200 text-sm dark:border-slate-700">
										<thead class="bg-slate-200 dark:bg-slate-700">
										<tr>
											<th class="px-3 py-2 text-left font-semibold text-slate-600 dark:text-slate-300"><?= lang('account_sub_type') ?></th>
											<th class="px-3 py-2 text-right font-semibold text-slate-600 dark:text-slate-300"><?= lang('debit') ?></th>
											<th class="px-3 py-2 text-right font-semibold text-slate-600 dark:text-slate-300"><?= lang('credit') ?></th>
										</tr>
										</thead>
										<tbody class="divide-y divide-slate-100 dark:divide-slate-800">
										<?php
										foreach ($right_side_account as $all_value) {
											foreach ($all_value as $value) {
												if (empty($value['name'])) {
													continue;
												}
												$total_debit += $value['debit'];
												$total_credit += $value['credit'];
												?>
												<tr>
													<td class="px-3 py-2 text-slate-700 dark:text-slate-200"><?= $value['name'] ?></td>
													<td class="px-3 py-2 text-right text-slate-700 dark:text-slate-200"><?= display_money($value['debit'], default_currency()) ?></td>
													<td class="px-3 py-2 text-right text-slate-700 dark:text-slate-200"><?= display_money($value['credit'], default_currency()) ?></td>
												</tr>
											<?php }
										}
										?>
										</tbody>
									</table>
								</div>
							</div>
						</div>
					</div>
				<?php } ?>
			</div>
		</div>
	</div>
</div>

<script>
	function print_sales_report(printReport) {
		var printEl = document.getElementById(printReport);
		if (!printEl) return;

		var styles = '';
		document.querySelectorAll('link[rel="stylesheet"], style').forEach(function(node) {
			styles += node.outerHTML;
		});

		var printWindow = window.open('', '_blank', 'width=1200,height=800');
		if (!printWindow) return;

		printWindow.document.open();
		printWindow.document.write(
			'<!doctype html><html><head><title><?= lang('reports') ?></title>' +
			styles +
			'<style>' +
			'@page{size:auto;margin:10mm;}' +
			'html,body{background:#fff !important;color:#000 !important;}' +
			'body{padding:0;margin:0;font-size:12px;}' +
			'.show_print{display:block !important;}' +
			'.rounded-2xl,.rounded-xl{border-radius:0 !important;}' +
			'.shadow-sm,.shadow-lg{box-shadow:none !important;}' +
			'a{text-decoration:none !important;color:#000 !important;}' +
			'table{width:100% !important;border-collapse:collapse !important;}' +
			'th,td{page-break-inside:avoid;}' +
			'</style></head><body>' +
			printEl.innerHTML +
			'</body></html>'
		);
		printWindow.document.close();
		printWindow.focus();

		setTimeout(function() {
			printWindow.print();
			printWindow.close();
		}, 400);
	}
</script>
