<?php
session_start();
if (!isset($_SESSION['admin_logged_in']) || $_SESSION['admin_logged_in'] !== true) {
    header('Location: index.php');
    exit;
}
require 'dbconn.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['id'])) {
    $id = intval($_POST['id']);
    $member_id = intval($_POST['member_id']);

    $name         = trim($_POST['family_name']);
    $main_gam     = trim($_POST['family_main_gam']);
    $relation     = trim($_POST['family_relation']);
    $dob          = $_POST['family_dob'];
    $marital      = $_POST['marital_status'];
    $mosal        = trim($_POST['family_mosal']);
    $education    = trim($_POST['family_education']);
    $blood        = $_POST['family_blood_group'];
    $b_add        = trim($_POST['family_business_address']);
    $r_add        = trim($_POST['family_residential_address']);
    $mobile       = trim($_POST['family_mobile']);
    $email        = trim($_POST['family_email']);

    $query = "UPDATE family_members SET
        name = ?, 
        main_gam = ?, 
        relation = ?, 
        dob = ?, 
        marital_status = ?, 
        mosal = ?, 
        education = ?, 
        blood = ?, 
        b_add = ?, 
        r_add = ?, 
        mobile = ?, 
        email = ?
        WHERE id = ? AND member_id = ?";

    $stmt = $mysqli->prepare($query);
    $stmt->bind_param("ssssssssssssii", $name, $main_gam, $relation, $dob, $marital, $mosal, $education, $blood, $b_add, $r_add, $mobile, $email, $id, $member_id);

    if ($stmt->execute()) {
       header("Location: family_members.php?member_id=" . $member_id . "&updated=1");
exit;

    } else {
        echo "Error updating family member: " . $stmt->error;
    }
} else {
    echo "Invalid request.";
}
