<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Warehouse Transfer</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/css/select2.min.css" rel="stylesheet" />
    <style>
       :root {
    --primary-color: #018ca3;      /* base blue-green */
    --primary-light: #3fb4c6;      /* lighter tint */
    --primary-dark: #016a7c;       /* darker shade */
    
    --secondary-color: #e0f5f8;    /* soft complementary background */
    --border-color: #b0dfe5;       /* subtle border matching theme */
    
    --success-color: #01a379;      /* green-tinted variation */
    --danger-color: #a30137;       /* reddish contrast to primary */
}

        body {
            background-color: #f5f7fa;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            font-size: 0.92rem;
        }

        .bg-primary-custom {
            background-color: var(--primary-color) !important;
        }

        .btn-primary-custom {
            background-color: var(--primary-color);
            border-color: var(--primary-dark);
            color: white;
        }

        .btn-primary-custom:hover {
            background-color: var(--primary-dark);
            border-color: var(--primary-dark);
            color: white;
        }

        .header-card {
            border-top: 3px solid var(--primary-color);
            border-radius: 0.75rem;
            box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.1);
            overflow: hidden;
        }

        .card-header-custom {
            border-radius: 0.75rem 0.75rem 0 0 !important;
            padding: 1rem;
            background: linear-gradient(to right, var(--primary-color), var(--primary-light));
        }

        .table-header {
            background-color: var(--primary-light);
            color: white;
        }

        .totals-panel {
            background-color: var(--secondary-color);
            border: 1px solid var(--border-color);
            border-radius: 0.5rem;
            padding: 12px;
        }

        .back-link {
            display: inline-block;
            margin-bottom: 15px;
            color: var(--primary-color);
            font-weight: 600;
        }

        .form-label {
            font-weight: 600;
            margin-bottom: 0.4rem;
            color: #495057;
            font-size: 0.9rem;
        }

        .form-control, .form-select {
            border-radius: 0.4rem;
            padding: 0.65rem;
            border: 1px solid var(--border-color);
            font-size: 0.9rem;
            transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
        }

        .form-control:focus, .form-select:focus {
            border-color: var(--primary-color);
            box-shadow: 0 0 0 0.2rem rgba(255, 140, 0, 0.25);
        }

        .section-header {
            font-weight: bold;
            font-size: 1.05rem;
            border-bottom: 2px solid var(--primary-color);
            padding-bottom: 0.6rem;
            margin-bottom: 1.2rem;
            color: var(--primary-color);
        }

        .action-icon {
            color: var(--danger-color);
            cursor: pointer;
            font-size: 1.1rem;
            transition: all 0.2s;
        }

        .action-icon:hover {
            color: #bd2130;
            transform: scale(1.1);
        }

        .compact-table th, .compact-table td {
            padding: 0.6rem 0.4rem;
            vertical-align: middle;
            font-size: 0.88rem;
        }

        .compact-table thead th {
            background-color: var(--primary-light);
            color: white;
            font-weight: 600;
            position: sticky;
            top: 0;
            font-size: 0.9rem;
        }

        .add-line-btn {
            margin-top: 0.8rem;
            padding: 0.4rem 0.8rem;
            font-size: 0.9rem;
        }

        .table-hover tbody tr:hover {
            background-color: rgba(255, 140, 0, 0.05);
        }

        .alert-container {
            position: fixed;
            top: 140px;
            left: 50%;
            transform: translateX(-50%);
            z-index: 1050;
            width: 100%;
            max-width: 550px;
            padding: 0 12px;
        }

        .scrollable-section {
            max-height: 450px;
            overflow-y: auto;
            border: 1px solid var(--border-color);
            border-radius: 0.4rem;
            padding: 12px;
            background: white;
            margin-bottom: 15px;
        }

        .line-item-row td {
            padding: 8px 6px;
        }

        .form-control-sm {
            padding: 0.45rem;
            font-size: 0.85rem;
        }

        .form-select-sm {
            padding: 0.45rem 2rem 0.45rem 0.65rem;
            font-size: 0.85rem;
        }

        .fixed-header {
            position: sticky;
            top: 0;
            background: white;
            z-index: 10;
            padding-bottom: 12px;
            margin-bottom: 8px;
        }

        .table-wrapper {
            overflow-x: auto;
        }

        .settings-icon {
            position: absolute;
            top: 18px;
            right: 18px;
            font-size: 1.1rem;
            color: white;
            cursor: pointer;
            z-index: 100;
        }

        .select2-container--default .select2-results__option--highlighted[aria-selected] {
            background-color: var(--primary-color);
        }

        .select2-container--default .select2-results__option[aria-selected=true] {
            background-color: var(--primary-light);
        }
        
        /* Compact textareas */
        .compact-table textarea.form-control-sm {
            height: 36px;
            resize: vertical;
            min-height: 36px;
        }
        
        /* Smaller number inputs */
        input[type="number"].form-control-sm {
            padding: 0.4rem;
        }
        
        /* Updated column widths */
        .compact-table th:nth-child(1), .compact-table td:nth-child(1) { width: 3%; }        /* # */
        .compact-table th:nth-child(2), .compact-table td:nth-child(2) { width: 12%; }       /* Item */
        .compact-table th:nth-child(3), .compact-table td:nth-child(3) { width: 20%; }       /* Item Description */
        .compact-table th:nth-child(4), .compact-table td:nth-child(4) { width: 20%; }       /* Reference */
        .compact-table th:nth-child(5), .compact-table td:nth-child(5) { width: 8%; }        /* UOM */
        .compact-table th:nth-child(6), .compact-table td:nth-child(6) { width: 10%; }       /* Available From */
        .compact-table th:nth-child(7), .compact-table td:nth-child(7) { width: 10%; }       /* Quantity */
        .compact-table th:nth-child(8), .compact-table td:nth-child(8) { width: 10%; }       /* Available To */
        .compact-table th:nth-child(9), .compact-table td:nth-child(9) { width: 7%; }        /* Action */

        /* Responsive adjustments */
        @media (max-width: 1700px) {
            .compact-table th:nth-child(3), .compact-table td:nth-child(3) { width: 18%; }   /* Item Description */
        }
    </style>
</head>
<body>
    <div class="container-fluid py-3">
        <a href="{{ route('home') }}" class="back-link">
            <i class="fas fa-arrow-left me-2"></i> Back to Dashboard
        </a>

        <div class="card header-card">
            <div class="card-header bg-primary-custom text-white card-header-custom">
                <h4 class="mb-0"><i class="fas fa-truck-loading me-2"></i> Warehouse Transfer</h4>
                <div class="text-light mt-1 fw-light" style="font-size: 0.85rem;">
                    Document #: WT-{{ str_pad($nextId, 6, '0', STR_PAD_LEFT) }}
                </div>
            </div>

            <div class="card-body">
                <form method="POST" action="{{ route('warehouse.transfer.store') }}" id="transferForm">
                    @csrf
                    <input type="hidden" name="document_number" value="WT-{{ str_pad($nextId, 6, '0', STR_PAD_LEFT) }}">

                    <div class="row mb-3">
                        <!-- Transfer Information -->
                        <div class="col-md-6">
                            <div class="section-header mb-3">
                                <i class="fas fa-info-circle me-2"></i> Transfer Information
                            </div>

                            <div class="mb-2">
                                <label class="form-label">Transfer Date <span class="text-danger">*</span></label>
                                <input type="date" name="transfer_date" class="form-control"
                                       value="{{ old('transfer_date', now()->format('Y-m-d')) }}" required>
                            </div>

                            <div class="mb-2">
                                <label class="form-label">From Warehouse <span class="text-danger">*</span></label>
                                <select name="from_warehouse_id" class="form-select" id="fromWarehouseSelect" required>
                                    <option value="">Select Warehouse</option>
                                    @foreach($warehouses as $warehouse)
                                        <option value="{{ $warehouse->id }}">{{ $warehouse->code }} - {{ $warehouse->description }}</option>
                                    @endforeach
                                </select>
                            </div>

                            <div class="mb-2">
                                <label class="form-label">To Warehouse <span class="text-danger">*</span></label>
                                <select name="to_warehouse_id" class="form-select" id="toWarehouseSelect" required>
                                    <option value="">Select Warehouse</option>
                                    @foreach($warehouses as $warehouse)
                                        <option value="{{ $warehouse->id }}">{{ $warehouse->code }} - {{ $warehouse->description }}</option>
                                    @endforeach
                                </select>
                            </div>
                        </div>

                        <!-- Document Information -->
                        <div class="col-md-6">
                            <div class="section-header mb-3">
                                <i class="fas fa-file-alt me-2"></i> Document Information
                            </div>

                            <div class="mb-2">
                                <label class="form-label">Document Number</label>
                                <input type="text" class="form-control" value="WT-{{ str_pad($nextId, 6, '0', STR_PAD_LEFT) }}" readonly>
                            </div>

                            <div class="mb-2">
                                <label class="form-label">Reference Number</label>
                                <input type="text" name="reference_number" class="form-control" value="{{ old('reference_number') }}">
                            </div>

                            <div class="mb-2">
                                <label class="form-label">Notes</label>
                                <textarea name="notes" class="form-control" rows="2">{{ old('notes') }}</textarea>
                            </div>
                        </div>
                    </div>

                    <!-- Line Items -->
                    <div class="scrollable-section">
                        <div class="fixed-header">
                            <div class="section-header d-flex justify-content-between align-items-center">
                                <div>
                                    <i class="fas fa-list me-2"></i> Transfer Items
                                </div>
                                <button type="button" class="btn btn-primary-custom btn-sm add-line-btn">
                                    <i class="fas fa-plus me-1"></i> Add Item
                                </button>
                            </div>
                        </div>

                        <div class="table-wrapper">
                            <table class="table table-bordered compact-table table-hover" id="transferItemsTable">
                                <thead class="table-header">
                                    <tr>
                                        <th>#</th>
                                        <th>Item</th>
                                        <th>Item Description</th>
                                        <th>Reference</th>
                                        <th>UOM</th>
                                        <th>Available (From)</th>
                                        <th>Quantity</th>
                                        <th>Available (To)</th>
                                        <th></th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <!-- Lines will be added dynamically -->
                                </tbody>
                            </table>
                        </div>
                    </div>

                    <div class="d-flex justify-content-between mt-3 pt-3 border-top">
                        <a href="{{ route('home') }}" class="btn btn-outline-secondary">
                            <i class="fas fa-times me-2"></i> Cancel
                        </a>
                        <button type="submit" class="btn btn-success" id="processBtn">
                            <i class="fas fa-paper-plane me-2"></i> Process Transfer
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>

    <script>
    document.addEventListener('DOMContentLoaded', function() {
        const items = @json($items);
        const uoms = @json($uoms);

        // Add Line Functionality
        function addLine() {
            const tbody = document.querySelector('#transferItemsTable tbody');
            const idx = tbody.children.length;
            const row = document.createElement('tr');
            row.className = 'line-item-row';
            row.innerHTML = `
                <td>${idx+1}</td>
                <td>
                    <select name="lines[${idx}][item_id]" class="form-select form-select-sm item-select">
                        <option value="">Select Item</option>
                        ${generateItemOptions()}
                    </select>
                </td>
                <td><input type="text" readonly class="form-control form-control-sm item-description"></td>
                <td><input type="text" name="lines[${idx}][reference]" class="form-control form-control-sm"></td>
                <td>
                    <select name="lines[${idx}][uom_id]" class="form-select form-select-sm uom-select">
                        <option value="">Select UOM</option>
                        ${generateUomOptions()}
                    </select>
                </td>
                <td><input type="number" readonly class="form-control form-control-sm available-from" value="0"></td>
                <td><input type="number" name="lines[${idx}][quantity]" class="form-control form-control-sm quantity" value="1" min="0.01" step="0.01"></td>
                <td><input type="number" readonly class="form-control form-control-sm available-to" value="0"></td>
                <td class="text-center"><i class="fas fa-trash-alt action-icon"></i></td>
            `;
            tbody.appendChild(row);

            // Event Listeners
            row.querySelector('.item-select').addEventListener('change', e => updateItem(e.target, row));
            row.querySelector('.action-icon').addEventListener('click', () => {
                row.remove(); renumber();
            });
        }

        function generateItemOptions() {
            return items.sort((a,b) => a.code.localeCompare(b.code))
                        .map(i => `<option value="${i.id}" 
                            data-desc="${i.description1}" 
                            data-uom="${i.uom}">${i.code} - ${i.description1}</option>`)
                        .join('');
        }

        function generateUomOptions() {
            return uoms.map(u => `<option value="${u.id}">${u.description}</option>`).join('');
        }

        function updateItem(sel, row) {
            const itemId = sel.value;
            const fromWarehouse = document.getElementById('fromWarehouseSelect').value;
            const toWarehouse = document.getElementById('toWarehouseSelect').value;
            
            if (!itemId || !fromWarehouse || !toWarehouse) return;
            
            const i = items.find(x => x.id == itemId);
            if (!i) return;
            
            row.querySelector('.item-description').value = i.description1 || '';
            row.querySelector('.uom-select').value = i.uom || '';
            
            // Fetch available quantities
            fetchAvailableQuantities(itemId, fromWarehouse, toWarehouse)
                .then(data => {
                    row.querySelector('.available-from').value = data.availableFrom;
                    row.querySelector('.available-to').value = data.availableTo;
                });
        }
        
        function fetchAvailableQuantities(itemId, fromWarehouseId, toWarehouseId) {
            return fetch(`/warehouse/available/${itemId}/${fromWarehouseId}/${toWarehouseId}`)
                .then(response => response.json());
        }

        function renumber() {
            document.querySelectorAll('#transferItemsTable tbody tr').forEach((r, i) => {
                r.querySelector('td').textContent = i+1;
            });
        }

        // Initialize
        document.querySelector('.add-line-btn').addEventListener('click', addLine);
        addLine();
        
        // Update available quantities when warehouses change
        ['#fromWarehouseSelect', '#toWarehouseSelect'].forEach(selector => {
            document.querySelector(selector).addEventListener('change', function() {
                document.querySelectorAll('.line-item-row').forEach(row => {
                    const itemSelect = row.querySelector('.item-select');
                    if (itemSelect.value) {
                        updateItem(itemSelect, row);
                    }
                });
            });
        });
    });
    </script>
</body>
</html>