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

/* -------------------------------------------------
   1.  Stop if the request did not come from <form>
   ------------------------------------------------- */
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    header('Location: add_member_form.php');
    exit;
}

/* -------------------------------------------------
   2.  Insert main‑member record
   ------------------------------------------------- */
$main_gam            = $_POST['main_gam']           ?? '';
$name                = $_POST['name']               ?? '';
$dob                 = $_POST['dob']                ?: null;
$marital_status      = $_POST['marital_status']     ?? '';
$mosal               = $_POST['mosal']              ?? '';
$education           = $_POST['education']          ?? '';
$blood_group         = $_POST['blood_group']        ?? '';               // ← see note ①
$business_address    = $_POST['business_address']   ?? '';
$residential_address = $_POST['residential_address']?? '';
$area                = $_POST['area']               ?? '';
$mobile              = $_POST['mobile']             ?? '';
$email               = $_POST['email']              ?? '';

/* use a prepared statement to stay safe */
$stmt = $mysqli->prepare(
    "INSERT INTO members
     (main_gam,name,dob,marital_status,mosal,education,blood,
      b_add,r_add,area,mobile,email)
     VALUES (?,?,?,?,?,?,?,?,?,?,?,?)"
);
$stmt->bind_param(
    "ssssssssssss",
    $main_gam,$name,$dob,$marital_status,$mosal,$education,$blood_group,
    $business_address,$residential_address,$area,$mobile,$email
);
$stmt->execute();

$member_id = $stmt->insert_id;   // primary key for the family table
$stmt->close();

/* -------------------------------------------------
   3.  Loop through any family‑member cards and insert
   ------------------------------------------------- */
if (!empty($_POST['family_name'])) {

    $f_main_gam            = $_POST['family_main_gam'];
    $f_name                = $_POST['family_name'];
    $f_relation            = $_POST['family_relation'];
    $f_dob                 = $_POST['family_dob'];
    $f_marital_status      = $_POST['family_marital_status'];
    $f_mosal               = $_POST['family_mosal'];
    $f_education           = $_POST['family_education'];
    $f_blood_group         = $_POST['family_blood_group'];
    $f_business_address    = $_POST['family_business_address'];
    $f_residential_address = $_POST['family_residential_address'];
    $f_mobile              = $_POST['family_mobile'];
    $f_email               = $_POST['family_email'];

    $fStmt = $mysqli->prepare(
        "INSERT INTO family_members
         (member_id,main_gam,name,relation,dob,marital_status,mosal,
          education,blood,b_add,r_add,mobile,email)
         VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)"
    );

    for ($i = 0; $i < count($f_name); $i++) {

        /* skip completely empty rows (user clicked ‘add’ but filled nothing) */
        if (trim($f_name[$i]) === '') {
            continue;
        }

        $fStmt->bind_param(
            "issssssssssss",
            $member_id,                       // FK to members.id
            $f_main_gam[$i],
            $f_name[$i],
            $f_relation[$i],
            $f_dob[$i] ,
            $f_marital_status[$i],
            $f_mosal[$i],
            $f_education[$i],
            $f_blood_group[$i],
            $f_business_address[$i],
            $f_residential_address[$i],
            $f_mobile[$i],
            $f_email[$i]
        );
        $fStmt->execute();
    }
    $fStmt->close();
}

/* -------------------------------------------------
   4.  Done – go back to list page
   ------------------------------------------------- */
$_SESSION['success'] = 'Member added successfully';
header('Location: add_members.php');
exit;
