<style type="text/css">
    .nav-tabs-custom>.nav-tabs>li.active {
        border-top-color: #faa21c;
    }
</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('outstandingfees'); ?></h3>
                    </div>
                    <div class="box-body">
<div class="row"> 
    <div class="col-md-12">
        <form id="reportForm" role="form" action="<?php echo site_url('admin/BIreports/outstanding_fees'); ?>" method="post" class="form-horizontal">
            <?php echo $this->customlib->getCSRF(); ?> 

            <!-- Existing filters -->
            <div class="form-group">
                <!-- Report Type -->
                <div class="col-sm-4">
                    <label>Report Type</label>
                    <select id="report_type" name="report_type" class="form-control">
                        <option value="summary" <?= ($report_type == 'summary') ? 'selected' : '' ?>>Summary</option>
                        <option value="detailed" <?= ($report_type == 'detailed') ? 'selected' : '' ?>>Detailed</option>
                    </select>
                </div>

                <!-- Date From -->
                <div class="col-sm-4">
                    <label><?php echo $this->lang->line('date_from'); ?></label>
                    <input id="datefrom" name="date_from" type="text" class="form-control date"  
                           value="<?php echo set_value('date_from', date($this->customlib->getSchoolDateFormat())); ?>" readonly="readonly"/>
                </div>

                <!-- Date To -->
                <div class="col-sm-4">
                    <label><?php echo $this->lang->line('date_to'); ?></label>
                    <input id="dateto" name="date_to" type="text" class="form-control date"  
                           value="<?php echo set_value('date_to', date($this->customlib->getSchoolDateFormat())); ?>" readonly="readonly"/>
                </div>
            </div>

            <!-- New filters: Overall + Broken Down -->
            <div class="form-group">
                <!-- View Mode -->
                <div class="col-sm-4">
                    <label>View Mode</label>
                    <select id="view_mode" name="view_mode" class="form-control" onchange="toggleBreakdown()">
                        <option value="overall" <?= (isset($view_mode) && $view_mode == 'overall') ? 'selected' : '' ?>>Overall</option>
                        <option value="broken" <?= (isset($view_mode) && $view_mode == 'broken') ? 'selected' : '' ?>>Broken Down</option>
                    </select>
                </div>

                <!-- Class (hidden until 'Broken Down') -->
                <div class="col-sm-4" id="class_filter" style="display:none;">
                    <label>Level From</label>
                    <select id="level_id" name="level_id" class="form-control">
                        <option value="">-- Select Level --</option>
                        <?php foreach ($levellist as $level): ?>
                            <option value="<?php echo $level['id']; ?>" <?= (isset($level_id) && $level_id == $level['id']) ? 'selected' : '' ?>>
                                <?php echo $level['class']; ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>

                <!-- Level (hidden until 'Broken Down') -->
                <div class="col-sm-4" id="level_filter" style="display:none;">
                    <label>Level To</label>
                    <select id="level_id" name="level_id" class="form-control">
                        <option value="">-- Select Level --</option>
                        <?php foreach ($levellist as $level): ?>
                            <option value="<?php echo $level['id']; ?>" <?= (isset($level_id) && $level_id == $level['id']) ? 'selected' : '' ?>>
                                <?php echo $level['class']; ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
            </div>

            <!-- Search Button -->
            <div class="form-group">
                <div class="col-sm-12">
                    <button type="submit" name="search" value="search_filter" class="btn btn-primary btn-sm pull-right">
                        <i class="fa fa-search"></i> <?php echo $this->lang->line('search'); ?>
                    </button>
                </div>
            </div>
        </form>
    <?php if (!empty($outstanding_fees)): ?>
    <div class="box box-info">
        <div class="box-header with-border">
            <h3 class="box-title">Outstanding Fees Summary</h3>
        </div>
        <div class="box-body table-responsive">
            <table class="table table-striped table-bordered">
                <thead>
                    <tr style="background-color:#f4f4f4;">
                        <th>Date</th>
                        <th>Student Name</th>
                        <th>Level</th>
                        <th>Owing Balance (<?php echo $currency_symbol; ?>)</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($outstanding_fees as $row): ?>
                        <tr>
                            <td><?php echo date('d M Y', strtotime($row['Date'])); ?></td>
                            <td><?php echo $row['Student_Name']; ?></td>
                            <td><?php echo $row['Level']; ?></td>
                            <td><?php echo number_format($row['Owing_Balance'], 2); ?></td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
                <tfoot>
                    <tr style="font-weight:bold; background-color:#e6f2ff;">
                        <td colspan="3" class="text-right">Grand Total:</td>
                        <td><?php echo number_format($grand_total_outstanding, 2); ?></td>
                    </tr>
                </tfoot>
            </table>
        </div>
    </div>
<?php else: ?>
    <div class="alert alert-info text-center">No outstanding fees found for the selected period.</div>
<?php endif; ?>

    </div>
</div>

