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

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $faliya_id = $_POST['faliya_id'] ?? null;
    $faliya_name = trim($_POST['faliya_name']);
    $village_id = (int)$_POST['village_id'];

    if ($faliya_name !== '' && $village_id) {
        if ($faliya_id) {
            $stmt = $conn->prepare("UPDATE tbl_faliyu SET faliya_name=?, village_id=? WHERE faliya_id=?");
            $stmt->bind_param("sii", $faliya_name, $village_id, $faliya_id);
        } else {
            $stmt = $conn->prepare("INSERT INTO tbl_faliyu (faliya_name, village_id) VALUES (?, ?)");
            $stmt->bind_param("si", $faliya_name, $village_id);
        }
        $stmt->execute();
        $stmt->close();
    }
    header("Location: index.php?page=faliyu");
    exit();
}

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

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

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

<h2>Manage Faliyu (Habitation Names)</h2>

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

    <div class="mb-3">
        <label>Village</label>
        <select name="village_id" class="form-control" required>
            <option value="">Select Village</option>
            <?php while ($v = $villages->fetch_assoc()): ?>
                <option value="<?= $v['village_id'] ?>" <?= ($edit_faliyu && $edit_faliyu['village_id'] == $v['village_id']) ? 'selected' : '' ?>>
                    <?= $v['village_name'] ?>
                </option>
            <?php endwhile; ?>
        </select>
    </div>
    <div class="mb-3">
        <label>Faliyu Name</label>
        <input type="text" name="faliya_name" class="form-control" required value="<?= $edit_faliyu['faliya_name'] ?? '' ?>">
    </div>

    

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

<table class="table table-bordered">
    <thead>
        <tr>
            <th>ID</th>
            <th>Faliyu Name</th>
            <th>Village Name</th>
            <th>Actions</th>
        </tr>
    </thead>
    <tbody>
        <?php while ($row = $faliyas->fetch_assoc()): ?>
        <tr>
            <td><?= $row['faliya_id'] ?></td>
            <td><?= $row['faliya_name'] ?></td>
            <td><?= $row['village_name'] ?? '—' ?></td>
            <td>
                <a href="index.php?page=faliyu&edit=<?= $row['faliya_id'] ?>" class="btn btn-sm btn-warning">Edit</a>
                <a href="index.php?page=faliyu&delete=<?= $row['faliya_id'] ?>" class="btn btn-sm btn-danger" onclick="return confirm('Delete this faliyu?');">Delete</a>
            </td>
        </tr>
        <?php endwhile; ?>
    </tbody>
</table>