<?php
require '../../config.php';
if (session_status() == PHP_SESSION_NONE) {
  session_start();
}

if ($_SESSION['peran'] != 'admin') {
  exit('Akses ditolak.');
}

$id = $_POST['id'];
$nama = mysqli_real_escape_string($conn, $_POST['nama_diskon']);
$persen = (int) $_POST['persen'];
$aktif = $_POST['aktif'];

if ($id) {
  // Edit
  mysqli_query($conn, "UPDATE diskon SET nama_diskon='$nama', persen=$persen, aktif='$aktif' WHERE id=$id");
} else {
  // Tambah
  mysqli_query($conn, "INSERT INTO diskon (nama_diskon, persen, aktif) VALUES ('$nama', $persen, '$aktif')");
}

header("Location: daftar_diskon.php");
