

<?php $__env->startSection('title', 'Edit Invoice ' . $invoice->invoice_number); ?>

<?php $__env->startSection('content'); ?>
<div class="container-fluid">
    <div class="row">
        <!-- Left Panel - Customer & Items -->
        <div class="col-md-8">
            <div class="card">
                <div class="card-header bg-warning text-dark">
                    <h5 class="mb-0"><i class="fas fa-edit"></i> Edit Invoice <?php echo e($invoice->invoice_number); ?></h5>
                </div>
                <div class="card-body">
                    <form id="editForm" action="<?php echo e(route('factory-pos.update', $invoice)); ?>" method="POST">
                        <?php echo csrf_field(); ?>
                        <?php echo method_field('PUT'); ?>

                        <!-- Customer Selection -->
                        <div class="row mb-3">
                            <div class="col-md-6">
                                <label class="form-label fw-bold">Customer</label>
                                <select class="form-select form-select-lg" name="customer_id" required>
                                    <option value="">Select Customer</option>
                                    <?php $__currentLoopData = $customers; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $customer): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <option value="<?php echo e($customer->id); ?>" <?php echo e($customer->id == $invoice->customer_account_id ? 'selected' : ''); ?>>
                                            <?php echo e($customer->name); ?>

                                        </option>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </select>
                            </div>
                            <div class="col-md-6">
                                <label class="form-label fw-bold">Supervising Employee</label>
                                <select class="form-select form-select-lg" name="supervising_employee_id" required>
                                    <option value="">Select Employee</option>
                                    <?php $__currentLoopData = $employees; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $employee): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <option value="<?php echo e($employee->id); ?>" <?php echo e($employee->id == $invoice->supervising_employee_account_id ? 'selected' : ''); ?>>
                                            <?php echo e($employee->name); ?>

                                        </option>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </select>
                            </div>
                        </div>

                        <!-- Discount Section -->
                        <div class="row mb-3">
                            <div class="col-md-6">
                                <div class="card border-primary">
                                    <div class="card-header bg-primary text-white">
                                        <h5 class="card-title mb-0"><i class="fas fa-percent"></i> Discount</h5>
                                    </div>
                                    <div class="card-body">
                                        <div class="row">
                                            <div class="col-md-6">
                                                <label class="form-label">Discount Amount ($)</label>
                                                <input type="number" class="form-control" name="discount_amount" id="discountAmount"
                                                       value="<?php echo e(old('discount_amount', $invoice->discount_amount ?? 0)); ?>" min="0" step="0.01" placeholder="0.00">
                                            </div>
                                            <div class="col-md-6">
                                                <label class="form-label">Discount Percentage (%)</label>
                                                <input type="number" class="form-control" name="discount_percentage" id="discountPercentage"
                                                       value="<?php echo e(old('discount_percentage', $invoice->discount_percentage ?? '')); ?>" min="0" max="100" step="0.01" placeholder="0.00">
                                                <small class="form-text text-muted">Enter percentage or amount (not both)</small>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="card border-success">
                                    <div class="card-header bg-success text-white">
                                        <h5 class="card-title mb-0"><i class="fas fa-calculator"></i> Invoice Summary</h5>
                                    </div>
                                    <div class="card-body">
                                        <div class="row text-center">
                                            <div class="col-4">
                                                <span>Items:</span>
                                                <span id="summaryItems" class="fw-bold">0</span>
                                            </div>
                                            <div class="col-4">
                                                <span>Total Quantity:</span>
                                                <span id="summaryQuantity" class="fw-bold">0</span>
                                            </div>
                                            <div class="col-4">
                                                <span>Total Amount:</span>
                                                <span id="summaryTotal" class="fw-bold text-primary fs-5">$0.00</span>
                                            </div>
                                        </div>
                                        <hr>
                                        <div class="row text-center">
                                            <div class="col-6">
                                                <span>Discount:</span>
                                                <span id="summaryDiscount" class="fw-bold text-danger">-$0.00</span>
                                            </div>
                                            <div class="col-6">
                                                <span class="fw-bold">Net Total:</span>
                                                <span id="summaryNetTotal" class="fw-bold text-success fs-4">$0.00</span>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <!-- Current Items -->
                        <div class="mb-3">
                            <h6 class="fw-bold">Current Items</h6>
                            <div class="table-responsive">
                                <table class="table table-sm">
                                    <thead class="table-light">
                                        <tr>
                                            <th>Item</th>
                                            <th>Qty</th>
                                            <th>Price</th>
                                            <th>Total</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php $__currentLoopData = $invoice->items; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $item): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                            <tr>
                                                <td><?php echo e($item->item->name ?? 'N/A'); ?></td>
                                                <td><?php echo e($item->ordered_quantity); ?></td>
                                                <td>$<?php echo e(number_format($item->ordered_unit_price, 2)); ?></td>
                                                <td>$<?php echo e(number_format($item->ordered_total, 2)); ?></td>
                                            </tr>
                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>

                        <!-- Edit Warning -->
                        <div class="alert alert-warning">
                            <i class="fas fa-exclamation-triangle"></i>
                            <strong>Note:</strong> You can only edit invoices that are still pending warehouse action.
                            Once an invoice is picked or processed, it cannot be modified.
                        </div>
                    </form>
                </div>
            </div>
        </div>

        <!-- Right Panel - Actions -->
        <div class="col-md-4">
            <!-- Invoice Summary -->
            <div class="card mb-3">
                <div class="card-header bg-light">
                    <h6 class="mb-0 fw-bold"><i class="fas fa-info-circle"></i> Invoice Summary</h6>
                </div>
                <div class="card-body">
                    <div class="d-flex justify-content-between mb-2">
                        <span>Invoice #:</span>
                        <span class="fw-bold"><?php echo e($invoice->invoice_number); ?></span>
                    </div>
                    <div class="d-flex justify-content-between mb-2">
                        <span>Status:</span>
                        <span>
                            <?php if($invoice->status === 'warehouse_pending'): ?>
                                <span class="badge bg-warning">Warehouse Pending</span>
                            <?php elseif($invoice->status === 'picked'): ?>
                                <span class="badge bg-info">Picked</span>
                            <?php elseif($invoice->status === 'delivered'): ?>
                                <span class="badge bg-primary">Delivered</span>
                            <?php elseif($invoice->status === 'completed'): ?>
                                <span class="badge bg-success">Completed</span>
                            <?php endif; ?>
                        </span>
                    </div>
                    <div class="d-flex justify-content-between mb-2">
                        <span>Items:</span>
                        <span class="fw-bold"><?php echo e($invoice->items->count()); ?></span>
                    </div>
                    <div class="d-flex justify-content-between mb-2">
                        <span>Total Quantity:</span>
                        <span class="fw-bold"><?php echo e($invoice->items->sum('ordered_quantity')); ?></span>
                    </div>
                    <hr class="my-2">
                    <div class="d-flex justify-content-between">
                        <span class="fw-bold">Total Amount:</span>
                        <span class="fw-bold text-primary fs-5">$<?php echo e(number_format($invoice->total_ordered_amount, 2)); ?></span>
                    </div>
                </div>
            </div>

            <!-- Actions -->
            <div class="card">
                <div class="card-body">
                    <?php if($invoice->isPendingWarehouse()): ?>
                        <button type="button" class="btn btn-success btn-lg w-100 mb-2" onclick="saveChanges()" id="saveBtn">
                            <i class="fas fa-save"></i> Save Changes
                        </button>
                    <?php else: ?>
                        <div class="alert alert-info mb-2">
                            <i class="fas fa-lock"></i> This invoice cannot be edited as it has already been processed.
                        </div>
                    <?php endif; ?>

                    <a href="<?php echo e(route('factory-pos.show', $invoice)); ?>" class="btn btn-outline-primary w-100 mb-2">
                        <i class="fas fa-eye"></i> View Invoice
                    </a>

                    <a href="<?php echo e(route('factory-pos.index')); ?>" class="btn btn-outline-secondary w-100">
                        <i class="fas fa-arrow-left"></i> Back to List
                    </a>
                </div>
            </div>

            <?php if(!$invoice->isPendingWarehouse()): ?>
                <!-- Status Information -->
                <div class="card mt-3">
                    <div class="card-header bg-light">
                        <h6 class="mb-0 fw-bold"><i class="fas fa-history"></i> Processing History</h6>
                    </div>
                    <div class="card-body">
                        <?php if($invoice->warehouse_picked_at): ?>
                            <div class="mb-2">
                                <i class="fas fa-box text-info"></i>
                                <strong>Warehouse Picked:</strong><br>
                                <small class="text-muted"><?php echo e($invoice->warehouse_picked_at->format('M d, Y H:i')); ?></small>
                            </div>
                        <?php endif; ?>

                        <?php if($invoice->delivered_at): ?>
                            <div class="mb-2">
                                <i class="fas fa-truck text-success"></i>
                                <strong>Delivered:</strong><br>
                                <small class="text-muted"><?php echo e($invoice->delivered_at->format('M d, Y H:i')); ?></small>
                            </div>
                        <?php endif; ?>

                        <?php if($invoice->deliveryReceipts->count() > 0): ?>
                            <div>
                                <i class="fas fa-receipt text-primary"></i>
                                <strong>Delivery Receipts:</strong> <?php echo e($invoice->deliveryReceipts->count()); ?>

                            </div>
                        <?php endif; ?>
                    </div>
                </div>
            <?php endif; ?>
        </div>
    </div>
</div>
<?php $__env->stopSection(); ?>

<?php $__env->startPush('scripts'); ?>
<script>
function updateTotals() {
    // For edit form, we use the existing invoice data
    const grandTotal = <?php echo e($invoice->total_ordered_amount ?? 0); ?>;
    const discountAmount = parseFloat($('#discountAmount').val()) || 0;
    const discountPercentage = parseFloat($('#discountPercentage').val()) || 0;

    let actualDiscount = discountAmount;
    if (discountPercentage > 0) {
        actualDiscount = (grandTotal * discountPercentage) / 100;
        $('#discountAmount').val(actualDiscount.toFixed(2));
    }

    const netTotal = grandTotal - actualDiscount;

    $('#summaryTotal').text('$' + grandTotal.toFixed(2));
    $('#summaryDiscount').text('-$' + actualDiscount.toFixed(2));
    $('#summaryNetTotal').text('$' + netTotal.toFixed(2));
    // Items and quantity are static in edit form for now
    $('#summaryItems').text('<?php echo e($invoice->items->count()); ?>');
    $('#summaryQuantity').text('<?php echo e($invoice->items->sum("ordered_quantity")); ?>');
}

function saveChanges() {
    // Validate form
    const customerId = $('[name=customer_id]').val();
    const employeeId = $('[name=supervising_employee_id]').val();

    if (!customerId) {
        alert('Please select a customer');
        $('[name=customer_id]').focus();
        return;
    }

    if (!employeeId) {
        alert('Please select a supervising employee');
        $('[name=supervising_employee_id]').focus();
        return;
    }

    // Disable save button
    $('#saveBtn').prop('disabled', true).html('<i class="fas fa-spinner fa-spin"></i> Saving...');

    // Submit form
    $('#editForm').submit();
}

// Discount input event listeners
$('#discountAmount, #discountPercentage').on('input', function() {
    // Clear the other field when one is entered
    if ($(this).attr('id') === 'discountAmount' && $(this).val()) {
        $('#discountPercentage').val('');
    } else if ($(this).attr('id') === 'discountPercentage' && $(this).val()) {
        $('#discountAmount').val('');
    }
    updateTotals();
});

// Initialize totals on page load
$(document).ready(function() {
    updateTotals();
});

// Note: In a real implementation, you might want to allow editing of items as well,
// but for simplicity, this edit form only allows changing customer and supervisor
</script>
<?php $__env->stopPush(); ?>

<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\laragon\www\habibcables-ERP\resources\views\factory-pos\edit.blade.php ENDPATH**/ ?>