<?php
use BLR\Backline\Core\Money;
use BLR\Backline\Core\Views;
use BLR\Backline\Modules\Finances\Module as FinancesModule;
use BLR\Backline\Modules\Finances\Pages as FinancesPages;

if (!defined('ABSPATH')) {
    exit;
}

$__blr_ba_data = isset($ctx['finances_list']) && is_array($ctx['finances_list']) ? $ctx['finances_list'] : FinancesPages::list_data($ctx);
foreach ($__blr_ba_data as $__blr_ba_key => $__blr_ba_value) {
    ${$__blr_ba_key} = $__blr_ba_value;
}
unset($__blr_ba_data, $__blr_ba_key, $__blr_ba_value);
?>
<div class="container-fluid p-0">
    <div class="d-flex flex-column flex-md-row align-items-md-center justify-content-between gap-3 mb-3">
        <div>
            <h1 class="h3 mb-0">
                <?php echo esc_html__('Finances', 'blr-backline'); ?>
                <span class="text-muted small"><?php echo esc_html('(' . number_format_i18n($summary['total']) . ')'); ?></span>
            </h1>
        </div>
        <div>
            <a class="btn btn-primary" href="<?php echo esc_url($base . 'finances/new/'); ?>"><?php echo esc_html__('New Record', 'blr-backline'); ?></a>
        </div>
    </div>

    <?php if ($saved_notice) : ?>
        <div class="alert alert-success alert-dismissible fade show" role="alert">
            <?php echo esc_html__('Saved.', 'blr-backline'); ?>
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="<?php echo esc_attr__('Close', 'blr-backline'); ?>"></button>
        </div>
    <?php endif; ?>

    <?php if ($delete_error_message !== '') : ?>
	<div class="alert alert-danger" role="alert"><?php echo esc_html($delete_error_message); ?></div>
<?php endif; ?>

<?php if ($deleted_notice) : ?>
        <div class="alert alert-success alert-dismissible fade show" role="alert">
            <?php echo esc_html__('Deleted.', 'blr-backline'); ?>
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="<?php echo esc_attr__('Close', 'blr-backline'); ?>"></button>
        </div>
    <?php endif; ?>

    <div class="row row-cols-1 row-cols-md-2 row-cols-xl-5 g-3 mb-3">
        <div class="col">
            <div class="card shadow-sm border-0 h-100">
                <div class="card-body">
                    <div class="small text-uppercase text-muted fw-semibold mb-2"><?php echo esc_html__('Current Balance', 'blr-backline'); ?></div>
                    <div class="display-6 lh-1 mb-2 <?php echo esc_attr($balance_class); ?>"><?php echo esc_html('$' . number_format_i18n((float) $current_balance, 2)); ?></div>
                    <div class="small text-muted"><?php echo esc_html__('Settled income and invoices minus settled expenses.', 'blr-backline'); ?></div>
                </div>
            </div>
        </div>

        <div class="col">
            <div class="card shadow-sm border-0 h-100">
                <div class="card-body">
                    <div class="small text-uppercase text-muted fw-semibold mb-2"><?php echo esc_html__('Income This Month', 'blr-backline'); ?></div>
                    <div class="display-6 lh-1 mb-2"><?php echo esc_html('$' . number_format_i18n((float) $month_totals['income'], 2)); ?></div>
                    <div class="small text-muted"><?php echo esc_html__('Paid income in the current month.', 'blr-backline'); ?></div>
                </div>
            </div>
        </div>

        <div class="col">
            <div class="card shadow-sm border-0 h-100">
                <div class="card-body">
                    <div class="small text-uppercase text-muted fw-semibold mb-2"><?php echo esc_html__('Expenses This Month', 'blr-backline'); ?></div>
                    <div class="display-6 lh-1 mb-2"><?php echo esc_html('$' . number_format_i18n((float) $month_totals['expense'], 2)); ?></div>
                    <div class="small text-muted"><?php echo esc_html__('Paid expenses in the current month.', 'blr-backline'); ?></div>
                </div>
            </div>
        </div>

        <div class="col">
            <div class="card shadow-sm border-0 h-100">
                <div class="card-body">
                    <div class="small text-uppercase text-muted fw-semibold mb-2"><?php echo esc_html__('Outstanding Invoices', 'blr-backline'); ?></div>
                    <div class="display-6 lh-1 mb-2"><?php echo esc_html('$' . number_format_i18n((float) $month_totals['outstanding_invoices'], 2)); ?></div>
                    <div class="small text-muted"><?php echo esc_html__('Pending, partial, and overdue invoices.', 'blr-backline'); ?></div>
                </div>
            </div>
        </div>

        <div class="col">
            <div class="card shadow-sm border-0 h-100">
                <div class="card-body">
                    <div class="small text-uppercase text-muted fw-semibold mb-2"><?php echo esc_html__('Overdue Invoices', 'blr-backline'); ?></div>
                    <div class="display-6 lh-1 mb-2"><?php echo esc_html(number_format_i18n($overdue_count)); ?></div>
                    <div class="small text-muted"><?php echo esc_html__('How many invoices are past due.', 'blr-backline'); ?></div>
                </div>
            </div>
        </div>
    </div>

    <div class="card flex-fill mb-3">
        <div class="card-body py-3">
            <form method="get" class="row g-2 align-items-end">
                <div class="col-12 col-md-4 col-lg-3">
                    <label class="form-label mb-1"><?php echo esc_html__('Search', 'blr-backline'); ?></label>
                    <input class="form-control" type="search" name="s" value="<?php echo esc_attr($search_term); ?>" placeholder="<?php echo esc_attr__('Title or notes', 'blr-backline'); ?>">
                </div>
                <div class="col-12 col-md-4 col-lg-3">
                    <label class="form-label mb-1"><?php echo esc_html__('Type', 'blr-backline'); ?></label>
                    <select class="form-select" name="kind">
                        <option value=""><?php echo esc_html__('All types', 'blr-backline'); ?></option>
                        <?php foreach (FinancesModule::kinds() as $kind_option) : ?>
                            <option value="<?php echo esc_attr($kind_option); ?>" <?php selected($kind_filter, $kind_option); ?>><?php echo esc_html(FinancesModule::kind_label($kind_option)); ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="col-12 col-md-4 col-lg-3">
                    <label class="form-label mb-1"><?php echo esc_html__('Status', 'blr-backline'); ?></label>
                    <select class="form-select" name="status">
                        <option value=""><?php echo esc_html__('All statuses', 'blr-backline'); ?></option>
                        <?php foreach (FinancesModule::statuses() as $status_option) : ?>
                            <option value="<?php echo esc_attr($status_option); ?>" <?php selected($status_filter, $status_option); ?>><?php echo esc_html(FinancesModule::status_label($status_option)); ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="col-12 col-md-auto">
                    <button class="btn btn-outline-secondary" type="submit"><?php echo esc_html__('Filter', 'blr-backline'); ?></button>
                    <a class="btn btn-link text-decoration-none" href="<?php echo esc_url($base . 'finances/'); ?>"><?php echo esc_html__('Reset', 'blr-backline'); ?></a>
                </div>
            </form>
        </div>
    </div>

    <div class="card flex-fill">
        <div class="table-responsive">
            <table class="table table-hover my-0 align-middle">
                <thead>
                    <tr>
                        <th><?php echo esc_html__('Record', 'blr-backline'); ?></th>
                        <th class="d-none d-md-table-cell"><?php echo esc_html__('Type', 'blr-backline'); ?></th>
                        <th class="d-none d-md-table-cell"><?php echo esc_html__('Status', 'blr-backline'); ?></th>
                        <th><?php echo esc_html__('Amount', 'blr-backline'); ?></th>
                        <th class="d-none d-lg-table-cell"><?php echo esc_html__('Date', 'blr-backline'); ?></th>
                        <th class="d-none d-xl-table-cell"><?php echo esc_html__('Contact', 'blr-backline'); ?></th>
                        <th class="d-none d-xl-table-cell"><?php echo esc_html__('Reference', 'blr-backline'); ?></th>
                        <th class="text-end"><?php echo esc_html__('Actions', 'blr-backline'); ?></th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($record_items)) : ?>
                        <tr>
                            <td colspan="8" class="text-muted"><?php echo esc_html__('No finance records found.', 'blr-backline'); ?></td>
                        </tr>
                    <?php else : ?>
                        <?php foreach ($record_items as $item) : ?>
                            <tr>
                                <td>
                                    <div class="fw-semibold"><a class="text-decoration-none" href="<?php echo esc_url($item['view_url']); ?>"><?php echo esc_html($item['title']); ?></a></div>
                                    <div class="small text-muted d-md-none mt-1 d-flex flex-wrap gap-2">
                                        <span><?php echo wp_kses_post($item['kind_badge']); ?></span>
                                        <span><?php echo wp_kses_post($item['status_badge']); ?></span>
                                    </div>
                                    <?php if ($item['category'] !== '') : ?>
                                        <div class="small text-muted mt-1"><?php echo esc_html($item['category']); ?></div>
                                    <?php endif; ?>
                                </td>
                                <td class="d-none d-md-table-cell"><?php echo wp_kses_post($item['kind_badge']); ?></td>
                                <td class="d-none d-md-table-cell"><?php echo wp_kses_post($item['status_badge']); ?></td>
                                <td class="fw-semibold"><?php echo esc_html($item['amount']); ?></td>
                                <td class="d-none d-lg-table-cell">
                                    <div><?php echo esc_html($item['finance_date'] !== '' ? $item['finance_date'] : '—'); ?></div>
                                    <?php if ($item['due_date'] !== '') : ?>
                                        <div class="small text-muted"><?php echo esc_html__('Due', 'blr-backline'); ?> <?php echo esc_html($item['due_date']); ?></div>
                                    <?php endif; ?>
                                </td>
                                <td class="d-none d-xl-table-cell"><?php echo esc_html($item['contact_label'] !== '' ? $item['contact_label'] : '—'); ?></td>
                                <td class="d-none d-xl-table-cell"><?php echo esc_html($item['reference'] !== '' ? $item['reference'] : '—'); ?></td>
                                <td class="text-end">
                                    <div class="btn-group" role="group">
                                        <a class="btn btn-sm btn-outline-secondary" href="<?php echo esc_url($item['view_url']); ?>"><?php echo esc_html__('View', 'blr-backline'); ?></a>
                                        <a class="btn btn-sm btn-outline-secondary" href="<?php echo esc_url($item['edit_url']); ?>"><?php echo esc_html__('Edit', 'blr-backline'); ?></a>
                                        <form method="post" action="<?php echo esc_url($item['delete_url']); ?>" class="d-inline blr-ba-delete-form" data-item-label="<?php echo esc_attr(__('finance record', 'blr-backline')); ?>">
                                            <?php wp_nonce_field('blr_ba_delete_blr_finance_record_' . $item['id']); ?>
                                            <button type="submit" class="btn btn-sm btn-outline-danger"><?php echo esc_html__('Delete', 'blr-backline'); ?></button>
                                        </form>
                                    </div>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
        <div class="card-body d-flex flex-column flex-md-row align-items-md-center justify-content-between gap-2">
            <div class="small text-muted"><?php echo esc_html(Views::render_pagination_summary($records, 'records', 'pg')); ?></div>
            <div><?php echo wp_kses_post(Views::render_pagination($records, $pagination_base_url, 'pg')); ?></div>
        </div>
    </div>
</div>
