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

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

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $nic = sanitize_input($_POST['nic']);
    $old_pass = $_POST['old_password'];
    $new_pass = $_POST['new_password'];
    $confirm_pass = $_POST['confirm_password'];

    // Verify User
    $stmt = $conn->prepare("SELECT nic, password FROM employee_login WHERE nic=?");
    $stmt->bind_param("s", $nic);
    $stmt->execute();
    $stmt->bind_result($fetched_nic, $hash);
    
    if($stmt->fetch()) {
        $stmt->close(); // Close previous statement
        
        // Check Old Password
        if(password_verify($old_pass, $hash)) {
            // Check New Password Match
            if($new_pass === $confirm_pass) {
                // Update Password
                $new_hash = password_hash($new_pass, PASSWORD_DEFAULT);
                $update = $conn->prepare("UPDATE employee_login SET password=? WHERE nic=?");
                $update->bind_param("ss", $new_hash, $nic);
                
                if($update->execute()) {
                    $msg = "Password updated successfully. You can now login.";
                } else {
                    $error = "Error updating password. Please try again.";
                }
            } else {
                $error = "New passwords do not match.";
            }
        } else {
            $error = "Current password is incorrect.";
        }
    } else {
        $error = "No worker account found (NIC not registered).";
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Change Password | Worker</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">
    <style> body { font-family: 'Poppins', sans-serif; } </style>
</head>
<body class="bg-indigo-50 min-h-screen flex items-center justify-center p-4">
    
    <div class="max-w-md w-full bg-white rounded-3xl shadow-xl overflow-hidden border border-slate-100">
        <div class="bg-emerald-600 p-8 text-center">
            <h1 class="text-2xl font-bold text-white mb-2">Change Password</h1>
            <p class="text-emerald-100 text-sm">Secure your worker account</p>
        </div>

        <div class="p-8">
            <?php if($msg): ?>
                <div class="bg-emerald-50 border border-emerald-200 text-emerald-700 px-4 py-3 rounded-xl mb-6 text-sm text-center">
                    <?=$msg?>
                    <div class="mt-2">
                        <a href="register.php?mode=login" class="font-bold underline">Login Now</a>
                    </div>
                </div>
            <?php endif; ?>

            <?php if($error): ?>
                <div class="bg-rose-50 border border-rose-200 text-rose-700 px-4 py-3 rounded-xl mb-6 text-sm text-center">
                    <?=$error?>
                </div>
            <?php endif; ?>

            <?php if(!$msg): ?>
            <form method="post" class="space-y-4">
                <div>
                    <label class="block text-xs font-bold text-slate-600 uppercase mb-1">NIC Number</label>
                    <input type="text" name="nic" required class="w-full px-4 py-3 bg-slate-50 border border-slate-200 rounded-xl text-sm focus:bg-white focus:border-emerald-500 outline-none transition" placeholder="Enter Registration NIC">
                </div>
                
                <div>
                    <label class="block text-xs font-bold text-slate-600 uppercase mb-1">Current Password</label>
                    <input type="password" name="old_password" required class="w-full px-4 py-3 bg-slate-50 border border-slate-200 rounded-xl text-sm focus:bg-white focus:border-emerald-500 outline-none transition" placeholder="Enter current password">
                </div>

                <div class="border-t border-slate-100 my-4 pt-4">
                    <label class="block text-xs font-bold text-slate-600 uppercase mb-1">New Password</label>
                    <input type="password" name="new_password" required class="w-full px-4 py-3 bg-slate-50 border border-slate-200 rounded-xl text-sm focus:bg-white focus:border-emerald-500 outline-none transition" placeholder="Create new password">
                </div>

                <div>
                    <label class="block text-xs font-bold text-slate-600 uppercase mb-1">Confirm New Password</label>
                    <input type="password" name="confirm_password" required class="w-full px-4 py-3 bg-slate-50 border border-slate-200 rounded-xl text-sm focus:bg-white focus:border-emerald-500 outline-none transition" placeholder="Repeat new password">
                </div>

                <div class="pt-2">
                    <button type="submit" class="w-full bg-emerald-600 hover:bg-emerald-700 text-white font-bold py-3 rounded-xl shadow-lg transition transform active:scale-95">
                        Update Password
                    </button>
                </div>
            </form>
            <?php endif; ?>

            <div class="mt-6 text-center">
                <a href="register.php?mode=login" class="text-slate-400 text-xs hover:text-slate-600 font-medium">Back to Login</a>
            </div>
        </div>
    </div>

</body>
</html>
