<?php
require_once __DIR__ . '/../config/whatsapp.php';

if (!isset($data_admin['agent_name']) || ($data_admin['agent_level'] ?? '') === 'admin') {
    header('Location: ./');
    exit();
}

$agentName = agent_public_name((array)$data_admin);
$contact = wa_get_agent_contact($call, $agentName);
$currentWhatsapp = (string)($contact['whatsapp'] ?? '');
$maskedWhatsapp = $currentWhatsapp !== '' ? wa_mask_number($currentWhatsapp) : '';
$otpSentAt = (string)($contact['otp_requested_at'] ?? '');
$otpExpiresAt = (string)($contact['otp_expires_at'] ?? '');
?>
<div class="app-page-shell">
    <div class="row justify-content-center">
    <div class="col-12 col-lg-8">
        <div class="card border border-warning-subtle">
            <div class="card-header">
                <h4 class="card-title mb-1">Langkah Verifikasi</h4>
                <p class="text-muted mb-0">Isi nomor WhatsApp lalu masukkan kode OTP yang diterima.</p>
            </div>
            <div class="card-body">
                <div class="alert alert-warning">
                    <strong>Akses dibatasi sementara.</strong><br>
                    Agent <strong><?= htmlspecialchars($agentName, ENT_QUOTES, 'UTF-8') ?></strong> belum menyelesaikan verifikasi WhatsApp.
                </div>

                <div class="row g-3 mb-4">
                    <div class="col-12 col-md-6">
                        <div class="border rounded p-3 h-100">
                            <div class="text-muted small mb-1">Agent Name</div>
                            <div class="fw-semibold"><?= htmlspecialchars($agentName, ENT_QUOTES, 'UTF-8') ?></div>
                        </div>
                    </div>
                    <div class="col-12 col-md-6">
                        <div class="border rounded p-3 h-100">
                            <div class="text-muted small mb-1">Nomor Saat Ini</div>
                            <div class="fw-semibold"><?= $maskedWhatsapp !== '' ? htmlspecialchars($maskedWhatsapp, ENT_QUOTES, 'UTF-8') : 'Belum diisi' ?></div>
                        </div>
                    </div>
                </div>

                <div class="row g-4">
                    <div class="col-12 col-md-6">
                        <div class="border rounded p-3 h-100">
                            <h5 class="mb-3">1. Isi Nomor & Kirim OTP</h5>
                            <form action="function/whatsapp_verification.php" method="POST">
                                <div class="mb-3">
                                    <label class="form-label">Nomor WhatsApp</label>
                                    <input
                                        type="text"
                                        class="form-control"
                                        name="agent_whatsapp"
                                        value="<?= htmlspecialchars($currentWhatsapp, ENT_QUOTES, 'UTF-8') ?>"
                                        placeholder="08xxxxxxxxxx / 628xxxxxxxxxx / +6012xxxxxxx"
                                        required>
                                    <small class="text-muted">Gunakan format internasional lengkap untuk nomor luar negeri.</small>
                                </div>

                                

                                <button type="submit" name="send_otp" class="btn btn-success w-100">Kirim OTP ke WhatsApp</button>
                            </form>
                        </div>
                    </div>

                    <div class="col-12 col-md-6">
                        <div class="border rounded p-3 h-100">
                            <h5 class="mb-3">2. Masukkan Kode OTP</h5>
                            <form action="function/whatsapp_verification.php" method="POST">
                                <div class="mb-3">
                                    <label class="form-label">Kode OTP</label>
                                    <input
                                        type="text"
                                        class="form-control"
                                        name="otp_code"
                                        placeholder="6 digit kode OTP"
                                        inputmode="numeric"
                                        autocomplete="one-time-code"
                                        required>
                                </div>
                                <button type="submit" name="verify_otp" class="btn btn-primary w-100">Verifikasi Sekarang</button>
                            </form>
                        </div>
                    </div>
                </div>

                
            </div>
        </div>
    </div>
</div>
</div>
