@extends('layouts.admin')

@section('title', 'Manage Help FAQs')

@section('admin-content')
<style>
    /* Specific FAQ Styles */
    .faqs-page .question-truncate { max-width: 350px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-weight: 600; color: #1a1a2e; }
    .faqs-page .answer-truncate { max-width: 350px; font-size: 0.85rem; color: #6c757d; }
    .faqs-page .badge-category { font-size: 0.8rem; background: #e3f2fd; color: #1565c0; padding: 4px 10px; border-radius: 6px; }
</style>

<div class="faqs-page">
    <!-- Page Header -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h4 class="fw-bold mb-1">Help Page FAQs</h4>
            <p class="text-muted mb-0 small">Manage frequently asked questions and guides</p>
        </div>
        <div class="d-flex gap-2">
            <a href="{{ route('admin.help-faqs.create') }}" class="btn btn-primary shadow-sm btn-lg d-flex align-items-center gap-2" 
               style="border-radius: 12px; font-size: 0.95rem;">
                <i class="bi bi-plus-lg"></i> Add New FAQ
            </a>
        </div>
    </div>

    @if(session('success'))
        <div class="alert alert-success border-0 shadow-sm rounded-3 mb-4 d-flex align-items-center">
            <i class="bi bi-check-circle-fill me-2 fs-5"></i>
            {{ session('success') }}
            <button type="button" class="btn-close ms-auto" data-bs-dismiss="alert"></button>
        </div>
    @endif
    @if(session('error'))
        <div class="alert alert-danger border-0 shadow-sm rounded-3 mb-4 d-flex align-items-center">
            <i class="bi bi-exclamation-triangle-fill me-2 fs-5"></i>
            {{ session('error') }}
            <button type="button" class="btn-close ms-auto" data-bs-dismiss="alert"></button>
        </div>
    @endif

    <!-- Stats -->
    <div class="row g-4 mb-4">
        @php
            $totalFaqs = \App\Models\HelpFaq::count();
            $activeFaqs = \App\Models\HelpFaq::where('is_active', true)->count();
        @endphp
        <div class="col-md-4">
            <div class="stat-card d-flex align-items-center gap-3">
                <div class="stat-icon primary">
                    <i class="bi bi-question-lg"></i>
                </div>
                <div>
                    <div class="stat-value">{{ $totalFaqs }}</div>
                    <div class="stat-label">Total FAQs</div>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="stat-card d-flex align-items-center gap-3">
                <div class="stat-icon success">
                    <i class="bi bi-check-circle-fill"></i>
                </div>
                <div>
                    <div class="stat-value">{{ $activeFaqs }}</div>
                    <div class="stat-label">Published Active</div>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="stat-card d-flex align-items-center gap-3">
                <div class="stat-icon warning">
                    <i class="bi bi-grid-fill"></i>
                </div>
                <div>
                    <div class="stat-value">{{ count($categories) }}</div>
                    <div class="stat-label">FAQ Categories</div>
                </div>
            </div>
        </div>
    </div>
    
    <!-- Filter -->
    <div class="card border-0 shadow-sm rounded-3 mb-4">
        <div class="card-body p-3">
            <form action="{{ route('admin.help-faqs.index') }}" method="GET" class="d-flex align-items-center gap-3">
                <span class="fw-bold text-muted small text-nowrap">Filter by Category:</span>
                <select name="category" class="form-select form-select-sm" onchange="this.form.submit()" style="max-width: 250px;">
                    <option value="">All Categories</option>
                    @foreach($categories as $key => $label)
                        <option value="{{ $key }}" {{ request('category') == $key ? 'selected' : '' }}>{{ $label }}</option>
                    @endforeach
                </select>
            </form>
        </div>
    </div>

    <!-- Usage Table -->
    <div class="data-table">
        <div class="table-responsive">
            <table class="table mb-0">
                <thead>
                    <tr>
                        <th class="ps-4">Question & Answer Preview</th>
                        <th>Category</th>
                        <th class="text-center">Order</th>
                        <th class="text-center">Status</th>
                        <th class="text-end pe-4">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($faqs as $faq)
                        <tr>
                            <td class="ps-4">
                                <div class="question-truncate" title="{{ $faq->question }}">{{ Str::limit($faq->question, 60) }}</div>
                                <div class="answer-truncate mt-1 text-wrap" title="{{ strip_tags($faq->answer) }}">
                                    {{ Str::limit(strip_tags($faq->answer), 120) }}
                                </div>
                            </td>
                            <td>
                                <span class="badge-category">
                                    {{ $categories[$faq->category] ?? $faq->category }}
                                </span>
                            </td>
                            <td class="text-center">
                                <span class="badge bg-light text-dark border">{{ $faq->sort_order }}</span>
                            </td>
                            <td class="text-center">
                                <form action="{{ route('admin.help-faqs.toggle', $faq) }}" method="POST" class="d-inline-block">
                                    @csrf
                                    <div class="form-check form-switch d-flex justify-content-center">
                                        <input class="form-check-input" type="checkbox" role="switch" onchange="this.form.submit()" {{ $faq->is_active ? 'checked' : '' }} style="cursor: pointer;">
                                    </div>
                                    <div class="small mt-1 fw-bold {{ $faq->is_active ? 'text-success' : 'text-muted' }}">
                                        {{ $faq->is_active ? 'Active' : 'Hidden' }}
                                    </div>
                                </form>
                            </td>
                            <td class="text-end pe-4">
                                <div class="d-flex gap-1 justify-content-end">
                                    <a href="{{ route('admin.help-faqs.edit', $faq) }}" class="action-btn primary" title="Edit FAQ" data-bs-toggle="tooltip">
                                        <i class="bi bi-pencil"></i>
                                    </a>
                                    
                                    <form action="{{ route('admin.help-faqs.destroy', $faq) }}" method="POST" class="d-inline"
                                          onsubmit="return confirm('Are you sure you want to delete this FAQ?');">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit" class="action-btn danger" title="Delete FAQ" data-bs-toggle="tooltip">
                                            <i class="bi bi-trash"></i>
                                        </button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="5" class="text-center py-5">
                                <div class="py-4">
                                    <i class="bi bi-question-diamond fs-1 text-muted opacity-50 d-block mb-3"></i>
                                    <h5 class="fw-bold text-muted">No FAQs Found</h5>
                                    <p class="text-muted small mb-0">Help your users by adding frequently asked questions.</p>
                                </div>
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        
        @if($faqs->hasPages())
            <div class="px-3 border-top">
                {{ $faqs->links() }}
            </div>
        @endif
    </div>
</div>

<script>
    document.addEventListener('DOMContentLoaded', function() {
        var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'));
        tooltipTriggerList.map(function (el) {
            return new bootstrap.Tooltip(el);
        });
    });
</script>
@endsection
