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

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

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

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

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