<?php
// File: admin/form_keunggulan.php
session_start();
if (!isset($_SESSION['admin'])) {
  header("Location: login.php");
  exit;
}

include '../config/koneksi.php';

$edit = false;
$id = '';
$icon = '';
$judul = '';
$deskripsi = '';

if (isset($_GET['id'])) {
  $edit = true;
  $id = $_GET['id'];
  $query = mysqli_query($conn, "SELECT * FROM section_keunggulan WHERE id = $id");
  $data = mysqli_fetch_assoc($query);

  if ($data) {
    $icon = $data['icon'];
    $judul = $data['judul'];
    $deskripsi = $data['deskripsi'];
  } else {
    header("Location: section_keunggulan.php");
    exit;
  }
}

if (isset($_POST['submit'])) {
  $icon = $_POST['icon'];
  $judul = $_POST['judul'];
  $deskripsi = $_POST['deskripsi'];

  if ($edit) {
    $query = "UPDATE section_keunggulan SET icon='$icon', judul='$judul', deskripsi='$deskripsi' WHERE id=$id";
    mysqli_query($conn, $query);
    header("Location: section_keunggulan.php?edit=success");
  } else {
    $query = "INSERT INTO section_keunggulan (icon, judul, deskripsi) VALUES ('$icon', '$judul', '$deskripsi')";
    mysqli_query($conn, $query);
    header("Location: section_keunggulan.php?add=success");
  }
  exit;
}

include 'partials/header.php';
include 'partials/topbar.php';
include 'partials/sidebar.php';
?>

<!-- Content Wrapper -->
<div class="content-wrapper">
  <!-- Content Header -->
  <div class="content-header">
    <div class="container-fluid">
      <h1 class="m-0"><?= $edit ? 'Edit Keunggulan' : 'Tambah Keunggulan' ?></h1>
    </div>
  </div>

  <!-- Main Content -->
  <section class="content">
    <div class="container-fluid">

      <div class="card">
        <div class="card-header">
          <h3 class="card-title"><?= $edit ? 'Form Edit Keunggulan' : 'Form Tambah Keunggulan' ?></h3>
        </div>
        <div class="card-body">
          <form method="post">
            <div class="form-group">
              <label>Icon (class Bootstrap Icons)</label>
              <input type="text" name="icon" class="form-control" required value="<?= htmlspecialchars($icon) ?>">
              <small class="form-text text-muted">
                Contoh: <code>bi bi-code-slash</code> — 
                <a href="https://icons.getbootstrap.com/" target="_blank">Lihat ikon</a>
              </small>
            </div>

            <div class="form-group">
              <label>Judul</label>
              <input type="text" name="judul" class="form-control" required value="<?= htmlspecialchars($judul) ?>">
            </div>

            <div class="form-group">
              <label>Deskripsi</label>
              <textarea name="deskripsi" class="form-control" rows="4" required><?= htmlspecialchars($deskripsi) ?></textarea>
            </div>

            <button type="submit" name="submit" class="btn btn-primary">
              <i class="fas fa-save"></i> <?= $edit ? 'Update' : 'Simpan' ?>
            </button>
            <a href="section_keunggulan.php" class="btn btn-secondary">
              <i class="fas fa-arrow-left"></i> Kembali
            </a>
          </form>
        </div>
      </div>

    </div>
  </section>
</div>

<?php include 'partials/footer.php'; ?>