<?php
session_start();
include 'db.php';

// පරිශීලකයා දැනටමත් Log වී ඇත්නම් ස්වයංක්‍රීයව අදාළ පිටුවට යොමු කිරීම
if (isset($_SESSION['user_id'])) {
    $u_name = strtolower(trim($_SESSION['username'] ?? ''));
    if ($u_name === 'secretary' || $u_name === 'admin') {
        header("Location: secretary_dashboard.php");
        exit();
    } elseif ($u_name === 'establishment' || $u_name === 'accounts') {
        header("Location: officers_list.php");
        exit();
    } else {
        header("Location: staff_dashboard.php");
        exit();
    }
}

$error = "";

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $username = $conn->real_escape_string(trim($_POST['username']));
    $password = trim($_POST['password']);

    $sql = "SELECT * FROM users WHERE LOWER(username) = '" . strtolower($username) . "'";
    $result = $conn->query($sql);

    if ($result && $result->num_rows > 0) {
        $user = $result->fetch_assoc();
        
        if ($password === $user['password']) {
            $_SESSION['user_id']           = $user['id'];
            $_SESSION['username']          = $user['username'];
            $_SESSION['officer_title']     = $user['officer_title'];
            $_SESSION['allowed_divisions'] = explode(',', $user['allowed_divisions']);

            $u_name = strtolower(trim($user['username']));

            // ලේකම් සහ Admin කෙලින්ම secretary_dashboard.php වෙත යැවීම
            if ($u_name === 'secretary' || $u_name === 'admin') {
                header("Location: secretary_dashboard.php");
            } elseif ($u_name === 'establishment' || $u_name === 'accounts') {
                header("Location: officers_list.php");
            } else {
                header("Location: staff_dashboard.php");
            }
            exit();
        } else {
            $error = "ඇතුළත් කළ මුරපදය (Password) අසත්‍ය වේ!";
        }
    } else {
        $error = "මෙම පරිශීලක නාමය (Username) පද්ධතිය තුළ ලියාපදිංචි කර නොමැත!";
    }
}
?>

<!DOCTYPE html>
<html lang="si">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>පද්ධති පිවිසුම - මාතලේ ප්‍රාදේශීය ලේකම් කාර්යාලය</title>
    <style>
        :root {
            --primary-blue: #0f2b5c;
            --secondary-blue: #1e40af;
            --accent-gold: #d4af37;
            --bg-gradient: linear-gradient(135deg, #091a3a 0%, #102a45 40%, #1a365d 100%);
        }

        body { 
            font-family: 'Iskoola Pota', 'Segoe UI', Arial, sans-serif; 
            background: var(--bg-gradient); 
            margin: 0; 
            padding: 20px; 
            display: flex; 
            justify-content: center; 
            align-items: center; 
            min-height: 100vh;
            box-sizing: border-box;
        }

        .login-wrapper { width: 100%; max-width: 440px; animation: fadeIn 0.6s ease-in-out; }

        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(-15px); }
            to { opacity: 1; transform: translateY(0); }
        }

        .login-card { 
            background: #ffffff; 
            border-radius: 12px; 
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.35); 
            overflow: hidden;
            border-top: 6px solid var(--accent-gold);
            border-bottom: 2px solid var(--accent-gold);
        }

        .card-header {
            background: linear-gradient(180deg, #f8fafc 0%, #edf2f7 100%);
            padding: 28px 25px 18px 25px;
            text-align: center;
            border-bottom: 1px solid #e2e8f0;
        }

        .emblem-box {
            width: 90px;
            height: 90px;
            margin: 0 auto 12px auto;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .emblem-box img {
            max-width: 100%;
            max-height: 100%;
            object-fit: contain;
            filter: drop-shadow(0px 3px 6px rgba(0,0,0,0.15));
        }

        .republic-title {
            font-size: 11px;
            font-weight: bold;
            color: #718096;
            letter-spacing: 0.5px;
            margin: 0 0 4px 0;
            text-transform: uppercase;
        }

        .inst-title { 
            color: var(--primary-blue); 
            margin: 0 0 6px 0; 
            font-size: 20px; 
            font-weight: bold;
            line-height: 1.3;
        }

        .system-subtitle { 
            color: #4a5568; 
            font-size: 12px; 
            margin: 0;
            font-weight: 600;
            background: #e2e8f0;
            display: inline-block;
            padding: 3px 12px;
            border-radius: 12px;
        }

        .card-body { padding: 28px 30px 30px 30px; }

        .form-group { text-align: left; margin-bottom: 20px; }

        .form-group label { display: block; font-weight: bold; margin-bottom: 7px; color: #2d3748; font-size: 13px; }

        .input-wrapper { position: relative; display: flex; align-items: center; }

        .input-icon { position: absolute; left: 12px; font-size: 16px; color: #718096; pointer-events: none; }

        .form-control { 
            width: 100%; 
            padding: 11px 12px 11px 38px; 
            border: 1px solid #cbd5e0; 
            border-radius: 6px; 
            box-sizing: border-box; 
            font-size: 14px; 
            transition: all 0.25s ease;
            background: #fafafa;
        }

        .form-control:focus {
            border-color: var(--secondary-blue);
            background: #ffffff;
            box-shadow: 0 0 0 3px rgba(30, 64, 175, 0.15);
            outline: none;
        }

        .btn-login { 
            background: linear-gradient(135deg, var(--primary-blue) 0%, var(--secondary-blue) 100%); 
            color: #ffffff; 
            border: none; 
            padding: 12px; 
            width: 100%; 
            border-radius: 6px; 
            font-size: 15px; 
            font-weight: bold; 
            cursor: pointer; 
            transition: all 0.25s ease;
            box-shadow: 0 4px 10px rgba(15, 43, 92, 0.3);
            margin-top: 5px;
        }

        .btn-login:hover { 
            background: linear-gradient(135deg, #0b2046 0%, #1d4ed8 100%);
            box-shadow: 0 6px 15px rgba(15, 43, 92, 0.45);
            transform: translateY(-1px);
        }

        .error-msg { 
            background: #fff5f5; 
            color: #c53030; 
            border-left: 4px solid #e53e3e;
            padding: 10px 12px; 
            border-radius: 4px; 
            margin-bottom: 20px; 
            font-size: 13px; 
            font-weight: bold; 
            text-align: left;
        }

        .footer-text { margin-top: 22px; font-size: 11px; color: #a0aec0; text-align: center; }
    </style>
</head>
<body>

    <div class="login-wrapper">
        <div class="login-card">
            <div class="card-header">
                <div class="emblem-box">
                    <img src="emblem.png" alt="ශ්‍රී ලංකා රාජ්‍ය ලංඡනය" onerror="this.src='https://upload.wikimedia.org/wikipedia/commons/5/5f/Emblem_of_Sri_Lanka.svg'">
                </div>
                <div class="republic-title">ශ්‍රී ලංකා ප්‍රජාතාන්ත්‍රික සමාජවාදී ජනරජය</div>
                <h2 class="inst-title">මාතලේ ප්‍රාදේශීය ලේකම් කාර්යාලය</h2>
                <div class="system-subtitle">තොරතුරු & කළමනාකරණ පද්ධති පිවිසුම</div>
            </div>

            <div class="card-body">
                <?php if($error): ?>
                    <div class="error-msg">⚠️ <?php echo $error; ?></div>
                <?php endif; ?>

                <form method="POST" autocomplete="off">
                    <div class="form-group">
                        <label for="username">පරිශීලක නාමය (Username):</label>
                        <div class="input-wrapper">
                            <span class="input-icon">👤</span>
                            <input type="text" id="username" name="username" class="form-control" required placeholder="උදා: secretary / establishment / accounts">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="password">මුරපදය (Password):</label>
                        <div class="input-wrapper">
                            <span class="input-icon">🔒</span>
                            <input type="password" id="password" name="password" class="form-control" required placeholder="ඔබගේ මුරපදය ඇතුළත් කරන්න">
                        </div>
                    </div>

                    <button type="submit" class="btn-login">🔑 පද්ධතියට පිවිසෙන්න</button>
                </form>

                <div class="footer-text">
                    © <?php echo date('Y'); ?> මාතලේ ප්‍රාදේශීය ලේකම් කාර්යාලය.<br>තොරතුරු තාක්ෂණ අංශයේ නිර්මාණයකි.
                </div>
            </div>
        </div>
    </div>

</body>
</html>