@extends('web.default.panel.layouts.panel_layout')

@section('content')
    <div class="container">
        <div class="row">
            <div class="col-12">
                <div class="card">
                    <div class="card-header">
                        <h4 class="card-title">
                            <i class="fas fa-id-card"></i>
                            My Serial Number & Certificates
                        </h4>
                    </div>
                    <div class="card-body">
                        <!-- Serial Number Section -->
                        <div class="row mb-4">
                            <div class="col-md-6">
                                <div class="alert alert-info">
                                    <h5><i class="fas fa-info-circle"></i> Your Unique Serial Number</h5>
                                    <div class="input-group">
                                        <input type="text" class="form-control form-control-lg" 
                                               value="{{ $userSerialNumber }}" 
                                               id="serialNumber" 
                                               readonly 
                                               style="font-family: monospace; font-size: 18px; font-weight: bold;">
                                        <div class="input-group-append">
                                            <button class="btn btn-outline-primary" type="button" onclick="copySerialNumber()">
                                                <i class="fas fa-copy"></i> Copy
                                            </button>
                                        </div>
                                    </div>
                                    <small class="text-muted mt-2 d-block">
                                        Use this serial number to search for all your certificates on our website.
                                    </small>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="card bg-light">
                                    <div class="card-body">
                                        <h6><i class="fas fa-user"></i> User Information</h6>
                                        <p><strong>Name:</strong> {{ $user->full_name }}</p>
                                        <p><strong>Email:</strong> {{ $user->email }}</p>
                                        <p><strong>Total Certificates:</strong> {{ $certificateCount }}</p>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <!-- Certificates Section -->
                        <div class="row">
                            <div class="col-12">
                                <h5><i class="fas fa-certificate"></i> My Certificates ({{ $certificateCount }})</h5>
                                
                                @if($certificates->count() > 0)
                                    <div class="table-responsive">
                                        <table class="table table-striped">
                                            <thead>
                                                <tr>
                                                    <th>#</th>
                                                    <th>Type</th>
                                                    <th>Course/Quiz</th>
                                                    <th>Grade</th>
                                                    <th>Date</th>
                                                    <th>Actions</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                @include(getTemplate() . '.panel.certificates._certificate_rows', ['certificates' => $certificates])
                                            </tbody>
                                        </table>
                                    </div>
                                @else
                                    <div class="alert alert-warning">
                                        <i class="fas fa-exclamation-triangle"></i>
                                        You don't have any certificates yet. Complete courses or quizzes to earn certificates!
                                    </div>
                                @endif
                            </div>
                        </div>

                        <!-- Search Instructions -->
                        <div class="row mt-4">
                            <div class="col-12">
                                <div class="alert alert-success">
                                    <h6><i class="fas fa-search"></i> How to Search Your Certificates</h6>
                                    <p class="mb-2">You can search for your certificates using your serial number:</p>
                                    <ol>
                                        <li>Go to the main search page on our website</li>
                                        <li>Enter your serial number: <strong>{{ $userSerialNumber }}</strong></li>
                                        <li>Click search to see all your certificates</li>
                                    </ol>
                                    <a href="/search?search={{ $userSerialNumber }}" class="btn btn-success btn-sm">
                                        <i class="fas fa-search"></i> Search My Certificates
                                    </a>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
        function copySerialNumber() {
            const serialNumber = document.getElementById('serialNumber');
            serialNumber.select();
            serialNumber.setSelectionRange(0, 99999); // For mobile devices
            
            try {
                document.execCommand('copy');
                // Show success message
                const button = event.target.closest('button');
                const originalText = button.innerHTML;
                button.innerHTML = '<i class="fas fa-check"></i> Copied!';
                button.classList.remove('btn-outline-primary');
                button.classList.add('btn-success');
                
                setTimeout(() => {
                    button.innerHTML = originalText;
                    button.classList.remove('btn-success');
                    button.classList.add('btn-outline-primary');
                }, 2000);
            } catch (err) {
                console.error('Failed to copy: ', err);
            }
        }
    </script>
@endsection 