@include('includes.header')

<div class="page-wrapper">
    <div class="page-content">

        <!-- Page Header + Controls -->
        <div class="card border-0 shadow-sm mb-3">
            <div class="card-body d-flex flex-wrap justify-content-between align-items-center gap-2">

                <!-- Left: Title + Info -->
                <div>
                    <h5 class="mb-0 text-uppercase">Products List</h5>

                </div>

                <!-- Right: Actions -->
                <div class="d-flex align-items-center gap-2">

                    <!-- Search -->
                    <input type="text" class="form-control form-control-sm" style="width: 220px;"
                        placeholder="🔍 Search store...">

                    <!-- Add Button -->
                    <a href="{{ route('store.product.create') }}" class="btn btn-success btn-sm px-3">
                        <i class="bx bx-plus"></i> Add Product
                    </a>

                </div>

            </div>


            <!-- Table Card -->
            <div class="card border-0 shadow-sm">
                <div class="card-body p-0">

                    <div class="table-responsive">
                        <table class="table table-hover align-middle mb-0">

                            <!-- Table Head -->
                            <thead class="bg-light">
                                <tr>
                                    <th class="ps-4" width="60">S.No</th>
                                    <th>Category</th>
                                    <th>Name</th>
                                    <th>Image</th>
                                    <th width="160">Description</th>
                                    <th>Manage</th>
                                    <th>Type</th>
                                    <th width="160" class="text-center">Actions</th>
                                </tr>
                            </thead>

                            <!-- Table Body -->
                            <tbody>
                                @foreach ($products as $store)
                                    <tr>
                                        <td>{{ $loop->iteration }}</td>
                                        <td>
                                            <div class="d-flex align-items-center gap-3">

                                                <div>
                                                    <div>{{ $store->category->name }}</div>

                                                </div>
                                            </div>
                                        </td>

                                        <td>{{ $store->name }}</td>
                                        <td>
                                            <img src="{{ asset('uploads/products/' . $store->image) }}"
                                                class="rounded border" width="45" height="45">
                                        </td>

                                        <td>{{ \Illuminate\Support\Str::limit($store->description, 50, '...') }}</td>

                                        <td>
                                            <!-- Manage -->
                                            <a class="btn btn-sm btn-outline-warning" data-bs-toggle="modal"
                                                data-bs-target="#typeModal" data-id="{{ $store->id }}"
                                                data-type="{{ $store->is_type }}">
                                                <i class="bx bx-cog"></i>Manage
                                            </a>
                                        </td>
                                        <td>
                                            <span class="badge bg-info text-dark">
                                                {{ ucfirst($store->is_type) }}
                                            </span>
                                        </td>

                                        <td class="text-center">
                                            <div class="d-flex justify-content-center gap-2">

                                                <!-- Edit -->
                                                <a href="{{ route('store.product.edit', $store->id) }}"
                                                    class="btn btn-sm btn-outline-primary">
                                                    <i class="bx bx-edit"></i>
                                                </a>

                                                <!-- Delete -->
                                                <a href="{{ route('store.product.destroy', $store->id) }}"
                                                    class="btn btn-sm btn-outline-danger"
                                                    onclick="return confirm('Are you sure you want to delete this product?')">
                                                    <i class="bx bx-trash"></i>
                                                </a>

                                            </div>
                                        </td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>

                </div>

                <!-- Pagination -->
                <div class="card-footer bg-white d-flex justify-content-between align-items-center">

                    <small class="text-muted">
                        Showing <b>1</b> to <b>2</b> of <b>10</b> entries
                    </small>

                    <ul class="pagination pagination-sm mb-0">
                        <li class="page-item disabled"><a class="page-link">Previous</a></li>
                        <li class="page-item active"><a class="page-link">1</a></li>
                        <li class="page-item"><a class="page-link">2</a></li>
                        <li class="page-item"><a class="page-link">3</a></li>
                        <li class="page-item"><a class="page-link">Next</a></li>
                    </ul>

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

    </div>
</div>
<div class="modal fade" id="typeModal" tabindex="-1">
    <div class="modal-dialog">
        <form method="POST" id="typeForm">
            @csrf
            @method('PUT')

            <input type="hidden" name="id" id="type_category_id">

            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">Manage Product Type</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>

                <div class="modal-body">

                    <label for="is_type">Select Type *</label>
                    <select name="is_type" id="is_type" class="form-control">
                        <option value="new">New Arrival</option>
                        <option value="feature">Feature Product</option>
                        <option value="top">Top Product</option>
                        <option value="best">Best Product</option>
                    </select>

                </div>

                <div class="modal-footer">
                    <button type="submit" class="btn btn-success">Update</button>
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                </div>
            </div>
        </form>
    </div>
</div>

<script>
    document.addEventListener('DOMContentLoaded', function() {

        var typeModal = document.getElementById('typeModal');

        typeModal.addEventListener('show.bs.modal', function(event) {
            var button = event.relatedTarget;

            var id = button.getAttribute('data-id');
            var type = button.getAttribute('data-type');

            document.getElementById('type_category_id').value = id;
            document.getElementById('is_type').value = type ?? 'new';

            // ✅ FIXED ROUTE
            document.getElementById('typeForm').action = "{{ url('store/product/type') }}/" + id;
        });

    });
</script>

@include('includes.footer')
