<?php
session_start();
require_once 'config.php';

// Check if logged in
$logged_in = isset($_SESSION['admin_logged_in']) && $_SESSION['admin_logged_in'] === true;

if (!$logged_in) {
    header('Location: admin.php');
    exit;
}

// Handle password change
$password_change_success = false;
$password_change_error = '';

if (isset($_POST['change_password'])) {
    $current_password = $_POST['current_password'] ?? '';
    $new_password = $_POST['new_password'] ?? '';
    $confirm_password = $_POST['confirm_password'] ?? '';
    
    if (empty($current_password) || empty($new_password) || empty($confirm_password)) {
        $password_change_error = 'Please fill in all fields';
    } elseif ($new_password !== $confirm_password) {
        $password_change_error = 'New passwords do not match';
    } elseif (strlen($new_password) < 6) {
        $password_change_error = 'Password must be at least 6 characters long';
    } else {
        $conn = getDBConnection();
        $admin_id = $_SESSION['admin_id'] ?? 0;
        $stmt = $conn->prepare("SELECT password FROM admins WHERE id = ?");
        $stmt->bind_param("i", $admin_id);
        $stmt->execute();
        $result = $stmt->get_result();
        
        if ($result->num_rows === 1) {
            $admin = $result->fetch_assoc();
            if (password_verify($current_password, $admin['password'])) {
                $new_password_hash = password_hash($new_password, PASSWORD_DEFAULT);
                $stmt->close();
                $stmt = $conn->prepare("UPDATE admins SET password = ? WHERE id = ?");
                $stmt->bind_param("si", $new_password_hash, $admin_id);
                
                if ($stmt->execute()) {
                    $password_change_success = true;
                } else {
                    $password_change_error = 'Failed to update password';
                }
            } else {
                $password_change_error = 'Current password is incorrect';
            }
        }
        
        $stmt->close();
        $conn->close();
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Change Password - Evently</title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        html, body {
            height: 100%;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
            background-color: #000000;
            color: #333;
            padding: 0;
            margin: 0;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
        }

        .navbar {
            background-color: #ffffff;
            padding: 15px 20px;
            border-bottom: 1px solid #e0e0e0;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .navbar-brand {
            color: #000000;
            font-family: 'Inter', sans-serif;
            font-size: 20px;
            font-weight: 700;
            text-decoration: none;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .navbar-brand img {
            height: 30px;
            width: auto;
        }

        .navbar-nav {
            display: flex;
            gap: 20px;
            align-items: center;
        }

        .navbar-nav a {
            color: #000000;
            text-decoration: none;
            font-size: 14px;
            transition: opacity 0.2s;
        }

        .navbar-nav a:hover {
            opacity: 0.7;
        }

        .navbar-nav span {
            color: #000000;
        }

        .hamburger {
            display: none;
            flex-direction: column;
            cursor: pointer;
            padding: 5px;
            gap: 4px;
        }

        .hamburger span {
            width: 25px;
            height: 3px;
            background-color: #000000;
            transition: all 0.3s;
        }

        .hamburger.active span:nth-child(1) {
            transform: rotate(45deg) translate(5px, 5px);
        }

        .hamburger.active span:nth-child(2) {
            opacity: 0;
        }

        .hamburger.active span:nth-child(3) {
            transform: rotate(-45deg) translate(7px, -6px);
        }

        @media (max-width: 768px) {
            .hamburger {
                display: flex;
            }

            .navbar-nav {
                position: fixed;
                top: 60px;
                left: 0;
                right: 0;
                background-color: #ffffff;
                flex-direction: column;
                padding: 20px;
                border-bottom: 1px solid #e0e0e0;
                box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
                transform: translateY(-100%);
                opacity: 0;
                visibility: hidden;
                transition: all 0.3s ease;
                z-index: 1000;
            }

            .navbar-nav.active {
                transform: translateY(0);
                opacity: 1;
                visibility: visible;
            }

            .navbar-nav a,
            .navbar-nav span {
                padding: 12px 0;
                border-bottom: 1px solid #f0f0f0;
                width: 100%;
            }

            .navbar-nav a:last-child,
            .navbar-nav span:last-child {
                border-bottom: none;
            }
        }

        .content-wrapper {
            padding: 20px;
            flex: 1;
        }

        .container {
            max-width: 800px;
            margin: 0 auto;
            background-color: #ffffff;
            border-radius: 0;
            padding: 40px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
        }

        h1 {
            font-size: 28px;
            font-weight: 400;
            margin-bottom: 30px;
            color: #2c3e50;
        }

        .form-group {
            margin-bottom: 20px;
        }

        label {
            display: block;
            margin-bottom: 8px;
            font-size: 14px;
            font-weight: 500;
            color: #495057;
        }

        input[type="password"] {
            width: 100%;
            padding: 12px;
            border: 1px solid #ced4da;
            border-radius: 0;
            font-size: 16px;
            font-family: inherit;
            transition: border-color 0.2s;
        }

        input[type="password"]:focus {
            outline: none;
            border-color: #667eea;
        }

        .btn {
            padding: 12px 30px;
            background-color: #000000;
            color: #ffffff;
            border: none;
            border-radius: 0;
            font-size: 16px;
            cursor: pointer;
            transition: transform 0.2s, box-shadow 0.2s;
            width: 100%;
        }

        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.4);
            background-color: #333333;
        }

        .alert {
            padding: 15px;
            border-radius: 0;
            margin-bottom: 20px;
        }

        .alert-success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }

        .alert-error {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }

        .nav-links {
            margin-bottom: 20px;
        }

        .nav-links a {
            color: #2c3e50;
            text-decoration: none;
            margin-right: 15px;
            font-size: 14px;
        }

        .nav-links a:hover {
            text-decoration: underline;
        }

        .footer {
            background-color: #ffffff;
            padding: 20px;
            border-top: 1px solid #e0e0e0;
            text-align: center;
            color: #666;
            font-size: 14px;
            margin-top: auto;
        }

        @media (max-width: 768px) {
            .container {
                padding: 20px 15px;
            }

            .content-wrapper {
                padding: 10px;
            }

            h1 {
                font-size: 24px;
                margin-bottom: 20px;
            }

            .form-group {
                margin-bottom: 15px;
            }

            input[type="text"],
            input[type="password"] {
                font-size: 16px; /* Prevents zoom on iOS */
            }

            .btn {
                padding: 12px 20px;
                font-size: 16px;
            }
        }
    </style>
</head>
<body>
    <nav class="navbar">
        <a href="admin.php" class="navbar-brand">
            <img src="assets/img/logo.svg" alt="Evently">
            <span>Evently - Admin Panel</span>
        </a>
        <div class="navbar-nav">
            <a href="admin.php">Upload Event</a>
            <a href="admin_events.php">View Events</a>
            <a href="admin_password.php">Change Password</a>
            <span><?php echo htmlspecialchars($_SESSION['admin_username'] ?? 'Admin'); ?></span>
            <a href="admin.php?logout=1">Logout</a>
        </div>
    </nav>
    <div class="content-wrapper">
        <div class="container">
            <h1>Change Password</h1>
            
            <?php if ($password_change_success): ?>
                <div class="alert alert-success">Password changed successfully!</div>
            <?php endif; ?>
            
            <?php if ($password_change_error): ?>
                <div class="alert alert-error"><?php echo htmlspecialchars($password_change_error); ?></div>
            <?php endif; ?>
            
            <form method="POST">
                <div class="form-group">
                    <label for="current_password">Current Password</label>
                    <input type="password" id="current_password" name="current_password" required>
                </div>
                <div class="form-group">
                    <label for="new_password">New Password</label>
                    <input type="password" id="new_password" name="new_password" required minlength="6">
                </div>
                <div class="form-group">
                    <label for="confirm_password">Confirm New Password</label>
                    <input type="password" id="confirm_password" name="confirm_password" required minlength="6">
                </div>
                <button type="submit" name="change_password" class="btn">Change Password</button>
            </form>
        </div>
    </div>
    <footer class="footer">
        <p>&copy; <?php echo date('Y'); ?> Evently. All rights reserved.</p>
    </footer>
</body>
</html>

