<?php
session_start();
require_once __DIR__ . "/../config/db.php";

if (!isset($_SESSION['user_id'])) {
    header("Location: ../login.php");
    exit();
}

$user_id = $_SESSION['user_id'];

/* FETCH ACTIVE EQUIPMENT */
$equipment = mysqli_query($conn, 
    "SELECT id, equipment_name, serial_number, department_id
     FROM equipment
     WHERE status='Active'
     ORDER BY equipment_name ASC"
);

/* SAVE REQUEST */
if ($_SERVER["REQUEST_METHOD"] == "POST") {

    $equipment_id = intval($_POST['equipment_id']);
    $issue_description = mysqli_real_escape_string($conn, $_POST['issue_description']);
    $urgency = $_POST['urgency'];

    /* GET DEPARTMENT FROM EQUIPMENT */
    $deptQuery = mysqli_query($conn, 
        "SELECT department_id FROM equipment WHERE id = $equipment_id LIMIT 1"
    );
    $deptData = mysqli_fetch_assoc($deptQuery);
    $department_id = $deptData['department_id'];

    $stmt = mysqli_prepare($conn,
        "INSERT INTO maintenance_request
        (equipment_id, requested_by, issue_description, urgency, department_id, status)
        VALUES (?, ?, ?, ?, ?, 'Pending')"
    );

    mysqli_stmt_bind_param($stmt, "iissi",
        $equipment_id,
        $user_id,
        $issue_description,
        $urgency,
        $department_id
    );

    mysqli_stmt_execute($stmt);

    header("Location: ../maintenance.php");
    exit();
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Request Maintenance</title>
    <link rel="stylesheet" href="../assets/style.css">
    <link rel="stylesheet" href="../assets/maintenance.css">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css" rel="stylesheet">
</head>

<body>

<?php include __DIR__ . '/../includes/sidebar.php'; ?>

<div class="main">

<div class="page-header">
    <div>
        <h1>Maintenance Request</h1>
        <p>Submit maintenance request for equipment issue.</p>
    </div>
    <a href="../maintenance.php" class="btn cancel">
        <i class="fas fa-arrow-left"></i> Back
    </a>
</div>

<div class="form-container">

<form method="POST" class="two-column-form">

<div class="form-left">

    <label>Equipment *</label>
    <select name="equipment_id" required>
        <option value="">Select equipment...</option>
        <?php while($eq = mysqli_fetch_assoc($equipment)): ?>
            <option value="<?= $eq['id'] ?>">
                <?= htmlspecialchars($eq['equipment_name']) ?>
                (<?= htmlspecialchars($eq['serial_number']) ?>)
            </option>
        <?php endwhile; ?>
    </select>

    <label>Urgency *</label>
    <select name="urgency" required>
        <option value="Low">Low</option>
        <option value="Medium" selected>Medium</option>
        <option value="High">High</option>
        <option value="Critical">Critical</option>
    </select>

    <label>Issue Description *</label>
    <textarea name="issue_description" rows="5"
        placeholder="Describe the issue in detail..." required></textarea>

</div>

<div class="form-actions">
    <button type="submit" class="btn primary">
        <i class="fas fa-paper-plane"></i> Submit Request
    </button>
</div>

</form>
</div>

</div>

</body>
</html>
