<?php
include 'includes/config.php';

if (isset($_POST['submit_popup'])) {
    // Collect and sanitize form data
    $type = 'customer'; // Change as necessary
    $business_id = 3;
    $name = $_POST['name'];
    $contact_id = $_POST['contact_id'];
    $tax_number = $_POST['tax_number'];
    $opening_balance = $_POST['opening_balance'];
    $pay_term_type = $_POST['pay_term_type'];
    $credit_limit = $_POST['credit_limit'];
    $email = $_POST['email'];
    $mobile = $_POST['mobile'];
    $alternate_number = $_POST['alternate_number'];
    $landline = $_POST['landline'];
    $city = $_POST['city'];
    $state = $_POST['state'];
    $country = $_POST['country'];
    $landmark = $_POST['landmark'];
    $custom_field1 = $_POST['custom_field1'];
    $custom_field2 = $_POST['custom_field2'];
    $custom_field3 = $_POST['custom_field3'];
    $custom_field4 = $_POST['custom_field4'];

    // Prepare and execute the SQL statement
    $stmt = $conn->prepare("INSERT INTO contacts (type, business_id, name, contact_id, tax_number, opening_balance, pay_term_type, credit_limit, email, mobile, alternate_number, landline, city, state, country, landmark, custom_field1, custom_field2, custom_field3, custom_field4) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");

    if ($stmt->execute([$type, $business_id, $name, $contact_id, $tax_number, $opening_balance, $pay_term_type, $credit_limit, $email, $mobile, $alternate_number, $landline, $city, $state, $country, $landmark, $custom_field1, $custom_field2, $custom_field3, $custom_field4])) {
        // Redirect to add_sale.php upon successful insertion
        header("Location: add_sale.php");
        exit;
    } else {
        echo "Error inserting data.";
    }
}
?>
