<?php $__env->startSection('title', 'Inventory Stock Level Report'); ?>
<?php $__env->startSection('content'); ?>

<div class="container-fluid py-3">
    <div class="d-flex justify-content-end mb-3">
        <a href="<?php echo e(route('home')); ?>" class="btn-back">
            <i class="fas fa-arrow-left me-2"></i> Back to Dashboard
        </a>
    </div>

    <div class="card shadow-sm">
        <div class="card-header bg-primary text-white py-3">
            <h5 class="mb-0"><i class="fas fa-boxes me-2"></i> Inventory Stock Level Report</h5>
        </div>

        <div class="card-body">
            <?php if(session('success')): ?>
                <div class="alert alert-success"><?php echo e(session('success')); ?></div>
            <?php endif; ?>

            <?php if($errors->any()): ?>
                <div class="alert alert-danger">
                    <ul class="mb-0">
                        <?php $__currentLoopData = $errors->all(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $error): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                            <li><?php echo e($error); ?></li>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                    </ul>
                </div>
            <?php endif; ?>

            <form action="<?php echo e(route('inventoryStockLevel.generate')); ?>" method="POST" id="stockLevelForm">
                <?php echo csrf_field(); ?>
                <div class="row mb-3">
                    <div class="col-md-3">
                        <label class="form-label"><strong>Item From</strong></label>
                        <select class="form-control select2" name="item_from" id="item_from" required>
                            <option value="" disabled selected>Select Item</option>
                            <?php $__currentLoopData = $items; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $item): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($item->code); ?>" <?php echo e((isset($itemFrom) && $itemFrom == $item->code) ? 'selected' : ''); ?>>
                                    <?php echo e($item->code); ?> - <?php echo e($item->description1); ?>

                                </option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                    </div>
                    <div class="col-md-3">
                        <label class="form-label"><strong>Item To</strong></label>
                        <select class="form-control select2" name="item_to" id="item_to" required>
                            <option value="" disabled selected>Select Item</option>
                            <?php $__currentLoopData = $items; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $item): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($item->code); ?>" <?php echo e((isset($itemTo) && $itemTo == $item->code) ? 'selected' : ''); ?>>
                                    <?php echo e($item->code); ?> - <?php echo e($item->description1); ?>

                                </option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                    </div>
                    <div class="col-md-2">
                        <label class="form-label"><strong>Warehouse</strong></label>
                        <select class="form-control select2" name="warehouse_id" id="warehouse_id">
                            <option value="">All Warehouses</option>
                            <?php $__currentLoopData = $warehouses; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $warehouse): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($warehouse->id); ?>" <?php echo e((isset($warehouseFilter) && $warehouseFilter == $warehouse->id) ? 'selected' : ''); ?>>
                                    <?php echo e($warehouse->description); ?>

                                </option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                    </div>
                    <div class="col-md-2">
                        <label class="form-label"><strong>Date From</strong></label>
                        <input type="date" class="form-control" name="date_from" id="date_from" value="<?php echo e(isset($dateFrom) ? $dateFrom : $currentDate); ?>" required>
                    </div>
                    <div class="col-md-2">
                        <label class="form-label"><strong>Date To</strong></label>
                        <input type="date" class="form-control" name="date_to" id="date_to" value="<?php echo e(isset($dateTo) ? $dateTo : $currentDate); ?>" required>
                    </div>

                </div>
                <div class="d-flex justify-content-end">
                    <button type="submit" class="btn btn-primary">Generate Report</button>
                    <button type="button" class="btn btn-secondary ml-2" onclick="resetForm()">Refresh</button>
                </div>




            </form>
        </div>
    </div>

    <?php if(isset($stockData) && count($stockData) > 0): ?>
        <!-- PDF Download Button -->
        <div class="d-flex justify-content-end mt-4">
            <form action="<?php echo e(route('inventoryStockLevel.downloadPdf')); ?>" method="POST" class="d-inline">
                <?php echo csrf_field(); ?>
                <input type="hidden" name="item_from" value="<?php echo e($itemFrom); ?>">
                <input type="hidden" name="item_to" value="<?php echo e($itemTo); ?>">
                <input type="hidden" name="date_from" value="<?php echo e($dateFrom); ?>">
                <input type="hidden" name="date_to" value="<?php echo e($dateTo); ?>">
                <input type="hidden" name="warehouse_id" value="<?php echo e($warehouseFilter); ?>">
                <button type="submit" class="btn btn-process-all btn-sm">
                    <i class="fas fa-download me-1"></i> DOWNLOAD PDF
                </button>
            </form>
        </div>

        <!-- Display Stock Level Data -->
        <div class="card shadow-sm mt-4">
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-bordered table-hover">
                        <thead class="table-light">
                            <tr>
                                <th>Item Code</th>
                                <th>Item Description</th>
                                <th>Warehouse</th>
                                <th>Category</th>
                                <th class="text-end">Qty Available</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                                $currentItem = null;
                                $itemShown = false;
                            ?>

                            <?php $__currentLoopData = $stockData; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $data): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <?php if($currentItem != $data['item']->id): ?>
                                    <?php if($currentItem !== null && $itemShown): ?>
                                        <tr class="table-active">
                                            <td colspan="4" class="text-end fw-bold">Total for <?php echo e($currentItemCode); ?> - <?php echo e($currentItemDescription); ?></td>
                                            <td class="text-end fw-bold"><?php echo e(number_format($itemTotals[$currentItem], 2)); ?></td>
                                        </tr>
                                    <?php endif; ?>

                                    <?php
                                        $currentItem = $data['item']->id;
                                        $currentItemCode = $data['item']->code;
                                        $currentItemDescription = $data['item']->description1;
                                        $itemShown = true;
                                    ?>
                                <?php endif; ?>

                                <tr>
                                    <td><?php echo e($data['item']->code); ?></td>
                                    <td><?php echo e($data['item']->description1); ?></td>
                                    <td><?php echo e($data['warehouse']->description ?? 'N/A'); ?></td>
                                    <td><?php echo e($data['category']->name ?? 'N/A'); ?></td>
                                    <td class="text-end"><?php echo e(number_format($data['available_quantity'], 2)); ?></td>
                                </tr>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>

                            <?php if($currentItem !== null && $itemShown): ?>
                                <tr class="table-active">
                                    <td colspan="4" class="text-end fw-bold">Total for <?php echo e($currentItemCode); ?> - <?php echo e($currentItemDescription); ?></td>
                                    <td class="text-end fw-bold"><?php echo e(number_format($itemTotals[$currentItem], 2)); ?></td>
                                </tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <div class="mt-4 text-center">
            <p class="text-muted">Copyright © Melsoft (Private) Limited</p>
        </div>

    <?php elseif(isset($stockData) && count($stockData) == 0): ?>
        <div class="card shadow-sm mt-4">
            <div class="card-body">
                <div class="alert alert-info mt-3">
                    No inventory transactions found for the selected criteria.
                </div>
            </div>
        </div>
    <?php endif; ?>
</div>

<?php $__env->stopSection(); ?>

<?php $__env->startSection('styles'); ?>
<style>
.btn-back {
    text-decoration: none;
    color: #1AB9D9;
    font-weight: 500;
    display: inline-flex;
    align-items: center;
    padding: 5px 15px;
    border-radius: 4px;
    transition: all 0.3s;
}
.btn-back:hover {
    background-color: rgba(26, 185, 217, 0.1);
    text-decoration: none;
}
.card {
    border-radius: 8px;
    overflow: hidden;
    border: none;
    box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.1);
}
.card-header {
    border-bottom: 1px solid rgba(0,0,0,0.1);
    padding: 12px 20px;
}
.table th {
    background-color: #f8f9fa;
}
.table-responsive {
    overflow-x: auto;
}
.table-active {
    background-color: rgba(0,0,0,.05) !important;
}
.btn-process-all {
    background-color: #ffffff !important;
    color: #1AB9D9 !important;
    border: 1px solid rgba(26,185,217,0.35) !important;
    font-weight: 600 !important;
    padding: 7px 14px !important;
    box-shadow: none !important;
    line-height: 1 !important;
}
.btn-process-all:hover {
    background-color: #f5fbfc !important;
    color: #1498b0 !important;
    border-color: rgba(20,152,176,0.45) !important;
}

@media print {
    .btn-back, .card-header .btn, form {
        display: none !important;
    }
    .card {
        box-shadow: none;
        border: 1px solid #ddd;
    }
    .table-responsive {
        overflow-x: visible;
    }
}
</style>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('scripts'); ?>
<script>
    // Initialize Select2 for dropdowns
    $(document).ready(function() {
        $('.select2').select2({
            placeholder: "Select Option",
            allowClear: true
        });

        // Set today's date as default if not already set
        var today = new Date().toISOString().split('T')[0];
        if (!$('#date_from').val()) {
            $('#date_from').val(today);
        }
        if (!$('#date_to').val()) {
            $('#date_to').val(today);
        }
    });

    function resetForm() {
        // Reset the select2 dropdowns
        $('#item_from').val(null).trigger('change');
        $('#item_to').val(null).trigger('change');
        $('#warehouse_id').val(null).trigger('change');

        // Set the date inputs to today
        var today = new Date().toISOString().split('T')[0];
        $('#date_from').val(today);
        $('#date_to').val(today);
    }
</script>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.master', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH H:\systems\Quanta\demo\resources\views/reports/inventory-stock-level.blade.php ENDPATH**/ ?>