<x-app-layout>
    <div class="container my-4">
        <!-- Display Success/Error Messages -->
        @if (session('success'))
            <div class="alert alert-success">{{ session('success') }}</div>
        @endif

        @if (session('error'))
            <div class="alert alert-danger">{{ session('error') }}</div>
        @endif

        <!-- Add or Edit Inventory Form -->
        <div class="card mb-4">
            <div class="card-header">
                <h2>{{ isset($inventoryItem) ? 'Edit Inventory Item' : 'Add New Inventory Item' }}</h2>
            </div>
            <div class="card-body">
                <form action="{{ isset($inventoryItem) ? route('vendor.inventory.update', $inventoryItem->id) : route('vendor.inventory.store') }}" method="POST" enctype="multipart/form-data">
                    @csrf
                    @if(isset($inventoryItem))
                        @method('PUT')
                    @endif

                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group mb-3">
                                <label for="itemName">Item Name</label>
                                <input type="text" class="form-control @error('itemName') is-invalid @enderror" name="itemName" id="itemName" value="{{ old('itemName', isset($inventoryItem) ? $inventoryItem->itemName : '') }}" required>
                                @error('itemName')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>

                            <div class="form-group mb-3">
                                <label for="itemPrice">Item Price</label>
                                <input type="number" class="form-control @error('itemPrice') is-invalid @enderror" name="itemPrice" id="itemPrice" value="{{ old('itemPrice', isset($inventoryItem) ? $inventoryItem->itemPrice : '') }}" required>
                                @error('itemPrice')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>

                            <div class="form-group mb-3">
                                <label for="itemQty">Item Quantity</label>
                                <input type="number" class="form-control @error('itemQty') is-invalid @enderror" name="itemQty" id="itemQty" value="{{ old('itemQty', isset($inventoryItem) ? $inventoryItem->itemQty : '') }}" required>
                                @error('itemQty')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>

                            <div class="form-group mb-3">
                                <label for="category">Category</label>
                                <input type="text" class="form-control @error('category') is-invalid @enderror" name="category" id="category" value="{{ old('category', isset($inventoryItem) ? $inventoryItem->category : '') }}" required>
                                @error('category')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>

                            <div class="form-group mb-3">
                                <label for="photo_path">Item Photo</label>
                                <input type="file" class="form-control-file @error('photo_path') is-invalid @enderror" name="photo_path" id="photo_path">
                                @error('photo_path')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                                @if(isset($inventoryItem) && $inventoryItem->photo_path)
                                    <div class="mt-2">
                                        <img src="{{ asset('storage/' . $inventoryItem->photo_path) }}" alt="Current Image" width="100" class="img-thumbnail">
                                    </div>
                                @endif
                            </div>

                            <button type="submit" class="btn btn-primary">{{ isset($inventoryItem) ? 'Update Item' : 'Add Item' }}</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>

        <!-- Display Inventory Items -->
        <div class="card mb-4">
            <div class="card-header">
                <h2>Inventory Items</h2>
            </div>
            <div class="card-body">
                <table class="table table-striped table-bordered">
                    <thead>
                        <tr>
                            <th>Item Name</th>
                            <th>Price</th>
                            <th>Quantity</th>
                            <th>Category</th>
                            <th>Photo</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach ($inventory as $item)
                            <tr>
                                <td>{{ $item->itemName }}</td>
                                <td>{{ $item->itemPrice }}</td>
                                <td>{{ $item->itemQty }}</td>
                                <td>{{ $item->category }}</td>
                                <td>
                                    @if ($item->photo_path)
                                        <img src="{{ asset('storage/' . $item->photo_path) }}" alt="{{ $item->itemName }}" width="100" class="img-thumbnail">
                                    @else
                                        No Image
                                    @endif
                                </td>
                                <td>
                                    <!-- Removed Edit Button -->
                                    <form action="{{ route('vendor.inventory.destroy', $item->id) }}" method="POST" style="display:inline;">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit" class="btn btn-danger btn-sm">Delete</button>
                                    </form>
                                </td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        </div>

        <!-- Display Vendor Orders -->
        <div class="card mb-4">
            <div class="card-header">
                <h2>Vendor Orders</h2>
            </div>
            <div class="card-body">
                <table class="table table-striped table-bordered">
                    <thead>
                        <tr>
                            <th>Order ID</th>
                            <th>Item Name</th>
                            <th>Quantity</th>
                            <th>Vendor ID</th>
                            <th>Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach ($orders as $order)
                            <tr>
                                <td>{{ $order->id }}</td>
                                <td>{{ $order->inventory->itemName }}</td>
                                <td>{{ $order->quantity }}</td>
                                <td>{{ $order->vendor_id }}</td>
                                <td>
                                    <form action="{{ route('vendor.order.destroy', $order->id) }}" method="POST" style="display:inline;">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit" class="btn btn-danger btn-sm">Delete</button>
                                    </form>
                                </td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</x-app-layout>
