<?php require_once('../../db.php'); ?>
<?php 
// if isset post
if ( isset($_POST["client_id"]) && isset($_POST["client_id"]) ) {
	
	// first check if client id exists!
	try {
		$core = Core::getInstance();
		$stmt = $core -> dbh -> prepare("SELECT * FROM myclients WHERE client_id = :client_id");
		$stmt -> bindParam("client_id", $_POST["client_id"], PDO::PARAM_INT);
		$stmt -> execute();
		$row_count = $stmt -> rowCount();
		
		if (isset($_POST["premium"]) && $_POST["premium"] == "1") {
			$premium = "1";
		} else {
			$premium = "0";
		}
		
		if ($row_count > 0) {
			
			try {
				$core = Core::getInstance();
				$stmt_up = $core -> dbh -> prepare("UPDATE myclients set client_name = :client_name, client_date = :client_date, last_edited = NOW(), description = :description, premium = :premium, ag = :ag WHERE client_id = :client_id");
				$stmt_up -> bindParam("client_name", $_POST["client_name"], PDO::PARAM_STR);
				$stmt_up -> bindParam("client_date", $_POST["client_date"], PDO::PARAM_STR);
				$stmt_up -> bindParam("description", $_POST["client_description"], PDO::PARAM_STR);
				$stmt_up -> bindParam("premium", $premium, PDO::PARAM_STR);
				$stmt_up -> bindParam("client_id", $_POST["client_id"], PDO::PARAM_INT);
				$stmt_up -> bindParam("ag", $_POST["client_ag"], PDO::PARAM_INT);
				$stmt_up -> execute();
				$row_up = $stmt_up -> rowCount();
				
				if ($row_up > 0) {
					echo "true";
				} else {
					echo "false";
				}
			} catch(PDOException $pe) {
				trigger_error('Could not connect to MySQL database. ' . $pe->getMessage() , E_USER_ERROR);
			}
			
		} else {
			echo "Client does not exist";
		}
	} catch(PDOException $pe) {
		trigger_error('Could not connect to MySQL database. ' . $pe->getMessage() , E_USER_ERROR);
	}
	
}
?>