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

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $village_id = $_POST['village_id'] ?? null;
    $village_name = trim($_POST['village_name']);
    $taluka_id = (int)$_POST['taluka_id'];
    $talati_name = trim($_POST['talati_name']);
    $talati_mobile_no = trim($_POST['talati_mobile_no']);
    $created_date = date('Y-m-d H:i:s');

    if ($village_name !== '' && $taluka_id) {
        if ($village_id) {
            $stmt = $conn->prepare("UPDATE tbl_village SET village_name=?, taluka_id=?, talati_name=?, talati_mobile_no=? WHERE village_id=?");
            $stmt->bind_param("sissi", $village_name, $taluka_id, $talati_name, $talati_mobile_no, $village_id);
        } else {
            $stmt = $conn->prepare("INSERT INTO tbl_village (village_name, taluka_id, talati_name, talati_mobile_no, created_date) VALUES (?, ?, ?, ?, ?)");
            $stmt->bind_param("sisss", $village_name, $taluka_id, $talati_name, $talati_mobile_no, $created_date);
        }
        $stmt->execute();
        $stmt->close();
    }
    header("Location: index.php?page=villages");
    exit();
}

if (isset($_GET['delete'])) {
    $delete_id = (int)$_GET['delete'];
    $conn->query("DELETE FROM tbl_village WHERE village_id = $delete_id");
    header("Location: index.php?page=villages");
    exit();
}

$edit_village = null;
if (isset($_GET['edit'])) {
    $edit_id = (int)$_GET['edit'];
    $result = $conn->query("SELECT * FROM tbl_village WHERE village_id = $edit_id");
    $edit_village = $result->fetch_assoc();
}

$talukas = $conn->query("SELECT * FROM tbl_taluko ORDER BY taluka_name");
$villages = $conn->query("SELECT v.*, t.taluka_name FROM tbl_village v JOIN tbl_taluko t ON v.taluka_id = t.taluka_id ORDER BY village_id DESC");
?>

<h2>Manage Villages</h2>

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

    <div class="mb-3">
        <label>Village Name</label>
        <input type="text" name="village_name" class="form-control" required value="<?= $edit_village['village_name'] ?? '' ?>">
    </div>

    <div class="mb-3">
        <label>Taluka</label>
        <select name="taluka_id" class="form-control" required>
            <option value="">Select Taluka</option>
            <?php while ($t = $talukas->fetch_assoc()): ?>
                <option value="<?= $t['taluka_id'] ?>" <?= ($edit_village && $edit_village['taluka_id'] == $t['taluka_id']) ? 'selected' : '' ?>>
                    <?= $t['taluka_name'] ?>
                </option>
            <?php endwhile; ?>
        </select>
    </div>

    <div class="mb-3">
        <label>Talati Name</label>
        <input type="text" name="talati_name" class="form-control" value="<?= $edit_village['talati_name'] ?? '' ?>">
    </div>

    <div class="mb-3">
        <label>Talati Mobile No</label>
        <input type="text" name="talati_mobile_no" class="form-control" value="<?= $edit_village['talati_mobile_no'] ?? '' ?>">
    </div>

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

<table class="table table-bordered">
    <thead>
        <tr>
            <th>ID</th>
            <th>Village Name</th>
            <th>Taluka</th>
            <th>Talati</th>
            <th>Mobile</th>
            <th>Actions</th>
        </tr>
    </thead>
    <tbody>
        <?php while ($row = $villages->fetch_assoc()): ?>
        <tr>
            <td><?= $row['village_id'] ?></td>
            <td><?= $row['village_name'] ?></td>
            <td><?= $row['taluka_name'] ?></td>
            <td><?= $row['talati_name'] ?></td>
            <td><?= $row['talati_mobile_no'] ?></td>
            <td>
                <a href="index.php?page=villages&edit=<?= $row['village_id'] ?>" class="btn btn-sm btn-warning">Edit</a>
                <a href="index.php?page=villages&delete=<?= $row['village_id'] ?>" class="btn btn-sm btn-danger" onclick="return confirm('Delete this village?');">Delete</a>
            </td>
        </tr>
        <?php endwhile; ?>
    </tbody>
</table>