<?php
ob_start();
include 'master.php';

$_ep_err = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $cur_pass  = trim($_POST['current_password'] ?? '');
    $new_pass  = trim($_POST['password'] ?? '');
    $conf_pass = trim($_POST['password_confirmation'] ?? '');

    $email = $_SESSION['email'] ?? '';
    $stmt  = $conn->prepare("SELECT password FROM users WHERE email = ? LIMIT 1");
    $stmt->bind_param('s', $email);
    $stmt->execute();
    $stmt->bind_result($db_pass);
    $stmt->fetch();
    $stmt->close();

    if (empty($cur_pass) || empty($new_pass) || empty($conf_pass)) {
        $_ep_err = 'All fields are required.';
    } elseif (!password_verify($cur_pass, $db_pass)) {
        $_ep_err = 'Current password is incorrect.';
    } elseif ($new_pass !== $conf_pass) {
        $_ep_err = 'New passwords do not match.';
    } elseif (strlen($new_pass) < 8) {
        $_ep_err = 'New password must be at least 8 characters.';
    } else {
        $hashed = password_hash($new_pass, PASSWORD_DEFAULT);
        $upd = $conn->prepare("UPDATE users SET password = ?, confirm_password = ? WHERE email = ?");
        $upd->bind_param('sss', $hashed, $new_pass, $email);
        $upd->execute();
        $upd->close();
        session_destroy();
        ob_end_clean();
        header('Location: ../login?msg=password_changed');
        exit;
    }
}
?>


    

                <!-- Main Content -->
                <main class="flex-1 overflow-y-auto bg-gray-50 dark:bg-gray-900 min-h-0">
                    <div class="h-full overflow-y-auto">
                        
<div class="p-4 space-y-4">
    <!-- Alerts -->
    <?php if (!empty($_ep_err)): ?>
        <div class="p-3 bg-red-100 dark:bg-red-900/30 border border-red-300 dark:border-red-700 rounded-xl text-sm text-red-700 dark:text-red-300 flex items-center space-x-2">
            <i class="fa-solid fa-circle-exclamation text-red-500 text-xs"></i>
            <span><?php echo htmlspecialchars($_ep_err); ?></span>
        </div>
    <?php endif; ?>

    <!-- Mobile Header -->
    <div class="lg:hidden mb-2">
        <div class="lg:hidden" x-data="{ showMobileNotifications: false }">
    <!-- Header Section -->
    <div class="flex items-center justify-between mb-6">
        <div class="flex items-center space-x-2">
                            <!-- Back Button -->
                <a href="account-settings" class="w-10 h-10 bg-gray-100 dark:bg-gray-800 rounded-full flex items-center justify-center backdrop-blur-sm transition-all duration-200 hover:bg-gray-200 dark:hover:bg-gray-700 mr-2">
                    <i class="fa-solid fa-arrow-left text-gray-700 dark:text-gray-300 text-base"></i>
                </a>
                        
            <div>
                                    <!-- Custom Title -->
                    <p class="text-gray-900 dark:text-white font-semibold text-base">Security Settings</p>
                            </div>
        </div>
        
        <div class="flex items-center space-x-2">
              <!-- Dark Mode Toggle -->
             <!--   <button @click="toggleDarkMode()" 
                        class="w-10 h-10 bg-white/10 dark:bg-white/20 rounded-full flex items-center justify-center backdrop-blur-sm transition-all duration-200 hover:bg-white/20 dark:hover:bg-white/30">
                    <i x-show="!darkMode" class="fa-solid fa-moon text-white text-base"></i>
                    <i x-show="darkMode" class="fa-solid fa-sun text-yellow-400 text-base"></i>
                </button> 

                <!-- Notifications -->
             <!--   <div class="relative">
                    <button @click="showMobileNotifications = !showMobileNotifications" class="w-10 h-10 bg-white/10 dark:bg-white/20 rounded-full flex items-center justify-center backdrop-blur-sm transition-all duration-200 hover:bg-white/20 dark:hover:bg-white/30">
                        <i class="fa-solid fa-bell text-white text-base"></i>
                    </button>
                                                        </div>-->
                    </div>
    </div>

            <!-- Mobile Notifications Panel -->
        <div x-show="showMobileNotifications" 
             x-transition:enter="transition ease-out duration-300" 
             x-transition:enter-start="opacity-0 -translate-y-4" 
             x-transition:enter-end="opacity-100 translate-y-0"
             x-transition:leave="transition ease-in duration-200" 
             x-transition:leave-start="opacity-100 translate-y-0" 
             x-transition:leave-end="opacity-0 -translate-y-4"
             class="fixed inset-0 z-50 bg-black/20 backdrop-blur-sm" 
             @click="showMobileNotifications = false">
            
            <div @click.stop class="absolute top-0 left-0 right-0 bg-gradient-to-br from-slate-900 via-slate-800 to-slate-900 dark:from-slate-950 dark:via-slate-900 dark:to-slate-950 text-white rounded-b-3xl shadow-2xl dark:shadow-slate-900/50 max-h-[80vh] overflow-hidden">
                <!-- Header -->
                <div class="flex items-center justify-between p-4 border-b border-white/10 dark:border-white/20">
                    <div class="flex items-center space-x-3">
                        <div class="w-10 h-10 bg-white/10 dark:bg-white/20 rounded-2xl flex items-center justify-center">
                            <i class="fa-solid fa-bell text-white text-sm"></i>
                        </div>
                        <div>
                            <h3 class="text-lg font-bold text-white">Notifications</h3>
                                                                                        <p class="text-xs text-white/60">All caught up!</p>
                                                    </div>
                    </div>
                    <button @click="showMobileNotifications = false" class="w-8 h-8 bg-white/10 dark:bg-white/20 rounded-xl flex items-center justify-center hover:bg-white/20 dark:hover:bg-white/30 transition-colors">
                        <i class="fa-solid fa-times text-white text-sm"></i>
                    </button>
                </div>

                <!-- Notifications List -->
                <div class="overflow-y-auto max-h-96 custom-scrollbar">
                                        
                                        <div class="p-8 text-center">
                        <div class="w-16 h-16 bg-white/10 dark:bg-white/20 rounded-3xl flex items-center justify-center mx-auto mb-4">
                            <i class="fa-solid fa-bell-slash text-white/40 dark:text-white/60 text-2xl"></i>
                        </div>
                        <p class="text-white font-semibold mb-1">No notifications yet</p>
                        <p class="text-xs text-white/60 dark:text-white/70">We'll notify you when something happens</p>
                    </div>
                                    </div>
                
                <!-- Footer Actions -->
                            </div>
        </div>
    </div>     </div>

    <!-- Desktop Header -->
    <div class="hidden lg:block">
        <div class="bg-white/80 dark:bg-gray-800/80 backdrop-blur-xl rounded-2xl shadow-lg dark:shadow-gray-900/50 border border-white/20 dark:border-gray-700/50 p-4">
            <div class="flex items-center justify-between">
                <div>
                    <h1 class="text-lg font-bold text-gray-900 dark:text-white">Security Settings</h1>
                    <p class="text-xs text-gray-500 dark:text-gray-400 mt-1">Update your account password to maintain security</p>
                </div>
                <div class="w-8 h-8 bg-primary-500 rounded-xl flex items-center justify-center">
                    <i class="fa-solid fa-shield text-white text-sm"></i>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="bg-white/80 dark:bg-gray-800/80 backdrop-blur-xl rounded-2xl shadow-lg dark:shadow-gray-900/50 border border-white/20 dark:border-gray-700/50 overflow-hidden">
        <!-- Content Header -->
        <div class="border-b border-gray-200/50 dark:border-gray-700/50 px-4 py-3">
            <div class="flex items-center">
                <div class="w-6 h-6 bg-primary-500 rounded-lg flex items-center justify-center mr-2">
                    <i class="fa-solid fa-key text-white text-xs"></i>
                </div>
                <div>
                    <h2 class="text-sm font-bold text-gray-900 dark:text-white">Change Password</h2>
                    <p class="text-xs text-gray-500 dark:text-gray-400">Update your account password to maintain security</p>
                </div>
            </div>
        </div>
                
        <!-- Form Content -->
        <div class="p-4">
            <form action="editpass" method="post" class="space-y-4">                
                <!-- Current Password -->
                <div>
                    <label for="current_password" class="block text-xs font-medium text-gray-700 dark:text-gray-300 mb-1">Current Password</label>
                    <div class="relative" x-data="{ showPassword: false }">
                        <div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
                            <i class="fa-solid fa-lock text-gray-400 text-xs"></i>
                        </div>
                        <input 
                            :type="showPassword ? 'text' : 'password'" 
                            id="current_password" 
                            name="current_password"
                            class="block w-full pl-8 pr-8 py-2 text-xs border border-gray-200 dark:border-gray-600 rounded-xl bg-white dark:bg-gray-700 text-gray-700 dark:text-gray-300 focus:outline-none focus:ring-2 focus:ring-primary-500 focus:border-primary-500 transition-all"
                            placeholder="Enter your current password"
                            required
                        />
                        <div class="absolute inset-y-0 right-0 pr-3 flex items-center">
                            <button 
                                type="button"
                                @click="showPassword = !showPassword"
                                class="text-gray-400 hover:text-primary-600 dark:hover:text-primary-400 focus:outline-none"
                            >
                                <i x-show="!showPassword" class="fa-solid fa-eye text-xs"></i>
                                <i x-show="showPassword" class="fa-solid fa-eye-slash text-xs"></i>
                            </button>
                        </div>
                    </div>
                </div>
                
                <!-- New Password -->
                <div>
                    <label for="password" class="block text-xs font-medium text-gray-700 dark:text-gray-300 mb-1">New Password</label>
                    <div class="relative" x-data="{ showPassword: false }">
                        <div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
                            <i class="fa-solid fa-key text-gray-400 text-xs"></i>
                        </div>
                        <input 
                            :type="showPassword ? 'text' : 'password'" 
                            id="password" 
                            name="password"
                            class="block w-full pl-8 pr-8 py-2 text-xs border border-gray-200 dark:border-gray-600 rounded-xl bg-white dark:bg-gray-700 text-gray-700 dark:text-gray-300 focus:outline-none focus:ring-2 focus:ring-primary-500 focus:border-primary-500 transition-all"
                            placeholder="Enter your new password"
                            required
                        />
                        <div class="absolute inset-y-0 right-0 pr-3 flex items-center">
                            <button 
                                type="button"
                                @click="showPassword = !showPassword"
                                class="text-gray-400 hover:text-primary-600 dark:hover:text-primary-400 focus:outline-none"
                            >
                                <i x-show="!showPassword" class="fa-solid fa-eye text-xs"></i>
                                <i x-show="showPassword" class="fa-solid fa-eye-slash text-xs"></i>
                            </button>
                        </div>
                    </div>
                </div>
                
                <!-- Confirm Password -->
                <div>
                    <label for="password_confirmation" class="block text-xs font-medium text-gray-700 dark:text-gray-300 mb-1">Confirm Password</label>
                    <div class="relative" x-data="{ showPassword: false }">
                        <div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
                            <i class="fa-solid fa-check-circle text-gray-400 text-xs"></i>
                        </div>
                        <input 
                            :type="showPassword ? 'text' : 'password'" 
                            id="password_confirmation" 
                            name="password_confirmation"
                            class="block w-full pl-8 pr-8 py-2 text-xs border border-gray-200 dark:border-gray-600 rounded-xl bg-white dark:bg-gray-700 text-gray-700 dark:text-gray-300 focus:outline-none focus:ring-2 focus:ring-primary-500 focus:border-primary-500 transition-all"
                            placeholder="Confirm your new password"
                            required
                        />
                        <div class="absolute inset-y-0 right-0 pr-3 flex items-center">
                            <button 
                                type="button"
                                @click="showPassword = !showPassword"
                                class="text-gray-400 hover:text-primary-600 dark:hover:text-primary-400 focus:outline-none"
                            >
                                <i x-show="!showPassword" class="fa-solid fa-eye text-xs"></i>
                                <i x-show="showPassword" class="fa-solid fa-eye-slash text-xs"></i>
                            </button>
                        </div>
                    </div>
                </div>
                
                <!-- Password Requirements Card -->
                <div class="bg-blue-50 dark:bg-blue-900/20 rounded-xl p-3 border border-blue-100 dark:border-blue-800/50">
                    <h4 class="text-blue-800 dark:text-blue-300 font-medium mb-2 flex items-center text-xs">
                        <i class="fa-solid fa-shield text-blue-600 dark:text-blue-400 text-xs mr-1"></i>
                        Password Requirements
                    </h4>
                    <p class="text-xs text-blue-600 dark:text-blue-400 mb-2">Ensure that these requirements are met:</p>
                    <ul class="space-y-1">
                        <li class="flex items-center text-xs text-blue-700 dark:text-blue-300">
                            <div class="h-4 w-4 mr-2 flex items-center justify-center">
                                <div class="h-1.5 w-1.5 bg-blue-500 rounded-full"></div>
                            </div>
                            Minimum 8 characters long - the more, the better
                        </li>
                        <li class="flex items-center text-xs text-blue-700 dark:text-blue-300">
                            <div class="h-4 w-4 mr-2 flex items-center justify-center">
                                <div class="h-1.5 w-1.5 bg-blue-500 rounded-full"></div>
                            </div>
                            At least one lowercase character
                        </li>
                        <li class="flex items-center text-xs text-blue-700 dark:text-blue-300">
                            <div class="h-4 w-4 mr-2 flex items-center justify-center">
                                <div class="h-1.5 w-1.5 bg-blue-500 rounded-full"></div>
                            </div>
                            At least one uppercase character
                        </li>
                        <li class="flex items-center text-xs text-blue-700 dark:text-blue-300">
                            <div class="h-4 w-4 mr-2 flex items-center justify-center">
                                <div class="h-1.5 w-1.5 bg-blue-500 rounded-full"></div>
                            </div>
                            At least one number
                        </li>
                    </ul>
                </div>
                
                <!-- Security Notice -->
                <div class="bg-yellow-50 dark:bg-yellow-900/20 rounded-xl p-3 border border-yellow-100 dark:border-yellow-800/50">
                    <div class="flex">
                        <div class="flex-shrink-0">
                            <i class="fa-solid fa-exclamation-triangle text-yellow-500 text-sm"></i>
                        </div>
                        <div class="ml-2">
                            <h3 class="text-xs font-medium text-yellow-800 dark:text-yellow-300">Security Reminder</h3>
                            <p class="mt-1 text-xs text-yellow-700 dark:text-yellow-400">
                                After changing your password, you'll be required to log in again with your new credentials.
                            </p>
                        </div>
                    </div>
                </div>
                
                <!-- Submit Button -->
                <div class="pt-2">
                    <button 
                        type="submit"
                        class="w-full px-4 py-2 border border-transparent rounded-xl text-xs font-medium text-white bg-primary-600 hover:bg-primary-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-primary-500 transition-colors flex items-center justify-center"
                    >
                        <i class="fa-solid fa-save text-xs mr-1"></i>
                        Change Password
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>

<style>
    [x-cloak] { display: none !important; }
</style>

                                </div>
             <?php include 'footer.php'; ?>