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

<?php
$host = "localhost";
$user = "odmr9632_userdb";
$pass = "@Sukabumi2018";
$db   = "odmr9632_order_db";

$conn = new mysqli($host, $user, $pass, $db);
if ($conn->connect_error) {
  die("Koneksi gagal: " . $conn->connect_error);
}
?>
if ($conn->connect_error) {
  die("Koneksi gagal: " . $conn->connect_error);
}

$id = (int)$_GET['id'];
$result = $conn->query("SELECT * FROM cs WHERE id = $id");
if ($result->num_rows === 0) {
  die("CS tidak ditemukan.");
}
$cs = $result->fetch_assoc();

$msg = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  $nama = trim($_POST['nama_cs']);
  if ($nama !== '') {
    $stmt = $conn->prepare("UPDATE cs SET nama_cs = ? WHERE id = ?");
    $stmt->bind_param("si", $nama, $id);
    if ($stmt->execute()) {
      header("Location: tambah_cs.php");
      exit;
    } else {
      $msg = '❌ Gagal memperbarui CS.';
    }
  } else {
    $msg = '❗ Nama CS tidak boleh kosong.';
  }
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Edit CS</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      background: #f0f0f0;
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
    }
    .form-box {
      background: white;
      padding: 30px;
      border-radius: 10px;
      box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
      text-align: center;
      width: 100%;
      max-width: 400px;
    }
    input {
      padding: 10px;
      width: 100%;
      margin-top: 10px;
      font-size: 16px;
      border: 1px solid #ccc;
      border-radius: 6px;
      box-sizing: border-box;
    }
    button {
      background: #f44336;
      color: white;
      border: none;
      padding: 10px;
      margin-top: 15px;
      width: 100%;
      font-size: 16px;
      border-radius: 6px;
      cursor: pointer;
    }
    button:hover {
      background: #d32f2f;
    }
    .msg {
      margin-top: 15px;
      color: red;
      font-weight: bold;
    }
    .back-link {
      display: block;
      text-align: center;
      margin-top: 20px;
      text-decoration: none;
      color: #f44336;
    }
  </style>
</head>
<body>
  <div class="form-box">
    <h2>✏️ Edit CS</h2>
    <form method="POST">
      <input type="text" name="nama_cs" value="<?= htmlspecialchars($cs['nama_cs']) ?>" required>
      <button type="submit">Simpan Perubahan</button>
    </form>
    <?php if ($msg): ?><div class="msg"><?= $msg ?></div><?php endif; ?>
    <a href="tambah_cs.php" class="back-link">⬅️ Kembali ke Daftar CS</a>
  </div>
</body>
</html>
