<?php  header('Content-type: text/html; charset=utf-8'); ?>
<html>
<link rel="stylesheet" type="text/css" href="css/style.css" />
<body>
<center><table width=400><tr><td colspan=2><span class="header1">Lägg till skjutning</span><br>
<?php
	include("../scripts/sqlinfo.inc");
	include("../cdbmysql.php");

	mysql_connect($host, $user, $pass);
	mysql_selectdb("pyrot");

	$db = new CDBMySQL($host, $user, $pass, "pyrot");

	extract($_REQUEST);
	
	if($del){
		mysql_query("delete from event where col_id=$del");
		$del=0;
	}
	if(!$submit){
		if(!$nr){
	?>
			<form method="post" action="add_event.php">
			<table border=0>
			<tr><td>Rubrik:</td><td><input type="text" name="rubrik"></td></tr>
			<tr><td>Datum (ÅÅÅÅ-MM-DD>:</td><td><input type="text" name="datum"></td></tr>
			<tr><td>Tid (HH:MM-HH:MM):</td><td><input type="text" name="tid"></td></tr>
			<tr><td>Plats:</td><td><input type="text" name="plats"></td></tr>
			<tr><td>Beställare:</td><td><input type="text" name="customer"></td></tr>
			<tr><td>Länk (till evenemang):</td><td><input type="text" name="eventlink"></td></tr>
			
			<tr><td>Ansvarig:</td><td><select name="chief">
			<option value="0">Ingen ännu</option>
	<?php
			$result=mysql_query("SELECT col_id, namn from medlemmar where status != \"utbrand\" order by namn");
			if($r=mysql_fetch_array($result)){
				do {
					printf("<option value=\"%s\">%s</option>", $r["col_id"], $r["namn"]);
				} while($r=mysql_fetch_array($result));
			}
	?>
			</select>
			<tr><td valign="top">Beskrivning:</td><td><textarea name="info" rows=7 cols=40></textarea>
			<tr><td> </td><td><input type="submit" name="submit" value="Spara"></td></tr>
			</table>
			</form>
	<?php
		
		}
		else
		{
			$result=mysql_query("select * from event where col_id=$nr");

			if ($r=mysql_fetch_array($result))
			{
			printf(
				"<form method=\"post\" action=\"add_event.php?nr=%s\">
				<tr><td>Rubrik:</td><td><input type=\"text\" name=\"rubrik\" value=\"%s\"></td></tr>
				<tr><td>Datum (ÅÅÅÅ-MM-DD>:</td><td><input type=\"text\" name=\"datum\" value=\"%s\"></td></tr>
				<tr><td>Tid (HH:MM-HH:MM):</td><td><input type=\"text\" name=\"tid\" value=\"%s\"></td></tr>
				<tr><td>Plats:</td><td><input type=\"text\" name=\"plats\" value=\"%s\"></td></tr>
				<tr><td>Beställare:</td>
					<td><input type=\"text\" name=\"customer\" value=\"%s\"></td></tr>
				<tr><td>Länk (till evenemang:</td>
					<td><input type=\"text\" name=\"eventlink\" value=\"%s\"></td></tr>
				<tr><td>Ansvarig:</td>
				<td><select name=\"chief\">", $nr, $r["rubrik"], $r["datum"], $r["tid"], 
					$r["plats"], $r["customer"], $r["eventlink"]
				);

				$query = "SELECT col_id, namn from medlemmar where status!='utbrand' order by namn";
				$result=mysql_query($query);

			if($r1=mysql_fetch_array($result))
			{
				do {
					printf("<option value=\"%s\"", $r1["col_id"]);
					if ($r1["col_id"] == $r["chief"]) printf(" selected");
					printf(">%s</option>\n", $r1["namn"]);
				} while ($r1=mysql_fetch_array($result));
			}
		}
		?>

		</select>
		<tr><td valign="top">Beskrivning:</td>
		<td><textarea name="info" rows=7 cols=40><?php echo $r["info"]; ?></textarea>
		<tr><td> </td><td><input type="submit" name="submit" value="Spara"></td></tr>
		</table>
		</form>

	<?php	}
		$result=mysql_query("select * from event order by datum desc");

		if($r=mysql_fetch_array($result))
		{
			printf("<center><table width=400 border=0 cellspacing=0 cellpadding=0>
				<tr><td colspan=2><span class=\"header1\">Skjutningar</span></td></tr>");
		do {
			printf("<tr><td colspan=2><b>
				<a href=\"add_event.php?nr=%s\">[Ändra]</a>
				<a href=\"add_event.php?del=%s\">[Radera]</a>
				%s %s, %s</b></td></tr><tr><td colspan=2>Plats: %s</td></tr>"
				,$r["col_id"], $r["col_id"],$r["datum"], $r["tid"], $r["rubrik"], $r["plats"]);
			if ($r["chief"] != "0") {
				$db->Query("SELECT namn FROM medlemmar WHERE col_id = " . $r["chief"]);
				$cname = "";
				if ($db->ReadRow()) {
					$cname = $db->RowData["namn"];
				}
				printf("
					<tr><td>Ansvarig: <a href=\"members.php?id=%s\">%s</a></td></tr>
					<tr><td>Beställare: %s</td></tr>
					<tr><td>Länk (till evenemang): %s</td></tr>

					<tr><td colspan=2>%s<hr></td></tr>",
				 	$r["chief"], $cname, $r["customer"], $r["eventlink"], $r["info"] );
			} else {
				printf("<tr><td>Ansvarig: Ingen &auml;nnu<tr><td colspan=2>%s<hr></td></tr>",
				 	$r["info"]
					);
			}
		}while($r=mysql_fetch_array($result));

		printf("</table></center>");
		}
	}
	else
	{
		$datum 		= addslashes($datum);
		$tid 		= addslashes($tid);
		$plats 		= addslashes($plats);
		$rubrik 	= addslashes($rubrik);
		$info		= addslashes($info);
		$eventlink 	= addslashes($eventlink);
		$customer 	= addslashes($customer);

		if($nr)
		{
			$query = "update event set datum='" . $datum . "', tid='" . $tid . "', plats='" .
				$plats . "', rubrik='" . $rubrik . "', info='" . $info .
				"', customer='" . $customer . "', eventlink='" .
				$eventlink . "', chief = " . $chief . " where col_id = " . $nr;
			$res=mysql_query($query);
		}
		else
		{
			$res=mysql_query("insert into event values
				(null, \"$datum\", \"$tid\", \"$plats\", \"$rubrik\", 
					\"$info\",  \"\", \"\", \"$customer\", \"$eventlink\", " . $chief . ")");
		}

		if ($res=1) {
			printf("Arrangemang %s.", ($nr) ? "uppdaterat" : "inlagt");
		} else {
			printf("Något gick snett...");
		}
		
	}

	$db->Close();

?>
</td></tr></table></center>
</body>
</html>
