@extends('layouts.app')

@section('title', 'Daftar Produk')

@section('content')
<div class="container-fluid">
    <!-- Page Heading -->
    <div class="d-sm-flex align-items-center justify-content-between mb-4">
        <h1 class="h3 mb-0 text-gray-800">Daftar Produk</h1>
        <button class="btn btn-primary btn-sm shadow-sm" data-toggle="modal" data-target="#addProductModal">
            <i class="fas fa-plus fa-sm text-white-50"></i> Tambah Produk
        </button>
    </div>

    <!-- Success Message -->
    @if (session('success'))
        <div class="alert alert-success alert-dismissible fade show" role="alert">
            {{ session('success') }}
            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
        </div>
    @endif

    <!-- Category Filter -->
    <div class="mb-4">
        <form action="{{ route('products.index') }}" method="GET">
            <div class="form-group">
                <label for="category">Filter Kategori</label>
                <select name="category" id="category" class="form-control w-25" onchange="this.form.submit()">
                    <option value="">Semua Kategori</option>
                    <option value="Meja" {{ request('category') == 'Meja' ? 'selected' : '' }}>Meja</option>
                    <option value="Kursi" {{ request('category') == 'Kursi' ? 'selected' : '' }}>Kursi</option>
                    <option value="Lemari" {{ request('category') == 'Lemari' ? 'selected' : '' }}>Lemari</option>
                </select>
            </div>
        </form>
    </div>

    <!-- Products Table -->
    <div class="card shadow mb-4">
        <div class="card-header py-3">
            <h6 class="m-0 font-weight-bold text-primary">Daftar Produk</h6>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Nama</th>
                            <th>Kategori</th>
                            <th>Harga</th>
                            <th>Deskripsi</th>
                            <th>Gambar</th>
                            <th>Aksi</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse ($products as $product)
                            <tr>
                                <td>{{ $product->id }}</td>
                                <td>{{ $product->name }}</td>
                                <td>{{ $product->category }}</td>
                                <td>Rp {{ number_format($product->price, 0, ',', '.') }}</td>
                                <td>{{ Str::limit($product->description, 50) }}</td>
                                <td>
                                    @if ($product->image)
                                        <img class="img-fluid img-thumbnail" src="{{ asset($product->image) }}" alt="{{ $product->name }}" style="max-width: 100px; height: auto;" />
                                    @else
                                        <img class="img-fluid img-thumbnail" src="{{ asset('assets/img/portfolio/furniture-placeholder.jpg') }}" alt="Placeholder" style="max-width: 100px; height: auto;" />
                                    @endif
                                </td>
                                <td>
                                    <a href="#" class="btn btn-warning btn-sm" data-toggle="modal" data-target="#editProductModal{{ $product->id }}">
                                        <i class="fas fa-edit"></i>
                                    </a>
                                    <form action="{{ route('products.destroy', $product->id) }}" method="POST" style="display:inline;">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit" class="btn btn-danger btn-sm" onclick="return confirm('Apakah Anda yakin?')">
                                            <i class="fas fa-trash"></i>
                                        </button>
                                    </form>
                                </td>
                            </tr>

                            <!-- Edit Modal -->
                            <div class="modal fade" id="editProductModal{{ $product->id }}" tabindex="-1" role="dialog" aria-labelledby="editProductModalLabel{{ $product->id }}" aria-hidden="true">
                                <div class="modal-dialog" role="document">
                                    <div class="modal-content">
                                        <form action="{{ route('products.update', $product->id) }}" method="POST" enctype="multipart/form-data">
                                            @csrf
                                            @method('PUT')
                                            <div class="modal-header">
                                                <h5 class="modal-title" id="editProductModalLabel{{ $product->id }}">Edit Produk</h5>
                                                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                                    <span aria-hidden="true">&times;</span>
                                                </button>
                                            </div>
                                            <div class="modal-body">
                                                <div class="form-group">
                                                    <label>Nama</label>
                                                    <input type="text" name="name" class="form-control" value="{{ old('name', $product->name) }}" required>
                                                    @error('name')
                                                        <span class="text-danger">{{ $message }}</span>
                                                    @enderror
                                                </div>
                                                <div class="form-group">
                                                    <label>Kategori</label>
                                                    <select name="category" class="form-control" required>
                                                        <option value="Meja" {{ old('category', $product->category) == 'Meja' ? 'selected' : '' }}>Meja</option>
                                                        <option value="Kursi" {{ old('category', $product->category) == 'Kursi' ? 'selected' : '' }}>Kursi</option>
                                                        <option value="Lemari" {{ old('category', $product->category) == 'Lemari' ? 'selected' : '' }}>Lemari</option>
                                                    </select>
                                                    @error('category')
                                                        <span class="text-danger">{{ $message }}</span>
                                                    @enderror
                                                </div>
                                                <div class="form-group">
                                                    <label>Harga</label>
                                                    <input type="number" name="price" class="form-control" value="{{ old('price', $product->price) }}" required>
                                                    @error('price')
                                                        <span class="text-danger">{{ $message }}</span>
                                                    @enderror
                                                </div>
                                                <div class="form-group">
                                                    <label>Deskripsi</label>
                                                    <textarea name="description" class="form-control" required>{{ old('description', $product->description) }}</textarea>
                                                    @error('description')
                                                        <span class="text-danger">{{ $message }}</span>
                                                    @enderror
                                                </div>
                                                <div class="form-group">
                                                    <label>Gambar</label>
                                                    <input type="file" name="image" class="form-control-file">
                                                    @if ($product->image)
                                                        <img class="img-fluid img-thumbnail mt-2" src="{{ asset($product->image) }}" alt="{{ $product->name }}" style="max-width: 100px; height: auto;" />
                                                    @else
                                                        <img class="img-fluid img-thumbnail mt-2" src="{{ asset('assets/img/portfolio/furniture-placeholder.jpg') }}" alt="Placeholder" style="max-width: 100px; height: auto;" />
                                                    @endif
                                                    @error('image')
                                                        <span class="text-danger">{{ $message }}</span>
                                                    @enderror
                                                </div>
                                            </div>
                                            <div class="modal-footer">
                                                <button type="button" class="btn btn-secondary" data-dismiss="modal">Batal</button>
                                                <button type="submit" class="btn btn-primary">Perbarui</button>
                                            </div>
                                        </form>
                                    </div>
                                </div>
                            </div>
                        @empty
                            <tr>
                                <td colspan="7" class="text-center">Tidak ada produk yang tersedia.</td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <!-- Add Modal -->
    <div class="modal fade" id="addProductModal" tabindex="-1" role="dialog" aria-labelledby="addProductModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <form action="{{ route('products.store') }}" method="POST" enctype="multipart/form-data">
                    @csrf
                    <div class="modal-header">
                        <h5 class="modal-title" id="addProductModalLabel">Tambah Produk Baru</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <div class="modal-body">
                        <div class="form-group">
                            <label>Nama</label>
                            <input type="text" name="name" class="form-control" value="{{ old('name') }}" required>
                            @error('name')
                                <span class="text-danger">{{ $message }}</span>
                            @enderror
                        </div>
                        <div class="form-group">
                            <label>Kategori</label>
                            <select name="category" class="form-control" required>
                                <option value="Meja" {{ old('category') == 'Meja' ? 'selected' : '' }}>Meja</option>
                                <option value="Kursi" {{ old('category') == 'Kursi' ? 'selected' : '' }}>Kursi</option>
                                <option value="Lemari" {{ old('category') == 'Lemari' ? 'selected' : '' }}>Lemari</option>
                            </select>
                            @error('category')
                                <span class="text-danger">{{ $message }}</span>
                            @enderror
                        </div>
                        <div class="form-group">
                            <label>Harga</label>
                            <input type="number" name="price" class="form-control" value="{{ old('price') }}" required>
                            @error('price')
                                <span class="text-danger">{{ $message }}</span>
                            @enderror
                        </div>
                        <div class="form-group">
                            <label>Deskripsi</label>
                            <textarea name="description" class="form-control" required>{{ old('description') }}</textarea>
                            @error('description')
                                <span class="text-danger">{{ $message }}</span>
                            @enderror
                        </div>
                        <div class="form-group">
                            <label>Gambar</label>
                            <input type="file" name="image" class="form-control-file">
                            @error('image')
                                <span class="text-danger">{{ $message }}</span>
                            @enderror
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Batal</button>
                        <button type="submit" class="btn btn-primary">Tambah Produk</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
@endsection

@section('scripts')
<script>
    $(document).ready(function() {
        $('#dataTable').DataTable({
            responsive: true,
            autoWidth: false,
            order: [[0, "desc"]]
        });
    });
</script>
@endsection
