<?php
$currency_symbol = !empty($currency_symbol) ? $currency_symbol : default_currency();
$total_accounts = isset($total_accounts) ? (int) $total_accounts : 0;
$total_amount = isset($total_amount) ? (float) $total_amount : 0;
$positive_accounts = isset($positive_accounts) ? (int) $positive_accounts : 0;
$negative_accounts = isset($negative_accounts) ? (int) $negative_accounts : 0;
?>

<div class="space-y-6">
    <div id="printReport" class="space-y-6">
        <div class="show_print hidden">
            <div style="width: 100%; border-bottom: 2px solid black;">
                <table style="width: 100%; vertical-align: middle;">
                    <tr>
                        <td style="width: 50px; border: 0;">
                            <img style="width: 50px;height: 50px;margin-bottom: 5px;" src="<?= base_url() . config_item('company_logo') ?>" alt="" class="img-circle"/>
                        </td>
                        <td style="border: 0;">
                            <p style="margin-left: 10px; font: 14px lighter;"><?= config_item('company_name') ?></p>
                        </td>
                    </tr>
                </table>
            </div>
            <br/>
        </div>

        <section class="overflow-hidden rounded-3xl border border-border bg-card shadow-sm">
            <div class="border-b border-border px-6 py-4">
                <div class="flex flex-col gap-4 xl:flex-row xl:items-start xl:justify-between">
                    <div class="min-w-0 space-y-2">
                        <div class="inline-flex items-center gap-2 rounded-full border border-emerald-500/20 bg-emerald-500/10 px-3 py-1 text-[11px] font-semibold uppercase tracking-[0.22em] text-emerald-600 dark:text-emerald-300">
                            <i data-lucide="scale" class="h-3.5 w-3.5"></i>
                            <?= lang('balance_sheet') ?>
                        </div>
                        <div>
                            <h2 class="text-xl font-semibold tracking-tight text-foreground"><?= lang('balance_sheet') ?></h2>
                            <p class="mt-1 max-w-3xl text-sm text-muted-foreground">Review account balances across the full chart of accounts with quick treasury visibility and export-ready reporting.</p>
                        </div>
                    </div>
                    <div class="flex flex-col gap-3 xl:items-end">
                        <div class="grid grid-cols-1 gap-2 sm:grid-cols-2 xl:min-w-[440px]">
                            <div class="rounded-2xl border border-emerald-500/15 bg-emerald-500/5 px-4 py-3">
                                <p class="text-[11px] font-semibold uppercase tracking-[0.18em] text-emerald-600 dark:text-emerald-300">Strongest Account</p>
                                <p class="mt-1 truncate text-sm font-semibold text-foreground"><?= !empty($top_account->account_name) ? html_escape($top_account->account_name) : '-' ?></p>
                                <p class="mt-0.5 text-xs text-muted-foreground"><?= !empty($top_account) ? display_money($top_account->balance, $currency_symbol) : '-' ?></p>
                            </div>
                            <div class="rounded-2xl border border-amber-500/15 bg-amber-500/5 px-4 py-3">
                                <p class="text-[11px] font-semibold uppercase tracking-[0.18em] text-amber-600 dark:text-amber-300">Lowest Account</p>
                                <p class="mt-1 truncate text-sm font-semibold text-foreground"><?= !empty($lowest_account->account_name) ? html_escape($lowest_account->account_name) : '-' ?></p>
                                <p class="mt-0.5 text-xs text-muted-foreground"><?= !empty($lowest_account) ? display_money($lowest_account->balance, $currency_symbol) : '-' ?></p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="grid grid-cols-1 gap-4 border-t border-border p-6 sm:grid-cols-2 xl:grid-cols-4">
                <div class="rounded-2xl border border-border bg-background/70 p-5">
                    <p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('accounts') ?></p>
                    <p class="mt-3 text-2xl font-bold text-foreground"><?= $total_accounts ?></p>
                    <p class="mt-1 text-xs text-muted-foreground">All financial accounts included in the current balance sheet</p>
                </div>
                <div class="rounded-2xl border border-border bg-background/70 p-5">
                    <p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('total') . ' ' . lang('balance') ?></p>
                    <p class="mt-3 text-2xl font-bold <?= $total_amount >= 0 ? 'text-sky-600 dark:text-sky-300' : 'text-rose-600 dark:text-rose-300' ?>"><?= display_money($total_amount, $currency_symbol) ?></p>
                    <p class="mt-1 text-xs text-muted-foreground">Combined account balance across the full ledger</p>
                </div>
                <div class="rounded-2xl border border-border bg-background/70 p-5">
                    <p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground">Positive Accounts</p>
                    <p class="mt-3 text-2xl font-bold text-emerald-600 dark:text-emerald-300"><?= $positive_accounts ?></p>
                    <p class="mt-1 text-xs text-muted-foreground">Accounts currently holding positive balance</p>
                </div>
                <div class="rounded-2xl border border-border bg-background/70 p-5">
                    <p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground">Negative Accounts</p>
                    <p class="mt-3 text-2xl font-bold text-amber-600 dark:text-amber-300"><?= $negative_accounts ?></p>
                    <p class="mt-1 text-xs text-muted-foreground">Accounts that need review or reconciliation</p>
                </div>
            </div>
        </section>

        <section class="rounded-3xl border border-border bg-card shadow-sm">
            <div class="flex flex-col gap-3 border-b border-border px-6 py-4 lg:flex-row lg:items-center lg:justify-between">
                <div>
                    <h3 class="text-lg font-semibold text-foreground"><?= lang('balance_sheet') ?></h3>
                    <p class="text-sm text-muted-foreground">A cleaner account balance ledger for treasury review, balance confirmation, and finance close activities.</p>
                </div>
                <div class="inline-flex items-center gap-2 rounded-full border border-border bg-background px-3 py-1 text-xs font-medium text-muted-foreground">
                    <i data-lucide="shield-check" class="h-3.5 w-3.5"></i>
                    Ledger snapshot based on current account balances
                </div>
            </div>
            <div class="overflow-x-auto p-6 pt-4">
                <table class="table DataTables min-w-full text-sm" id="DataTables" cellspacing="0" width="100%">
                    <thead class="border-b border-border text-xs uppercase tracking-wide text-muted-foreground">
                        <tr>
                            <th class="px-4 py-3 text-left"><?= lang('account') ?></th>
                            <th class="px-4 py-3 text-right"><?= lang('balance') ?></th>
                            <th class="px-4 py-3 text-right">Status</th>
                        </tr>
                    </thead>
                    <tbody></tbody>
                </table>
            </div>
        </section>
    </div>
</div>

<script type="text/javascript">
    $(document).ready(function () {
        list = base_url + "admin/transactions/balance_sheet_list";
    });

    window.balanceSheetPrintHtml = <?= json_encode(isset($balance_sheet_print_html) ? $balance_sheet_print_html : '', JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP) ?>;
    function print_sales_report(printReport) {
        var fallback = document.getElementById(printReport);
        var printHtml = window.balanceSheetPrintHtml || (fallback ? fallback.outerHTML : '');
        if (!printHtml) return false;

        var printWindow = window.open('', 'PRINT', 'height=900,width=1200');
        if (!printWindow) {
            if (typeof window.erpNativePrint === 'function') {
                window.erpNativePrint();
            } else if (typeof window.print === 'function') {
                window.print();
            }
            return false;
        }

        printWindow.document.open();
        printWindow.document.write(printHtml);
        printWindow.document.close();

        var printed = false;
        var doPrint = function() {
            if (printed || printWindow.closed) return;
            printed = true;
            printWindow.focus();
            if (typeof printWindow.print === 'function') {
                printWindow.print();
            }
            printWindow.close();
        };
        printWindow.onload = function() {
            printWindow.setTimeout(doPrint, 350);
        };
        printWindow.setTimeout(doPrint, 1000);
        return false;
    }
</script>
