<?php
$all_transaction_info = $this->db->get('tbl_transactions')->result();
$currency = $this->report_model->check_by(array('code' => config_item('default_currency')), 'tbl_currencies');
$total_amount = 0;
$total_debit = 0;
$total_credit = 0;
$balance_total = 0;
if (!empty($all_transaction_info)) {
    foreach ($all_transaction_info as $v_transaction) {
        $total_amount += $v_transaction->amount;
        $total_debit += $v_transaction->debit;
        $total_credit += $v_transaction->credit;
    }
}
$balance_total = $total_credit - $total_debit;
$transaction_entries = !empty($all_transaction_info) ? count($all_transaction_info) : 0;

$summary_cards = array(
    array(
        'label' => lang('total_amount'),
        'value' => display_money($total_amount, $currency->symbol),
        'meta' => 'Total posted amount across all transactions',
        'icon' => 'wallet-cards',
        'tone' => 'text-sky-600 dark:text-sky-300 bg-sky-500/10',
    ),
    array(
        'label' => lang('credit'),
        'value' => display_money($total_credit, $currency->symbol),
        'meta' => 'Total incoming movement across the ledger',
        'icon' => 'arrow-down-left',
        'tone' => 'text-emerald-600 dark:text-emerald-300 bg-emerald-500/10',
    ),
    array(
        'label' => lang('debit'),
        'value' => display_money($total_debit, $currency->symbol),
        'meta' => 'Total outgoing movement across the ledger',
        'icon' => 'arrow-up-right',
        'tone' => 'text-rose-600 dark:text-rose-300 bg-rose-500/10',
    ),
    array(
        'label' => lang('balance'),
        'value' => display_money($balance_total, $currency->symbol),
        'meta' => $transaction_entries . ' ledger entries loaded',
        'icon' => 'scale',
        'tone' => $balance_total >= 0
            ? 'text-violet-600 dark:text-violet-300 bg-violet-500/10'
            : 'text-rose-600 dark:text-rose-300 bg-rose-500/10',
    ),
);
?>
<div class="space-y-6 sales-report-page">
    <div class="grid grid-cols-1 gap-3 md:grid-cols-2 xl:grid-cols-4 sales-report-cardgrid">
        <?php foreach ($summary_cards as $card): ?>
            <div class="rounded-xl border border-border bg-card p-4 shadow-sm sales-report-card">
                <div class="flex items-start justify-between gap-3">
                    <div>
                        <p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground"><?= $card['label'] ?></p>
                        <p class="mt-2 text-xl font-bold text-foreground"><?= $card['value'] ?></p>
                        <p class="mt-1 text-xs text-muted-foreground"><?= $card['meta'] ?></p>
                    </div>
                    <span class="inline-flex h-10 w-10 items-center justify-center rounded-xl <?= $card['tone'] ?>">
                        <i data-lucide="<?= $card['icon'] ?>" class="h-5 w-5"></i>
                    </span>
                </div>
            </div>
        <?php endforeach; ?>
    </div>

    <div id="printReport" class="space-y-6">
        <div class="show_print hidden">
                <div class="report-print-brand">
                    <div class="report-print-brand-row">
                        <img class="report-print-brand-logo" src="<?= base_url() . config_item('company_logo') ?>" alt="<?= html_escape(config_item('company_name')) ?>" />
                        <p class="report-print-brand-name"><?= config_item('company_name') ?></p>
                    </div>
                </div>
        </div>

        <section class="grid grid-cols-1 gap-6">
            <div class="rounded-2xl border border-border bg-background/60 sales-report-module p-4">
                <div class="border-b border-border px-6 py-4">
                    <h3 class="text-lg font-semibold text-foreground"><?= lang('transactions_report') ?></h3>
                    <p class="text-sm text-muted-foreground">Use the account filter to narrow down journal movement while keeping the existing DataTables flow.</p>
                </div>
                <div class="p-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="w-[15%]"><?= lang('date') ?></th>
                            <th class="w-[15%]"><?= lang('account') ?></th>
                            <th><?= lang('journal_entry') ?></th>
                            <th><?= lang('type') ?></th>
                            <th><?= lang('name') ?></th>
                            <th><?= lang('amount') ?></th>
                            <th><?= lang('credit') ?></th>
                            <th><?= lang('debit') ?></th>
                            <th><?= lang('balance') ?></th>
                        </tr>
                        </thead>
                        <tbody></tbody>
                    </table>
                </div>
            </div>
        </section>
    </div>
</div>

<textarea class="hidden" style="display:none" data-all-transaction-print-html><?= html_escape(isset($all_transaction_print_html) ? $all_transaction_print_html : '') ?></textarea>
<script type="text/javascript">
    $(document).ready(function () {
        list = base_url + "admin/transactions/transactions_reportList";
    });

    function print_sales_report(printReport) {
        var textarea = document.querySelector('[data-all-transaction-print-html]');
        var fallback = document.getElementById(printReport);
        var printHtml = (textarea ? textarea.value : '') || (fallback ? fallback.outerHTML : '');
        if (!printHtml) return false;

        var printWindow = window.open('', 'PRINT', 'height=900,width=1200');
        if (!printWindow) {
            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();
            printWindow.print();
            printWindow.close();
        };
        printWindow.onload = function() {
            printWindow.setTimeout(doPrint, 350);
        };
        printWindow.setTimeout(doPrint, 900);
        return false;
    }

    function allTransactionAccountFilter(id) {
        table_url(base_url + "admin/transactions/transactions_reportList/" + (id || ""));
    }
</script>
