<?php
require_once __DIR__.'/../includes/db.php';
require_once __DIR__.'/../includes/auth.php';

$act = $_GET['a'] ?? 'list';

if ($act === 'save' && $_SERVER['REQUEST_METHOD'] === 'POST') {
    $id = (int)($_POST['id'] ?? 0);
    $title = $_POST['title'] ?? '';
    $content = $_POST['content'] ?? '';
    $address = $_POST['address'] ?? '';
    $phone = $_POST['phone'] ?? '';
    $email = $_POST['email'] ?? '';
    $lat = $_POST['lat'] ?? '';
    $lng = $_POST['lng'] ?? '';

    if ($id > 0) {
        $stmt = $mysqli->prepare("UPDATE profil_desa SET title=?,content=?,address=?,phone=?,email=?,lat=?,lng=? WHERE id=?");
        $stmt->bind_param('sssssssi', $title, $content, $address, $phone, $email, $lat, $lng, $id);
    } else {
        $stmt = $mysqli->prepare("INSERT INTO profil_desa(title,content,address,phone,email,lat,lng) VALUES (?,?,?,?,?,?,?)");
        $stmt->bind_param('sssssss', $title, $content, $address, $phone, $email, $lat, $lng);
    }
    $stmt->execute();
    $stmt->close();
    header('Location: profil.php?msg=saved');
    exit;
}

if ($act === 'del') {
    $id = (int)($_GET['id'] ?? 0);
    if ($id > 0) {
        $stmt = $mysqli->prepare("DELETE FROM profil_desa WHERE id=?");
        $stmt->bind_param('i', $id);
        $stmt->execute();
        $stmt->close();
    }
    header('Location: profil.php?msg=deleted');
    exit;
}

$row = null;
if ($act === 'edit') {
    $id = (int)($_GET['id'] ?? 0);
    $stmt = $mysqli->prepare("SELECT * FROM profil_desa WHERE id=?");
    $stmt->bind_param('i', $id);
    $stmt->execute();
    $row = $stmt->get_result()->fetch_assoc();
    $stmt->close();
}

include __DIR__.'/../includes/header.php';
$msg = $_GET['msg'] ?? '';
?>

<section class="page-hero">
    <div class="page-title">
        <span class="page-title-icon"><i class="ri-government-fill"></i></span>
        <div>
            <h1><?= $act === 'list' ? 'Profil Desa' : (($act === 'edit' && $row) ? 'Edit Profil Desa' : 'Tambah Profil Desa') ?></h1>
            <p>Kelola identitas, alamat, kontak, koordinat, dan deskripsi resmi Desa Mambu.</p>
        </div>
    </div>
    <div class="page-actions">
        <?php if ($act === 'list'): ?>
            <a class="action-btn primary" href="profil.php?a=form"><i class="ri-add-circle-fill"></i> Tambah Profil</a>
        <?php else: ?>
            <a class="action-btn secondary" href="profil.php"><i class="ri-arrow-left-line"></i> Kembali</a>
        <?php endif; ?>
    </div>
</section>

<?php if ($msg): ?>
    <div class="alert"><i class="ri-information-fill"></i><span><?= $msg === 'deleted' ? 'Profil desa berhasil dihapus.' : 'Profil desa berhasil disimpan.' ?></span></div>
<?php endif; ?>

<?php if ($act === 'list'): ?>
    <?php $res = $mysqli->query("SELECT * FROM profil_desa ORDER BY id DESC"); ?>
    <article class="data-card">
        <div class="data-card-head">
            <h2>Daftar Profil Desa</h2>
            <span class="status-pill blue">Data Identitas Desa</span>
        </div>
        <div class="table-wrap">
            <table class="admin-table">
                <thead><tr><th>ID</th><th>Judul</th><th>Alamat</th><th>Telepon</th><th>Email</th><th>Aksi</th></tr></thead>
                <tbody>
                <?php if ($res && $res->num_rows): while($r = $res->fetch_assoc()): ?>
                    <tr>
                        <td><span class="id-badge"><?= (int)$r['id'] ?></span></td>
                        <td class="td-nama"><?= esc($r['title']) ?></td>
                        <td><?= esc($r['address']) ?></td>
                        <td><?= esc($r['phone']) ?></td>
                        <td><?= esc($r['email'] ?? '-') ?></td>
                        <td style="white-space:nowrap">
                            <a class="action-btn success small" href="profil.php?a=edit&id=<?= (int)$r['id'] ?>"><i class="ri-pencil-line"></i> Edit</a>
                            <a class="action-btn danger small" href="profil.php?a=del&id=<?= (int)$r['id'] ?>" onclick="return confirmDelete('Hapus profil desa ini?')"><i class="ri-delete-bin-line"></i> Hapus</a>
                        </td>
                    </tr>
                <?php endwhile; else: ?>
                    <tr><td colspan="6">Belum ada data profil desa.</td></tr>
                <?php endif; ?>
                </tbody>
            </table>
        </div>
    </article>
<?php else: ?>
    <?php $isEdit = ($act === 'edit' && $row); ?>
    <article class="data-card">
        <div class="data-card-head"><h2><?= $isEdit ? 'Form Edit Profil' : 'Form Tambah Profil' ?></h2><span class="status-pill blue">Data Desa</span></div>
        <div class="data-card-body">
            <form class="form-grid" method="post" action="profil.php?a=save">
                <input type="hidden" name="id" value="<?= (int)($row['id'] ?? 0) ?>">
                <div class="form-field"><label>Judul</label><input class="input" name="title" value="<?= esc($row['title'] ?? '') ?>" required></div>
                <div class="form-field"><label>Alamat</label><input class="input" name="address" value="<?= esc($row['address'] ?? '') ?>"></div>
                <div class="form-field"><label>Telepon</label><input class="input" name="phone" value="<?= esc($row['phone'] ?? '') ?>"></div>
                <div class="form-field"><label>Email</label><input class="input" type="email" name="email" value="<?= esc($row['email'] ?? '') ?>"></div>
                <div class="form-field"><label>Latitude</label><input class="input" name="lat" value="<?= esc($row['lat'] ?? '') ?>"></div>
                <div class="form-field"><label>Longitude</label><input class="input" name="lng" value="<?= esc($row['lng'] ?? '') ?>"></div>
                <div class="form-field" style="grid-column:1/-1"><label>Konten</label><textarea class="input" name="content"><?= esc($row['content'] ?? '') ?></textarea></div>
                <div class="form-actions" style="grid-column:1/-1"><button class="action-btn primary" type="submit"><i class="ri-save-3-fill"></i> Simpan</button><a class="action-btn secondary" href="profil.php"><i class="ri-close-line"></i> Batal</a></div>
            </form>
        </div>
    </article>
<?php endif; ?>

<?php include __DIR__.'/../includes/footer.php'; ?>
