<?php

include "../../config/master.php";

/* Allow Super Admin + Village Admin + Operator */
allowRoles([1,5,6]);

include "../layout/header.php";
include "../layout/sidebar.php";

/* GET FAMILY ID */

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

if($family_id == 0){
echo "<script>
alert('Family not selected');
window.location='../families/view_families.php';
</script>";
exit;
}

/* SESSION DATA */

$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){
echo "<script>
alert('Family not found');
window.location='../families/view_families.php';
</script>";
exit;
}


/* ADD MEMBER */

if(isset($_POST['save']))
{

$name = mysqli_real_escape_string($conn,$_POST['name']);
$gender = mysqli_real_escape_string($conn,$_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);


/* SAVE 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')");
}


echo "<script>
alert('Member Added Successfully');
window.location='add_member.php?family_id=$family_id';
</script>";

}

?>

<div class="main">

<h2>Add Members</h2>

<div style="background:#fff;padding:25px;border-radius:6px;width:600px;box-shadow:0 2px 6px rgba(0,0,0,0.1);">

<h4>Family Head: <?php echo htmlspecialchars($family['family_head']); ?></h4>
<p>House No: <?php echo htmlspecialchars($family['house_number']); ?></p>

<form method="POST">

<div style="margin-bottom:15px;">
<label>Name</label>
<input type="text" name="name" required style="width:100%;padding:8px;">
</div>

<div style="margin-bottom:15px;">
<label>Gender</label>

<select name="gender" style="width:100%;padding:8px;">
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>

</div>

<div style="margin-bottom:15px;">
<label>Age</label>
<input type="number" name="age" style="width:100%;padding:8px;">
</div>

<div style="margin-bottom:15px;">
<label>DOB</label>
<input type="date" name="dob" style="width:100%;padding:8px;">
</div>

<div style="margin-bottom:15px;">
<label>Marital Status</label>

<select name="marital_status" style="width:100%;padding:8px;">
<option value="Unmarried">Unmarried</option>
<option value="Married">Married</option>
</select>

</div>

<div style="margin-bottom:15px;">
<label>Contact No</label>
<input type="text" name="mobile" style="width:100%;padding:8px;">
</div>


<!-- RELATION TYPE -->

<div style="margin-bottom:15px;">
<label>Relation Type</label>

<select name="relation_type" style="width:100%;padding:8px;" required>

<option value="">Select 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 of</option>

</select>

</div>


<!-- RELATION WITH -->

<div style="margin-bottom:15px;">
<label>Relation With</label>

<select name="parent_id" style="width:100%;padding:8px;">

<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>

</div>


<!-- EDUCATION -->

<div style="margin-bottom:15px;">
<label>Education</label>

<select name="education" style="width:100%;padding:8px;">

<option value="">Select Education</option>

<?php
$edu=mysqli_query($conn,"SELECT id,education_name FROM education_master ORDER BY education_name");

while($e=mysqli_fetch_assoc($edu))
{
?>

<option value="<?php echo $e['id']; ?>">
<?php echo htmlspecialchars($e['education_name']); ?>
</option>

<?php
}
?>

</select>

</div>


<!-- OCCUPATION -->

<div style="margin-bottom:15px;">
<label>Occupation</label>

<select name="occupation" style="width:100%;padding:8px;">

<option value="">Select Occupation</option>

<?php
$occ=mysqli_query($conn,"SELECT id,occupation_name FROM occupation_master ORDER BY occupation_name");

while($o=mysqli_fetch_assoc($occ))
{
?>

<option value="<?php echo $o['id']; ?>">
<?php echo htmlspecialchars($o['occupation_name']); ?>
</option>

<?php
}
?>

</select>

</div>


<div style="margin-bottom:20px;">
<label>Health Status</label>

<select name="health_status" style="width:100%;padding:8px;">
<option value="Healthy">Healthy</option>
<option value="Sick">Sick</option>
<option value="Disabled">Disabled</option>
</select>

</div>

<button type="submit" name="save"
style="background:#2c7be5;color:#fff;padding:10px 18px;border:none;border-radius:4px;">
Add Member
</button>

</form>

</div>

</div>

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