<style type="text/css"> 
.nav-tabs-custom>.nav-tabs>li.active {
    border-top-color: #faa21c;
}

/* Step 1: Ensure uniform filters are clickable */
.uniform-filters {
    pointer-events: auto;
    position: relative;
    z-index: 2;
}
</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'); ?>
        </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('stockmovement'); ?>
                        </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/collections'); ?>" 
                                      method="post" class="form-horizontal">
                                    <?php echo $this->customlib->getCSRF(); ?> 

                                    <div class="form-group">
                                        <!-- Inventory Item -->
                                        <div class="col-sm-4">
                                            <label>Inventory Item</label>
                                            <select id="inventory_item" name="inventory_item" 
                                                    class="form-control" onchange="toggleUniformFilters()">
                                                <option value="">Select Item</option>
                                                <option value="uniforms" <?php echo (isset($inventory_item) && $inventory_item == 'uniforms') ? 'selected' : ''; ?>>Uniforms</option>
                                                <option value="other" <?php echo (isset($inventory_item) && $inventory_item == 'other') ? 'selected' : ''; ?>>Other Inventory Items</option>
                                            </select>
                                        </div>

                                        <!-- Uniform From -->
                                        <div class="col-sm-4 uniform-filters" style="display:none;">
                                            <label>From Uniform</label>
                                            <select id="uniform_from" name="uniform_from" class="form-control">
                                                <option value="">-- Select Start --</option>
                                                <?php foreach($uniformlist as $u): ?>
                                                    <option value="<?php echo htmlspecialchars($u['itemdescript'], ENT_QUOTES); ?>" 
                                                        <?php echo (isset($uniform_from) && $uniform_from == $u['itemdescript']) ? 'selected' : '' ?>>
                                                        <?php echo $u['itemdescript']; ?>
                                                    </option>
                                                <?php endforeach; ?>
                                            </select>
                                        </div>

                                        <!-- Uniform To -->
                                        <div class="col-sm-4 uniform-filters" style="display:none;">
                                            <label>To Uniform</label>
                                            <select id="uniform_to" name="uniform_to" class="form-control">
                                                <option value="">-- Select End --</option>
                                                <?php foreach($uniformlist as $u): ?>
                                                    <option value="<?php echo htmlspecialchars($u['itemdescript'], ENT_QUOTES); ?>" 
                                                        <?php echo (isset($uniform_to) && $uniform_to == $u['itemdescript']) ? 'selected' : '' ?>>
                                                        <?php echo $u['itemdescript']; ?>
                                                    </option>
                                                <?php endforeach; ?>
                                            </select>
                                        </div>

                                        <!-- Warehouse -->
                                        <div class="col-sm-4">
                                            <label>Warehouse</label>
                                            <select id="warehouse" name="warehouse" class="form-control">
                                                <option value="">Select Warehouse</option>
                                                <?php foreach($warehouselist as $wh): ?>
                                                    <option value="<?php echo $wh['warehouse']; ?>" 
                                                        <?php echo (isset($warehouse_id) && $warehouse_id == $wh['warehouse']) ? 'selected' : '' ?>>
                                                        <?php echo $wh['warehouse']; ?>
                                                    </option>
                                                <?php endforeach; ?>
                                            </select>
                                        </div>
                                    </div>  

                                    <div class="form-group">
                                        <!-- Show all uniforms -->
                                        <div class="col-sm-4 uniform-filters" style="display:none;">
                                            <div class="checkbox">
                                                <input type="checkbox" id="show_all_uniforms" name="show_all_uniforms" 
                                                    <?php echo isset($show_all_uniforms) ? 'checked' : ''; ?>>
                                                <label for="show_all_uniforms">Show all Uniforms</label>
                                            </div>
                                        </div>

                                        <!-- Date From -->
                                        <div class="col-sm-4">
                                            <label><?php echo $this->lang->line('date_from'); ?></label>
                                            <div class="input-group">
                                                <input autofocus="" id="datefrom" name="date_from" type="text" 
                                                       class="form-control date"  
                                                       value="<?php echo set_value('date_from', isset($date_from) ? $date_from : date($this->customlib->getSchoolDateFormat())); ?>" 
                                                       readonly="readonly"/>
                                                <span class="input-group-addon calendar-trigger" data-target="#datefrom">
                                                    <i class="fa fa-calendar"></i>
                                                </span>
                                            </div>
                                        </div>

                                        <!-- Date To -->
                                        <div class="col-sm-4">
                                            <label><?php echo $this->lang->line('date_to'); ?></label>
                                            <div class="input-group">
                                                <input id="dateto" name="date_to" type="text" 
                                                       class="form-control date"  
                                                       value="<?php echo set_value('date_to', isset($date_to) ? $date_to : date($this->customlib->getSchoolDateFormat())); ?>" 
                                                       readonly="readonly"/>
                                                <span class="input-group-addon calendar-trigger" data-target="#dateto">
                                                    <i class="fa fa-calendar"></i>
                                                </span>
                                            </div>
                                        </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 checkbox-toggle pull-right">
                                                <i class="fa fa-search"></i> <?php echo $this->lang->line('search'); ?>
                                            </button>
                                        </div>
                                    </div>
                                </form>
                            </div>
                        </div>

                        <!-- Stock Movement Table -->
                        <?php if (isset($search) && $search == 'search_filter'): ?>
                        <div class="row">
                            <div class="col-md-12">
                                <table class="table table-bordered table-striped">
                                    <thead>
                                        <tr>
                                            <th>ID</th>
                                            <th>Date</th>
                                            <th>Item Code</th>
                                            <th>Description</th>
                                            <th>Warehouse</th>
                                            <th>Quantity In</th>
                                            <th>Quantity Out</th>
                                            <th>Quantity Left</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php 
                                        $grand_total = 0; 
                                        if (!empty($uniform_sales)): 
                                            foreach ($uniform_sales as $row): 
                                                $grand_total += isset($row->balance) ? $row->balance : 0;
                                        ?>
                                            <tr>
                                                <td><?php echo !empty($row->id) ? $row->id : ''; ?></td>
                                                <td><?php echo !empty($row->date) ? $row->date : ''; ?></td>
                                                <td><?php echo !empty($row->itemcode) ? $row->itemcode : ''; ?></td>
                                                <td><?php echo !empty($row->description) ? $row->description : ''; ?></td>
                                                <td><?php echo !empty($row->warehouse) ? $row->warehouse : ''; ?></td>
                                                <td><?php echo !empty($row->qnty_in) ? $row->qnty_in : '0'; ?></td>
                                                <td><?php echo !empty($row->qnty_out) ? $row->qnty_out : '0'; ?></td>
                                                <td><?php echo !empty($row->balance) ? $row->balance : '0'; ?></td>
                                            </tr>
                                        <?php endforeach; ?>
                                            <tr class="bg-gray">
                                                <td colspan="7" class="text-right"><strong>Total of Quantity Left:</strong></td>
                                                <td><strong><?php echo number_format($grand_total); ?></strong></td>
                                            </tr>
                                        <?php else: ?>
                                            <tr>
                                                <td colspan="8" class="text-center">No records found</td>
                                            </tr>
                                        <?php endif; ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                        <?php endif; ?>

                    </div>
                </div>
            </div>
        </div>
    </section>

<script>
function toggleUniformFilters() {
    var inventoryItem = document.getElementById('inventory_item').value;
    var uniformFilters = document.querySelectorAll('.uniform-filters');
    
    if (inventoryItem === 'uniforms') {
        uniformFilters.forEach(function(filter) {
            filter.style.display = 'block';
        });
    } else {
        uniformFilters.forEach(function(filter) {
            filter.style.display = 'none';
        });
    }
}

$(document).ready(function() {
    // Initialize datepickers
    $('.date').datepicker({
        autoclose: true,
        todayHighlight: true,
        format: 'yyyy-mm-dd' // adjust if your schoolDateFormat is different
    });

    // Trigger datepicker when calendar icon is clicked
    $('.calendar-trigger').on('click', function() {
        var target = $(this).data('target');
        $(target).datepicker('show');
    });
});
</script>
