<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 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('stocklevels'); ?></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/sales'); ?>" method="post" class="form-horizontal">
                                <?php echo $this->customlib->getCSRF(); ?> 

                                <!-- Hidden flag to indicate user pressed Search -->
                                <input type="hidden" name="search_clicked" id="search_clicked" value="<?php echo isset($_POST['search_clicked']) ? $_POST['search_clicked'] : '0'; ?>">

                                <div class="form-group">
                                    <!-- Inventory Item Filter -->
                                    <div class="col-sm-4">
                                        <label>Inventory Item</label>
                                        <select id="inventory_item" name="inventory_item" class="form-control">
                                            <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>

                                    <!-- Warehouse Filter -->
                                    <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 class="col-sm-4"></div>
                                </div>

                                <!-- Uniform-specific options -->
                                <div class="form-group" id="uniformOptions" style="display: none;">
                                    <div class="col-sm-4">
                                        <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>

                                    <div class="col-sm-4">
                                        <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>

                                    <div class="col-sm-4" style="display:flex; align-items:center;">
                                        <div class="checkbox" style="margin:0;">
                                            <label style="font-weight:normal;">
                                                <input type="checkbox" id="show_all" name="show_all" value="1" <?php echo !empty($_POST['show_all']) ? 'checked' : ''; ?>>
                                                &nbsp;Show All Uniforms
                                            </label>
                                        </div>
                                    </div>
                                </div>

                                <!-- Other Inventory Items options -->
                                <div class="form-group" id="otherItemOptions" style="display: none;">
                                    <div class="col-sm-4">
                                        <label>From Item</label>
                                        <select id="other_item_from" name="other_item_from" class="form-control">
                                            <option value="">-- Select Start --</option>
                                            <?php foreach($itemlist as $i): ?>
                                                <option value="<?php echo htmlspecialchars($i['item_id'], ENT_QUOTES); ?>"
                                                    <?php echo (isset($other_item_from) && $other_item_from == $i['item_id']) ? 'selected' : '' ?>>
                                                    <?php echo $i['description']; ?>
                                                </option>
                                            <?php endforeach; ?>
                                        </select>
                                    </div>

                                    <div class="col-sm-4">
                                        <label>To Item</label>
                                        <select id="other_item_to" name="other_item_to" class="form-control">
                                            <option value="">-- Select End --</option>
                                            <?php foreach($itemlist as $i): ?>
                                                <option value="<?php echo htmlspecialchars($i['item_id'], ENT_QUOTES); ?>"
                                                    <?php echo (isset($other_item_to) && $other_item_to == $i['item_id']) ? 'selected' : '' ?>>
                                                    <?php echo $i['description']; ?>
                                                </option>
                                            <?php endforeach; ?>
                                        </select>
                                    </div>

                                    <div class="col-sm-4" style="display:flex; align-items:center;">
                                        <div class="checkbox" style="margin:0;">
                                            <label style="font-weight:normal;">
                                                <input type="checkbox" id="show_all_other" name="show_all_other" value="1" <?php echo !empty($_POST['show_all_other']) ? 'checked' : ''; ?>>
                                                &nbsp;Show All Items
                                            </label>
                                        </div>
                                    </div>
                                </div>

                                <div class="form-group">
                                    <div class="col-sm-12">
                                        <button type="button" id="searchBtn" class="btn btn-primary btn-sm checkbox-toggle pull-right" disabled>
                                            <i class="fa fa-search"></i> <?php echo $this->lang->line('search'); ?>
                                        </button>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>  

                    <!-- Uniform Results Table -->
                    <div class="row" id="resultsSection" style="display: none;">
                        <div class="col-md-12">
                            <div class="box box-success">
                                <div class="box-header with-border">
                                    <h3 class="box-title">Uniform Stock Levels</h3>
                                </div>
                                <div class="box-body">
                                    <div class="table-responsive">
                                        <table class="table table-bordered table-striped">
                                            <thead>
                                                <tr>
                                                    <th>Item Number</th>
                                                    <th>Item Code</th>
                                                    <th>Description</th>
                                                    <th>Warehouse</th>
                                                    <th>Quantity On Hand</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php 
                                                $grand_total = 0; 
                                                if (!empty($uniform_sales)): 
                                                    foreach ($uniform_sales as $row): 
                                                        $grand_total += isset($row->qnty_on_hand) ? $row->qnty_on_hand : 0;
                                                ?>
                                                    <tr>
                                                        <td><?php echo !empty($row->id) ? $row->id : ''; ?></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_on_hand) ? $row->qnty_on_hand : '0'; ?></td>
                                                    </tr>
                                                <?php endforeach; ?>
                                                    <tr class="bg-gray">
                                                        <td colspan="4" class="text-right"><strong>Total Number Of Uniforms:</strong></td>
                                                        <td><strong><?php echo number_format($grand_total); ?></strong></td>
                                                    </tr>
                                                <?php else: ?>
                                                    <tr>
                                                        <td colspan="5" class="text-center">No records found</td>
                                                    </tr>
                                                <?php endif; ?>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Other Inventory Items Results Table -->
                    <div class="row" id="otherResultsSection" style="display: none;">
                        <div class="col-md-12">
                            <div class="box box-success">
                                <div class="box-header with-border">
                                    <h3 class="box-title">Other Inventory Items</h3>
                                </div>
                                <div class="box-body">
                                    <div class="table-responsive">
                                        <table class="table table-bordered table-striped">
                                            <thead>
                                                <tr>
                                                    <th>ID</th>
                                                    <th>Item ID</th>
                                                    <th>Description</th>
                                                    <th>Quantity</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php if (!empty($item_stock)): ?>
                                                    <?php foreach ($item_stock as $row):
                                                         $grand_total += isset($row->quantity) ? $row->quantity : 0;
                                                         ?>
                                                        <tr>
                                                            <td><?php echo $row->id; ?></td>
                                                            <td><?php echo $row->item_id; ?></td>
                                                            <td><?php echo $row->description; ?></td>
                                                            <td><?php echo $row->quantity; ?></td>
                                                        </tr>
                                                    <?php endforeach; ?>
                                                    <tr class="bg-gray">
                                                        <td colspan="3" class="text-right"><strong>Total Number Of Items:</strong></td>
                                                        <td><strong><?php echo number_format($grand_total); ?></strong></td>
                                                    </tr>

                                                <?php else: ?>
                                                    <tr>
                                                        <td colspan="4" class="text-center">No records found</td>
                                                    </tr>
                                                <?php endif; ?>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div> <!-- /.otherResultsSection -->
                </div>
            </div>
        </div>
    </div>
</section>

<script>
document.addEventListener('DOMContentLoaded', function() {
    const inventoryItemSelect = document.getElementById('inventory_item');
    const uniformOptionsDiv = document.getElementById('uniformOptions');
    const otherItemOptionsDiv = document.getElementById('otherItemOptions');
    const uniformFrom = document.getElementById('uniform_from');
    const uniformTo = document.getElementById('uniform_to');
    const showAllCheckbox = document.getElementById('show_all');
    const otherItemFrom = document.getElementById('other_item_from');
    const otherItemTo = document.getElementById('other_item_to');
    const showAllOtherCheckbox = document.getElementById('show_all_other');
    const warehouseSelect = document.getElementById('warehouse');
    const searchBtn = document.getElementById('searchBtn');
    const form = document.getElementById('reportForm');
    const resultsSection = document.getElementById('resultsSection');
    const otherResultsSection = document.getElementById('otherResultsSection');
    const searchClickedInput = document.getElementById('search_clicked');

    function toggleOptions() {
        if (inventoryItemSelect.value === 'uniforms') {
            uniformOptionsDiv.style.display = 'flex';
            otherItemOptionsDiv.style.display = 'none';
        } else if (inventoryItemSelect.value === 'other') {
            uniformOptionsDiv.style.display = 'none';
            otherItemOptionsDiv.style.display = 'flex';
        } else {
            uniformOptionsDiv.style.display = 'none';
            otherItemOptionsDiv.style.display = 'none';
        }
        updateSearchButton();
        searchClickedInput.value = '0';
        resultsSection.style.display = 'none';
        otherResultsSection.style.display = 'none';
    }

    function toggleRangeDisabled() {
        // For uniforms
        if (showAllCheckbox && showAllCheckbox.checked) {
            uniformFrom.disabled = true;
            uniformTo.disabled = true;
        } else {
            uniformFrom.disabled = false;
            uniformTo.disabled = false;
        }
        
        // For other items
        if (showAllOtherCheckbox && showAllOtherCheckbox.checked) {
            otherItemFrom.disabled = true;
            otherItemTo.disabled = true;
        } else {
            otherItemFrom.disabled = false;
            otherItemTo.disabled = false;
        }
    }

    function canSearch() {
        if (inventoryItemSelect.value === 'uniforms') {
            if (!warehouseSelect.value) return false;
            if (showAllCheckbox.checked) return true;
            if (uniformFrom.value && uniformTo.value) return true;
            return false;
        }
        if (inventoryItemSelect.value === 'other') {
            if (showAllOtherCheckbox.checked) return true;
            if (otherItemFrom.value && otherItemTo.value) return true;
            return false;
        }
        return false;
    }

    function updateSearchButton() {
        searchBtn.disabled = !canSearch();
    }

    searchBtn.addEventListener('click', function() {
        if (!canSearch()) return;
        searchClickedInput.value = '1';
        form.submit();
    });

    function onFilterChange() {
        searchClickedInput.value = '0';
        resultsSection.style.display = 'none';
        otherResultsSection.style.display = 'none';
        updateSearchButton();
    }

    // Initialize event listeners
    inventoryItemSelect.addEventListener('change', toggleOptions);
    
    if (showAllCheckbox) {
        showAllCheckbox.addEventListener('change', function() {
            toggleRangeDisabled();
            onFilterChange();
        });
    }
    
    if (showAllOtherCheckbox) {
        showAllOtherCheckbox.addEventListener('change', function() {
            toggleRangeDisabled();
            onFilterChange();
        });
    }
    
    if (uniformFrom) uniformFrom.addEventListener('change', onFilterChange);
    if (uniformTo) uniformTo.addEventListener('change', onFilterChange);
    if (otherItemFrom) otherItemFrom.addEventListener('change', onFilterChange);
    if (otherItemTo) otherItemTo.addEventListener('change', onFilterChange);
    warehouseSelect.addEventListener('change', onFilterChange);

    // Initialize the page
    toggleRangeDisabled();
    toggleOptions();
    updateSearchButton();

    // Show results after reload if search was clicked
    <?php if (!empty($_POST['search_clicked']) && $_POST['search_clicked'] == '1'): ?>
        <?php if (!empty($_POST['inventory_item']) && $_POST['inventory_item'] == 'uniforms'): ?>
            resultsSection.style.display = 'block';
        <?php elseif (!empty($_POST['inventory_item']) && $_POST['inventory_item'] == 'other'): ?>
            otherResultsSection.style.display = 'block';
        <?php endif; ?>
    <?php endif; ?>
});
</script>
