<?php
session_start();

$message = "";
$shift_status = "";
$current_date = (new DateTime())->format('Y-m-d H:i:s');

// Get machine_id from URL
if (!isset($_GET['code'])) {
    die("Invalid access! Machine ID missing.");
}

//$machine_id = utility::app_crypt($_GET['machine_id'], 'd'); // decrypt
$code = $_GET['code']; // decrypt

echo $sql = "SELECT machine_type, id, no_of_table 
        FROM station_master_table 
        WHERE machine_tab_code = '$code'";


$result = mysqli_query($conn, $sql);

if ($result && mysqli_num_rows($result) > 0) {
    $row = mysqli_fetch_assoc($result);
    $machine_type = $row['machine_type'];
    $no_of_table  = $row['no_of_table'];
	$machine_id  = $row['id'];

}


if (isset($_POST['clock_in'])) {
    $clock_no = $_POST['clock_no'];
    $sql = "SELECT * FROM admin WHERE clock_no = '$clock_no' AND id='".$_SESSION['user_id']."'";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        $user = $result->fetch_assoc();
        $_SESSION['engineer_id'] = $user['id'];
        $_SESSION['machine_id'] = $machine_id;

        if ($machine_type == 1) {
            header("Location: index?page=dashboard&machine_id=" . urlencode($machine_id));
        } elseif ($machine_type == 3) {
            header("Location: index?page=view_cnc&machine_id=" . urlencode($machine_id));
        } elseif ($machine_type == 2) {
            header("Location: index?page=view_assm&machine_id=" . urlencode($machine_id));
        }elseif ($machine_type == 4) {
            header("Location: index?page=view_pu&machine_id=" . urlencode($machine_id));
        } else {
            header("Location: index?page=dashboard&machine_id=" .  urlencode($machine_id));
        }
        exit();
    } else {
        $message = "Incorrect ID Code.";
    }
}

?>

<div class="content-header" style="visibility:hidden;">
    <div class="container-fluid">
        <div class="row mb-2">
            <div class="col-sm-12">
                <h1 class="m-0 text-primary">Engineer Login</h1>
            </div>
        </div>
    </div>
</div>

<section class="content">
    <div class="container-fluid">
        <div class="row justify-content-center">
            <div class="col-lg-6">
                <div class="form-container">
                    <h2>Engineer Login</h2>
                    <form method="POST" action="">
                        <input type="hidden" name="machine_id" value="<?= htmlspecialchars($_GET['machine_id']); ?>">
                        <input type="text" name="clock_no" placeholder="Enter ID Code" required>
                        <?php if ($shift_status != "active" && $shift_status != "paused"): ?>
                            <button type="submit" name="clock_in">Log In</button>
                        <?php endif; ?>
                    </form>
                    <p class="error-message"><?= $message; ?></p>
                </div>
            </div>
        </div>
    </div>
</section>

<style>
.form-container {
    background: white;
    padding: 30px;
    border-radius: 10px;
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
    text-align: center;
    max-width: 100%;
    width: 100%;
    animation: fadeIn 1s ease-in-out;
	margin-top: 15vh; 
}
@keyframes fadeIn {
    from { opacity: 0; transform: translateY(-20px); }
    to { opacity: 1; transform: translateY(0); }
}
h2 {
    color: #9c4893;
    font-size: 28px;
    font-weight: 600;
    text-shadow: 1px 1px 3px rgba(0, 0, 0, 0.2);
}
input[type="text"] {
    width: 100%;
    padding: 12px;
    margin: 10px 0;
    font-size: 16px;
    border: 2px solid #9c4893;
    border-radius: 5px;
    text-align: center;
    transition: 0.3s;
}
input[type="text"]:focus {
    border-color: #9c4893;
    outline: none;
}
button {
    display: inline-block;
    width: 100%;
    padding: 12px 16px;
    font-size: 16px;
    font-weight: 500;
    font-family: "Segoe UI", Roboto, sans-serif;
    background: #9c4893;
    color: #fff;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    transition: all 0.3s ease;
    text-align: center;
    line-height: 1.4;
    letter-spacing: 0.5px;
    box-shadow: 0 3px 8px rgba(156, 72, 147, 0.3);
}

button:hover {
    background: #7d3876;
    box-shadow: 0 5px 12px rgba(156, 72, 147, 0.4);
    transform: translateY(-2px);
}

button:active {
    background: #632c5c;
    transform: translateY(0);
    box-shadow: 0 2px 6px rgba(156, 72, 147, 0.35);
}

button:focus {
    outline: none;
    box-shadow: 0 0 0 3px rgba(156, 72, 147, 0.5);
}

button:disabled {
    background: #c9a6c6;
    color: #eee;
    cursor: not-allowed;
    box-shadow: none;
    transform: none;
}

.error-message {
    margin-top: 10px;
    color: red;
    font-weight: bold;
}
</style>
