<div class="payment-search-section text-center py-5">
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-lg-6">
                <div class="payment-search-card">
                    <div class="payment-icon mb-3">
                        <i class="fas fa-credit-card fa-4x text-primary"></i>
                    </div>
                    <h3 class="mb-3">Bayar Tagihan Internet</h3>
                    <p class="text-muted mb-4">Masukkan nomor layanan atau WhatsApp untuk mencari tagihan Anda</p>
                    
                    <!-- Form Pencarian -->
                    <form id="homepageSearchForm" class="mb-3">
                        <div class="input-group input-group-lg">
                            <input type="text" class="form-control" id="homepageSearchValue" 
                                   placeholder="Nomor layanan atau WhatsApp" required>
                            <button type="submit" class="btn btn-primary">
                                <i class="fas fa-search me-2"></i>Cari Tagihan
                            </button>
                        </div>
                        <small class="text-muted d-block mt-2">Contoh: 12345 atau 081234567890</small>
                    </form>
                    
                    <!-- Loading -->
                    <div id="homepageLoading" class="text-center py-3" style="display: none;">
                        <div class="spinner-border text-primary" role="status">
                            <span class="visually-hidden">Loading...</span>
                        </div>
                        <p class="mt-2 text-muted">Mencari tagihan...</p>
                    </div>
                    
                    <!-- Error Message -->
                    <div id="homepageError" class="alert alert-danger mt-3" style="display: none;">
                        <i class="fas fa-exclamation-triangle me-2"></i>
                        <span class="error-text"></span>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<style>
.payment-search-section {
    background: linear-gradient(135deg, rgba(79, 70, 229, 0.1), rgba(124, 58, 237, 0.1));
    border-radius: 20px;
    margin: 2rem 0;
}

.payment-search-card {
    background: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(10px);
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-radius: 20px;
    padding: 2rem;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
    transition: transform 0.3s ease;
}

.payment-search-card:hover {
    transform: translateY(-5px);
}

.payment-icon {
    animation: float 3s ease-in-out infinite;
}

@keyframes float {
    0% { transform: translateY(0px); }
    50% { transform: translateY(-10px); }
    100% { transform: translateY(0px); }
}

.input-group-lg .form-control {
    border-radius: 10px 0 0 10px;
    border: 2px solid #e2e8f0;
    padding: 0.75rem 1rem;
}

.input-group-lg .btn {
    border-radius: 0 10px 10px 0;
    background: linear-gradient(135deg, #4f46e5, #7c3aed);
    border: none;
    font-weight: 600;
    padding: 0.75rem 1.5rem;
}

.input-group-lg .btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 20px rgba(79, 70, 229, 0.4);
}

.form-control:focus {
    border-color: #4f46e5;
    box-shadow: 0 0 0 0.2rem rgba(79, 70, 229, 0.25);
}
</style>

<script>
document.addEventListener("DOMContentLoaded", function() {
    const form = document.getElementById("homepageSearchForm");
    const loading = document.getElementById("homepageLoading");
    const error = document.getElementById("homepageError");
    
    if (form) {
        form.addEventListener("submit", function(e) {
            e.preventDefault();
            
            const searchValue = document.getElementById("homepageSearchValue").value.trim();
            
            if (!searchValue) {
                showError("Masukkan nomor layanan atau WhatsApp Anda");
                return;
            }
            
            // Show loading
            loading.style.display = "block";
            error.style.display = "none";
            
            // Create form data
            const formData = new FormData();
            formData.append("search_value", searchValue);
            
            // Add CSRF token
            const csrfToken = document.querySelector("meta[name=csrf-token]");
            if (csrfToken) {
                formData.append("_token", csrfToken.getAttribute("content"));
            }
            
            // Search invoice
            fetch("/payment/search-invoice", {
                method: "POST",
                body: formData,
                headers: {
                    "X-Requested-With": "XMLHttpRequest"
                }
            })
            .then(response => response.json())
            .then(data => {
                loading.style.display = "none";
                
                if (data.success) {
                    // Redirect to payment page with search value
                    window.location.href = "/bayar-tagihan?search=" + encodeURIComponent(searchValue);
                } else {
                    showError(data.message || "Tagihan tidak ditemukan");
                }
            })
            .catch(error => {
                loading.style.display = "none";
                showError("Terjadi kesalahan saat mencari tagihan");
                console.error("Error:", error);
            });
        });
    }
    
    function showError(message) {
        const errorDiv = document.getElementById("homepageError");
        const errorText = errorDiv.querySelector(".error-text");
        if (errorText) {
            errorText.textContent = message;
            errorDiv.style.display = "block";
        }
    }
});
</script>