<?php
session_start();
ob_start(); 
require('../include/startup.php');
$message = "";
$shift_status = "";
$current_date = (new DateTime())->format('Y-m-d H:i:s');

// Get machine_id from URL
if (!isset($_GET['code'])) {
    die("Please enter valid machine code");
}


$code = trim($_GET['code']);

// ======================================
// Replica Machine (Not Stored in Database)
// ======================================
if ($code === "Replica8") {

    $machine_type = 8;
    $machine_id   = 8; 

} else {

    // Fetch machine from database
    $code = mysqli_real_escape_string($conn, $code);

    $sql = "SELECT id, machine_type, no_of_table
            FROM station_detail_table
            WHERE resource_code = '$code'
            LIMIT 1";

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

    if (!$result || mysqli_num_rows($result) == 0) {
        die("Invalid Machine Code.");
    }

    $row = mysqli_fetch_assoc($result);

    $machine_type = (int)$row['machine_type'];
    $machine_id   = (int)$row['id'];
    $no_of_table  = (int)$row['no_of_table'];
}


if (isset($_POST['clock_in'])) {
    $clock_no = $_POST['clock_no'];
    $sql = "SELECT * FROM admin WHERE clock_no = '$clock_no'";
    $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_machines_assambly&machine_id=" . urlencode($machine_id));
        }elseif ($machine_type == 4) {
            header("Location: index?page=view_pu&machine_id=" . urlencode($machine_id));
        }elseif ($machine_type == 5) {
            header("Location: index?page=view_machines_assypu&machine_id=" . urlencode($machine_id));
        }elseif ($machine_type == 8) {
            header("Location: index?page=replica_wood_dashboard&machine_id=" . urlencode($machine_id));
        } else {
            header("Location: index?page=dashboard&machine_id=" .  urlencode($machine_id));
        }
        exit();
    } else {
        $message = "Incorrect ID Clock NO.";
    }
}
 if($_REQUEST['page']=='dashboard'){
die("trrrr");
include 'dashboard.php';

exit;

}

?>

<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>

    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        
        body {
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
        }
        
        .content {
            width: 100%;
            max-width: 1200px;
        }
        
        .container-fluid {
            width: 100%;
            padding: 0 15px;
            margin: 0 auto;
        }
        
        .row {
            display: flex;
            flex-wrap: wrap;
            margin: 0 -15px;
        }
        
        .justify-content-center {
            justify-content: center;
        }
        
        .col-lg-6 {
            flex: 0 0 50%;
            max-width: 50%;
            padding: 0 15px;
        }
        
        .form-container {
            background: white;
            padding: 40px;
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
            text-align: center;
            width: 100%;
            animation: fadeInUp 0.8s ease-out;
            position: relative;
            overflow: hidden;
        }
        
        .form-container::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 5px;
            background: linear-gradient(90deg, #9c4893, #6a2a63);
        }
        
        @keyframes fadeInUp {
            from { 
                opacity: 0; 
                transform: translateY(30px); 
            }
            to { 
                opacity: 1; 
                transform: translateY(0); 
            }
        }
        
        h2 {
            color: #333;
            font-size: 32px;
            font-weight: 600;
            margin-bottom: 30px;
            position: relative;
            padding-bottom: 15px;
        }
        
        h2::after {
            content: '';
            position: absolute;
            bottom: 0;
            left: 50%;
            transform: translateX(-50%);
            width: 60px;
            height: 3px;
            background: #9c4893;
            border-radius: 3px;
        }
        
        form {
            width: 100%;
        }
        
        input[type="text"] {
            width: 100%;
            padding: 16px 20px;
            margin: 15px 0;
            font-size: 18px;
            border: 2px solid #e1e1e1;
            border-radius: 8px;
            text-align: center;
            transition: all 0.3s;
            outline: none;
            letter-spacing: 1px;
        }
        
        input[type="text"]:focus {
            border-color: #9c4893;
            box-shadow: 0 0 0 3px rgba(156, 72, 147, 0.2);
        }
        
        input[type="text"]::placeholder {
            color: #aaa;
            letter-spacing: normal;
        }
        
        button {
            display: inline-block;
            width: 100%;
            padding: 18px;
            font-size: 20px;
            font-weight: 600;
            background: linear-gradient(to right, #9c4893, #7d3876);
            color: #fff;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            transition: all 0.3s ease;
            text-align: center;
            letter-spacing: 1px;
            box-shadow: 0 4px 15px rgba(156, 72, 147, 0.3);
            margin-top: 10px;
        }
        
        button:hover {
            background: linear-gradient(to right, #7d3876, #632c5c);
            box-shadow: 0 6px 20px rgba(156, 72, 147, 0.4);
            transform: translateY(-3px);
        }
        
        button:active {
            transform: translateY(0);
            box-shadow: 0 3px 10px rgba(156, 72, 147, 0.4);
        }
        
        .error-message {
            margin-top: 20px;
            color: #e74c3c;
            font-weight: 500;
            font-size: 16px;
            padding: 10px;
            border-radius: 5px;
            background-color: #fadbd8;
        }
        
        /* Responsive adjustments */
        @media (max-width: 992px) {
            .col-lg-6 {
                flex: 0 0 75%;
                max-width: 75%;
            }
        }
        
        @media (max-width: 768px) {
            .col-lg-6 {
                flex: 0 0 100%;
                max-width: 100%;
            }
            
            .form-container {
                padding: 30px 20px;
            }
            
            h2 {
                font-size: 28px;
            }
            
            input[type="text"] {
                padding: 14px;
                font-size: 16px;
            }
            
            button {
                padding: 16px;
                font-size: 18px;
            }
        }
    </style>

    <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>
<script>
setInterval(function () {
    $.get('keep_alive.php');
}, 300000);
</script>