<?php
ini_set("display_errors", 0);
error_reporting(0);
session_start();
require_once("includes/functions.php");
if (isset($_POST['login'])) {
	if (login($_POST['username'], $_POST['password'])) {
		$_SESSION[$authVar] = $_POST['username'];
		header("Location: dashboard.php?anasayfa");
		exit;
	} else {
		$_SESSION['error'] = "Unrecognised username and password combination";
		header("Location: index.php");
		exit;
	}
}
if (!isset($_SESSION[$authVar])) {
	exit;
}
if (isset($_GET['dns-delete']) && isset($_SESSION[$authVar])) {
	deleteDNS($_GET['id']);
	header("Location: dashboard.php?dns");
	exit;
}

if (isset($_GET['ibo-users-delete']) && isset($_SESSION[$authVar])) {
	deleteIBOUser($_GET['section'],$_GET['id']);
	header("Location: dashboard.php?ibo-users&id=" . $_GET['section']);
	exit;
}
if (isset($_POST['adddns']) && isset($_SESSION[$authVar])) {
	addDNS($_POST['name'], $_POST['portal'], $_SESSION[$authVar]);
	header("Location: dashboard.php?dns");
	exit;
}
if (isset($_POST['updatedns']) && isset($_SESSION[$authVar])) {
	$active = false;
	if ($_POST['active'] == "on") {
		$active = true;
	}
	saveDNS($_POST['id'], $_POST['name'], $_POST['portal'], $active);
	header("Location: dashboard.php?dns");
	exit;
}
if (isset($_POST['updateprofile'])) {
    $id = intval(getUserIdFromUsername($_POST['username']));
    $username = $_POST['username'];
    $password = $_POST['password'];

    if (!empty($password)) {
        saveProfile($id, $username, $password);
    } else {
        saveProfile($id, $username, null);
    }

    header("Location: dashboard.php?profile");
    exit;
}
if (isset($_POST['updateibo']) && isset($_SESSION[$authVar])) {
	$data = $_POST;
	unset($data['updateibo']);
	updateDbEntry("ibo_settings", $data);
	header("Location: dashboard.php?ibo-settings");
	exit;
}

if (isset($_POST['addibouser']) && isset($_SESSION[$authVar])) {
	$data = $_POST;
	$username = $_SESSION[$authVar];
	unset($data['addibouser']);
	addDbEntry("ibo_users", $data);

    $query = $mysqli->prepare("SELECT id FROM users WHERE username = ?");
    $query->bind_param("s", $username);
    $query->execute();
    $result = $query->get_result();
    $user = $result->fetch_assoc();
    $user_id = $user['id'];

    $query = $mysqli->prepare("UPDATE users SET credit = credit - 1 WHERE id = ?");
    $query->bind_param("i", $user_id);
    $query->execute();

	header("Location: dashboard.php?ibo-users&id=" . $_POST['dns_id']);
	exit;
}
if (isset($_POST['updateibouser']) && isset($_SESSION[$authVar])) {
	$data = $_POST;
	unset($data['updateibouser']);
	addOrUpdateAccount($data['dns_id'], $data['mac_address'], $data['username'], $data['password']);
	header("Location: dashboard.php?ibo-users&id=" . $_POST['dns_id']);
	exit;
}

if (isset($_POST['addbayi']) && isset($_SESSION[$authVar])) {
    $username = $_POST['username'];
    $password = $_POST['password'];
    $kredi = $_POST['kredi'];

    bayiEkle($username, $password, $kredi);
	header("Location: dashboard.php?bayiedit");
}

if (isset($_POST['anasayfa']) && isset($_SESSION[$authVar])) {
    bayiEkle($username, $password, $kredi);
	header("Location: dashboard.php?bayiedit");
}

if (isset($_POST['updatedealer']) && isset($_SESSION[$authVar])) {
    $id = $_POST['id'];
    $name = $_POST['name'];
    $credit = $_POST['credit'];
    
    $query = $mysqli->prepare("UPDATE users SET username = ?, credit = ? WHERE id = ?");
    $query->bind_param("ssi", $name, $credit, $id);
    
    if ($query->execute()) {
        header("Location: dashboard.php?bayiedit");
        exit;
    } else {
        echo "Güncelleme işlemi başarısız oldu.";
    }
}

if (isset($_GET['deletedealer']) && isset($_SESSION[$authVar])) {
	deleteDealer($_GET['id']);
	header("Location: dashboard.php?bayiedit");
	exit;
}