<?php
include('../config/config.php');
include('../config/session_check.php'); // Ensure DB connection is established

$branch_id = $_SESSION['branch_id'];


if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST['customer_code']) || empty($_POST['customer_name'])) {
        echo "Error: Required fields are missing.";
        exit();
    }

    $customer_code = $_POST['customer_code'];
    $customer_name = $_POST['customer_name'];
    $address = $_POST['address'];
    $city = $_POST['city'];
    $pincode = $_POST['pincode'];
    $contact_person = $_POST['contact_person'];
    $phone = $_POST['phone'];

    // ✅ Correct SQL Query with '?' Placeholders
    $stmt = $conn->prepare("INSERT INTO customers (customer_code, customer_name, address, city, pincode, contact_person, phone, status,branch_id) VALUES (?, ?, ?, ?, ?, ?, ?, 'active',?)");

    // ✅ Correctly Bind Parameters (All Strings)
    $stmt->bind_param("ssssssss", $customer_code, $customer_name, $address, $city, $pincode, $contact_person, $phone,$branch_id);

    // ✅ Execute and Check for Errors
    if ($stmt->execute()) {
        echo "Customer added successfully!";
    } else {
        echo "Error: " . $stmt->error;
    }

    $stmt->close();
    $conn->close();
}
?>
