<?php

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

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

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

/* Get role */
$role_id = $_SESSION['role_id'] ?? 0;

/* ADD FAMILY */
if(isset($_POST['save']))
{

    /* Village logic */

    if($role_id == 1)
    {
        $village_id = intval($_POST['village_id'] ?? 0);
    }
    else
    {
        $village_id = intval($_SESSION['village_id'] ?? 0);
    }

    if($village_id == 0)
    {
        echo "<script>alert('Please select village');</script>";
    }
    else
    {

        $house_number    = trim($_POST['house_number'] ?? '');
        $family_head     = trim($_POST['family_head'] ?? '');
        $mobile          = trim($_POST['mobile'] ?? '');
        $caste = $_POST['caste'] ?? '';
        $family_category = $_POST['family_category'] ?? '';
        $family_type     = $_POST['family_type'] ?? '';
        $total_members   = intval($_POST['total_members'] ?? 0);
        $address         = trim($_POST['address'] ?? '');

        if($house_number == '' || $family_head == '')
        {
            echo "<script>alert('House Number and Family Head are required');</script>";
        }
        else
        {

            $house_number    = mysqli_real_escape_string($conn,$house_number);
            $family_head     = mysqli_real_escape_string($conn,$family_head);
            $mobile          = mysqli_real_escape_string($conn,$mobile);
            $caste = mysqli_real_escape_string($conn,$caste);
            $family_category = mysqli_real_escape_string($conn,$family_category);
            $family_type     = mysqli_real_escape_string($conn,$family_type);
            $address         = mysqli_real_escape_string($conn,$address);

            $insert = mysqli_query($conn,"
           INSERT INTO families
(village_id,house_number,family_head,mobile,caste,family_category,family_type,total_members,address)
VALUES
('$village_id','$house_number','$family_head','$mobile','$caste','$family_category','$family_type','$total_members','$address')
            ");

            if($insert)
            {
                $family_id = mysqli_insert_id($conn);

                header("Location: ../members/add_member.php?family_id=".$family_id);
                exit;
            }
            else
            {
                echo "<script>alert('Error creating family');</script>";
            }

        }

    }

}

?>

<div class="main">

<h2>Add Family</h2>

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

<form method="POST">

<?php if($role_id == 1){ ?>

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

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

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

<?php
$v=mysqli_query($conn,"SELECT * FROM villages ORDER BY village_name");

while($row=mysqli_fetch_assoc($v))
{
?>

<option value="<?php echo $row['id']; ?>">
<?php echo $row['village_name']; ?>
</option>

<?php } ?>

</select>

</div>

<?php } ?>

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

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

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


<!-- Marital Status -->

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

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

</div>


<!-- Caste -->

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

<select name="caste" style="width:100%;padding:8px;">
<option value="">Select Caste</option>
<option value="General">General</option>
<option value="SC">SC</option>
<option value="ST">ST</option>
<option value="OBC">OBC</option>
<option value="Other">Other</option>
</select>

</div>


<!-- Surname -->

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

<select name="surname" style="width:100%;padding:8px;">
<option value="">Select Surname</option>
<option value="Sharma">Sharma</option>
<option value="Yadav">Yadav</option>
<option value="Kumar">Kumar</option>
<option value="Singh">Singh</option>
<option value="Verma">Verma</option>
</select>

</div>


<!-- Education -->

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

<select name="education" style="width:100%;padding:8px;">
<option value="">Select Education</option>
<option value="Illiterate">Illiterate</option>
<option value="Primary">Primary</option>
<option value="Middle">Middle</option>
<option value="10th">10th</option>
<option value="12th">12th</option>
<option value="Graduate">Graduate</option>
<option value="Post Graduate">Post Graduate</option>
</select>

</div>


<!-- Occupation -->

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

<select name="occupation" style="width:100%;padding:8px;">
<option value="">Select Occupation</option>
<option value="Farmer">Farmer</option>
<option value="Labour">Labour</option>
<option value="Government Job">Government Job</option>
<option value="Private Job">Private Job</option>
<option value="Business">Business</option>
<option value="Student">Student</option>
<option value="Unemployed">Unemployed</option>
</select>

</div>


<!-- Health Status -->

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

<select name="health_status" style="width:100%;padding:8px;">
<option value="">Select Health Status</option>
<option value="Healthy">Healthy</option>
<option value="Disabled">Disabled</option>
<option value="Chronic Disease">Chronic Disease</option>
<option value="Bedridden">Bedridden</option>
</select>

</div>


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

<select name="family_category" style="width:100%;padding:8px;">
<option value="">Select Category</option>
<option value="APL">APL</option>
<option value="BPL">BPL</option>
</select>

</div>

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

<select name="family_type" style="width:100%;padding:8px;">
<option value="">Select Type</option>
<option value="General">General</option>
<option value="SC">SC</option>
<option value="OBC">OBC</option>
</select>

</div>

<div style="margin-bottom:15px;">
<label>Total Family Members</label>
<input type="number" name="total_members" required style="width:100%;padding:8px;">
</div>

<div style="margin-bottom:20px;">
<label>Address</label>
<textarea name="address" style="width:100%;padding:8px;"></textarea>
</div>

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

</form>

</div>

</div>

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