<?php
include_once 'api/config.php';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $cust_id = $_POST['editId'];
    $cust_name = mysqli_real_escape_string($link, $_POST['cust_name']);
    $cust_email = mysqli_real_escape_string($link, $_POST['cust_email']);
    $allowed_downloads = $_POST['allowed_downloads'];
    $current_downloads = $_POST['current_downloads'];
    $cust_details = mysqli_real_escape_string($link, $_POST['cust_details']);
    $allowed_ips = mysqli_real_escape_string($link, $_POST['allowed_ips']); // Use 'allowed_ips' instead of 'cust_address'
    $cust_status = $_POST['cust_status'];

    if ($cust_id) {
        // Update existing customer
        $sql = "UPDATE customers SET 
                cust_name = '$cust_name', 
                cust_email = '$cust_email', 
                allowed_downloads = '$allowed_downloads', 
                current_downloads = '$current_downloads', 
                cust_details = '$cust_details', 
                allowed_ips = '$allowed_ips',  
                cust_status = '$cust_status' 
                WHERE cust_id = '$cust_id'";
    } else {
        // Insert new customer
        $sql = "INSERT INTO customers (cust_name, cust_email, allowed_downloads, current_downloads, cust_details, allowed_ips, cust_status) 
                VALUES ('$cust_name', '$cust_email', '$allowed_downloads', '$current_downloads', '$cust_details', '$allowed_ips', '$cust_status')";
    }

    if (mysqli_query($link, $sql)) {
        echo "Customer saved successfully!";
    } else {
        echo "Error saving customer: " . mysqli_error($link);
    }
}
?>
