<div class="space-y-6">
    <?= form_open(base_url() . 'admin/report/date_wise_report', array('id' => 'form', 'class' => 'rounded-3xl border border-border bg-card p-6 shadow-sm')) ?>
    <div class="mx-auto grid w-full max-w-5xl grid-cols-1 gap-4 md:grid-cols-3 md:items-end">
        <div class="w-full">
            <?= form_datepicker('start_date', lang('start_date'), !empty($start_date) ? $start_date : date('Y-m-d'), true, ['data-max' => '#end_date', 'data-auto-close' => 'true']); ?>
        </div>
        <div class="w-full">
            <?= form_datepicker('end_date', lang('end_date'), !empty($end_date) ? $end_date : date('Y-m-d'), true, ['data-min' => '#start_date', 'data-auto-close' => 'true']); ?>
        </div>
        <div class="flex justify-center md:justify-start">
            <button type="submit" class="inline-flex h-10 w-full items-center justify-center gap-2 rounded-xl bg-primary px-5 text-sm font-semibold text-primary-foreground hover:bg-primary/90 md:w-auto">
                <i data-lucide="play" class="h-4 w-4"></i>
                <?= lang('submit') ?>
            </button>
        </div>
    </div>
    </form>

    <?php if (!empty($report)):
        $total_amount = 0;
        $total_debit = 0;
        $total_credit = 0;
        $currency = $this->report_model->check_by(array('code' => config_item('default_currency')), 'tbl_currencies');
        ?>
        <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="rounded-3xl border border-border bg-card shadow-sm">
                <div class="flex flex-col gap-4 border-b border-border px-6 py-5 lg:flex-row lg:items-start lg:justify-between">
                    <div>
                        <h3 class="text-lg font-semibold text-foreground"><?= lang('date_wise_report') ?></h3>
                        <p class="text-sm text-muted-foreground"><?= display_date($start_date) ?> - <?= display_date($end_date) ?></p>
                    </div>
                    <?php if (!empty($all_transaction_info)): ?>
                        <div class="hidden-print flex flex-wrap items-center gap-2">
                            <a href="<?= base_url() ?>admin/report/date_wise_report_pdf/<?= date('Y-m-d', safe_strtotime($start_date)) . '/' . date('Y-m-d', safe_strtotime($end_date)) ?>" class="inline-flex items-center gap-2 rounded-lg border border-border bg-background px-4 py-2 text-sm font-medium text-foreground hover:bg-muted/60">
                                <i data-lucide="file-text" class="h-4 w-4"></i>
                                <?= lang('pdf') ?>
                            </a>
                            <button type="button" onclick="print_sales_report('printReport')" class="inline-flex items-center gap-2 rounded-lg bg-primary px-4 py-2 text-sm font-semibold text-primary-foreground hover:bg-primary/90">
                                <i data-lucide="printer" class="h-4 w-4"></i>
                                <?= lang('print') ?>
                            </button>
                        </div>
                    <?php endif; ?>
                </div>

                <div class="grid grid-cols-1 gap-4 p-6 md:grid-cols-3">
                    <?php
                    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;
                        }
                    }
                    ?>
                    <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_amount') ?></p>
                        <p class="mt-3 text-2xl font-bold text-foreground"><?= display_money($total_amount, $currency->symbol) ?></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('credit') ?></p>
                        <p class="mt-3 text-2xl font-bold text-emerald-600 dark:text-emerald-300"><?= display_money($total_credit, $currency->symbol) ?></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('debit') ?></p>
                        <p class="mt-3 text-2xl font-bold text-rose-600 dark:text-rose-300"><?= display_money($total_debit, $currency->symbol) ?></p>
                    </div>
                </div>

                <div class="overflow-x-auto">
                    <table class="min-w-full text-left text-sm">
                        <thead class="border-b border-border text-xs uppercase tracking-wide text-muted-foreground">
                        <tr>
                            <th class="px-4 py-3"><?= lang('date') ?></th>
                            <th class="px-4 py-3"><?= lang('account') ?></th>
                            <th class="px-4 py-3"><?= lang('type') ?></th>
                            <th class="px-4 py-3"><?= lang('notes') ?></th>
                            <th class="px-4 py-3 text-right"><?= lang('amount') ?></th>
                            <th class="px-4 py-3 text-right"><?= lang('credit') ?></th>
                            <th class="px-4 py-3 text-right"><?= lang('debit') ?></th>
                            <th class="px-4 py-3 text-right"><?= lang('balance') ?></th>
                        </tr>
                        </thead>
                        <tbody class="divide-y divide-border">
                        <?php if (!empty($all_transaction_info)): foreach ($all_transaction_info as $v_transaction):
                            $account_info = $this->report_model->check_by(array('account_id' => $v_transaction->account_id), 'tbl_accounts');
                            ?>
                            <tr class="hover:bg-muted/30">
                                <td class="px-4 py-3 text-foreground"><?= display_date($v_transaction->date); ?></td>
                                <td class="px-4 py-3 text-foreground"><?= !empty($account_info->account_name) ? $account_info->account_name : '-' ?></td>
                                <td class="px-4 py-3 text-muted-foreground"><?= lang($v_transaction->type) ?></td>
                                <td class="px-4 py-3 text-muted-foreground"><?= $v_transaction->notes ?></td>
                                <td class="px-4 py-3 text-right font-medium text-foreground"><?= display_money($v_transaction->amount, $currency->symbol) ?></td>
                                <td class="px-4 py-3 text-right font-semibold text-emerald-600 dark:text-emerald-300"><?= display_money($v_transaction->credit, $currency->symbol) ?></td>
                                <td class="px-4 py-3 text-right font-semibold text-rose-600 dark:text-rose-300"><?= display_money($v_transaction->debit, $currency->symbol) ?></td>
                                <td class="px-4 py-3 text-right font-medium text-foreground"><?= display_money($v_transaction->total_balance, $currency->symbol) ?></td>
                            </tr>
                        <?php endforeach; endif; ?>
                        </tbody>
                        <tfoot class="border-t border-border bg-muted/10">
                        <tr>
                            <td colspan="4" class="px-4 py-3 text-right font-semibold text-foreground"><?= lang('total') ?></td>
                            <td class="px-4 py-3 text-right font-bold text-foreground"><?= display_money($total_amount, $currency->symbol) ?></td>
                            <td class="px-4 py-3 text-right font-bold text-emerald-600 dark:text-emerald-300"><?= display_money($total_credit, $currency->symbol) ?></td>
                            <td class="px-4 py-3 text-right font-bold text-rose-600 dark:text-rose-300"><?= display_money($total_debit, $currency->symbol) ?></td>
                            <td class="px-4 py-3 text-right font-bold text-foreground"><?= display_money($total_credit - $total_debit, $currency->symbol) ?></td>
                        </tr>
                        </tfoot>
                    </table>
                </div>
            </section>
        </div>
    <?php endif; ?>
</div>

<script type="text/javascript">
    function print_sales_report(printReport) {
        var printContents = document.getElementById(printReport).innerHTML;
        var originalContents = document.body.innerHTML;
        document.body.innerHTML = printContents;
        window.print();
        document.body.innerHTML = originalContents;
    }
</script>
