@extends('backend.layout')

@section('backend_content')
    <div class="container-fluid">
        {{-- ══ Header ══ --}}
        <div class="d-flex justify-content-between align-items-center mb-4">
            <div>
                <h4 class="fw-bold mb-1">Shop Management </h4>
                <small class="text-muted">Manage shops and their details</small>
            </div>
            @if (auth()->user()->hasRole('super-admin'))
                <div class="d-flex gap-2">
                    <a href="{{ route('admin.shops.create') }}" class="btn btn-primary">
                        <i class="bx bx-plus"></i> Create Shop
                    </a>
                </div>
            @endif
        </div>

        {{-- ══ Shops Table ══ --}}
        <div class="card border-0 shadow-sm">
            <div class="card-body table-responsive p-0">
                <table class="table align-middle mb-0" id="shopsTable">
                    <thead class="table-light">
                        <tr>
                            <th>Shop</th>
                            <th>Brand Color</th>
                            <th>Creator</th>
                            <th>Staff Count</th>
                            <th class="text-end">Action</th>
                        </tr>
                    </thead>
                    <tbody id="shopsBody">
                        @forelse ($shops as $shop)
                            <tr>
                                <td>
                                    <div class="d-flex align-items-center gap-2">
                                        @if ($shop->logo)
                                            <img src="{{ asset('storage/' . $shop->logo) }}" alt="{{ $shop->name }}"
                                                class="rounded" style="width: 40px; height: 40px; object-fit: cover;">
                                        @else
                                            <div class="bg-primary text-white rounded d-flex align-items-center justify-content-center"
                                                style="width: 40px; height: 40px;">
                                                <i class="bx bx-store"></i>
                                            </div>
                                        @endif
                                        <div>
                                            <div class="fw-semibold">{{ $shop->name }}</div>
                                            <small class="text-muted">{{ $shop->contact['phone'] ?? 'No contact' }}</small>
                                        </div>
                                    </div>
                                </td>
                                <td>
                                    <div class="d-flex align-items-center gap-2">
                                        <div
                                            style="width: 20px; height: 20px; background-color: {{ $shop->brand_color }}; border-radius: 50%;">
                                        </div>
                                        {{ $shop->brand_color }}
                                    </div>
                                </td>
                                <td>{{ $shop->creator->name }}</td>
                                @php
                                    $staffCount = $shop->users->count();
                                    if ($shop->creator && !$shop->users->contains('id', $shop->creator->id)) {
                                        $staffCount++;
                                    }
                                @endphp
                                <td><span class="badge bg-light text-dark">{{ $staffCount }} Staff</span></td>
                                <td class="text-end">
                                    <div class="d-inline-flex align-items-center gap-2">
                                        <a style="padding: 2px" href="{{ route('admin.shops.show', $shop) }}"
                                            class="btn btn-sm btn-outline-info " title="View"><i
                                                class="bx bx-show"></i></a>
                                        <a style="padding: 2px" href="{{ route('admin.shops.edit', $shop) }}"
                                            class="btn btn-sm btn-outline-warning" title="Edit"><i
                                                class="bx bx-edit"></i></a>
                                        <a style="padding: 2px" href="{{ route('admin.shops.report', $shop) }}"
                                            class="btn btn-sm btn-outline-primary" title="Report"><i
                                                class="bx bx-line-chart"></i></a>
                                        <a style="padding: 2px" href="{{ route('admin.shops.products.index', $shop) }}"
                                            class="btn btn-sm btn-outline-secondary" title="Products"><i
                                                class="bx bx-package"></i></a>
                                        <button style="padding: 2px" type="button" class="btn btn-sm btn-outline-dark"
                                            title="Copy Landing Link" onclick="copyLandingLink({{ $shop->id }})"><i
                                                class="bx bx-link"></i></button>
                                        <button style="padding: 2px" type="button" class="btn btn-sm btn-outline-danger"
                                            title="Delete" onclick="confirmDelete({{ $shop->id }})"><i
                                                class="bx bx-trash"></i></button>
                                    </div>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="5" class="text-center text-muted py-4">
                                    <i class="bx bx-info-circle fs-3"></i>
                                    <div class="mt-2">No shops found. Please create a shop.</div>
                                </td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    {{-- Delete Confirmation Modal --}}
    <div class="modal fade" id="deleteModal" tabindex="-1">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">Delete Shop</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    Are you sure you want to delete this shop? This action cannot be undone.
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                    <form id="deleteForm" method="POST" style="display: inline;">
                        @csrf
                        @method('DELETE')
                        <button type="submit" class="btn btn-danger">Delete</button>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <script>
        function confirmDelete(shopId) {
            document.getElementById('deleteForm').action = `/admin/shops/${shopId}`;
            new bootstrap.Modal(document.getElementById('deleteModal')).show();
        }

        function copyLandingLink(shopId) {
            const base = @json(rtrim(config('app.url'), '/'));
            const link = `${base}/shops/${shopId}/landing`;
            navigator.clipboard.writeText(link)
                .then(() => window.appToast?.('Landing page link copied successfully.', 'success'))
                .catch(() => window.appToast?.(`Could not copy link. Copy manually: ${link}`, 'error'));
        }
    </script>
@endsection
