<?php
session_start();

date_default_timezone_set('Asia/Manila');

$server = "localhost";
$username = "root";
$password = "";
$dbname = "qrattendance";

$conn = new mysqli($server, $username, $password, $dbname);

if($conn->connect_error){
    die("Connection failed: " . $conn->connect_error);
}

if(isset($_POST['empid'])){
    
    //$raw_input = trim($_POST['empidn']);
    
    //$json_data = json_decode($raw_input, true);
    //if(is_array($json_data)){
      //  $empid = isset($json_data['id']) ? $json_data['id'] : (isset($json_data['employee_no']) ? $json_data['employee_no'] : $raw_input);
    //} else {
      //  $empid = $raw_input;
    // }

    $empid = $_POST['empid'];
    $empname = $_POST['empname'];
    $klek = $_POST['klak'];
    $klak = preg_replace('/[^a-zA-Z0-9]/', '', $klek);
    $empnid = $_POST['lats'];
    $empnid = $_POST['longs'];
    $date = date('Y-m-d');
    $time = date('H:i:s');
    $taymnaw = date("Y-m-d H:i:s");

    $insert_sql = "INSERT INTO qrattendance (EMPID, logdate, status, origin) VALUES ('$empid', '$taymnaw',  '$klak','$empname')";
        if($conn->query($insert_sql) === TRUE){
            $_SESSION['tsk'] = 0;
            $_SESSION['timein'] = "Welcome, $name! Clocked in at $time";
        } else {
            $_SESSION['warn'] = "Database error: " . $conn->error;
        }
}

$conn->close();
header("Location: http://localhost/lcc/da.php");
exit();
?> 