<?php
include 'includes/config.php';
ini_set('display_errors', 1);
error_reporting(E_ALL);

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $id = $_POST['edit_id'] ?? '';
    $name = $_POST['edit_name'] ?? '';
    $email = $_POST['edit_email'] ?? '';
    $opening_balance = $_POST['edit_opening_balance'] ?? '0.00';
    $mobile = $_POST['edit_mobile'] ?? '';
    $home_address = $_POST['edit_home_address'] ?? '';

    if ($id) {
        $sql = "UPDATE contacts 
                SET 
                    name = :name,
                    email = :email,
                    opening_balance = :opening_balance,
                    mobile = :mobile,
                    home_address = :home_address
                WHERE id = :id";

        $stmt = $conn->prepare($sql);
        $success = $stmt->execute([
            ':id' => $id,
            ':name' => $name,
            ':email' => $email,
            ':opening_balance' => $opening_balance,
            ':mobile' => $mobile,
            ':home_address' => $home_address
        ]);

        echo $success ? "Contact updated successfully" : "Failed to update contact";
    } else {
        echo "Invalid ID";
    }
} else {
    echo "Invalid request method";
}
?>
