<?php
if ($c['level'] == "superadmin") {

	if (isset($_POST['upload'])) {

		$ekstensi_diperbolehkan = array('png','jpg','jpeg');
		$nama      = $_FILES['file']['name'];
		$x         = explode('.', $nama);
		$ekstensi  = strtolower(end($x));
		$ukuran    = $_FILES['file']['size'];
		$file_tmp = $_FILES['file']['tmp_name'];

		$judul        = $_POST['judul'];
		$deskripsi    = $_POST['deskripsi'];
		$minimal_depo = $_POST['minimal_depo'];
		$bonus        = $_POST['bonus'];
		$to           = $_POST['to'];
		$persen       = $_POST['persen'];
		$status       = $_POST['status'];
		$type_bonus   = $_POST['type_bonus'];

		// === TANPA GAMBAR ===
		if ($nama == "") {

			$query = mysqli_query($koneksi, "
				INSERT INTO tb_promosi
				(type_bonus, judul, `text`, minimal_deposit, bonus, turnover, persen, status)
				VALUES
				('$type_bonus', '$judul', '$deskripsi', '$minimal_depo', '$bonus', '$to', '$persen', '$status')
			");

			if ($query) {
				echo "<script>alert('Promosi berhasil ditambahkan');window.location='?page=promosi';</script>";
			}

		// === DENGAN GAMBAR ===
		} else {

			if (in_array($ekstensi, $ekstensi_diperbolehkan)) {

				if ($ukuran < 1044070) {

					move_uploaded_file($file_tmp, '../../uploads/fotopromosi/'.$nama);

					$query = mysqli_query($koneksi, "
						INSERT INTO tb_promosi
						(gambar, type_bonus, judul, `text`, minimal_deposit, bonus, turnover, persen, status)
						VALUES
						('$nama', '$type_bonus', '$judul', '$deskripsi', '$minimal_depo', '$bonus', '$to', '$persen', '$status')
					");

					if ($query) {
						echo "<script>alert('Promosi berhasil ditambahkan');window.location='?page=promosi';</script>";
					}

				} else {
					echo "<script>alert('Ukuran gambar terlalu besar');</script>";
				}

			} else {
				echo "<script>alert('Ekstensi gambar tidak valid');</script>";
			}
		}
	}

} else {
	echo "<script>window.location='?page=dashboard';</script>";
}
?>