<?php
$title = 'CRUD (Create, Read, Update, and Delete) Database';
require '../utils/dbPDO.php';
require '../utils/validate.php';
$dalingk = new dalingkDB();
$db = $dalingk->connect();
function displayTable($db) {
	$query = $db->query('SELECT custID, NameF, NameL FROM tblCustomers;');
	$count = $query->rowCount();
	$output = "<h2 class=\"subheader\">Results</h2><p class=\"center\"><strong>$count</strong> records in the database.</p>\n<table class=\"simple\">\n<tr><th>CustID</th><th>First Name</th><th>Last Name</th><th>Delete</th><th>Update</th></tr>\n";
	foreach ($query->fetchAll() as $row) {
		$delete = "<form method=\"post\"><input type=\"submit\" value=\"Delete\"><input type=\"hidden\" value=\"$row[custID]\" name=\"deleteID\"></form>";
		$update = "<form method=\"get\"><input type=\"submit\" value=\"Update\"><input type=\"hidden\" value=\"$row[custID]\" name=\"updateID\"></form>";
		$output .= "<tr><td>$row[custID]</td><td>$row[NameF]</td><td>$row[NameL]</td><td>$delete</td><td>$update</td></tr>\n";
	}
	return $output .= '</table>';
}
function inputForm($namef = '', $namel = '', $custid = '') {
	$action = (is_numeric($custid)) ? "Update" : "Add";
	$title = (is_numeric($custid)) ? " customer #$custid" : ' new customer';
	$update = (is_numeric($custid)) ? "<input type=\"hidden\" name=\"custID\" value=\"$custid\">" : '';
	return <<<EOF
<h2 class="subheader">$action$title</h2>
<form method="post" class="center">
	<input type="text" placeholder="First Name" value="$namef" name="namef" autofocus>
	<input type="text" placeholder="Last Name" value="$namel" name="namel">
	$update
	<input type="submit" value="$action">
</form>
EOF;
}
function deleteCust($db, $custID) {
	$db->beginTransaction();
	$q = $db->prepare("DELETE FROM tblCustomers WHERE custID = ?;");
	$q->execute(array($custID));
	$db->commit();
}
function insertCust($db, $namef, $namel) {
	$db->beginTransaction();
	$q = $db->prepare("INSERT INTO tblCustomers (NameF, NameL) VALUES (?, ?);");
	$q->execute(array($namef, $namel));
	$db->commit();
}
function updateCust($db, $custID, $namef, $namel) {
	$cust = $db->prepare("SELECT custID FROM tblCustomers WHERE custID = ?;");
	$cust->execute(array($custID));
	if ($cust->rowCount() > 0) {
		$db->beginTransaction();
		$q = $db->prepare("UPDATE tblCustomers SET NameF = ?, NameL = ? WHERE custID = ?;");
		$q->execute(array($namef, $namel, $custID));
		$db->commit();
	} else {
		$main .= "Customer with custID $custID does not exist";
	}	
}
function getCust($db, $custID) {
	$q = $db->prepare("SELECT NameF, NameL, custID FROM tblCustomers WHERE custID = ? ORDER BY custID;");
	$q->execute(array($custID));
	return ($q->rowCount() == 1) ? $q->fetch() : false;
}

if (isset($_POST['custID']) && isset($_POST['namef']) && isset($_POST['namel'])) {
	updateCust($db, intval($_POST['custID']), htmlspecialchars($_POST['namef']), htmlspecialchars($_POST['namel']));
	header("location: crud.php");
	exit();
} else if (isset($_POST['namef']) && isset($_POST['namel'])) {
	insertCust($db, htmlspecialchars($_POST['namef']), htmlspecialchars($_POST['namel']));
	header("location: crud.php");
	exit();
} else if (isset($_POST['deleteID'])) {
	if (is_numeric($_POST['deleteID'])) {
		deleteCust($db, intval($_POST['deleteID']));
		header("location: crud.php");
		exit();
	}
} else if (isset($_GET['updateID'])) {
	if (is_numeric($_GET['updateID'])) {
		$custInfo = getCust($db, intval($_GET['updateID']));
		$main .= inputForm($custInfo['NameF'], $custInfo['NameL'], $custInfo['custID']);
	}
} else {
	$main = inputForm();
}
$main .= displayTable($db);
require '../utils/simpleTemplate.php'
?>
