<?php

error_reporting(E_ALL);
ini_set('display_errors',1);

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

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

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

/* HIDE SIDEBAR */
echo "<style>
.sidebar{display:none !important;}
.topbar{margin-left:0 !important;}
.main{margin-left:0 !important;padding-bottom:80px;}
</style>";

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

/* ADD FAMILY */
if(isset($_POST['save']))
{
    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 = mysqli_real_escape_string($conn,$_POST['house_number']);
        $family_head  = mysqli_real_escape_string($conn,$_POST['family_head']);
        $mobile       = mysqli_real_escape_string($conn,$_POST['mobile']);
        $family_category = $_POST['family_category'] ?? '';
        $family_type  = $_POST['family_type'] ?? '';
        $total_members= intval($_POST['total_members'] ?? 0);
        $address      = mysqli_real_escape_string($conn,$_POST['address']);

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

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

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

                echo "<script>
                alert('Family Added Successfully');
                window.location='/digital_panchayat/operator/members.php?family_id=".$family_id."';
                </script>";
                exit;

            } else {
                echo "<script>alert('Error creating family');</script>";
            }
        }
    }
}
?>

<div class="app-content">

<!-- HEADER -->
<div class="card-box">
    <h4>Add Family</h4>
    <p style="font-size:12px;color:#777;">Create new family record</p>
</div>

<!-- FORM -->
<div class="card-box">

<form method="POST">

<?php if($role_id == 1){ ?>
<label>Select Village</label>
<select name="village_id" class="form-control" required>
<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>
<?php } ?>

<label style="margin-top:10px;">House Number</label>
<input type="text" name="house_number" class="form-control" required>

<label style="margin-top:10px;">Family Head Name</label>
<input type="text" name="family_head" class="form-control" required>

<label style="margin-top:10px;">Contact Number</label>
<input type="text" name="mobile" class="form-control">

<label style="margin-top:10px;">Family Category</label>
<select name="family_category" class="form-control">
<option value="">Select Category</option>
<option>APL</option>
<option>BPL</option>
</select>

<label style="margin-top:10px;">Family Type</label>
<select name="family_type" class="form-control">
<option value="">Select Type</option>
<option>General</option>
<option>SC</option>
<option>OBC</option>
</select>

<label style="margin-top:10px;">Total Members</label>
<input type="number" name="total_members" class="form-control">

<label style="margin-top:10px;">Address</label>
<textarea name="address" class="form-control"></textarea>

<br>

<button type="submit" name="save" class="btn-mobile"
style="background:#27ae60;color:#fff;">
Save Family
</button>

</form>

</div>

</div>

<!-- ===== UPDATED BOTTOM NAV ===== -->

<style>
.bottom-nav{
position:fixed;
bottom:0;
left:0;
width:100%;
height:65px;
background:#fff;
border-top:1px solid #ddd;
display:flex;
justify-content:space-around;
align-items:center;
z-index:1000;
}

.bottom-nav a{
text-decoration:none;
color:#888;
font-size:11px;
text-align:center;
flex:1;
}

.bottom-nav a i{
display:block;
font-size:20px;
margin-bottom:3px;
}

.bottom-nav a.active{
color:#007bff;
}
</style>

<div class="bottom-nav">
    <a href="../dashboard.php">
        <i class="fa fa-home"></i>
        Home
    </a>

    <a href="../villages/village_data.php">
        <i class="fa fa-map"></i>
        Villages
    </a>

    <a href="../families/view_families.php">
        <i class="fa fa-users"></i>
        Families
    </a>

    <a href="view_schemes.php" class="active">
        <i class="fa fa-list"></i>
        Schemes
    </a>
</div>

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