<?php
session_name("admin_session");
session_start();
include('connection.php');

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $data = json_decode(file_get_contents("php://input"), true);

    $url = $data['url'];
    $title = $data['title'];
    $status = $data['status'];
    $ipAddress = $data['ipAddress'];
    $added_by=$_SESSION['username'];
    $currentDateTime = date('Y-m-d H:i:s');
    $checkQuery = "SELECT COUNT(*) as count FROM add_website WHERE website_url = '$url'";
    $checkResult = mysqli_query($conn, $checkQuery);

    $row = mysqli_fetch_assoc($checkResult);

    if ($row['count'] > 0) {
        echo "Error: URL already exists";
    } else {
        $insertQuery = "INSERT INTO add_website(website_url, title, status, ip_address,added_by,date_time) VALUES ('$url', '$title', '$status', '$ipAddress','$added_by','$currentDateTime')";
        if (mysqli_query($conn, $insertQuery)) {
            $lastInsertedId = mysqli_insert_id($conn);
            echo $lastInsertedId;
        } else {
            echo "Error: Failed insertion!!!";
        }
    }

    mysqli_close($conn);
}
?>
