<?php
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}

// Check if employee is logged in and the session ID is set
if ($_SESSION['role'] != 'Employee' || !isset($_SESSION['id'])) {
    header('Location: login.php'); // Redirect to login page if not logged in
    exit();
}

// Connect to the database
include('config.php');

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // Get form data
    $employee_id = $_SESSION['id']; // Employee ID from session
    $grievance_title = mysqli_real_escape_string($conn, $_POST['grievance_title']);
    $grievance_description = mysqli_real_escape_string($conn, $_POST['grievance_description']);
    
    // Insert grievance into database
    $sql = "INSERT INTO grievances (employee_id, title, description, status, created_at) 
            VALUES ('$employee_id', '$grievance_title', '$grievance_description', 'Pending', NOW())";
    
    if (mysqli_query($conn, $sql)) {
        $_SESSION['message'] = "Grievance submitted successfully.";
        header('Location: grievance.php');
        exit();
    } else {
        $_SESSION['error'] = "Error submitting grievance: " . mysqli_error($conn);
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Apply Grievance</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            background-color: #f8f9fa;
        }
        .form-container {
            width: 50%;
            padding: 20px;
            background-color: #fff;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        }
    </style>
</head>
<body>
    <div class="form-container">
        <h2>Apply Grievance</h2>

        <?php if (isset($_SESSION['message'])): ?>
            <div class="alert alert-success">
                <?= $_SESSION['message']; ?>
            </div>
            <?php unset($_SESSION['message']); ?>
        <?php endif; ?>

        <?php if (isset($_SESSION['error'])): ?>
            <div class="alert alert-danger">
                <?= $_SESSION['error']; ?>
            </div>
            <?php unset($_SESSION['error']); ?>
        <?php endif; ?>

        <form action="apply_grievance.php" method="POST">
            <div class="form-group">
                <label for="grievance_title">Grievance Title</label>
                <input type="text" class="form-control" id="grievance_title" name="grievance_title" required>
            </div>
            <div class="form-group">
                <label for="grievance_description">Grievance Description</label>
                <textarea class="form-control" id="grievance_description" name="grievance_description" rows="5" required></textarea>
            </div>
            <button type="submit" class="btn btn-primary">Submit Grievance</button>
        </form>
    </div>
</body>
</html>
