<?php
include('header.php');
	if (empty($_GET['do'])) {echo $do = '';} else {$do = $_GET['do'];}
	switch ($do) {
	
	case 'edit';
	$id = abs(intval($_GET['id']));
	if (isset($_POST['edit'])) {
	$skill_title = $_POST['skill_title'];
	$skill_range = $_POST['skill_range'];
	if (empty($skill_title)) {
	$message = $general->notification('error','Write The Skill Title Please');
	} else {
	$update = mysql_query("UPDATE skills SET skill_title='$skill_title',skill_range='$skill_range' WHERE skill_id='$id'");
	if ($update) {
	$message = $general->notification('success','Skill Is Saved Successfully');
	} else {
	$message = $general->notification('error','Error Happened');
	}
	}
	}
	$skill = $general->skill($id);
	echo '<div class="page_title">
	<h3>Skills - Edit Skill</h3>
	<a href="skills.php">Back To Skills</a>
	</div>';
	if (isset($message)) {echo $message;}
	echo '<form method="POST" action="">
	<table width="100%" cellpadding="5" cellspacing="0" style="margin-bottom:30px;">
	<tr>
	<td class="label_td" width="150">Skill Title</td>
	<td class="input_td"><input type="text" name="skill_title" size="20" value="'.$skill['skill_title'].'" /></td>
	</tr>
	<tr>
	<td class="label_td" width="150">Skill Range</td>
	<td class="input_td">
		<select name="skill_range">';
		for($i=1;$i<11;$i++) {
		echo '<option value="'.$i.'"'; if ($i == $skill['skill_range']) {echo 'SELECTED';} echo '>'.$i.'/10</option>';
		}
		echo '</select>
		</td>
	</tr>
	<tr>
	<td colspan="2"><input type="submit" name="edit" value="Save" class="inputbutton grey" /></td>
	</tr>
	</table>
	</form>';
	break;
	
	case 'delete';
	$id = abs(intval($_GET['id']));
	echo '<div class="page_title">
	<h3>Skills - Delete Skill</h3>
	<a href="skills.php">Back To Skills</a>
	</div>';
	if (isset($_POST['delete'])) {
	$delete = mysql_query("DELETE FROM skills WHERE skill_id='$id'");
	if ($delete) {
	$message = $general->notification('success','Skill Is Deleted Successfully. You Will Be Redirected To Skills Page In 3 Seconds.');
	echo "<meta http-equiv='refresh' content='3;URL=skills.php'>";
	} else {
	$message = $general->notification('error','Error Happened');
	}
	}
	$skill = $general->skill($id);
	if (isset($message)) {echo $message;}
	echo '<form method="POST" action="">
	<table width="100%" cellpadding="5" cellspacing="0" style="margin-bottom:30px;">
	<tr>
	<td class="label_td">Are You Sure To Delete Skill : <b>'.$skill['skill_title'].'</b> ?</td>
	</tr>
	<tr>
	<td><input type="submit" name="delete" value="Delete" class="inputbutton grey" /></td>
	</tr>
	</table>
	</form>';
	break;
	
	default;
	if (isset($_POST['add'])) {
	$skill_title = $_POST['skill_title'];
	$skill_range = abs(intval($_POST['skill_range']));
	if (empty($skill_title)) {
	$message = $general->notification('error','Write The Skill Title Please');
	} else {
	$insert = mysql_query("INSERT INTO skills (skill_title,skill_range) VALUES ('$skill_title','$skill_range')");
	if ($insert) {
	$message = $general->notification('success','Skill Is Added Successfully');
	} else {
	$message = $general->notification('error','Error Happened');
	}
	}
	}
	echo '<div class="page_title">
	<h3>Skills</h3></div>';
	echo '<form method="POST" action="">
	<table width="100%" cellpadding="5" cellspacing="0" style="margin-bottom:30px;">
	<tr><td colspan="2" class="divider_td">Add New Skill</td></tr>
	<tr>
		<td class="label_td" width="150">Skill Title</td>
		<td class="input_td"><input type="text" name="skill_title" size="20" /></td>
	</tr>
	<tr>
		<td class="label_td" width="150">Skill Range</td>
		<td class="input_td">
		<select name="skill_range">';
		for($i=1;$i<11;$i++) {
		echo '<option value="'.$i.'">'.$i.'/10</option>';
		}
		echo '</select>
		</td>
	</tr>
	<tr>
		<td colspan="2"><input type="submit" name="add" value="Add Skill" class="inputbutton grey" /></td>
	</tr>
	</table>
	</form>';
	if (isset($message)) {echo $message;}
	echo '<table width="100%" cellpadding="10" cellspacing="0">';
	echo '<tr><td colspan="3" class="divider_td">Skills</td></tr>';
	$skills = $general->skills();
	if ($skills != 0) {
	foreach ($skills as $skill) {
	echo '<tr>
		<td class="label_td">'.$skill['skill_title'].'</td>
		<td class="label_td" width="185"><img src="images/stars/'.$skill['skill_range'].'.png" /></td>
		<td class="label_td" width="150" align="right">
		<a href="?do=edit&id='.$skill['skill_id'].'" class="edit">Edit</a>
		<a href="?do=delete&id='.$skill['skill_id'].'" class="delete">Delete</a>
		</td>
	</tr>';
	}
	}
	echo '</table>';
	}
include('footer.php');
?>