<?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>
        #sortable_table .innertable td {
            padding: 8px 12px !important;
            color: #333 !important;
            background: white !important;
            font-size: 12px !important;
        }

        #sortable_table .innertable tr {
            display: table-row !important;
            background: white !important;
        }

        #sortable_table .innertable tbody td {
            padding: 8px 12px !important;
            color: #333333 !important;
            border: 1px solid #eee !important;
            font-size: 12px !important;
            background: white !important;
            visibility: visible !important;
            display: table-cell !important;
        }

        .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: 13px;
        }

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

        #sortable_table thead th {
            color: rgba(255, 255, 255, 0.85);
            padding: 13px 14px;
            text-align: left;
            font-weight: 600;
            font-size: 12px;
            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.summary_row_main {
            border-bottom: 1px solid rgba(0, 0, 0, 0.05);
            transition: background 0.15s;
            background: white;
        }

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

        #sortable_table tbody td {
            padding: 11px 14px;
            color: #333;
            border: none;
            font-size: 13px;
            vertical-align: middle;
        }

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

        #sortable_table tbody td a:hover {
            color: #e05f00;
            text-decoration: underline;
        }

        /* Expand button */
        .expand_btn {
            width: 24px;
            height: 24px;
            background: linear-gradient(135deg, #f47f20, #e05f00);
            color: #ffffff !important;
            border: none;
            border-radius: 6px;
            font-size: 14px;
            font-weight: 700;
            cursor: pointer;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            text-decoration: none !important;
            transition: all 0.2s;
            box-shadow: 0 2px 6px rgba(244, 127, 32, 0.3);
            line-height: 1;
        }

        .expand_btn:hover {
            background: linear-gradient(135deg, #ff8f30, #f47f20);
            transform: scale(1.1);
            color: white;
            text-decoration: none;
        }

        /* Details row */
        .details_row td {
            padding: 0 !important;
            background: rgba(240, 244, 255, 0.8) !important;
        }

        .innertable_wrapper {
            padding: 12px 16px 16px 40px;
            display: none;
        }

        .innertable {
            width: 100%;
            border-collapse: collapse;
            font-size: 12px;
            background: white;
            border-radius: 10px;
            overflow: hidden;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.06);
        }

        .innertable thead tr {
            background: linear-gradient(135deg, #f47f20, #e05f00);
        }

        .innertable thead th {
            color: white;
            padding: 9px 12px;
            text-align: left;
            font-weight: 600;
            font-size: 11px;
            letter-spacing: 0.3px;
            border: none;
            white-space: nowrap;
        }

        .innertable tbody tr {
            border-bottom: 1px solid rgba(0, 0, 0, 0.04);
            transition: background 0.1s;
        }

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

        .innertable tbody tr:last-child {
            border-bottom: none;
        }

        .innertable tbody td {
            padding: 8px 12px !important;
            color: #333333 !important;
            border: 1px solid #eee !important;
            font-size: 12px !important;
            background: white !important;
            visibility: visible !important;
            display: table-cell !important;
        }

        .innertable tbody tr {
            display: table-row !important;
            visibility: visible !important;
        }

        .innertable {
            display: table !important;
            visibility: visible !important;
        }

        .innertable_wrapper {
            padding: 12px 16px 16px 40px;
            visibility: visible !important;
            overflow: visible !important;
        }

        /* Summary card */
        .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;
        }

        .no_data {
            text-align: center;
            padding: 32px;
            color: #999;
            font-style: italic;
            font-size: 14px;
        }

        /* ── 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;
            }

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

            #sortable_table {
                min-width: 500px;
            }

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

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

            /* Inner table */
            .innertable_wrapper {
                padding: 10px 10px 12px 10px;
                overflow-x: auto;
            }

            .innertable {
                min-width: 400px;
            }

            .innertable thead th {
                padding: 7px 10px;
                font-size: 10px;
            }

            .innertable tbody td {
                padding: 6px 10px !important;
                font-size: 11px !important;
            }

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

            .summary_row {
                font-size: 13px;
            }
        }

        @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 6px;
                font-size: 10px;
                white-space: normal;
            }

            #sortable_table tbody td {
                padding: 7px 6px;
                font-size: 11px;
            }

            .expand_btn {
                width: 20px;
                height: 20px;
                font-size: 12px;
            }

            .innertable_wrapper {
                padding: 8px;
            }

            .innertable {
                min-width: unset;
                width: 100%;
            }

            .innertable thead th,
            .innertable tbody td {
                padding: 6px 8px !important;
                font-size: 10px !important;
                white-space: normal !important;
            }

            .summary_card {
                padding: 12px 14px;
            }

            .summary_row:last-child {
                font-size: 13px;
            }
        }
    </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>
                        <th style="width:40px;">+</th>
                        <?php foreach ($headers['summary'] as $header): ?>
                            <th><?php echo $header; ?></th>
                        <?php endforeach; ?>
                    </tr>
                </thead>
                <tbody>
                    <?php if (count($summary_data) == 0): ?>
                        <tr>
                            <td colspan="<?php echo count($headers['summary']) + 1; ?>" class="no_data">
                                No data found for the selected date range.
                            </td>
                        </tr>
                    <?php else: ?>
                        <?php foreach ($summary_data as $key => $row): ?>

                            <!-- Summary Row -->
                            <tr class="summary_row_main">
                                <td>
                                    <a href="#" class="expand_btn">+</a>
                                </td>
                                <?php foreach ($row as $cell): ?>
                                    <td><?php echo $cell; ?></td>
                                <?php endforeach; ?>
                            </tr>

                            <!-- Details Row -->
                            <tr class="details_row">
                                <td colspan="100">
                                    <div class="innertable_wrapper">

                                        <table class="innertable">
                                            <thead>
                                                <tr>
                                                    <?php foreach ($headers['details'] as $header): ?>
                                                        <th><?php echo $header; ?></th>
                                                    <?php endforeach; ?>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php foreach ($details_data[$key] as $row2): ?>
                                                    <tr>
                                                        <?php foreach ($row2 as $cell): ?>
                                                            <td style="color:#333 !important;background:white !important;padding:8px !important;border:1px solid #ddd !important;"><?php echo $cell; ?></td>
                                                        <?php endforeach; ?>
                                                    </tr>
                                                <?php endforeach; ?>
                                            </tbody>
                                        </table>
                                    </div>
                                </td>
                            </tr>

                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>

        <!-- Overall Summary -->
        <?php if (count($overall_summary_data) > 0): ?>
            <div class="summary_card">
                <div class="summary_card_title">📊 Overall Summary</div>
                <?php foreach ($overall_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">
        $(document).ready(function() {
            // Hide all detail rows initially
            $('.innertable_wrapper').hide();

            $(".expand_btn").click(function() {
                // jQuery 1.2.6 compatible - use parents() instead of closest()
                var thisRow = $(this).parents('tr:first');
                var nextRow = thisRow.next('tr');
                var wrapper = nextRow.find('.innertable_wrapper');

                wrapper.slideToggle(250);

                if ($(this).text() == '+') {
                    $(this).text('-');
                    $(this).css('background', 'linear-gradient(135deg, #0f3460, #16213e)');
                } else {
                    $(this).text('+');
                    $(this).css('background', 'linear-gradient(135deg, #f47f20, #e05f00)');
                }

                return false;
            });
        });
    </script>
<?php } ?>