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

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $taluka_name = trim($_POST['taluka_name']);
    $district_id = (int)$_POST['district_id'];
    $taluka_id = $_POST['taluka_id'] ?? null;

    if ($taluka_name !== '' && $district_id) {
        if ($taluka_id) {
            $stmt = $conn->prepare("UPDATE tbl_taluko SET taluka_name=?, district_id=? WHERE taluka_id=?");
            $stmt->bind_param("sii", $taluka_name, $district_id, $taluka_id);
        } else {
            $stmt = $conn->prepare("INSERT INTO tbl_taluko (taluka_name, district_id) VALUES (?, ?)");
            $stmt->bind_param("si", $taluka_name, $district_id);
        }
        $stmt->execute();
        $stmt->close();
    }
    header("Location: index.php?page=talukos");
    exit();
}

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

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

$districts = $conn->query("SELECT * FROM tbl_district ORDER BY district_name");
$talukos = $conn->query("SELECT t.*, d.district_name FROM tbl_taluko t JOIN tbl_district d ON t.district_id = d.district_id ORDER BY taluka_id DESC");
?>

<h2>Manage Talukos</h2>

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

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

    <div class="mb-3">
        <label>District</label>
        <select name="district_id" class="form-control" required>
            <option value="">Select District</option>
            <?php while ($d = $districts->fetch_assoc()): ?>
                <option value="<?= $d['district_id'] ?>" <?= ($edit_taluko && $edit_taluko['district_id'] == $d['district_id']) ? 'selected' : '' ?>>
                    <?= $d['district_name'] ?>
                </option>
            <?php endwhile; ?>
        </select>
    </div>

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

<table class="table table-bordered">
    <thead>
        <tr>
            <th>ID</th>
            <th>Taluka Name</th>
            <th>District</th>
            <th>Actions</th>
        </tr>
    </thead>
    <tbody>
        <?php while ($row = $talukos->fetch_assoc()): ?>
        <tr>
            <td><?= $row['taluka_id'] ?></td>
            <td><?= $row['taluka_name'] ?></td>
            <td><?= $row['district_name'] ?></td>
            <td>
                <a href="index.php?page=talukos&edit=<?= $row['taluka_id'] ?>" class="btn btn-sm btn-warning">Edit</a>
                <a href="index.php?page=talukos&delete=<?= $row['taluka_id'] ?>" class="btn btn-sm btn-danger" onclick="return confirm('Delete this taluko?');">Delete</a>
            </td>
        </tr>
        <?php endwhile; ?>
    </tbody>
</table>