@extends('admin.layouts.layout')
@section('title')لیست محصولات@endsection

@section('content')
<div class="container-xxl flex-grow-1 container-p-y">

    <div class="d-flex align-items-center justify-content-between mb-4">
        <div>
            <h4 class="mb-1">محصولات</h4>
            <p class="text-muted mb-0">لیست تمام محصولات فروشگاه</p>
        </div>
        <a href="{{ route('admin.products.create') }}" class="btn btn-primary">
            <i class="ti ti-plus me-1"></i> محصول جدید
        </a>
    </div>

    @if(session('success'))
        <div class="alert alert-success alert-dismissible mb-4">
            {{ session('success') }}<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    @endif

    <div class="card">
        <div class="table-responsive">
            <table class="table border-top align-middle mb-0">
                <thead class="table-light">
                    <tr>
                        <th>#</th>
                        <th>عنوان محصول</th>
                        <th>برند</th>
                        <th>نوع</th>
                        <th>وضعیت</th>
                        <th>تاریخ ثبت</th>
                        <th style="width:100px">عملیات</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($products as $product)
                        <tr>
                            <td><span class="text-muted small">{{ $loop->iteration }}</span></td>
                            <td>
                                <div class="d-flex align-items-center gap-2">
                                    @if($product->featured_image)
                                        <img src="{{ url('uploads/products/' . $product->featured_image) }}"
                                             class="product-list-thumb rounded" alt="{{ $product->featured_image_alt }}">
                                    @else
                                        <div class="product-list-thumb-placeholder rounded">
                                            <i class="ti ti-photo text-muted"></i>
                                        </div>
                                    @endif
                                    <div>
                                        <span class="fw-medium d-block">{{ $product->title }}</span>
                                        @if($product->title_en)
                                            <small class="text-muted">{{ $product->title_en }}</small>
                                        @endif
                                    </div>
                                </div>
                            </td>
                            <td>
                                @if($product->brand)
                                    <span class="badge bg-label-secondary">{{ $product->brand->name }}</span>
                                @else
                                    <span class="text-muted">—</span>
                                @endif
                            </td>
                            <td>
                                @if($product->type === 'simple')
                                    <span class="badge bg-label-primary">ساده</span>
                                @else
                                    <span class="badge bg-label-info">متغیر</span>
                                @endif
                            </td>
                            <td>
                                @if($product->is_active)
                                    <span class="badge bg-label-success">فعال</span>
                                @else
                                    <span class="badge bg-label-secondary">غیرفعال</span>
                                @endif
                            </td>
                            <td><span class="text-muted small">{{ $product->created_at->format('Y/m/d') }}</span></td>
                            <td>
                                <div class="d-flex gap-1">
                                    <a href="{{ route('admin.products.edit', $product->slug) }}"
                                       class="btn btn-sm btn-outline-primary">
                                        <i class="ti ti-edit ti-xs"></i>
                                    </a>
                                    <form method="POST" action="{{ route('admin.products.destroy', $product->slug) }}"
                                          class="delete-form">
                                        @csrf @method('DELETE')
                                        <button type="button" class="btn btn-sm btn-outline-danger btn-delete"
                                                data-name="{{ $product->title }}">
                                            <i class="ti ti-trash ti-xs"></i>
                                        </button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="7" class="text-center py-5 text-muted">
                                <i class="ti ti-package-off fs-3 d-block mb-2"></i>
                                هیچ محصولی ثبت نشده است.
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        @if($products->isNotEmpty())
            <div class="card-footer text-muted small d-flex align-items-center justify-content-between">
                <span>{{ $products->total() }} محصول</span>
                {{ $products->links() }}
            </div>
        @endif
    </div>

</div>
@endsection

@section('customjs')
<script>
document.querySelectorAll('.btn-delete').forEach(function (btn) {
    btn.addEventListener('click', function () {
        var name = this.dataset.name;
        var form = this.closest('.delete-form');
        Swal.fire({
            title: 'حذف محصول',
            html: 'آیا از حذف <strong>' + name + '</strong> مطمئن هستید؟',
            icon: 'warning',
            showCancelButton: true,
            showDenyButton: false,
            confirmButtonText: 'بله، حذف شود',
            cancelButtonText: 'انصراف',
            confirmButtonColor: '#d33',
            cancelButtonColor: '#6c757d',
            reverseButtons: true,
        }).then(function (result) {
            if (result.isConfirmed) form.submit();
        });
    });
});
</script>
@endsection
