<?php

include "../../config/master.php";
allowRoles([1,5,6]);

$family_id = intval($_GET['family_id'] ?? 0);

if($family_id == 0){
header("Location: ../families/view_families.php");
exit;
}

$village_id = $_SESSION['village_id'] ?? 0;

/* FETCH FAMILY */
$fam = mysqli_query($conn,"SELECT * FROM families WHERE id='$family_id' LIMIT 1");
$family = mysqli_fetch_assoc($fam);

if(!$family){
header("Location: ../families/view_families.php");
exit;
}

/* ===== AJAX SAVE ===== */
if(isset($_POST['ajax_save']))
{

$name = mysqli_real_escape_string($conn,$_POST['name']);
$gender = $_POST['gender'];
$age = intval($_POST['age']);
$dob = $_POST['dob'];
$marital_status = $_POST['marital_status'];
$mobile = mysqli_real_escape_string($conn,$_POST['mobile']);
$education = intval($_POST['education']);
$occupation = intval($_POST['occupation']);
$health_status = $_POST['health_status'];

$parent_id = intval($_POST['parent_id']);
$relation_type = $_POST['relation_type'];

/* INSERT MEMBER */
mysqli_query($conn,"
INSERT INTO family_members
(family_id,village_id,name,gender,age,dob,marital_status,mobile,education_id,occupation_id)
VALUES
('$family_id','$village_id','$name','$gender','$age','$dob','$marital_status','$mobile','$education','$occupation')
");

$member_id = mysqli_insert_id($conn);

/* RELATION */
if($parent_id >= 0){
mysqli_query($conn,"
INSERT INTO family_relationships
(member_id,parent_id,relation_type)
VALUES
('$member_id','$parent_id','$relation_type')
");
}

header('Content-Type: application/json');
echo json_encode(["status"=>"success"]);
exit;
}

/* NORMAL PAGE */
include "../layout/header.php";
?>

<div class="app-content">

<div class="card-box">
<h4>Add Member</h4>
<p style="font-size:12px;color:#777;">
👨‍👩‍👧 <?php echo htmlspecialchars($family['family_head']); ?> | 
🏠 <?php echo htmlspecialchars($family['house_number']); ?>
</p>
</div>

<!-- MESSAGE -->
<div id="msgBox" style="margin-bottom:10px;"></div>

<div class="card-box">

<form id="memberForm">

<input type="text" name="name" class="form-control" placeholder="Name" required>

<select name="gender" class="form-control">
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>

<input type="number" name="age" class="form-control" placeholder="Age">

<input type="date" name="dob" class="form-control">

<select name="marital_status" class="form-control">
<option value="Unmarried">Unmarried</option>
<option value="Married">Married</option>
</select>

<input type="text" name="mobile" class="form-control" placeholder="Mobile">

<!-- RELATION -->
<select name="relation_type" class="form-control" required>
<option value="">Relation</option>
<option value="Son">Son of</option>
<option value="Daughter">Daughter of</option>
<option value="Wife">Wife of</option>
<option value="Husband">Husband of</option>
<option value="Brother">Brother of</option>
<option value="Sister">Sister of</option>
<option value="Father">Father of</option>
<option value="Mother">Mother of</option>
<option value="Daughter in Law">Daughter in Law</option>
</select>

<select name="parent_id" class="form-control">
<option value="0">Head (<?php echo htmlspecialchars($family['family_head']); ?>)</option>

<?php
$pm=mysqli_query($conn,"SELECT id,name FROM family_members WHERE family_id='$family_id'");
while($p=mysqli_fetch_assoc($pm)){
?>
<option value="<?php echo $p['id']; ?>">
<?php echo htmlspecialchars($p['name']); ?>
</option>
<?php } ?>
</select>

<!-- EDUCATION -->
<select name="education" class="form-control">
<option value="">Education</option>
<?php
$edu=mysqli_query($conn,"SELECT id,education_name FROM education_master");
while($e=mysqli_fetch_assoc($edu)){
?>
<option value="<?php echo $e['id']; ?>">
<?php echo htmlspecialchars($e['education_name']); ?>
</option>
<?php } ?>
</select>

<!-- OCCUPATION -->
<select name="occupation" class="form-control">
<option value="">Occupation</option>
<?php
$occ=mysqli_query($conn,"SELECT id,occupation_name FROM occupation_master");
while($o=mysqli_fetch_assoc($occ)){
?>
<option value="<?php echo $o['id']; ?>">
<?php echo htmlspecialchars($o['occupation_name']); ?>
</option>
<?php } ?>
</select>

<select name="health_status" class="form-control">
<option value="Healthy">Healthy</option>
<option value="Sick">Sick</option>
<option value="Disabled">Disabled</option>
</select>

<br>

<button type="submit" class="btn-mobile" style="background:#2c7be5;color:#fff;">
Save Member
</button>

</form>

</div>

</div>

<?php include "../layout/footer.php"; ?>

<script>

/* AJAX SUBMIT */
document.getElementById("memberForm").addEventListener("submit",function(e){

e.preventDefault();

let formData=new FormData(this);
formData.append("ajax_save","1");

let msgBox=document.getElementById("msgBox");

fetch("",{method:"POST",body:formData})
.then(res=>res.json())
.then(res=>{

if(res.status==="success"){

msgBox.innerHTML=`<div style="background:#d4edda;color:#155724;padding:12px;border-radius:8px;">
✅ Member Added Successfully
</div>`;

window.scrollTo({top:0,behavior:"smooth"});

this.reset();

}else{
throw "error";
}

})
.catch(()=>{
msgBox.innerHTML=`<div style="background:#f8d7da;color:#721c24;padding:12px;border-radius:8px;">
❌ Something went wrong
</div>`;
});

});

</script>