<?php
include("config.php");

// (Backend code from previous update goes here...)

$villages = $conn->query("SELECT village_id, village_name FROM tbl_village ORDER BY village_name");
$faliyas = $conn->query("SELECT faliya_id, faliya_name FROM tbl_faliyu ORDER BY faliya_name");
$info_list = $conn->query("SELECT i.*, v.village_name, f.faliya_name FROM tbl_village_info i LEFT JOIN tbl_village v ON i.village_id = v.village_id LEFT JOIN tbl_faliyu f ON i.habitation_name_faliya_id = f.faliya_id ORDER BY i.id DESC");
?>

<h2>Manage Village Info</h2>

<form method="post" class="mb-4">
    <input type="hidden" name="id" value="<?= $edit_info['id'] ?? '' ?>">

    <div class="row">
        <div class="col-md-4">
            <label>Village</label>
            <select name="village_id" class="form-control" required>
                <option value="">Select</option>
                <?php $villages->data_seek(0); while ($v = $villages->fetch_assoc()): ?>
                    <option value="<?= $v['village_id'] ?>" <?= ($edit_info && $edit_info['village_id'] == $v['village_id']) ? 'selected' : '' ?>><?= $v['village_name'] ?></option>
                <?php endwhile; ?>
            </select>
        </div>
        <div class="col-md-4">
            <label>Faliyu</label>
            <select name="faliya_id" class="form-control" required>
                <option value="">Select</option>
                <?php while ($f = $faliyas->fetch_assoc()): ?>
                    <option value="<?= $f['faliya_id'] ?>" <?= ($edit_info && $edit_info['habitation_name_faliya_id'] == $f['faliya_id']) ? 'selected' : '' ?>><?= $f['faliya_name'] ?></option>
                <?php endwhile; ?>
            </select>
        </div>
        <div class="col-md-4">
            <label>LGD Code</label>
            <input type="text" name="lgd_code" class="form-control" value="<?= $edit_info['lgd_code'] ?? '' ?>">
        </div>
    </div>

    <div class="row mt-3">
        <div class="col">
            <label>Total Households</label>
            <input type="number" name="total_hhs" class="form-control" value="<?= $edit_info['total_hhs'] ?? 0 ?>">
        </div>
        <div class="col">
            <label>Total Population</label>
            <input type="number" name="total_population" class="form-control" value="<?= $edit_info['total_population'] ?? 0 ?>">
        </div>
        <div class="col">
            <label>PTG Households</label>
            <input type="number" name="ptg_hhs" class="form-control" value="<?= $edit_info['ptg_hhs'] ?? 0 ?>">
        </div>
        <div class="col">
            <label>PTG Population</label>
            <input type="number" name="ptg_population" class="form-control" value="<?= $edit_info['ptg_population'] ?? 0 ?>">
        </div>
    </div>

    <h5 class="mt-4">School Distance & Classroom Requirement</h5>
    <div class="row">
        <div class="col-md-3"><input name="dist_lps" class="form-control" placeholder="Dist. LPS" value="<?= $edit_info['dist_lower_primary_school_km'] ?? '' ?>"></div>
        <div class="col-md-3"><input name="dist_ups" class="form-control" placeholder="Dist. UPS" value="<?= $edit_info['dist_upper_primary_school_km'] ?? '' ?>"></div>
        <div class="col-md-3"><input name="dist_hs" class="form-control" placeholder="Dist. HS" value="<?= $edit_info['dist_high_school_km'] ?? '' ?>"></div>
        <div class="col-md-3"><input name="dist_hss" class="form-control" placeholder="Dist. HSS" value="<?= $edit_info['dist_higher_secondary_school_km'] ?? '' ?>"></div>
    </div>
    <div class="row mt-2">
        <div class="col"><input type="number" name="cr_1_5" class="form-control" placeholder="Classroom 1-5" value="<?= $edit_info['classrooms_required_1_to_5'] ?? 0 ?>"></div>
        <div class="col"><input type="number" name="cr_6_8" class="form-control" placeholder="Classroom 6-8" value="<?= $edit_info['classrooms_required_6_to_8'] ?? 0 ?>"></div>
        <div class="col"><input type="number" name="cr_9_10" class="form-control" placeholder="Classroom 9-10" value="<?= $edit_info['classrooms_required_9_to_10'] ?? 0 ?>"></div>
        <div class="col"><input type="number" name="cr_11_12" class="form-control" placeholder="Classroom 11-12" value="<?= $edit_info['classrooms_required_11_to_12'] ?? 0 ?>"></div>
    </div>

    <h5 class="mt-4">Other Facilities</h5>
    <div class="row">
        <div class="col">
            <label><input type="checkbox" name="dw_1_5" <?= !empty($edit_info['drinking_water_req_1_to_5']) ? 'checked' : '' ?>> Water 1-5</label>
        </div>
        <div class="col">
            <label><input type="checkbox" name="dw_6_8" <?= !empty($edit_info['drinking_water_req_6_to_8']) ? 'checked' : '' ?>> Water 6-8</label>
        </div>
        <div class="col">
            <label><input type="checkbox" name="dw_9_10" <?= !empty($edit_info['drinking_water_req_9_to_10']) ? 'checked' : '' ?>> Water 9-10</label>
        </div>
        <div class="col">
            <label><input type="checkbox" name="dw_11_12" <?= !empty($edit_info['drinking_water_req_11_to_12']) ? 'checked' : '' ?>> Water 11-12</label>
        </div>
    </div>
    <div class="row">
        <div class="col">
            <label><input type="checkbox" name="toilet_1_5" <?= !empty($edit_info['toilets_required_1_to_5']) ? 'checked' : '' ?>> Toilet 1-5</label>
        </div>
        <div class="col">
            <label><input type="checkbox" name="toilet_6_8" <?= !empty($edit_info['toilets_required_6_to_8']) ? 'checked' : '' ?>> Toilet 6-8</label>
        </div>
        <div class="col">
            <label><input type="checkbox" name="toilet_9_10" <?= !empty($edit_info['toilets_required_9_to_10']) ? 'checked' : '' ?>> Toilet 9-10</label>
        </div>
        <div class="col">
            <label><input type="checkbox" name="toilet_11_12" <?= !empty($edit_info['toilets_required_11_to_12']) ? 'checked' : '' ?>> Toilet 11-12</label>
        </div>
    </div>

    <div class="mb-3 mt-2">
        <label>Other School Facility Needs</label>
        <textarea name="school_other" class="form-control"><?= $edit_info['other_school_facility_need'] ?? '' ?></textarea>
    </div>

    <h5 class="mt-3">Health</h5>
    <div class="row">
        <div class="col-md-6">
            <label>Health Facility Type</label>
            <input name="health_type" class="form-control" value="<?= $edit_info['health_facility_type'] ?? '' ?>">
        </div>
        <div class="col-md-6">
            <label>Distance to Nearest Health Center (km)</label>
            <input name="dist_health_km" class="form-control" value="<?= $edit_info['dist_nearest_health_center_km'] ?? '' ?>">
        </div>
        <div class="col-md-6 mt-2">
            <label><input type="checkbox" name="awc_available" <?= !empty($edit_info['awc_available']) ? 'checked' : '' ?>> AWC Available</label>
        </div>
        <div class="col-md-6 mt-2">
            <label>Distance to Nearest AWC (km)</label>
            <input name="dist_awc_km" class="form-control" value="<?= $edit_info['dist_nearest_awc_km'] ?? '' ?>">
        </div>
    </div>

    <div class="mb-3 mt-3">
        <label>Road Requirement</label>
        <textarea name="road_need" class="form-control"><?= $edit_info['road_need'] ?? '' ?></textarea>
    </div>
    <div class="mb-3">
        <label>Other Physical Facility Need</label>
        <textarea name="facility_need" class="form-control"><?= $edit_info['other_physical_facility_need'] ?? '' ?></textarea>
    </div>
    <div class="mb-3">
        <label>Remarks</label>
        <textarea name="remarks" class="form-control"><?= $edit_info['remarks'] ?? '' ?></textarea>
    </div>

    <button type="submit" class="btn btn-primary mt-2"><?= $edit_info ? 'Update' : 'Add' ?> Village Info</button>
    <?php if ($edit_info): ?>
        <a href="index.php?page=village_info" class="btn btn-secondary">Cancel</a>
    <?php endif; ?>
</form>

<hr>

<h4 class="mt-5">Village Info Records</h4>
<table class="table table-bordered">
    <thead>
        <tr>
            <th>ID</th>
            <th>Village</th>
            <th>Faliyu</th>
            <th>Total HH</th>
            <th>Pop</th>
            <th>LGD</th>
            <th>Actions</th>
        </tr>
    </thead>
    <tbody>
        <?php while ($row = $info_list->fetch_assoc()): ?>
        <tr>
            <td><?= $row['id'] ?></td>
            <td><?= $row['village_name'] ?></td>
            <td><?= $row['faliya_name'] ?></td>
            <td><?= $row['total_hhs'] ?></td>
            <td><?= $row['total_population'] ?></td>
            <td><?= $row['lgd_code'] ?></td>
            <td>
                <a href="index.php?page=village_info&edit=<?= $row['id'] ?>" class="btn btn-sm btn-warning">Edit</a>
                <a href="index.php?page=village_info&delete=<?= $row['id'] ?>" class="btn btn-sm btn-danger" onclick="return confirm('Delete this record?');">Delete</a>
            </td>
        </tr>
        <?php endwhile; ?>
    </tbody>
</table>
