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

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

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

$q = mysqli_query($conn,"SELECT * FROM families WHERE id='$id'");
$data = mysqli_fetch_assoc($q);

if(!$data){
    echo "Family not found";
    exit;
}

/* UPDATE */
if(isset($_POST['update']))
{
    $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']);

    mysqli_query($conn,"
    UPDATE families SET
    house_number='$house_number',
    family_head='$family_head',
    mobile='$mobile'
    WHERE id='$id'
    ");

    echo "<script>alert('Updated Successfully');window.location='view_families.php';</script>";
}
?>

<div class="app-content">

<!-- HEADER CARD -->
<div class="card-box">
    <h4>Edit Family</h4>
    <p style="font-size:12px;color:#777;">Update family details</p>
</div>

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

<form method="POST">

<label>House Number</label>
<input type="text" name="house_number"
value="<?php echo htmlspecialchars($data['house_number']); ?>"
class="form-control" required>

<label style="margin-top:10px;">Family Head</label>
<input type="text" name="family_head"
value="<?php echo htmlspecialchars($data['family_head']); ?>"
class="form-control" required>

<label style="margin-top:10px;">Mobile</label>
<input type="text" name="mobile"
value="<?php echo htmlspecialchars($data['mobile']); ?>"
class="form-control">

<br>

<button type="submit" name="update" class="btn-mobile"
style="background:#f39c12;color:#fff;">
Update 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"; ?>