<?php

require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include('includes/function.php');

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);
$username = $_SESSION["username_" . $dbname];

$jsonData = file_get_contents('php://input');

// Decode JSON data to an associative array
$data = json_decode($jsonData, true);



$getdet = mysqli_query($conn, "SELECT * FROM `treatdet` WHERE vouchno='$data[vouchn]' and scheddate = '$data[scheddat]' and treatment='$data[treatmen]'") or die(mysqli_error($conn));

if (mysqli_num_rows($getdet) > 0) {
    mysqli_query($conn, "UPDATE `treatdet` SET `staff`='$data[staff]',`treat1`='$data[treat1]',`treat2`='$data[treat2]',`treat3`='$data[treat3]',
                                               `treat4`='$data[treat4]',`machine1`='$data[machine1]',`machine2`='$data[machine2]',
                                                updateby ='$username' where vouchno='$data[vouchn]' and scheddate = '$data[scheddat]' and treatment='$data[treatmen]' ") or die(mysqli_error($conn));
} else {
    mysqli_query($conn, "INSERT INTO `treatdet`(`vouchno`, `vouchdt`, `treatment`, `scheddate`, 
                                            `accode`, `staff`, `treat1`, `treat2`, `treat3`, `treat4`,
                                            `machine1`, `machine2`,addby)
                                  values   ('$data[vouchn]', '$data[vouchd]', '$data[treatmen]', '$data[scheddat]', 
                                            '$data[accod]', '$data[staff]', '$data[treat1]', '$data[treat2]', '$data[treat3]', '$data[treat4]',
                                            '$data[machine1]', '$data[machine2]','$username')") or die(mysqli_error($conn));
}


$rows = array();
$ctr = 0;
$rows = array(
    'success' => true,
    'vouchno' => "$mvouch"
);

header("Content-type: application\json");
echo json_encode($rows);
