<?php
session_start(); // Wajib ada di baris paling atas
include '../config/koneksi.php'; // Pastikan path ini benar

// ========================================================================
// --- INISIALISASI VARIABEL ---
// ========================================================================
$message = '';
$login_error = '';
$active_tab = $_GET['tab'] ?? 'pengaturan'; // Tab default adalah 'pengaturan'

// Variabel untuk menampung data yang akan diedit
$edit_testimoni = null;
$edit_ticker = null;
$edit_footer = null;


// ========================================================================
// --- LOGIKA LOGIN & LOGOUT (TETAP SAMA) ---
// ========================================================================

if (isset($_GET['logout'])) {
    session_destroy();
    header("Location: admin.php");
    exit;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['login'])) {
    $username = $_POST['username'];
    $password = $_POST['password'];
    $stmt = $koneksi->prepare("SELECT password FROM tb_user WHERE username = ?");
    $stmt->bind_param("s", $username);
    $stmt->execute();
    $result = $stmt->get_result();
    
    if ($result->num_rows === 1) {
        $user = $result->fetch_assoc();
        if (password_verify($password, $user['password'])) {
            $_SESSION['admin_logged_in'] = true;
            $_SESSION['username'] = $username;
            header("Location: admin.php?tab=pengaturan"); // Redirect ke tab pengaturan
            exit;
        }
    }
    $login_error = "Username atau password salah.";
}

// ========================================================================
// --- LOGIKA HALAMAN ADMIN (HANYA JIKA SUDAH LOGIN) ---
// ========================================================================

if (isset($_SESSION['admin_logged_in']) && $_SESSION['admin_logged_in'] === true) {

    // Arahkan 'tab' dari form POST
    if (isset($_POST['tab'])) {
        $active_tab = $_POST['tab'];
    }

    // --- LOGIKA PENGATURAN (UPDATE) ---
    if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_pengaturan'])) {
        $pengaturan_post = $_POST['pengaturan'];
        $stmt = $koneksi->prepare("UPDATE pengaturan SET nilai_pengaturan = ? WHERE nama_pengaturan = ?");
        
        foreach ($pengaturan_post as $nama => $nilai) {
            $stmt->bind_param("ss", $nilai, $nama);
            $stmt->execute();
        }
        $stmt->close();
        $message = "Pengaturan berhasil diperbarui!";
    }

    // --- LOGIKA PRODUK (TAMBAH) ---
    if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['tambah_produk'])) {
        $judul = $_POST['judul'];
        $deskripsi = $_POST['deskripsi'];
        $harga = $_POST['harga'];
        $gambar = '';

        if (isset($_FILES['gambar']) && $_FILES['gambar']['error'] == 0) {
            $target_dir = "../uploads/";
            if (!is_dir($target_dir)) { mkdir($target_dir, 0755, true); } // Buat folder jika belum ada

            $file_name = uniqid() . "_" . basename($_FILES["gambar"]["name"]);
            $target_file = $target_dir . $file_name;
            $imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
            $allowed = ['jpg', 'jpeg', 'png', 'webp'];

            if (in_array($imageFileType, $allowed)) {
                if (move_uploaded_file($_FILES["gambar"]["tmp_name"], $target_file)) {
                    $gambar = $file_name;
                } else { $message = "Gagal upload gambar."; }
            } else { $message = "Format gambar tidak didukung."; }
        }

        if ($gambar && $judul && $deskripsi && $harga) {
            $stmt = $koneksi->prepare("INSERT INTO produk (judul, deskripsi, harga, gambar) VALUES (?, ?, ?, ?)");
            $stmt->bind_param("ssss", $judul, $deskripsi, $harga, $gambar);
            if ($stmt->execute()) { $message = "Produk berhasil ditambahkan!"; } 
            else { $message = "Gagal menambah produk."; }
            $stmt->close();
        }
    }

    // --- LOGIKA PRODUK (HAPUS) ---
    if (isset($_GET['hapus_produk'])) {
        $id = (int)$_GET['hapus_produk'];
        $sql = "SELECT gambar FROM produk WHERE id = ?";
        $stmt = $koneksi->prepare($sql);
        $stmt->bind_param("i", $id);
        $stmt->execute();
        $row = $stmt->get_result()->fetch_assoc();
        if ($row && file_exists("../uploads/" . $row['gambar'])) {
            unlink("uploads/" . $row['gambar']);
        }
        $stmt->close();

        $hapus = $koneksi->prepare("DELETE FROM produk WHERE id = ?");
        $hapus->bind_param("i", $id);
        $hapus->execute();
        $hapus->close();
        header("Location: admin.php?tab=produk&message=Produk dihapus");
        exit;
    }

    // --- LOGIKA CRUD: TESTIMONI ---
    if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['tambah_testimoni'])) {
        $stmt = $koneksi->prepare("INSERT INTO testimoni (isi_testimoni, nama_penulis, jabatan_penulis, inisial_penulis) VALUES (?, ?, ?, ?)");
        $stmt->bind_param("ssss", $_POST['isi_testimoni'], $_POST['nama_penulis'], $_POST['jabatan_penulis'], $_POST['inisial_penulis']);
        $stmt->execute();
        $message = "Testimoni ditambahkan!";
    }
    if (isset($_GET['edit_testimoni_id'])) {
        $id_edit = (int)$_GET['edit_testimoni_id'];
        $stmt = $koneksi->prepare("SELECT * FROM testimoni WHERE id = ?");
        $stmt->bind_param("i", $id_edit);
        $stmt->execute();
        $edit_testimoni = $stmt->get_result()->fetch_assoc();
    }
    if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_testimoni'])) {
        $stmt = $koneksi->prepare("UPDATE testimoni SET isi_testimoni = ?, nama_penulis = ?, jabatan_penulis = ?, inisial_penulis = ? WHERE id = ?");
        $stmt->bind_param("ssssi", $_POST['isi_testimoni'], $_POST['nama_penulis'], $_POST['jabatan_penulis'], $_POST['inisial_penulis'], $_POST['id_to_edit']);
        $stmt->execute();
        header("Location: admin.php?tab=testimoni&message=Testimoni diperbarui");
        exit;
    }
    if (isset($_GET['hapus_testimoni'])) {
        $stmt = $koneksi->prepare("DELETE FROM testimoni WHERE id = ?");
        $stmt->bind_param("i", (int)$_GET['hapus_testimoni']);
        $stmt->execute();
        header("Location: admin.php?tab=testimoni&message=Testimoni dihapus");
        exit;
    }

    // --- LOGIKA CRUD: TICKER ---
    if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['tambah_ticker'])) {
        $stmt = $koneksi->prepare("INSERT INTO ticker (isi_ticker, icon_ticker) VALUES (?, ?)");
        $stmt->bind_param("ss", $_POST['isi_ticker'], $_POST['icon_ticker']);
        $stmt->execute();
        $message = "Ticker ditambahkan!";
    }
    if (isset($_GET['edit_ticker_id'])) {
        $id_edit = (int)$_GET['edit_ticker_id'];
        $stmt = $koneksi->prepare("SELECT * FROM ticker WHERE id = ?");
        $stmt->bind_param("i", $id_edit);
        $stmt->execute();
        $edit_ticker = $stmt->get_result()->fetch_assoc();
    }
    if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_ticker'])) {
        $stmt = $koneksi->prepare("UPDATE ticker SET isi_ticker = ?, icon_ticker = ? WHERE id = ?");
        $stmt->bind_param("ssi", $_POST['isi_ticker'], $_POST['icon_ticker'], $_POST['id_to_edit']);
        $stmt->execute();
        header("Location: admin.php?tab=ticker&message=Ticker diperbarui");
        exit;
    }
    if (isset($_GET['hapus_ticker'])) {
        $stmt = $koneksi->prepare("DELETE FROM ticker WHERE id = ?");
        $stmt->bind_param("i", (int)$_GET['hapus_ticker']);
        $stmt->execute();
        header("Location: admin.php?tab=ticker&message=Ticker dihapus");
        exit;
    }

    // --- LOGIKA CRUD: FOOTER LAYANAN ---
    if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['tambah_footer_layanan'])) {
        $stmt = $koneksi->prepare("INSERT INTO footer_layanan (nama_layanan, urutan) VALUES (?, ?)");
        $stmt->bind_param("si", $_POST['nama_layanan'], $_POST['urutan']);
        $stmt->execute();
        $message = "Layanan Footer ditambahkan!";
    }
    if (isset($_GET['edit_footer_id'])) {
        $id_edit = (int)$_GET['edit_footer_id'];
        $stmt = $koneksi->prepare("SELECT * FROM footer_layanan WHERE id = ?");
        $stmt->bind_param("i", $id_edit);
        $stmt->execute();
        $edit_footer = $stmt->get_result()->fetch_assoc();
    }
    if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_footer_layanan'])) {
        $stmt = $koneksi->prepare("UPDATE footer_layanan SET nama_layanan = ?, urutan = ? WHERE id = ?");
        $stmt->bind_param("sii", $_POST['nama_layanan'], $_POST['urutan'], $_POST['id_to_edit']);
        $stmt->execute();
        header("Location: admin.php?tab=footer_layanan&message=Layanan Footer diperbarui");
        exit;
    }
    if (isset($_GET['hapus_footer_layanan'])) {
        $stmt = $koneksi->prepare("DELETE FROM footer_layanan WHERE id = ?");
        $stmt->bind_param("i", (int)$_GET['hapus_footer_layanan']);
        $stmt->execute();
        header("Location: admin.php?tab=footer_layanan&message=Layanan Footer dihapus");
        exit;
    }


    // --- AMBIL SEMUA DATA UNTUK DITAMPILKAN ---
    $produk = $koneksi->query("SELECT * FROM produk ORDER BY id DESC");
    $testimoni_list = $koneksi->query("SELECT * FROM testimoni ORDER BY id DESC");
    $ticker_list = $koneksi->query("SELECT * FROM ticker ORDER BY id ASC");
    $footer_layanan_list = $koneksi->query("SELECT * FROM footer_layanan ORDER BY urutan ASC");
    
    // Ambil pengaturan ke array asosiatif
    $pengaturan_db = $koneksi->query("SELECT * FROM pengaturan");
    $pengaturan = [];
    while ($row = $pengaturan_db->fetch_assoc()) {
        $pengaturan[$row['nama_pengaturan']] = $row;
    }
    
    if (isset($_GET['message'])) {
        $message = htmlspecialchars($_GET['message']);
    }

} // --- AKHIR DARI BLOK 'SUDAH LOGIN' ---

?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Panel - dulohxd</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body { padding-top: 56px; background-color: #f8f9fa; }
        .login-card { max-width: 400px; }
        /* Style untuk textarea pengaturan */
        .form-pengaturan textarea { min-height: 100px; }
    </style>
</head>
<body>

<?php if (isset($_SESSION['admin_logged_in']) && $_SESSION['admin_logged_in'] === true): ?>
    
    <nav class="navbar navbar-dark bg-dark fixed-top">
        <div class="container">
            <span class="navbar-brand">Admin Panel - (<?= htmlspecialchars($_SESSION['username']) ?>)</span>
            <div>
                <a href="index.php" class="btn btn-outline-light btn-sm" target="_blank">Lihat Website</a>
                <a href="admin.php?logout=1" class="btn btn-outline-danger btn-sm ms-2">Logout</a>
            </div>
        </div>
    </nav>

    <div class="container mt-4">
        
        <?php if ($message): ?>
            <div class="alert alert-info alert-dismissible fade show" role="alert">
                <?= htmlspecialchars($message) ?>
                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
            </div>
        <?php endif; ?>

        <ul class="nav nav-tabs" id="adminTab" role="tablist">
            <li class="nav-item" role="presentation">
                <button class="nav-link <?= $active_tab == 'pengaturan' ? 'active' : '' ?>" id="pengaturan-tab" data-bs-toggle="tab" data-bs-target="#pengaturan" type="button" role="tab">Pengaturan Umum</button>
            </li>
            <li class="nav-item" role="presentation">
                <button class="nav-link <?= $active_tab == 'produk' ? 'active' : '' ?>" id="produk-tab" data-bs-toggle="tab" data-bs-target="#produk" type="button" role="tab">Produk</button>
            </li>
            <li class="nav-item" role="presentation">
                <button class="nav-link <?= $active_tab == 'testimoni' ? 'active' : '' ?>" id="testimoni-tab" data-bs-toggle="tab" data-bs-target="#testimoni" type="button" role="tab">Testimoni</button>
            </li>
            <li class="nav-item" role="presentation">
                <button class="nav-link <?= $active_tab == 'ticker' ? 'active' : '' ?>" id="ticker-tab" data-bs-toggle="tab" data-bs-target="#ticker" type="button" role="tab">Ticker Hero</button>
            </li>
            <li class="nav-item" role="presentation">
                <button class="nav-link <?= $active_tab == 'footer_layanan' ? 'active' : '' ?>" id="footer-layanan-tab" data-bs-toggle="tab" data-bs-target="#footer-layanan" type="button" role="tab">Layanan Footer</button>
            </li>
        </ul>

        <div class="tab-content" id="adminTabContent">

            <div class="tab-pane fade <?= $active_tab == 'pengaturan' ? 'show active' : '' ?>" id="pengaturan" role="tabpanel">
                <div class="card shadow-sm border-0 mt-3">
                    <div class="card-body p-4">
                        <h3 class="mb-4">Pengaturan Umum Website</h3>
                        <form method="POST" action="admin.php" class="form-pengaturan">
                            <input type="hidden" name="tab" value="pengaturan">
                            <div class="row g-3">
                                <?php foreach ($pengaturan as $nama => $item): ?>
                                    <div class="col-md-6">
                                        <label class="form-label text-capitalize fw-bold"><?= str_replace('_', ' ', $nama) ?></label>
                                        <?php if (strlen($item['nilai_pengaturan']) > 100): // Deteksi jika teks panjang, gunakan textarea ?>
                                            <textarea name="pengaturan[<?= $nama ?>]" class="form-control"><?= htmlspecialchars($item['nilai_pengaturan']) ?></textarea>
                                        <?php else: ?>
                                            <input type="text" name="pengaturan[<?= $nama ?>]" class="form-control" value="<?= htmlspecialchars($item['nilai_pengaturan']) ?>">
                                        <?php endif; ?>
                                    </div>
                                <?php endforeach; ?>
                            </div>
                            <button type="submit" name="update_pengaturan" class="btn btn-primary mt-4">Simpan Pengaturan</button>
                        </form>
                    </div>
                </div>
            </div>

            <div class="tab-pane fade <?= $active_tab == 'produk' ? 'show active' : '' ?>" id="produk" role="tabpanel">
                <div class="card shadow-sm border-0 mt-3">
                    <div class="card-body p-4">
                        <h3 class="mb-4">Tambah Produk Baru</h3>
                        <form method="POST" action="admin.php" enctype="multipart/form-data">
                            <input type="hidden" name="tab" value="produk">
                            <div class="mb-3">
                                <label class="form-label">Judul</label>
                                <input type="text" name="judul" class="form-control" required>
                            </div>
                            <div class="mb-3">
                                <label class="form-label">Deskripsi</label>
                                <textarea name="deskripsi" class="form-control" rows="3" required></textarea>
                            </div>
                            <div class="mb-3">
                                <label class="form-label">Harga</label>
                                <input type="text" name="harga" class="form-control" placeholder="Contoh: Rp 500.000" required>
                            </div>
                            <div class="mb-3">
                                <label class="form-label">Gambar (jpg, jpeg, png, webp)</label>
                                <input type="file" name="gambar" class="form-control" accept="image/*" required>
                            </div>
                            <button type="submit" name="tambah_produk" class="btn btn-primary">Tambah Produk</button>
                        </form>
                    </div>
                </div>

                <h3 class="mt-5 mb-3">Daftar Produk</h3>
                <div class="row">
                    <?php while ($row = $produk->fetch_assoc()): ?>
                        <div class="col-md-6 col-lg-4 mb-3">
                            <div class="card shadow-sm">
                                <img src="uploads/<?= htmlspecialchars($row['gambar']) ?>" class="card-img-top" style="height:150px; object-fit:cover;">
                                <div class="card-body">
                                    <h6 class="card-title"><?= htmlspecialchars($row['judul']) ?></h6>
                                    <a href="admin.php?hapus_produk=<?= $row['id'] ?>&tab=produk" class="btn btn-sm btn-danger w-100" onclick="return confirm('Hapus produk ini?')">Hapus</a>
                                </div>
                            </div>
                        </div>
                    <?php endwhile; ?>
                </div>
            </div>
            
            <div class="tab-pane fade <?= $active_tab == 'testimoni' ? 'show active' : '' ?>" id="testimoni" role="tabpanel">
                <div class="row mt-3">
                    <div class="col-md-4">
                        <div class="card shadow-sm border-0">
                            <div class="card-body p-4">
                                <h3 class="mb-4"><?= $edit_testimoni ? 'Edit Testimoni' : 'Tambah Testimoni' ?></h3>
                                <form method="POST" action="admin.php">
                                    <input type="hidden" name="tab" value="testimoni">
                                    <?php if ($edit_testimoni): ?>
                                        <input type="hidden" name="id_to_edit" value="<?= $edit_testimoni['id'] ?>">
                                    <?php endif; ?>
                                    <div class="mb-3">
                                        <label class="form-label">Isi Testimoni</label>
                                        <textarea name="isi_testimoni" class="form-control" rows="3" required><?= htmlspecialchars($edit_testimoni['isi_testimoni'] ?? '') ?></textarea>
                                    </div>
                                    <div class="mb-3">
                                        <label class="form-label">Nama Penulis</label>
                                        <input type="text" name="nama_penulis" class="form-control" value="<?= htmlspecialchars($edit_testimoni['nama_penulis'] ?? '') ?>" required>
                                    </div>
                                    <div class="mb-3">
                                        <label class="form-label">Jabatan</label>
                                        <input type="text" name="jabatan_penulis" class="form-control" value="<?= htmlspecialchars($edit_testimoni['jabatan_penulis'] ?? '') ?>">
                                    </div>
                                    <div class="mb-3">
                                        <label class="form-label">Inisial (1 Huruf)</label>
                                        <input type="text" name="inisial_penulis" class="form-control" maxlength="2" value="<?= htmlspecialchars($edit_testimoni['inisial_penulis'] ?? '') ?>">
                                    </div>
                                    <?php if ($edit_testimoni): ?>
                                        <button type="submit" name="update_testimoni" class="btn btn-success">Update</button>
                                        <a href="admin.php?tab=testimoni" class="btn btn-secondary">Batal</a>
                                    <?php else: ?>
                                        <button type="submit" name="tambah_testimoni" class="btn btn-primary">Tambah</button>
                                    <?php endif; ?>
                                </form>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-8">
                        <div class="card shadow-sm border-0">
                            <div class="card-body p-4">
                                <h3 class="mb-4">Daftar Testimoni</h3>
                                <table class="table table-striped table-hover">
                                    <thead>
                                        <tr>
                                            <th>Isi</th>
                                            <th>Nama</th>
                                            <th>Aksi</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php while($row = $testimoni_list->fetch_assoc()): ?>
                                        <tr>
                                            <td><?= htmlspecialchars(substr($row['isi_testimoni'], 0, 50)) ?>...</td>
                                            <td><?= htmlspecialchars($row['nama_penulis']) ?></td>
                                            <td>
                                                <a href="admin.php?edit_testimoni_id=<?= $row['id'] ?>&tab=testimoni" class="btn btn-sm btn-warning">Edit</a>
                                                <a href="admin.php?hapus_testimoni=<?= $row['id'] ?>&tab=testimoni" class="btn btn-sm btn-danger" onclick="return confirm('Hapus item ini?')">Hapus</a>
                                            </td>
                                        </tr>
                                        <?php endwhile; ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="tab-pane fade <?= $active_tab == 'ticker' ? 'show active' : '' ?>" id="ticker" role="tabpanel">
                <div class="row mt-3">
                    <div class="col-md-4">
                        <div class="card shadow-sm border-0">
                            <div class="card-body p-4">
                                <h3 class="mb-4"><?= $edit_ticker ? 'Edit Ticker' : 'Tambah Ticker' ?></h3>
                                <form method="POST" action="admin.php">
                                    <input type="hidden" name="tab" value="ticker">
                                    <?php if ($edit_ticker): ?>
                                        <input type="hidden" name="id_to_edit" value="<?= $edit_ticker['id'] ?>">
                                    <?php endif; ?>
                                    <div class="mb-3">
                                        <label class="form-label">Isi Ticker</label>
                                        <input type="text" name="isi_ticker" class="form-control" value="<?= htmlspecialchars($edit_ticker['isi_ticker'] ?? '') ?>" required>
                                    </div>
                                    <div class="mb-3">
                                        <label class="form-label">Icon (Font Awesome)</label>
                                        <input type="text" name="icon_ticker" class="form-control" value="<?= htmlspecialchars($edit_ticker['icon_ticker'] ?? 'fas fa-check') ?>" placeholder="Contoh: fas fa-bullhorn">
                                    </div>
                                    <?php if ($edit_ticker): ?>
                                        <button type="submit" name="update_ticker" class="btn btn-success">Update</button>
                                        <a href="admin.php?tab=ticker" class="btn btn-secondary">Batal</a>
                                    <?php else: ?>
                                        <button type="submit" name="tambah_ticker" class="btn btn-primary">Tambah</button>
                                    <?php endif; ?>
                                </form>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-8">
                        <div class="card shadow-sm border-0">
                            <div class="card-body p-4">
                                <h3 class="mb-4">Daftar Ticker</h3>
                                <table class="table table-striped table-hover">
                                    <thead>
                                        <tr>
                                            <th>Icon</th>
                                            <th>Isi</th>
                                            <th>Aksi</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php while($row = $ticker_list->fetch_assoc()): ?>
                                        <tr>
                                            <td><i class="<?= htmlspecialchars($row['icon_ticker']) ?>"></i></td>
                                            <td><?= htmlspecialchars($row['isi_ticker']) ?></td>
                                            <td>
                                                <a href="admin.php?edit_ticker_id=<?= $row['id'] ?>&tab=ticker" class="btn btn-sm btn-warning">Edit</a>
                                                <a href="admin.php?hapus_ticker=<?= $row['id'] ?>&tab=ticker" class="btn btn-sm btn-danger" onclick="return confirm('Hapus item ini?')">Hapus</a>
                                            </td>
                                        </tr>
                                        <?php endwhile; ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="tab-pane fade <?= $active_tab == 'footer_layanan' ? 'show active' : '' ?>" id="footer-layanan" role="tabpanel">
                <div class="row mt-3">
                    <div class="col-md-4">
                        <div class="card shadow-sm border-0">
                            <div class="card-body p-4">
                                <h3 class="mb-4"><?= $edit_footer ? 'Edit Layanan' : 'Tambah Layanan' ?></h3>
                                <form method="POST" action="admin.php">
                                    <input type="hidden" name="tab" value="footer_layanan">
                                    <?php if ($edit_footer): ?>
                                        <input type="hidden" name="id_to_edit" value="<?= $edit_footer['id'] ?>">
                                    <?php endif; ?>
                                    <div class="mb-3">
                                        <label class="form-label">Nama Layanan</label>
                                        <input type="text" name="nama_layanan" class="form-control" value="<?= htmlspecialchars($edit_footer['nama_layanan'] ?? '') ?>" required>
                                    </div>
                                    <div class="mb-3">
                                        <label class="form-label">Urutan</label>
                                        <input type="number" name="urutan" class="form-control" value="<?= htmlspecialchars($edit_footer['urutan'] ?? '99') ?>">
                                    </div>
                                    <?php if ($edit_footer): ?>
                                        <button type="submit" name="update_footer_layanan" class="btn btn-success">Update</button>
                                        <a href="admin.php?tab=footer_layanan" class="btn btn-secondary">Batal</a>
                                    <?php else: ?>
                                        <button type="submit" name="tambah_footer_layanan" class="btn btn-primary">Tambah</button>
                                    <?php endif; ?>
                                </form>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-8">
                        <div class="card shadow-sm border-0">
                            <div class="card-body p-4">
                                <h3 class="mb-4">Daftar Layanan di Footer</h3>
                                <table class="table table-striped table-hover">
                                    <thead>
                                        <tr>
                                            <th>Nama Layanan</th>
                                            <th>Urutan</th>
                                            <th>Aksi</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php while($row = $footer_layanan_list->fetch_assoc()): ?>
                                        <tr>
                                            <td><?= htmlspecialchars($row['nama_layanan']) ?></td>
                                            <td><?= htmlspecialchars($row['urutan']) ?></td>
                                            <td>
                                                <a href="admin.php?edit_footer_id=<?= $row['id'] ?>&tab=footer_layanan" class="btn btn-sm btn-warning">Edit</a>
                                                <a href="admin.php?hapus_footer_layanan=<?= $row['id'] ?>&tab=footer_layanan" class="btn btn-sm btn-danger" onclick="return confirm('Hapus item ini?')">Hapus</a>
                                            </td>
                                        </tr>
                                        <?php endwhile; ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

        </div> </div>

<?php else: ?>

    <div class="container d-flex justify-content-center align-items-center vh-100">
        <div class="card login-card shadow-sm border-0">
            <div class="card-body p-4">
                <h3 class="card-title text-center mb-4">Admin Login</h3>
                <?php if ($login_error): ?>
                    <div class="alert alert-danger"><?= htmlspecialchars($login_error) ?></div>
                <?php endif; ?>
                <form method="POST" action="admin.php">
                    <div class="mb-3">
                        <label for="username" class="form-label">Username</label>
                        <input type="text" name="username" id="username" class="form-control" required>
                    </div>
                    <div class="mb-3">
                        <label for="password" class="form-label">Password</label>
                        <input type="password" name="password" id="password" class="form-control" required>
                    </div>
                    <button type="submit" name="login" class="btn btn-primary w-100">Login</button>
                </form>
            </div>
        </div>
    </div>

<?php endif; ?>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
