<?php
//include("../../database.inc.php"); //database.inc.php is databse connection file which will be included
include("../../../include/startup.php"); 
if(isset($_REQUEST['action']) && $_REQUEST['action']=='change_status'){
  $page_id = $_REQUEST['page_id'];  
  $new_status = ($_REQUEST['status'] == 0) ? 1 : 0; 
  if($page_id){
    $update_page = "UPDATE pages SET live ='".$new_status."' WHERE id='".$page_id."' ";
    if(mysqli_query($conn,$update_page)){
      echo "Page status changed";
    }
  }else{
    echo "Page status not changed";
  }
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='logon'){
    
    if (!isset($_SESSION['engineer_id'])) {
        echo "error";
        exit();
    }

    $engineer_id = $_SESSION['engineer_id'];
    $work_assign_id = $_POST['work_assign_id'];
    $start_time = date("Y-m-d H:i:s");

    $query = "INSERT INTO work_logs (engineer_id, work_assign_id, start_time) 
              VALUES ('$engineer_id', '$work_assign_id', '$start_time')";

    if (mysqli_query($conn, $query)) {
        echo "success";
    } else {
        echo "error";
    }
}
else if(isset($_REQUEST['action']) && $_REQUEST['action']=='fetch_made_qty'){
    
    $work_assign_id = $_POST['work_assign_id'];

		$query = "SELECT SUM(quantity_made) AS already_worked FROM work_logs WHERE work_assign_id = $work_assign_id";
		$result = mysqli_query($conn, $query);
		$row = mysqli_fetch_assoc($result);
        $work_order_id = utility::getTableFieldById("work_order_assign","work_order_id","id",$work_assign_id);
         $order_quantity = utility::getTableFieldById("work_order", "order_quantity", "id", $work_order_id);
         $work_order_name = utility::getTableFieldById("work_order", "work_order_no", "id", $work_order_id);
		$already_worked = $row['already_worked'] ?? 0;

		// Calculate remaining quantity
		$remaining_quantity = $order_quantity - $already_worked;

		echo json_encode([
			'already_worked' => $already_worked,
			'order_quantity' => $order_quantity,
			'work_order_name' => $work_order_name,
			'remaining_quantity' => max($remaining_quantity, 0) // Ensure it doesn't go negative
		]);

}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='loggoff'){
    
   $work_logs_id = $_POST['work_logs_id'];
    $quantity_made = (int)$_POST['quantity_made'];
    $scrap = (int)$_POST['scrap'];
    $rework = (int)$_POST['rework'];
    $at_risk = (int)$_POST['at_risk'];
    $progress_notes = $_POST['progress_notes'];
    $end_time =  date("Y-m-d H:i:s");
	
        $update_query = "UPDATE work_logs SET 
                        
                        quantity_made = $quantity_made, 
                        end_time = '$end_time', 
                        scrap = $scrap, 
                        rework = $rework, 
                        at_risk = $at_risk, 
                        progress_notes = '$progress_notes' 
                        WHERE id = $work_logs_id";

        if (mysqli_query($conn, $update_query)) {
            echo json_encode([
                "status" => "success",
                "already_worked" => $new_already_worked,
                "remaining_quantity" => $new_remaining_quantity
            ]);
        } else {
            echo json_encode(["status" => "error", "message" => "Database update failed"]);
        }
    
}

die;
?>