<?php
if ($export_excel == 1) {
    ob_start();
    $this->load->view("partial/header_excel");
} else {
    $this->load->view("partial/header");
}
?>

<?php if ($export_excel != 1): ?>
    <style>
        .report_page {
            padding: 28px;
            font-family: 'Segoe UI', Arial, sans-serif;
            background: #f0f4ff;
            min-height: calc(100vh - 120px);
        }

        .report_header {
            text-align: center;
            margin-bottom: 24px;
        }

        .report_title {
            font-size: 26px;
            font-weight: 700;
            color: #1a1a2e;
            margin-bottom: 6px;
        }

        .report_subtitle {
            font-size: 14px;
            color: #999;
            font-weight: 500;
        }

        .report_actions {
            display: flex;
            justify-content: flex-end;
            margin-bottom: 16px;
        }

        .back_btn {
            background: rgba(15, 52, 96, 0.08);
            color: #0f3460;
            border: 1px solid rgba(15, 52, 96, 0.2);
            border-radius: 8px;
            padding: 8px 16px;
            font-size: 13px;
            font-weight: 600;
            text-decoration: none;
            transition: all 0.2s;
            font-family: 'Segoe UI', Arial, sans-serif;
            display: inline-block;
        }

        .back_btn:hover {
            background: #0f3460;
            color: white;
            text-decoration: none;
        }

        .report_table_card {
            background: rgba(255, 255, 255, 0.75);
            backdrop-filter: blur(12px);
            border: 1px solid rgba(255, 255, 255, 0.9);
            border-radius: 16px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.06);
            overflow: hidden;
            margin-bottom: 20px;
        }

        #sortable_table {
            width: 100%;
            border-collapse: collapse;
            font-family: 'Segoe UI', Arial, sans-serif;
            font-size: 14px;
        }

        #sortable_table thead tr {
            background: linear-gradient(135deg, #1a1a2e, #0f3460);
        }

        #sortable_table thead th {
            color: rgba(255, 255, 255, 0.85);
            padding: 14px 16px;
            text-align: left;
            font-weight: 600;
            font-size: 13px;
            letter-spacing: 0.3px;
            border: none;
            white-space: nowrap;
        }

        #sortable_table thead th a {
            color: rgba(255, 255, 255, 0.85);
            text-decoration: none;
        }

        #sortable_table tbody tr {
            border-bottom: 1px solid rgba(0, 0, 0, 0.05);
            transition: background 0.15s;
        }

        #sortable_table tbody tr:hover {
            background: rgba(244, 127, 32, 0.04);
        }

        #sortable_table tbody tr:nth-child(even) {
            background: rgba(240, 244, 255, 0.5);
        }

        #sortable_table tbody tr:nth-child(even):hover {
            background: rgba(244, 127, 32, 0.04);
        }

        #sortable_table tbody td {
            padding: 12px 16px;
            color: #333;
            border: none;
            font-size: 14px;
        }

        #sortable_table tbody td a {
            color: #f47f20;
            text-decoration: none;
            font-weight: 600;
        }

        .summary_card {
            background: rgba(255, 255, 255, 0.75);
            backdrop-filter: blur(12px);
            border: 1px solid rgba(255, 255, 255, 0.9);
            border-radius: 16px;
            padding: 24px 28px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.06);
            max-width: 400px;
            margin: 0 auto;
        }

        .summary_card_title {
            font-size: 12px;
            font-weight: 700;
            color: #f47f20;
            text-transform: uppercase;
            letter-spacing: 0.6px;
            margin-bottom: 16px;
            padding-bottom: 8px;
            border-bottom: 2px solid rgba(244, 127, 32, 0.2);
        }

        .summary_row {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 8px 0;
            font-size: 14px;
            color: #444;
            border-bottom: 1px solid rgba(0, 0, 0, 0.05);
            font-family: 'Segoe UI', Arial, sans-serif;
        }

        .summary_row:last-child {
            border-bottom: none;
            font-weight: 700;
            color: #1a1a2e;
            font-size: 15px;
            padding-top: 12px;
            margin-top: 4px;
        }

        /* ── Responsive ── */
        @media (max-width: 768px) {

            .report_page {
                padding: 12px;
            }

            .report_header {
                margin-bottom: 16px;
            }

            .report_title {
                font-size: 20px;
            }

            .report_subtitle {
                font-size: 13px;
            }

            .report_actions {
                justify-content: flex-start;
                margin-bottom: 12px;
            }

            .back_btn {
                font-size: 12px;
                padding: 7px 12px;
            }

            /* Table horizontal scroll */
            .report_table_card {
                overflow-x: auto;
                -webkit-overflow-scrolling: touch;
            }

            #sortable_table {
                min-width: 400px;
            }

            #sortable_table thead th {
                padding: 10px 12px;
                font-size: 12px;
            }

            #sortable_table tbody td {
                padding: 10px 12px;
                font-size: 13px;
            }

            .summary_card {
                max-width: 100%;
                padding: 16px 18px;
            }

            .summary_row {
                font-size: 13px;
            }

            .summary_row:last-child {
                font-size: 14px;
            }
        }

        @media (max-width: 480px) {

            .report_page {
                padding: 8px;
            }

            .report_title {
                font-size: 18px;
            }

            #sortable_table {
                min-width: unset;
                width: 100%;
            }

            #sortable_table thead th {
                padding: 8px;
                font-size: 11px;
                white-space: normal;
            }

            #sortable_table tbody td {
                padding: 8px;
                font-size: 12px;
            }

            .summary_card {
                padding: 12px 14px;
            }
        }
    </style>

    <div class="report_page">

        <div class="report_header">
            <div class="report_title"><?php echo $title; ?></div>
            <div class="report_subtitle"><?php echo $subtitle; ?></div>
        </div>

        <div class="report_actions">
            <a href="<?php echo site_url('reports'); ?>" class="back_btn">← Back to Reports</a>
        </div>

        <div class="report_table_card">
            <table class="tablesorter report" id="sortable_table">
                <thead>
                    <tr>
                        <?php foreach ($headers as $header): ?>
                            <th><?php echo $header; ?></th>
                        <?php endforeach; ?>
                    </tr>
                </thead>
                <tbody>
                    <?php if (count($data) == 0): ?>
                        <tr>
                            <td colspan="<?php echo count($headers); ?>" style="text-align:center;padding:24px;color:#999;font-style:italic;">
                                No data found for the selected date range.
                            </td>
                        </tr>
                    <?php else: ?>
                        <?php foreach ($data as $row): ?>
                            <tr>
                                <?php foreach ($row as $cell): ?>
                                    <td><?php echo $cell; ?></td>
                                <?php endforeach; ?>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>

        <!-- Summary -->
        <?php if (count($summary_data) > 0): ?>
            <div class="summary_card">
                <div class="summary_card_title">📊 Summary</div>
                <?php foreach ($summary_data as $name => $value): ?>
                    <div class="summary_row">
                        <span><?php echo $this->lang->line('reports_' . $name); ?></span>
                        <span><?php echo to_currency($value); ?></span>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>

    </div>

<?php endif; ?>

<?php
if ($export_excel == 1) {
    $this->load->view("partial/footer_excel");
    $content = ob_end_flush();
    $filename = str_replace(array(' ', '/', '\\') . '', $title);
    $filename .= "_Export.xls";
    header('Content-type: application/ms-excel');
    header('Content-Disposition: attachment; filename=' . $filename);
    echo $content;
    die();
} else {
    $this->load->view("partial/footer");
?>
    <script type="text/javascript">
        function init_table_sorting() {
            if ($('.tablesorter tbody tr').length > 1) {
                $("#sortable_table").tablesorter();
            }
        }
        $(document).ready(function() {
            init_table_sorting();
        });
    </script>
<?php } ?>