<?php
include_once 'config.php';
include_once 'db.php';
if (isset($_GET['key'])) {
	$key = $_GET['key'];
}
if ($key == $panel_key) {
	global $database, $panel;
	if ($panel) {
		if (isset($_POST['p'])) {
			$p = $_POST['p'];
			if ($p == 'a') {
				add_if_url_not_contain('channels', $_POST['u'], "INSERT INTO `channels`(`url`,`status`,`last`,`category`) VALUES ('" . $_POST['u'] . "',1,'','" . $_POST['c'] . "')");
			}
			if ($p == 'e') {
				$database->query("UPDATE `channels` SET `status`='" . $_POST['s'] . "' WHERE id='" . $_POST['i'] . "'");
			}
			if ($p == 'd') {
				$database->query("DELETE FROM `channels` WHERE id='" . $_POST['i'] . "'");
			}
			if ($p == 'c') {
				$database->query("UPDATE `channels` SET `category`='" . $_POST['c'] . "' WHERE id='" . $_POST['i'] . "'");
			}
		}
		$data = $database->query("SELECT * FROM `channels`");
		echo '<html><head><title>Kanallar Kontrol Paneli</title></head><form method="post">Yeni Kanal (Videos Bölümü URL): <input class="h" value="a" name="p"><input type="text" name="u"> WordPress Kategori ID: <input type="text" name="c" size="10"></input> <input type="submit" value="Ekle"></form>';
		if ($data->num_rows == 0) {
			echo 'Henuz kanal eklenmedi.';
		} else {
			echo '<table>
			<tr>
			<th>ID</th>
			<th>URL</th>
			<th>Durum</th>
			<th>Kategori</th>
			<th>Islemler</th>
			</tr>';
			while ($row = $data->fetch_assoc()) {
				$durum;
				$stt;
				$durumyap;
				if ($row['status'] == '0') {
					$durum = 'Pasif';
					$durumyap = 'Aktif';
					$stt = '1';
				}
				if ($row['status'] == '1') {
					$durum = 'Aktif';
					$durumyap = 'Pasif';
					$stt = '0';
				}
				$islemler = '';
				$islemler .= '<div class="islemler"><form method="post"><input class="h" value="d" name="p"><input class="h" value="' . $row['id'] . '" name="i"><input type="submit" value="Sil"></form>';
				$islemler .= '<form method="post"><input class="h" value="e" name="p"><input class="h" value="' . $row['id'] . '" name="i"><input class="h" value="' . $stt . '" name="s"><input type="submit" value="' . $durumyap . ' Yap"></form></div>';
				echo '<tr>
				<td>' . $row['id'] . '</td>
				<td>' . $row['url'] . '</td>
				<td>' . $durum . '</td>
				<td><form method="post"><input class="h" value="c" name="p"><input class="h" value="' . $row['id'] . '" name="i"><input value="' . $row['category'] . '" name="c"> <input type="submit" value="Kaydet"></form></td>
				<td>' . $islemler . '</td>
				</tr>';
			}
			echo '</table><style>table {
			  font-family: arial,sans-serif;
			  border-collapse: collapse;
			  width: 100%;
			}

			td,th {
			  border: 1px solid #dddddd;
			  text-align: left;
			  padding: 8px;
			}

			tr:nth-child(even) {
			  background-color: #dddddd;
			} .islemler{display:flex} .islemler form {margin:4px}</style>';
		}
		echo '<style>.h{display:none}</style></html>';
	} else {
		echo 'Yetkisiz giris.';
	}
} else {
	echo 'Yetkisiz giris.';
}
