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

<div class="max-w-4xl mx-auto mb-10 space-y-6">

    <div class="bg-white dark:bg-gray-800 rounded-xl shadow-sm border border-gray-200 dark:border-gray-700/50 overflow-hidden">
        <div class="p-5 border-b border-gray-200 dark:border-gray-700/50 bg-gray-50 dark:bg-gray-900/40 flex justify-between items-center">
            <h1 class="text-sm font-bold text-gray-900 dark:text-white flex items-center uppercase tracking-wider">
                <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>
                Personal Information
            </h1>
        </div>
        
        <div class="p-6 md:p-8">
            <!-- READ-ONLY SYSTEM DETAILS -->
            <div class="bg-blue-50 dark:bg-blue-900/10 border border-blue-200 dark:border-blue-800/30 rounded-xl p-5 mb-8 flex flex-col md:flex-row md:items-center justify-between">
                <div>
                    <h3 class="text-xs font-bold text-blue-800 dark:text-blue-400 uppercase tracking-wider mb-1">Account Level</h3>
                    <p class="text-sm text-blue-900 dark:text-blue-300 font-medium">
                        <?php echo $data['user']->role === 'partner' ? 'Agency' : ucfirst($data['user']->role); ?>
                        <?php if ($data['user']->role === 'staff' && !empty($data['user']->staff_role_name)): ?>
                            <span class="text-blue-600 dark:text-blue-500 font-normal ml-1">(<?php echo htmlspecialchars($data['user']->staff_role_name); ?>)</span>
                        <?php endif; ?>
                    </p>
                </div>
                
                <?php if ($data['user']->role === 'client' && !empty($data['user']->company_name)): ?>
                <div class="mt-4 md:mt-0 md:text-right border-t md:border-t-0 md:border-l border-blue-200 dark:border-blue-800/50 pt-4 md:pt-0 md:pl-6">
                    <h3 class="text-xs font-bold text-blue-800 dark:text-blue-400 uppercase tracking-wider mb-1">Registered Company</h3>
                    <p class="text-sm text-blue-900 dark:text-blue-300 font-medium"><?php echo htmlspecialchars($data['user']->company_name); ?></p>
                </div>
                <?php endif; ?>
            </div>

            <form action="<?php echo URLROOT; ?>/profile/index" method="POST" class="space-y-6">
                <input type="hidden" name="update_info" value="1">
                
                <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                    <div>
                        <label class="block text-xs font-bold text-gray-500 uppercase tracking-wider mb-2">Full 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 Address</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-8">Contact & Address Details</h3>
                <div class="grid grid-cols-1 md:grid-cols-2 gap-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="flex justify-end pt-4">
                    <button type="submit" class="w-full md:w-auto bg-primary hover:opacity-90 text-white font-bold py-2.5 px-8 rounded-lg shadow-sm transition-all focus:ring-4 focus:ring-primary/50">
                        Update Details
                    </button>
                </div>
            </form>
        </div>
    </div>

    <!-- Security & Password -->
    <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
    
        <div class="bg-white dark:bg-gray-800 rounded-xl shadow-sm border border-gray-200 dark:border-gray-700/50 overflow-hidden">
            <div class="p-5 border-b border-gray-200 dark:border-gray-700/50 bg-gray-50 dark:bg-gray-900/40">
                <h2 class="text-sm font-bold text-gray-900 dark:text-white uppercase tracking-wider 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>
                    Change Password
                </h2>
            </div>
            <div class="p-6">
                <form action="<?php echo URLROOT; ?>/profile/updatePassword" method="POST" class="space-y-4">
                    <div>
                        <label class="block text-[10px] 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" required>
                    </div>
                    <div>
                        <label class="block text-[10px] 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" required>
                    </div>
                    <div>
                        <label class="block text-[10px] 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" required>
                    </div>
                    <button type="submit" class="w-full bg-gray-800 hover:bg-gray-900 dark:bg-gray-700 dark:hover:bg-gray-600 text-white font-bold py-2.5 rounded-lg shadow-sm transition-colors mt-2">
                        Change Password
                    </button>
                </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">
            <div class="p-5 border-b border-gray-200 dark:border-gray-700/50 bg-gray-50 dark:bg-gray-900/40">
                <h2 class="text-sm font-bold text-gray-900 dark:text-white uppercase tracking-wider 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="M9 12l2 2 4-4m5.618-4.016A11.955 11.955 0 0112 2.944a11.955 11.955 0 01-8.618 3.04A12.02 12.02 0 003 9c0 5.591 3.824 10.29 9 11.622 5.176-1.332 9-6.03 9-11.622 0-1.042-.133-2.052-.382-3.016z"></path></svg>
                    Two-Factor Authentication
                </h2>
            </div>
            <div class="p-6">
                
                <?php if($data['totp_enabled'] == 1): ?>
                    
                    <div class="bg-green-50 dark:bg-green-900/10 border border-green-200 dark:border-green-800/30 rounded-lg p-5 mb-6 text-center">
                        <div class="mx-auto flex items-center justify-center h-12 w-12 rounded-full bg-green-100 dark:bg-green-800/50 mb-3">
                            <svg class="h-6 w-6 text-green-600 dark:text-green-400" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"></path></svg>
                        </div>
                        <h3 class="text-green-800 dark:text-green-400 font-bold mb-1">2FA is Currently Active</h3>
                        <p class="text-xs text-green-700 dark:text-green-500">Your account is secured with a time-based authenticator app.</p>
                    </div>

                    <?php if(isset($_SESSION['new_backup_codes'])): ?>
                        <div class="bg-yellow-50 dark:bg-yellow-900/10 border border-yellow-300 dark:border-yellow-700/50 p-5 rounded-lg mb-6 shadow-inner ring-4 ring-yellow-50 dark:ring-yellow-900/10">
                            <h3 class="text-yellow-800 dark:text-yellow-500 font-bold text-sm mb-2 flex items-center">
                                <svg class="w-4 h-4 mr-1.5" 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 Your Backup Codes
                            </h3>
                            <p class="text-xs text-yellow-700 dark:text-yellow-600 mb-4">Print or save these codes. If you lose your phone, these are the ONLY way to access your account. Each code works once.</p>
                            <div class="grid grid-cols-2 gap-3 text-center">
                                <?php foreach($_SESSION['new_backup_codes'] as $code): ?>
                                    <div class="bg-white dark:bg-gray-800 border border-yellow-200 dark:border-yellow-700/50 py-2 rounded text-sm font-mono font-bold text-gray-800 dark:text-gray-200"><?php echo $code; ?></div>
                                <?php endforeach; ?>
                            </div>
                            <?php unset($_SESSION['new_backup_codes']); ?>
                        </div>
                    <?php endif; ?>

                    <form action="<?php echo URLROOT; ?>/profile/manage2FA" method="POST" onsubmit="confirmAction(event, this, 'Disable 2FA', 'Are you sure you want to disable Two-Factor Authentication? Your account will be less secure.');">
                        <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 rounded-lg shadow-sm transition-colors text-sm">
                            Disable 2FA
                        </button>
                    </form>

                <?php else: ?>
                    <?php if(!isset($_GET['setup2fa'])): ?>
                        <div class="text-center py-4">
                            <p class="text-sm text-gray-600 dark:text-gray-400 mb-6">Enhance your account security by requiring a code from your mobile device when you log in.</p>
                            <a href="<?php echo URLROOT; ?>/profile/index?setup2fa=1" class="inline-block bg-primary hover:opacity-90 text-white font-bold py-2.5 px-6 rounded-lg shadow-sm transition-all focus:ring-4 focus:ring-primary/50 text-sm">
                                Setup Two-Factor Auth
                            </a>
                        </div>
                    <?php else: ?>
                        <div class="bg-gray-50 dark:bg-gray-900/50 p-5 rounded-lg border border-gray-200 dark:border-gray-700">
                            <ol class="list-decimal pl-4 space-y-4 text-sm text-gray-700 dark:text-gray-300 marker:font-bold marker:text-primary mb-6">
                                <li>Scan this QR code with an authenticator app (like Google Authenticator or Authy).</li>
                                <div class="bg-white p-3 inline-block rounded-lg shadow-sm border border-gray-200">
                                    <img src="<?php echo $data['qr_code_url']; ?>" alt="2FA QR Code" class="h-32 w-32">
                                </div>
                                <p class="text-[10px] text-gray-500 mt-1">Manual Entry Code: <code class="bg-gray-200 dark:bg-gray-700 px-1 py-0.5 rounded text-gray-800 dark:text-gray-200 select-all"><?php echo $data['setup_secret']; ?></code></p>
                                <li>Enter the 6-digit code generated by the app below to verify setup.</li>
                            </ol>

                            <form action="<?php echo URLROOT; ?>/profile/manage2FA" method="POST" class="space-y-4">
                                <input type="hidden" name="verify_2fa" value="1">
                                <input type="hidden" name="setup_secret" value="<?php echo $data['setup_secret']; ?>">
                                
                                <div>
                                    <input type="text" name="auth_code" placeholder="000000" class="w-full px-4 py-3 text-center text-lg tracking-[0.5em] font-mono rounded-lg border border-gray-300 dark:border-gray-700 bg-white dark:bg-gray-800 text-gray-900 dark:text-white focus:ring-2 focus:ring-primary outline-none" required autocomplete="off" maxlength="6">
                                </div>
                                
                                <button type="submit" class="w-full bg-green-600 hover:bg-green-700 text-white font-bold py-2.5 rounded-lg shadow-sm transition-colors">
                                    Verify & Activate
                                </button>
                                <a href="<?php echo URLROOT; ?>/profile/index" class="block text-center text-xs text-gray-500 hover:text-gray-700 dark:hover:text-gray-300 mt-3 font-bold">Cancel Setup</a>
                            </form>
                        </div>
                    <?php endif; ?>
                <?php endif; ?>
                
            </div>
        </div>
    </div>
</div>

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