<?php require APPROOT . '/app/Views/layouts/header.php'; ?>

<div class="max-w-5xl mx-auto mb-10 space-y-8">
    <div class="pb-4 border-b border-gray-200 dark:border-gray-700/50">
        <h1 class="text-2xl md:text-3xl font-bold text-gray-900 dark:text-white mb-1"><?php echo $data['title']; ?></h1>
        <p class="text-sm text-gray-600 dark:text-gray-400">Manage your administrative profile and security credentials.</p>
    </div>

    <?php if (isset($_SESSION['new_backup_codes'])): ?>
        <div class="bg-red-50 dark:bg-red-900/10 border border-red-200 dark:border-red-800/30 rounded-xl p-6 mb-8 shadow-sm relative overflow-hidden">
            <div class="absolute top-0 left-0 w-1 h-full bg-red-500"></div>
            <h3 class="text-lg font-bold text-red-800 dark:text-red-400 flex items-center mb-2">
                <svg class="w-5 h-5 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"></path></svg>
                Save These Backup Codes Now!
            </h3>
            <p class="text-xs text-red-700 dark:text-red-400/80 mb-6 leading-relaxed">
                If you lose your phone or access to your authenticator app, these codes are the <strong>ONLY</strong> way to recover your account. Each code can only be used once. Copy them and store them in a secure password manager immediately. <strong>They will not be shown again.</strong>
            </p>
            
            <div class="grid grid-cols-2 sm:grid-cols-4 gap-4 bg-white dark:bg-gray-900/50 p-6 rounded-lg border border-red-100 dark:border-red-800/20">
                <?php foreach($_SESSION['new_backup_codes'] as $code): ?>
                    <div class="text-center font-mono font-bold tracking-widest text-gray-900 dark:text-white bg-gray-50 dark:bg-gray-800 py-3 rounded border border-gray-200 dark:border-gray-700/50">
                        <?php echo $code; ?>
                    </div>
                <?php endforeach; ?>
            </div>
            
            <div class="mt-6 text-right">
                <button type="button" onclick="this.parentElement.parentElement.style.display='none';" class="text-xs font-bold text-red-600 dark:text-red-400 hover:text-red-800 dark:hover:text-red-300 underline transition-colors">
                    I have saved these codes securely, hide this message
                </button>
            </div>
        </div>
        <?php unset($_SESSION['new_backup_codes']); ?>
    <?php endif; ?>
    
    <div class="grid grid-cols-1 lg:grid-cols-2 gap-8">
        
        <div class="space-y-8">
            <div class="bg-white dark:bg-gray-800 rounded-xl shadow-sm border border-gray-200 dark:border-gray-700/50 overflow-hidden flex flex-col">
                <div class="px-6 py-4 border-b border-gray-200 dark:border-gray-700/50 bg-gray-50 dark:bg-gray-900/40 flex items-center">
                    <svg class="w-4 h-4 mr-2 text-primary" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z"></path></svg>
                    <h3 class="text-sm font-bold text-gray-900 dark:text-white uppercase tracking-wider">Profile Information</h3>
                </div>
                <div class="p-6 md:p-8 flex-1 flex flex-col">
                    <form action="<?php echo URLROOT; ?>/admin/profile" method="POST" class="flex-1 flex flex-col" onsubmit="confirmAction(event, this, 'Update Profile', 'Are you sure you want to save these profile changes?');">
                        <input type="hidden" name="update_info" value="1">
                        
                        <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-6">
                            <div>
                                <label class="block text-xs font-bold text-gray-500 uppercase tracking-wider mb-2">Name</label>
                                <input type="text" name="name" value="<?php echo htmlspecialchars($data['name']); ?>" class="w-full px-4 py-2.5 rounded-lg border <?php echo (!empty($data['name_err'])) ? 'border-red-500' : 'border-gray-300 dark:border-gray-700'; ?> bg-gray-50 dark:bg-gray-900/50 text-gray-900 dark:text-white focus:ring-2 focus:ring-primary outline-none transition-colors">
                                <span class="text-xs text-red-500 mt-1 block"><?php echo $data['name_err']; ?></span>
                            </div>
                            <div>
                                <label class="block text-xs font-bold text-gray-500 uppercase tracking-wider mb-2">Email</label>
                                <input type="email" name="email" value="<?php echo htmlspecialchars($data['email']); ?>" class="w-full px-4 py-2.5 rounded-lg border <?php echo (!empty($data['email_err'])) ? 'border-red-500' : 'border-gray-300 dark:border-gray-700'; ?> bg-gray-50 dark:bg-gray-900/50 text-gray-900 dark:text-white focus:ring-2 focus:ring-primary outline-none transition-colors">
                                <span class="text-xs text-red-500 mt-1 block"><?php echo $data['email_err']; ?></span>
                            </div>
                        </div>

                        <h3 class="text-sm font-bold text-gray-900 dark:text-white border-b border-gray-200 dark:border-gray-700/50 pb-2 mb-4 mt-2">Contact & Address Details</h3>
                        <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-6">
                            <div class="md:col-span-2">
                                <label class="block text-[10px] font-bold text-gray-500 uppercase tracking-wider mb-2">Contact Number</label>
                                <input type="text" name="contact_number" value="<?php echo htmlspecialchars($data['contact_number'] ?? ''); ?>" placeholder="+27 82 123 4567" class="w-full px-4 py-2.5 rounded-lg border border-gray-300 dark:border-gray-700 bg-gray-50 dark:bg-gray-900/50 text-gray-900 dark:text-white focus:ring-2 focus:ring-primary outline-none transition-colors">
                            </div>
                            <div class="md:col-span-2">
                                <label class="block text-[10px] font-bold text-gray-500 uppercase tracking-wider mb-2">Street Address</label>
                                <input type="text" name="address_street" value="<?php echo htmlspecialchars($data['address_street'] ?? ''); ?>" class="w-full px-4 py-2.5 rounded-lg border border-gray-300 dark:border-gray-700 bg-gray-50 dark:bg-gray-900/50 text-gray-900 dark:text-white focus:ring-2 focus:ring-primary outline-none transition-colors">
                            </div>
                            <div>
                                <label class="block text-[10px] font-bold text-gray-500 uppercase tracking-wider mb-2">Suburb</label>
                                <input type="text" name="address_suburb" value="<?php echo htmlspecialchars($data['address_suburb'] ?? ''); ?>" class="w-full px-4 py-2.5 rounded-lg border border-gray-300 dark:border-gray-700 bg-gray-50 dark:bg-gray-900/50 text-gray-900 dark:text-white focus:ring-2 focus:ring-primary outline-none transition-colors">
                            </div>
                            <div>
                                <label class="block text-[10px] font-bold text-gray-500 uppercase tracking-wider mb-2">City</label>
                                <input type="text" name="address_city" value="<?php echo htmlspecialchars($data['address_city'] ?? ''); ?>" class="w-full px-4 py-2.5 rounded-lg border border-gray-300 dark:border-gray-700 bg-gray-50 dark:bg-gray-900/50 text-gray-900 dark:text-white focus:ring-2 focus:ring-primary outline-none transition-colors">
                            </div>
                            <div>
                                <label class="block text-[10px] font-bold text-gray-500 uppercase tracking-wider mb-2">State / Province</label>
                                <input type="text" name="address_state" value="<?php echo htmlspecialchars($data['address_state'] ?? ''); ?>" class="w-full px-4 py-2.5 rounded-lg border border-gray-300 dark:border-gray-700 bg-gray-50 dark:bg-gray-900/50 text-gray-900 dark:text-white focus:ring-2 focus:ring-primary outline-none transition-colors">
                            </div>
                            <div>
                                <label class="block text-[10px] font-bold text-gray-500 uppercase tracking-wider mb-2">Postal / Zip Code</label>
                                <input type="text" name="address_zip" value="<?php echo htmlspecialchars($data['address_zip'] ?? ''); ?>" class="w-full px-4 py-2.5 rounded-lg border border-gray-300 dark:border-gray-700 bg-gray-50 dark:bg-gray-900/50 text-gray-900 dark:text-white focus:ring-2 focus:ring-primary outline-none transition-colors">
                            </div>
                            <div class="md:col-span-2">
                                <label class="block text-[10px] font-bold text-gray-500 uppercase tracking-wider mb-2">Country</label>
                                <input type="text" name="address_country" value="<?php echo htmlspecialchars($data['address_country'] ?? ''); ?>" class="w-full px-4 py-2.5 rounded-lg border border-gray-300 dark:border-gray-700 bg-gray-50 dark:bg-gray-900/50 text-gray-900 dark:text-white focus:ring-2 focus:ring-primary outline-none transition-colors">
                            </div>
                        </div>

                        <div class="mt-auto pt-4 border-t border-gray-200 dark:border-gray-700/50 flex justify-end">
                            <button type="submit" class="bg-primary hover:opacity-90 text-white font-bold py-2.5 px-8 rounded-lg transition-all focus:ring-4 focus:ring-primary/50 shadow-sm w-full md:w-auto">
                                Save Changes
                            </button>
                        </div>
                    </form>
                </div>
            </div>

            <div class="bg-white dark:bg-gray-800 rounded-xl shadow-sm border border-gray-200 dark:border-gray-700/50 overflow-hidden flex flex-col">
                <div class="px-6 py-4 border-b border-gray-200 dark:border-gray-700/50 bg-gray-50 dark:bg-gray-900/40 flex items-center">
                    <svg class="w-4 h-4 mr-2 text-primary" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 7a2 2 0 012 2m4 0a6 6 0 01-7.743 5.743L11 17H9v2H7v2H4a1 1 0 01-1-1v-2.586a1 1 0 01.293-.707l5.964-5.964A6 6 0 1121 9z"></path></svg>
                    <h3 class="text-sm font-bold text-gray-900 dark:text-white uppercase tracking-wider">Update Password</h3>
                </div>
                <div class="p-6 md:p-8 flex-1 flex flex-col">
                    <form action="<?php echo URLROOT; ?>/admin/updatePassword" method="POST" class="flex-1 flex flex-col" onsubmit="confirmAction(event, this, 'Update Password', 'Are you sure you want to change your password?');">
                        
                        <div class="mb-5">
                            <label class="block text-xs font-bold text-gray-500 uppercase tracking-wider mb-2">Current Password</label>
                            <input type="password" name="old_password" class="w-full px-4 py-2.5 rounded-lg border border-gray-300 dark:border-gray-700 bg-gray-50 dark:bg-gray-900/50 text-gray-900 dark:text-white focus:ring-2 focus:ring-primary outline-none transition-colors" required>
                        </div>
                        <div class="mb-5">
                            <label class="block text-xs font-bold text-gray-500 uppercase tracking-wider mb-2">New Password</label>
                            <input type="password" name="new_password" class="w-full px-4 py-2.5 rounded-lg border border-gray-300 dark:border-gray-700 bg-gray-50 dark:bg-gray-900/50 text-gray-900 dark:text-white focus:ring-2 focus:ring-primary outline-none transition-colors" required>
                        </div>
                        <div class="mb-6">
                            <label class="block text-xs font-bold text-gray-500 uppercase tracking-wider mb-2">Confirm New Password</label>
                            <input type="password" name="confirm_password" class="w-full px-4 py-2.5 rounded-lg border border-gray-300 dark:border-gray-700 bg-gray-50 dark:bg-gray-900/50 text-gray-900 dark:text-white focus:ring-2 focus:ring-primary outline-none transition-colors" required>
                        </div>
                        
                        <div class="mt-auto pt-2">
                            <button type="submit" class="bg-gray-800 dark:bg-gray-600 hover:bg-gray-900 dark:hover:bg-gray-500 text-white font-bold py-2.5 px-6 rounded-lg shadow-sm transition-all w-full">
                                Update Password
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>

        <div class="space-y-8">
            <div class="bg-white dark:bg-gray-800 rounded-xl shadow-sm border border-gray-200 dark:border-gray-700/50 overflow-hidden flex flex-col h-full">
                <div class="px-6 py-4 border-b border-gray-200 dark:border-gray-700/50 bg-gray-50 dark:bg-gray-900/40 flex items-center">
                    <svg class="w-4 h-4 mr-2 text-primary" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 15v2m-6 4h12a2 2 0 002-2v-6a2 2 0 00-2-2H6a2 2 0 00-2-2v6a2 2 0 002 2zm10-10V7a4 4 0 00-8 0v4h8z"></path></svg>
                    <h3 class="text-sm font-bold text-gray-900 dark:text-white uppercase tracking-wider">Two-Factor Authentication</h3>
                </div>
                <div class="p-6 md:p-8 flex-1 flex flex-col">
                    
                    <?php if ($data['totp_enabled']): ?>
                        <div class="bg-green-50 dark:bg-green-900/10 border border-green-200 dark:border-green-800/30 rounded-xl p-5 mb-6 flex items-start">
                            <svg class="w-6 h-6 text-green-500 mr-3 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg>
                            <div>
                                <h4 class="text-sm font-bold text-green-800 dark:text-green-400">2FA is Active</h4>
                                <p class="text-xs text-green-700 dark:text-green-400/80 mt-1 leading-relaxed">Your account is highly secure. You will be prompted for an authenticator code when logging in.</p>
                            </div>
                        </div>

                        <form action="<?php echo URLROOT; ?>/admin/manage2FA" method="POST" class="mt-auto pt-2" onsubmit="confirmAction(event, this, 'Disable 2FA', 'WARNING: Disabling Two-Factor Authentication will make your account less secure. Are you sure?');">
                            <input type="hidden" name="disable_2fa" value="1">
                            <button type="submit" class="w-full bg-red-500 hover:bg-red-600 text-white font-bold py-2.5 px-4 rounded-lg shadow-sm transition-colors focus:ring-4 focus:ring-red-500/50">
                                Disable 2FA Security
                            </button>
                        </form>

                    <?php elseif (!empty($data['qr_code_url'])): ?>
                        <p class="text-xs text-gray-500 dark:text-gray-400 mb-4 font-bold uppercase tracking-wider">1. Scan QR Code</p>
                        <div class="flex justify-center mb-4 bg-white p-4 rounded-xl border border-gray-200 inline-block mx-auto shadow-sm">
                            <img src="<?php echo $data['qr_code_url']; ?>" alt="2FA QR Code" class="w-40 h-40">
                        </div>
                        
                        <p class="text-xs text-center text-gray-500 dark:text-gray-400 mb-6">
                            Can't scan? Enter this setup key manually: <br>
                            <span class="font-mono font-bold text-gray-900 dark:text-white tracking-widest text-base bg-gray-100 dark:bg-gray-700 px-3 py-1.5 rounded-lg border border-gray-200 dark:border-gray-600 mt-3 inline-block shadow-sm"><?php echo htmlspecialchars($data['setup_secret']); ?></span>
                        </p>

                        <form action="<?php echo URLROOT; ?>/admin/manage2FA" method="POST" class="mt-auto">
                            <input type="hidden" name="verify_2fa" value="1">
                            <input type="hidden" name="setup_secret" value="<?php echo htmlspecialchars($data['setup_secret']); ?>">
                            
                            <p class="text-xs font-bold text-gray-500 dark:text-gray-400 uppercase tracking-wider mb-2">2. Enter 6-Digit Code</p>
                            <input type="text" name="auth_code" placeholder="000000" class="w-full px-4 py-3 text-center tracking-[0.5em] font-mono text-xl rounded-lg border border-gray-300 dark:border-gray-700 bg-gray-50 dark:bg-gray-900/50 text-gray-900 dark:text-white focus:ring-2 focus:ring-primary outline-none mb-6 shadow-inner transition-colors" required>
                            
                            <div class="flex space-x-3">
                                <a href="<?php echo URLROOT; ?>/admin/profile" class="w-1/3 flex justify-center items-center bg-gray-200 hover:bg-gray-300 dark:bg-gray-700 dark:hover:bg-gray-600 text-gray-800 dark:text-white font-bold py-2.5 rounded-lg transition-colors shadow-sm">Cancel</a>
                                <button type="submit" class="w-2/3 bg-green-600 hover:bg-green-700 text-white font-bold py-2.5 rounded-lg transition-colors shadow-sm focus:ring-4 focus:ring-green-500/50">Verify & Activate</button>
                            </div>
                        </form>
                    <?php else: ?>
                        <div class="bg-yellow-50 dark:bg-yellow-900/10 border border-yellow-200 dark:border-yellow-800/30 rounded-xl p-5 mb-6 flex items-start">
                            <svg class="w-6 h-6 text-yellow-600 dark:text-yellow-500 mr-3 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"></path></svg>
                            <div>
                                <h4 class="text-sm font-bold text-yellow-800 dark:text-yellow-400">Security Warning</h4>
                                <p class="text-xs text-yellow-700 dark:text-yellow-400/80 mt-1 leading-relaxed">Your account is currently only protected by a password. We highly recommend enabling 2FA to prevent unauthorized access.</p>
                            </div>
                        </div>

                        <a href="<?php echo URLROOT; ?>/admin/profile?setup2fa=true" class="mt-auto text-center flex justify-center items-center w-full bg-gray-800 dark:bg-gray-600 hover:bg-gray-900 dark:hover:bg-gray-500 text-white font-bold py-3 px-4 rounded-lg transition-colors shadow-sm focus:ring-4 focus:ring-gray-500/50">
                            <svg class="w-5 h-5 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 15v2m-6 4h12a2 2 0 002-2v-6a2 2 0 00-2-2H6a2 2 0 00-2-2v6a2 2 0 002 2zm10-10V7a4 4 0 00-8 0v4h8z"></path></svg>
                            Set Up 2FA Now
                        </a>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</div>

<?php require APPROOT . '/app/Views/layouts/footer.php'; ?>