<?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);
}

$msg = '';

// Simpan perubahan
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  foreach ($_POST['persen'] as $cs => $persen) {
    $persen = (int)$persen;
    $cek = $conn->query("SELECT * FROM cs_profit_setting WHERE nama_cs='$cs'");
    if ($cek->num_rows > 0) {
      $conn->query("UPDATE cs_profit_setting SET persen_cs=$persen WHERE nama_cs='$cs'");
    } else {
      $conn->query("INSERT INTO cs_profit_setting (nama_cs, persen_cs) VALUES ('$cs', $persen)");
    }
  }
  $msg = '✅ Persentase berhasil diperbarui';
}

// Ambil daftar CS
$cs_list = $conn->query("SELECT nama_cs FROM cs");
$persentase = [];
$result = $conn->query("SELECT * FROM cs_profit_setting");
while ($r = $result->fetch_assoc()) {
  $persentase[$r['nama_cs']] = $r['persen_cs'];
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Pengaturan Persentase Profit CS</title>
  <style>
    body { font-family: Arial; background: #f8f8f8; padding: 30px; }
    .container { max-width: 600px; margin: auto; background: white; padding: 20px; border-radius: 10px; box-shadow: 0 0 10px rgba(0,0,0,0.1); }
    table { width: 100%; border-collapse: collapse; margin-top: 20px; }
    th, td { padding: 10px; border: 1px solid #ccc; text-align: left; }
    th { background: #f44336; color: white; }
    input[type=number] { width: 80px; padding: 5px; border-radius: 4px; border: 1px solid #ccc; }
    .msg { text-align: center; color: green; font-weight: bold; margin-top: 10px; }
    button { padding: 10px 20px; background: #4CAF50; color: white; border: none; border-radius: 6px; cursor: pointer; margin-top: 15px; }
    button:hover { background: #45a049; }
    .back-link { display: inline-block; margin-top: 20px; text-align: center; background: gray; color: white; padding: 8px 16px; text-decoration: none; border-radius: 6px; }
  </style>
</head>
<body>
<div class="container">
  <h2>⚙️ Pengaturan Persentase Profit CS</h2>
  <form method="POST">
    <table>
      <tr><th>Nama CS</th><th>Persentase CS (%)</th></tr>
      <?php while($cs = $cs_list->fetch_assoc()): 
        $nama = $cs['nama_cs'];
        $persen = $persentase[$nama] ?? 50;
      ?>
        <tr>
          <td><?= htmlspecialchars($nama) ?></td>
          <td><input type="number" name="persen[<?= $nama ?>]" value="<?= $persen ?>" min="0" max="100"> %</td>
        </tr>
      <?php endwhile; ?>
    </table>
    <button type="submit">💾 Simpan Perubahan</button>
  </form>
  <?php if ($msg): ?><p class="msg"><?= $msg ?></p><?php endif; ?>
  <a class="back-link" href="laporan.php">⬅️ Kembali ke Laporan</a>
</div>
</body>
</html>
