<?php
session_start();
include '../db_config.php';
include '../functions.php';

check_login();
check_role('admin');

$msg = '';
$error = '';

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $current_pass = $_POST['current_password'];
    $new_pass = $_POST['new_password'];
    $confirm_pass = $_POST['confirm_password'];
    $username = $_SESSION['username'];

    if ($new_pass !== $confirm_pass) {
        $error = "New passwords do not match.";
    } else {
        // Verify current password
        $stmt = $conn->prepare("SELECT password FROM admin_login WHERE username = ?");
        $stmt->bind_param("s", $username);
        $stmt->execute();
        $stmt->bind_result($hash);
        $stmt->fetch();
        $stmt->close();

        if (password_verify($current_pass, $hash)) {
            // Update password
            $new_hash = password_hash($new_pass, PASSWORD_DEFAULT);
            $stmt = $conn->prepare("UPDATE admin_login SET password = ? WHERE username = ?");
            $stmt->bind_param("ss", $new_hash, $username);
            if ($stmt->execute()) {
                $msg = "Password updated successfully.";
            } else {
                $error = "Error updating password.";
            }
            $stmt->close();
        } else {
            $error = "Incorrect current password.";
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Change Password | Admin Panel</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <script src="https://unpkg.com/lucide@latest"></script>
    <style> body { font-family: 'Poppins', sans-serif; } </style>
</head>
<body class="bg-indigo-50 min-h-screen">

    <!-- Navbar -->
    <nav class="bg-white shadow-sm sticky top-0 z-50">
        <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="flex justify-between h-20 items-center">
                <a href="<?= BASE_URL ?>/index.php" class="text-2xl font-bold italic text-slate-800">
                    Work For <span class="text-indigo-600">Sri Lanka</span>
                    <span class="text-xs font-normal text-white bg-slate-800 px-2 py-0.5 rounded ml-2 align-middle">Admin Panel</span>
                </a>
                <div class="flex items-center gap-6">
                    <span class="text-sm font-medium text-slate-500 hidden md:inline">Logged in as: <span class="text-indigo-600"><?=$_SESSION['username']?></span></span>
                    <a href="../logout.php" class="text-slate-500 hover:text-red-600 font-medium transition text-sm flex items-center">
                        <i data-lucide="log-out" class="w-4 h-4 mr-2"></i> Logout
                    </a>
                </div>
            </div>
        </div>
    </nav>

    <div class="max-w-md mx-auto mt-10 px-4">
        <div class="bg-white rounded-3xl shadow-xl overflow-hidden border border-slate-100 p-8">
            <div class="mb-6 text-center">
                <div class="inline-flex items-center justify-center w-12 h-12 rounded-full bg-indigo-100 mb-4 text-indigo-600">
                    <i data-lucide="lock" class="w-6 h-6"></i>
                </div>
                <h2 class="text-2xl font-bold text-slate-800">Change Password</h2>
                <p class="text-slate-500 text-sm mt-1">Update your account security.</p>
            </div>

            <?php if($msg): ?>
                <div class="bg-green-50 border border-green-200 text-green-700 px-4 py-3 rounded-xl mb-6 text-sm flex items-center">
                    <i data-lucide="check-circle" class="w-4 h-4 mr-2"></i>
                    <?=$msg?>
                </div>
            <?php endif; ?>

            <?php if($error): ?>
                <div class="bg-red-50 border border-red-200 text-red-700 px-4 py-3 rounded-xl mb-6 text-sm flex items-center">
                    <i data-lucide="alert-circle" class="w-4 h-4 mr-2"></i>
                    <?=$error?>
                </div>
            <?php endif; ?>

            <form method="post" class="space-y-4">
                <div>
                    <label class="block text-sm font-medium text-slate-700 mb-1">Current Password</label>
                    <input type="password" name="current_password" required class="w-full px-4 py-2 border border-slate-200 rounded-xl focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 outline-none transition">
                </div>
                <div>
                    <label class="block text-sm font-medium text-slate-700 mb-1">New Password</label>
                    <input type="password" name="new_password" required class="w-full px-4 py-2 border border-slate-200 rounded-xl focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 outline-none transition">
                </div>
                <div>
                    <label class="block text-sm font-medium text-slate-700 mb-1">Confirm New Password</label>
                    <input type="password" name="confirm_password" required class="w-full px-4 py-2 border border-slate-200 rounded-xl focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 outline-none transition">
                </div>
                
                <div class="pt-2 flex gap-3">
                    <button type="submit" class="flex-1 bg-indigo-600 hover:bg-indigo-700 text-white font-bold py-2.5 rounded-xl shadow-lg shadow-indigo-200 transition active:scale-[0.98]">
                        Update Password
                    </button>
                    <a href="dashboard.php" class="bg-slate-100 hover:bg-slate-200 text-slate-600 font-bold py-2.5 px-4 rounded-xl transition text-center">
                        Cancel
                    </a>
                </div>
            </form>
        </div>
    </div>

    <script>
        lucide.createIcons();
    </script>
</body>
</html>
