 <?php 
include 'dheader.php';
if (isset($_POST['submitf'])) {
 	# code...
 	extract($_POST, EXTR_OVERWRITE);
	if (isset($_GET['edit'])) {
		$id = $_GET['edit'];
		$sql = $db->query("UPDATE categories SET name = '$cname' WHERE id = '$id'");
	}else{ 	
 		$sql = $db->query("INSERT INTO categories (name) VALUES ('$cname')");
 	}
 } 
if (isset($_GET['edit'])) {
 	# code...
 	$id = $_GET['edit'];
 	$edit = $db->query("SELECT * FROM categories WHERE id = '$id'");
	$editrow = $edit->fetchArray();
 } 
if (isset($_GET['delete'])) {
 	# code...
 	$id = $_GET['delete'];
 	$delete = $db->query("DELETE FROM categories WHERE id = '$id'");
 }  
?>
	<div class="modal fade hidden-print" id="myModal" tabindex="-1" role="dialog" aria-hidden="true"></div>
	<div class="wrapper ">
		<?php include 'leftbar.php'; ?>
		<!-- left-bar -->
		<?php include 'topbar.php'; ?>
			<!-- top-bar -->
			<div class="main-content">

<div class="row" id="form">
	<div class="spinner" id="grid-loader" style="display:none">
	  <div class="rect1"></div>
	  <div class="rect2"></div>
	  <div class="rect3"></div>
	</div>
	
	<div class="col-md-12">
		<form action="" id="customer_form" class="form-horizontal" enctype="multipart/form-data" method="post" accept-charset="utf-8" novalidate="novalidate">
			<div class="col-md-12">
	<div class="panel panel-piluku">
		<div class="panel-heading">
                <h3 class="panel-title">
                    <i class="ion-edit"></i> 
                    	Category Information						<small>(Fields in red are required)</small>
                </h3>
        </div>

			<div class="panel-body">
				<div class="item_navigation clearfix text-center">
					<ul class="list-inline">
						<li>
													</li>
						<li>
													</li>
					</ul>
				</div>

				<div class="form-group">
					<label for="name" class="col-sm-3 col-md-3 col-lg-2 control-label required wide">Category Name :</label>					<div class="col-sm-9 col-md-9 col-lg-10">
						<input type="text" name="cname" autofocus="on" value="<?php echo @$editrow['name'];  ?>" list="listcat" id="cname" class="form-control form-inps">
						<datalist id="listcat">
						<?php
							$listCat = $db->query("SELECT * FROM categories");
							while($listrow = $listCat->fetchArray()){
						?>
							<option value="<?php echo $listrow['name']; ?>"><?php echo $listrow['name']; ?></option>
						<?php } ?>	
						</datalist>
					</div>
				</div>
				<a href="categories.php" class="submit_button btn btn-success">New Category</a>
 		<div class="form-actions pull-right">
										&nbsp;

					<input type="submit" name="submitf" value="Submit" id="submitf" class="submit_button btn btn-primary">

				</div>

							</div><!--/panel-body -->
		</div><!-- /panel-piluku -->

	</div>
			
		</form>
			</div>
		</div><!-- /row -->


			<div class="container-fluid">
		<div class="row manage-table">
			<div class="panel panel-piluku">
				<div class="panel-heading">
				<h3 class="panel-title">
				<?php
					$countcategories = $db->query("SELECT count(*) AS totalCat FROM categories");
					$count = $countcategories->fetchArray();
				?>
					List of categories					<span title="2 total Categories" class="badge bg-primary tip-left"><?php echo $count['totalCat']; ?></span>
					<span class="panel-options custom">
											</span>
				</h3>
			</div>
				<div class="panel-body nopadding table_holder table-responsive" >
					<table class="tablesorter table  table-hover" id="sortable_table">
						<thead>
							<tr>
								<th>Category Id</th>
								<th>Name</th>
								<th>Edit</th>
								<th>Delete</th>
							</tr>
						</thead>
						<tbody>
						<?php
							$categories = $db->query("SELECT * FROM categories");
							while($row = $categories->fetchArray()){
						?>
							<tr>
								<td><?php echo $row['id']; ?></td>
								<td><a href="#" class="underline"><?php echo $row['name']; ?></a></td>
								<td><a href="categories.php?edit=<?php echo $row['id']; ?>" class="update-category" title="Update">Edit</a></td>
								<td><a href="categories.php?delete=<?php echo $row['id']; ?>" class="delete_category" title="Delete">Delete</a></td>
							</tr>
						<?php } ?>		
							</tbody>
						</table>			
				</div>	
		</div>	
				</div>

	

			
<?php include 'dfooter.php'; ?>