<style type="text/css">
    .nav-tabs-custom>.nav-tabs>li.active {
        border-top-color: #faa21c;
    }
    .small-box {
        border-radius: 5px;
        box-shadow: 0 1px 3px rgba(0,0,0,0.2);
    }
    .label-fee { background-color: #0073b7; }
    .label-uniform { background-color: #f39c12; }
</style>

<?php
$currency_symbol = $this->customlib->getSchoolCurrencyFormat();
?>

<div class="content-wrapper" style="min-height: 946px;">
    <section class="content-header">
        <h1>
            <i class="fa fa-line-chart"></i> 
            <?php echo $this->lang->line('financial_reports'); ?> 
            <small></small>
        </h1>
        <a href="<?php echo base_url(); ?>uniformreport" class="col-md pull-right">Uniform Report</a>
        <b style="padding-left:5px;padding-right:5px;" class="pull-right"> | </b>												 
        <a href="<?php echo base_url(); ?>admin/payroll/payrollreport" class="col-md pull-right">Payroll Report</a> 
        <b style="padding-left:5px;padding-right:5px;" class="pull-right"> | </b>
        <a href="<?php echo base_url(); ?>admin/transaction/searchtransaction" class="col-md pull-right">Transaction Report</a>	
        <b style="padding-left:5px;padding-right:5px;" class="pull-right"> | </b>
        <a href="<?php echo base_url(); ?>admin/transaction/studentacademicreport" class="col-md pull-right">Balance Fees Report</a>	
        <b style="padding-left:5px;padding-right:5px;" class="pull-right"> | </b>
    </section>

    <!-- Main content -->
    <section class="content">
        <div class="row">
            <div class="col-md-12">
                <div class="box box-primary">
                    <div class="box-header with-border">
                        <h3 class="box-title">
                            <i class="fa fa-balance-scale"></i> 
                            <?php echo $this->lang->line('combinedcollectionsreport'); ?>
                        </h3>
                    </div>

                    <div class="box-body">
                        <div class="row">
                            <div class="col-md-12">
                                <form id="reportForm" role="form" 
                                      action="<?php echo site_url('bireports/combined_collections'); ?>" 
                                      method="post" class="form-horizontal">
                                    <?php echo $this->customlib->getCSRF(); ?> 

                                    <!-- View Mode Selection -->
                                    <div class="form-group">
                                        <div class="col-sm-4">
                                            <label>View Mode</label>
                                            <select name="view_mode" id="view_mode" class="form-control" onchange="toggleFilterSections()">
                                                <option value="">Select View Mode</option>
                                                <option value="consolidated" <?php echo set_select('view_mode', 'consolidated', $view_mode == 'consolidated'); ?>>Consolidated</option>
                                                <option value="broken" <?php echo set_select('view_mode', 'broken', $view_mode == 'broken'); ?>>Broken Down</option>
                                            </select>
                                        </div>
                                    </div>

                                    <!-- Consolidated Date Filters -->
                                    <div id="consolidated_dates" style="display: <?php echo ($view_mode == 'consolidated') ? 'block' : 'none'; ?>;">
                                        <div class="form-group">
                                            <div class="col-sm-4">
                                                <label>Date From</label>
                                                <input name="date_from" type="date" 
                                                    class="form-control" 
                                                    value="<?php echo set_value('date_from', $date_from); ?>" />
                                            </div>
                                            <div class="col-sm-4">
                                                <label>Date To</label>
                                                <input name="date_to" type="date" 
                                                    class="form-control" 
                                                    value="<?php echo set_value('date_to', $date_to); ?>" />
                                            </div>
                                            <div class="col-sm-2">
                                                <button type="submit" name="search" value="search_filter" 
                                                    class="btn btn-primary" style="margin-top: 25px;">
                                                    <i class="fa fa-search"></i> Search
                                                </button>
                                            </div>
                                        </div>
                                    </div>

                                    <!-- Broken Down Type Selection -->
                                    <div id="broken_type" style="display: <?php echo ($view_mode == 'broken') ? 'block' : 'none'; ?>;">
                                        <div class="form-group">
                                            <div class="col-sm-6">
                                                <label>Select Collection Type</label>
                                                <select name="broken_type" id="broken_type_select" class="form-control" onchange="toggleBrokenDateFilters()">
                                                    <option value="">Select Type</option>
                                                    <option value="fees" <?php echo set_select('broken_type', 'fees'); ?>>Total Fees Collected</option>
                                                    <option value="uniforms" <?php echo set_select('broken_type', 'uniforms'); ?>>Total Uniforms Collected</option>
                                                </select>
                                            </div>
                                        </div>
                                    </div>

                                    <!-- Broken Down Date Filters -->
                                    <div id="broken_dates" style="display: none;">
                                        <div class="form-group">
                                            <div class="col-sm-4">
                                                <label>Date From</label>
                                                <input name="date_from_broken" type="date" 
                                                    class="form-control" 
                                                    value="<?php echo set_value('date_from_broken', $date_from); ?>" />
                                            </div>
                                            <div class="col-sm-4">
                                                <label>Date To</label>
                                                <input name="date_to_broken" type="date" 
                                                    class="form-control" 
                                                    value="<?php echo set_value('date_to_broken', $date_to); ?>" />
                                            </div>
                                            <div class="col-sm-2">
                                                <button type="submit" name="search" value="search_filter" 
                                                    class="btn btn-primary" style="margin-top: 25px;">
                                                    <i class="fa fa-search"></i> Search
                                                </button>
                                            </div>
                                        </div>
                                    </div>
                                </form>
                            </div>
                        </div>

                        <!-- Summary Cards - Only for Consolidated View -->
                        <?php if (!empty($totals) && $view_mode == 'consolidated'): ?>
                        <div class="row">
                            <div class="col-md-4">
                                <div class="small-box bg-green">
                                    <div class="inner">
                                        <h3><?php echo $currency_symbol . ' ' . number_format($totals['grand_total'], 2); ?></h3>
                                        <p>Grand Total Collected</p>
                                    </div>
                                    <div class="icon"><i class="fa fa-money"></i></div>
                                </div>
                            </div>
                            <div class="col-md-4">
                                <div class="small-box bg-blue">
                                    <div class="inner">
                                        <h3><?php echo $currency_symbol . ' ' . number_format($totals['total_fees'], 2); ?></h3>
                                        <p>Total Fees (<?php echo $totals['fee_count']; ?> transactions)</p>
                                    </div>
                                    <div class="icon"><i class="fa fa-graduation-cap"></i></div>
                                </div>
                            </div>
                            <div class="col-md-4">
                                <div class="small-box bg-orange">
                                    <div class="inner">
                                        <h3><?php echo $currency_symbol . ' ' . number_format($totals['total_uniforms'], 2); ?></h3>
                                        <p>Total Uniforms (<?php echo $totals['uniform_count']; ?> transactions)</p>
                                    </div>
                                    <div class="icon"><i class="fa fa-shopping-cart"></i></div>
                                </div>
                            </div>
                        </div>
                        <?php endif; ?>

                        <!-- Results Table for Consolidated View -->
                        <?php if (!empty($combined_data) && $view_mode == 'consolidated'): ?>
                            <div class="table-responsive">
                                <hr>
                                <h4>
                                    Consolidated Report Period: 
                                    <b>
                                        <?php 
                                        echo !empty($date_from) ? 
                                            $date_from : 'All Time'; 
                                        ?>
                                    </b> 
                                    - 
                                    <b>
                                        <?php 
                                        echo !empty($date_to) ? 
                                            $date_to : 'Current Date'; 
                                        ?>
                                    </b>
                                </h4>

                                <table class="table table-bordered table-striped">
                                    <thead>
                                        <tr>
                                            <th>#</th>
                                            <th>Date</th>
                                            <th>Type</th>
                                            <th>Item Name</th>
                                            <th>Receipt</th>
                                            <th>Student ID</th>
                                            <th>Description</th>
                                            <th>Amount (<?php echo $currency_symbol; ?>)</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php 
                                        $counter = 1;
                                        foreach ($combined_data as $collection): 
                                        ?>
                                        <tr>
                                            <td><?php echo $counter++; ?></td>
                                            <td><?php echo date('Y-m-d', strtotime($collection->collection_date)); ?></td>
                                            <td>
                                                <span class="label label-<?php echo $collection->collection_type == 'Fee' ? 'primary' : 'warning'; ?>">
                                                    <?php echo $collection->collection_type; ?>
                                                </span>
                                            </td>
                                            <td><?php echo $collection->item_name; ?></td>
                                            <td><?php echo $collection->receipt; ?></td>
                                            <td><?php echo $collection->student_id; ?></td>
                                            <td><?php echo $collection->description; ?></td>
                                            <td style="text-align: right;">
                                                <strong><?php echo number_format($collection->amount, 2); ?></strong>
                                            </td>
                                        </tr>
                                        <?php endforeach; ?>
                                    </tbody>
                                    <?php if (!empty($totals)): ?>
                                    <tfoot>
                                        <tr style="font-weight: bold; background-color: #f5f5f5;">
                                            <td colspan="7" class="text-right">Grand Total:</td>
                                            <td style="text-align: right;">
                                                <?php echo number_format($totals['grand_total'], 2); ?>
                                            </td>
                                        </tr>
                                    </tfoot>
                                    <?php endif; ?>
                                </table>
                            </div>

                        <!-- Results for Broken Down - Fees -->
                        <?php elseif (!empty($fees_data) && $view_mode == 'broken' && $this->input->post('broken_type') == 'fees'): ?>
                            <div class="table-responsive">
                                <hr>
                                <h4>
                                    Total Fees Collected - Period: 
                                    <b>
                                        <?php 
                                        echo !empty($date_from_broken) ? 
                                            $date_from_broken : 'All Time'; 
                                        ?>
                                    </b> 
                                    - 
                                    <b>
                                        <?php 
                                        echo !empty($date_to_broken) ? 
                                            $date_to_broken : 'Current Date'; 
                                        ?>
                                    </b>
                                </h4>

                                <table class="table table-bordered table-striped">
                                    <thead>
                                        <tr>
                                            <th>#</th>
                                            <th>Date</th>
                                            <th>Receipt</th>
                                            <th>Fee Group</th>
                                            <th>Session ID</th>
                                            <th>Amount (<?php echo $currency_symbol; ?>)</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php 
                                        $counter = 1;
                                        $fees_total = 0;
                                        foreach ($fees_data as $fee): 
                                            $fees_total += $fee->amount;
                                        ?>
                                        <tr>
                                            <td><?php echo $counter++; ?></td>
                                            <td><?php echo date('Y-m-d', strtotime($fee->created_at)); ?></td>
                                            <td><?php echo $fee->receipt; ?></td>
                                            <td><?php echo $fee->fee_group_name; ?></td>
                                            <td><?php echo $fee->session_id; ?></td>
                                            <td style="text-align: right;">
                                                <strong><?php echo number_format($fee->amount, 2); ?></strong>
                                            </td>
                                        </tr>
                                        <?php endforeach; ?>
                                    </tbody>
                                    <tfoot>
                                        <tr style="font-weight: bold; background-color: #f5f5f5;">
                                            <td colspan="5" class="text-right">Total Fees:</td>
                                            <td style="text-align: right;">
                                                <?php echo number_format($fees_total, 2); ?>
                                            </td>
                                        </tr>
                                    </tfoot>
                                </table>
                            </div>

                        <!-- Results for Broken Down - Uniforms -->
                        <?php elseif (!empty($uniforms_data) && $view_mode == 'broken' && $this->input->post('broken_type') == 'uniforms'): ?>
                            <div class="table-responsive">
                                <hr>
                                <h4>
                                    Total Uniforms Collected - Period: 
                                    <b>
                                        <?php 
                                        echo !empty($date_from_broken) ? 
                                            $date_from_broken : 'All Time'; 
                                        ?>
                                    </b> 
                                    - 
                                    <b>
                                        <?php 
                                        echo !empty($date_to_broken) ? 
                                            $date_to_broken : 'Current Date'; 
                                        ?>
                                    </b>
                                </h4>

                                <table class="table table-bordered table-striped">
                                    <thead>
                                        <tr>
                                            <th>#</th>
                                            <th>Date</th>
                                            <th>Item Name</th>
                                            <th>Student ID</th>
                                            <th>Description</th>
                                            <th>Batch Number</th>
                                            <th>Discount</th>
                                            <th>Amount (<?php echo $currency_symbol; ?>)</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php 
                                        $counter = 1;
                                        $uniforms_total = 0;
                                        foreach ($uniforms_data as $uniform): 
                                            $uniforms_total += $uniform->amount;
                                        ?>
                                        <tr>
                                            <td><?php echo $counter++; ?></td>
                                            <td><?php echo date('Y-m-d', strtotime($uniform->created_date)); ?></td>
                                            <td><?php echo $uniform->name; ?></td>
                                            <td><?php echo $uniform->student_id; ?></td>
                                            <td><?php echo $uniform->description; ?></td>
                                            <td><?php echo $uniform->batch_num; ?></td>
                                            <td><?php echo number_format($uniform->discount, 2); ?></td>
                                            <td style="text-align: right;">
                                                <strong><?php echo number_format($uniform->amount, 2); ?></strong>
                                            </td>
                                        </tr>
                                        <?php endforeach; ?>
                                    </tbody>
                                    <tfoot>
                                        <tr style="font-weight: bold; background-color: #f5f5f5;">
                                            <td colspan="7" class="text-right">Total Uniforms:</td>
                                            <td style="text-align: right;">
                                                <?php echo number_format($uniforms_total, 2); ?>
                                            </td>
                                        </tr>
                                    </tfoot>
                                </table>
                            </div>

                        <?php elseif ($this->input->post('search') == 'search_filter'): ?>
                            <hr>
                            <div class="alert alert-info text-center">
                                <i class="fa fa-info-circle"></i> No records found for the selected criteria.
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
    </section>
</div>

<script>
function toggleFilterSections() {
    var viewMode = document.getElementById('view_mode').value;
    var consolidatedDates = document.getElementById('consolidated_dates');
    var brokenType = document.getElementById('broken_type');
    var brokenDates = document.getElementById('broken_dates');
    
    // Hide all sections first
    consolidatedDates.style.display = 'none';
    brokenType.style.display = 'none';
    brokenDates.style.display = 'none';
    
    // Show the appropriate sections
    if (viewMode === 'consolidated') {
        consolidatedDates.style.display = 'block';
    } else if (viewMode === 'broken') {
        brokenType.style.display = 'block';
    }
}

function toggleBrokenDateFilters() {
    var brokenType = document.getElementById('broken_type_select').value;
    var brokenDates = document.getElementById('broken_dates');
    
    if (brokenType === 'fees' || brokenType === 'uniforms') {
        brokenDates.style.display = 'block';
    } else {
        brokenDates.style.display = 'none';
    }
}

// Initialize on page load
document.addEventListener('DOMContentLoaded', function() {
    toggleFilterSections();
    toggleBrokenDateFilters();
});
</script>