<?php
session_start();
include 'layouts/dbconn.php';
include 'layouts/session.php';

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

$msg = "";
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['update_status'])) {
    $equip_id = $_POST['equipment_id'];
    $status = $_POST['status'];
    $notes = $_POST['notes']; // Could be logged to an audit table
    
    $stmt = $conn->prepare("UPDATE lab_equipment SET status = ? WHERE equipment_id = ?");
    $stmt->bind_param("si", $status, $equip_id);
    if ($stmt->execute()) {
        $msg = "Equipment status updated successfully.";
    } else {
        $msg = "Error updating status.";
    }
}

$sql = "SELECT * FROM lab_equipment WHERE department = 'LABORATORY'";
$result = $conn->query($sql);
?>

<?php include 'layouts/main.php'; ?>
<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Quality Control')); ?>
    <?php include 'layouts/head-css.php'; ?>
    <link href="assets/libs/datatables.net-bs5/css/dataTables.bootstrap5.min.css" rel="stylesheet" />
</head>
<body>
    <div id="layout-wrapper">
        <?php include 'layouts/menu.php'; ?>
        <div class="main-content">
            <div class="page-content">
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-12">
                            <h4 class="mb-sm-0">Lab Quality Control</h4>
                        </div>
                    </div>
                    
                    <?php if ($msg): ?><div class="alert alert-success"><?php echo $msg; ?></div><?php endif; ?>
                    
                    <div class="card">
                        <div class="card-header">
                            <h5 class="card-title mb-0">Equipment Status Monitoring</h5>
                        </div>
                        <div class="card-body">
                            <table id="qcTable" class="table table-bordered dt-responsive nowrap">
                                <thead>
                                    <tr>
                                        <th>Equipment Name</th>
                                        <th>Code</th>
                                        <th>Status</th>
                                        <th>Last Maintenance</th>
                                        <th>Next Maintenance</th>
                                        <th>Action</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php while ($row = $result->fetch_assoc()): ?>
                                    <tr>
                                        <td><?php echo htmlspecialchars($row['equipment_name']); ?></td>
                                        <td><?php echo htmlspecialchars($row['equipment_code']); ?></td>
                                        <td>
                                            <span class="badge bg-<?php echo $row['status'] == 'OPERATIONAL' ? 'success' : ($row['status'] == 'UNDER_MAINTENANCE' ? 'warning' : 'danger'); ?>">
                                                <?php echo $row['status']; ?>
                                            </span>
                                        </td>
                                        <td><?php echo $row['last_maintenance_date']; ?></td>
                                        <td><?php echo $row['next_maintenance_date']; ?></td>
                                        <td>
                                            <button type="button" class="btn btn-sm btn-primary" data-bs-toggle="modal" data-bs-target="#statusModal<?php echo $row['equipment_id']; ?>">
                                                Update Status
                                            </button>
                                            
                                            <!-- Modal -->
                                            <div class="modal fade" id="statusModal<?php echo $row['equipment_id']; ?>" tabindex="-1">
                                                <div class="modal-dialog">
                                                    <div class="modal-content">
                                                        <form method="POST">
                                                            <div class="modal-header">
                                                                <h5 class="modal-title">Update Status: <?php echo htmlspecialchars($row['equipment_name']); ?></h5>
                                                                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                                                            </div>
                                                            <div class="modal-body">
                                                                <input type="hidden" name="equipment_id" value="<?php echo $row['equipment_id']; ?>">
                                                                <div class="mb-3">
                                                                    <label class="form-label">Status</label>
                                                                    <select class="form-select" name="status">
                                                                        <option value="OPERATIONAL" <?php echo $row['status']=='OPERATIONAL'?'selected':''; ?>>Operational</option>
                                                                        <option value="UNDER_MAINTENANCE" <?php echo $row['status']=='UNDER_MAINTENANCE'?'selected':''; ?>>Under Maintenance</option>
                                                                        <option value="OUT_OF_SERVICE" <?php echo $row['status']=='OUT_OF_SERVICE'?'selected':''; ?>>Out of Service</option>
                                                                    </select>
                                                                </div>
                                                                <div class="mb-3">
                                                                    <label class="form-label">Notes</label>
                                                                    <textarea class="form-control" name="notes"></textarea>
                                                                </div>
                                                            </div>
                                                            <div class="modal-footer">
                                                                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                                                                <button type="submit" name="update_status" class="btn btn-primary">Save Changes</button>
                                                            </div>
                                                        </form>
                                                    </div>
                                                </div>
                                            </div>
                                        </td>
                                    </tr>
                                    <?php endwhile; ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>
    <?php include 'layouts/vendor-scripts.php'; ?>
    <script src="assets/libs/datatables.net/js/jquery.dataTables.min.js"></script>
    <script src="assets/libs/datatables.net-bs5/js/dataTables.bootstrap5.min.js"></script>
    <script>$('#qcTable').DataTable();</script>
</body>
</html>