<?php
/**
 * Admin Password Reset Script
 * 
 * This script allows you to reset the password for an admin user.
 * 
 * SECURITY WARNING: This script should be deleted or secured after use!
 * 
 * Usage:
 * 1. Access this file via browser: http://yourdomain.com/reset_admin_password.php
 * 2. Enter the admin username and new password
 * 3. The password will be reset immediately
 * 
 * IMPORTANT: Delete this file after resetting the password for security!
 */

require_once 'config.php';

// Simple security check - you can add IP restriction here if needed
// For example, only allow from localhost:
// $allowedIPs = ['127.0.0.1', '::1'];
// if (!in_array($_SERVER['REMOTE_ADDR'], $allowedIPs)) {
//     die('Access denied');
// }

$message = '';
$messageType = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = trim($_POST['username'] ?? '');
    $newPassword = $_POST['new_password'] ?? '';
    $confirmPassword = $_POST['confirm_password'] ?? '';
    
    if (empty($username)) {
        $message = 'Please enter a username';
        $messageType = 'error';
    } elseif (empty($newPassword)) {
        $message = 'Please enter a new password';
        $messageType = 'error';
    } elseif (strlen($newPassword) < 6) {
        $message = 'Password must be at least 6 characters long';
        $messageType = 'error';
    } elseif ($newPassword !== $confirmPassword) {
        $message = 'Passwords do not match';
        $messageType = 'error';
    } else {
        // Check if admin user exists
        $checkStmt = $conn->prepare("SELECT id, username FROM admin_users WHERE username = ?");
        $checkStmt->bind_param("s", $username);
        $checkStmt->execute();
        $result = $checkStmt->get_result();
        
        if ($result->num_rows === 0) {
            $message = 'Admin user not found';
            $messageType = 'error';
        } else {
            // Hash the new password
            $passwordHash = password_hash($newPassword, PASSWORD_DEFAULT);
            
            // Update the password
            $updateStmt = $conn->prepare("UPDATE admin_users SET password_hash = ?, updated_at = NOW() WHERE username = ?");
            $updateStmt->bind_param("ss", $passwordHash, $username);
            
            if ($updateStmt->execute()) {
                $message = "Password reset successfully for user: {$username}";
                $messageType = 'success';
            } else {
                $message = 'Error resetting password: ' . $updateStmt->error;
                $messageType = 'error';
            }
            $updateStmt->close();
        }
        $checkStmt->close();
    }
}

// Get list of admin users
$adminUsersStmt = $conn->query("SELECT id, username, role, status FROM admin_users ORDER BY username");
$adminUsers = [];
if ($adminUsersStmt) {
    while ($row = $adminUsersStmt->fetch_assoc()) {
        $adminUsers[] = $row;
    }
    $adminUsersStmt->close();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Reset Admin Password</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }
        .container {
            background: white;
            border-radius: 12px;
            box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
            max-width: 500px;
            width: 100%;
            padding: 40px;
        }
        h1 {
            color: #333;
            margin-bottom: 10px;
            font-size: 28px;
        }
        .subtitle {
            color: #666;
            margin-bottom: 30px;
            font-size: 14px;
        }
        .warning {
            background: #fff3cd;
            border: 1px solid #ffc107;
            color: #856404;
            padding: 15px;
            border-radius: 8px;
            margin-bottom: 25px;
            font-size: 13px;
        }
        .warning strong {
            display: block;
            margin-bottom: 5px;
        }
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            color: #333;
            font-weight: 600;
            margin-bottom: 8px;
            font-size: 14px;
        }
        input[type="text"],
        input[type="password"] {
            width: 100%;
            padding: 12px;
            border: 2px solid #e0e0e0;
            border-radius: 8px;
            font-size: 14px;
            transition: border-color 0.3s;
        }
        input[type="text"]:focus,
        input[type="password"]:focus {
            outline: none;
            border-color: #667eea;
        }
        .btn {
            width: 100%;
            padding: 14px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: transform 0.2s, box-shadow 0.2s;
        }
        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 20px rgba(102, 126, 234, 0.3);
        }
        .btn:active {
            transform: translateY(0);
        }
        .message {
            padding: 15px;
            border-radius: 8px;
            margin-bottom: 20px;
            font-size: 14px;
        }
        .message.success {
            background: #d4edda;
            border: 1px solid #c3e6cb;
            color: #155724;
        }
        .message.error {
            background: #f8d7da;
            border: 1px solid #f5c6cb;
            color: #721c24;
        }
        .admin-list {
            background: #f8f9fa;
            border-radius: 8px;
            padding: 15px;
            margin-bottom: 25px;
        }
        .admin-list h3 {
            font-size: 14px;
            color: #333;
            margin-bottom: 10px;
        }
        .admin-list ul {
            list-style: none;
            font-size: 13px;
        }
        .admin-list li {
            padding: 5px 0;
            color: #666;
        }
        .admin-list li strong {
            color: #333;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>🔐 Reset Admin Password</h1>
        <p class="subtitle">Reset password for admin users</p>
        
        <div class="warning">
            <strong>⚠️ Security Warning:</strong>
            This script should be deleted or secured after use. It allows password reset without authentication.
        </div>
        
        <?php if (!empty($message)): ?>
        <div class="message <?php echo $messageType; ?>">
            <?php echo htmlspecialchars($message); ?>
        </div>
        <?php endif; ?>
        
        <?php if (!empty($adminUsers)): ?>
        <div class="admin-list">
            <h3>Available Admin Users:</h3>
            <ul>
                <?php foreach ($adminUsers as $admin): ?>
                <li>
                    <strong><?php echo htmlspecialchars($admin['username']); ?></strong> 
                    (<?php echo htmlspecialchars($admin['role']); ?>) 
                    - Status: <?php echo htmlspecialchars($admin['status']); ?>
                </li>
                <?php endforeach; ?>
            </ul>
        </div>
        <?php endif; ?>
        
        <form method="POST">
            <div class="form-group">
                <label for="username">Admin Username</label>
                <input 
                    type="text" 
                    id="username" 
                    name="username" 
                    required 
                    autocomplete="username"
                    placeholder="Enter admin username"
                    value="<?php echo htmlspecialchars($_POST['username'] ?? ''); ?>"
                >
            </div>
            
            <div class="form-group">
                <label for="new_password">New Password</label>
                <input 
                    type="password" 
                    id="new_password" 
                    name="new_password" 
                    required 
                    autocomplete="new-password"
                    placeholder="Enter new password (min 6 characters)"
                    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 
                    autocomplete="new-password"
                    placeholder="Confirm new password"
                    minlength="6"
                >
            </div>
            
            <button type="submit" class="btn">Reset Password</button>
        </form>
    </div>
</body>
</html>

