<?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['log_calibration'])) {
    $equip_id = $_POST['equipment_id'];
    $maintenance_date = $_POST['maintenance_date'];
    $next_date = $_POST['next_date'];
    
    $stmt = $conn->prepare("UPDATE lab_equipment SET last_maintenance_date = ?, next_maintenance_date = ? WHERE equipment_id = ?");
    $stmt->bind_param("ssi", $maintenance_date, $next_date, $equip_id);
    if ($stmt->execute()) {
        $msg = "Calibration/Maintenance logged successfully.";
    } else {
        $msg = "Error updating records.";
    }
}

$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' => 'Equipment Calibration')); ?>
    <?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">Equipment Calibration & Maintenance</h4>
                        </div>
                    </div>
                    
                    <?php if ($msg): ?><div class="alert alert-success"><?php echo $msg; ?></div><?php endif; ?>
                    
                    <div class="card">
                        <div class="card-body">
                            <table id="calibTable" class="table table-bordered dt-responsive nowrap">
                                <thead>
                                    <tr>
                                        <th>Equipment</th>
                                        <th>Last Maintenance</th>
                                        <th>Next Due</th>
                                        <th>Action</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php while ($row = $result->fetch_assoc()): ?>
                                    <tr>
                                        <td>
                                            <strong><?php echo htmlspecialchars($row['equipment_name']); ?></strong><br>
                                            <small class="text-muted"><?php echo htmlspecialchars($row['equipment_code']); ?></small>
                                        </td>
                                        <td><?php echo $row['last_maintenance_date'] ?? 'N/A'; ?></td>
                                        <td class="<?php echo ($row['next_maintenance_date'] && $row['next_maintenance_date'] < date('Y-m-d')) ? 'text-danger fw-bold' : ''; ?>">
                                            <?php echo $row['next_maintenance_date'] ?? 'N/A'; ?>
                                        </td>
                                        <td>
                                            <button type="button" class="btn btn-sm btn-info" data-bs-toggle="modal" data-bs-target="#calibModal<?php echo $row['equipment_id']; ?>">
                                                Log Calibration
                                            </button>
                                            
                                            <!-- Modal -->
                                            <div class="modal fade" id="calibModal<?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">Log Maintenance: <?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">Maintenance Date</label>
                                                                    <input type="date" class="form-control" name="maintenance_date" value="<?php echo date('Y-m-d'); ?>" required>
                                                                </div>
                                                                <div class="mb-3">
                                                                    <label class="form-label">Next Due Date</label>
                                                                    <input type="date" class="form-control" name="next_date" required>
                                                                </div>
                                                            </div>
                                                            <div class="modal-footer">
                                                                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                                                                <button type="submit" name="log_calibration" class="btn btn-primary">Save Record</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>$('#calibTable').DataTable();</script>
</body>
</html>