<?php include_once('includes/header.php'); ?>

<?php

	error_reporting(0);

	if (isset($_REQUEST['keyword']) && $_REQUEST['keyword']<>"") {
		$keyword = $_REQUEST['keyword'];
		$reload = "category.php";
		$sql =  "SELECT * FROM tbl_category WHERE category_name LIKE '%$keyword%'";
		$result = $connect->query($sql);
	} else {
		$reload = "category.php";
		$sql =  "SELECT * FROM tbl_category ORDER BY cid DESC";
		$result = $connect->query($sql);
	}

	$rpp = $postPerPage;
	$page = intval($_GET["page"]);
	if($page <= 0) $page = 1;  
	$tcount = mysqli_num_rows($result);
	$tpages = ($tcount) ? ceil($tcount / $rpp) : 1;
	$count = 0;
	$i = ($page-1) * $rpp;
	$no_urut = ($page-1) * $rpp;

	if (isset($_GET['add'])) {
		if (validateResourceAccess($inputToken)) {
			$data = array('featured' => '1');	
			$hasil = update('tbl_category', $data, "WHERE cid = '".$_GET['add']."'");
			if ($hasil > 0) {
				$_SESSION['msg'] = 'Success added to featured category...';
				header( "Location:category.php");
				exit;
			}
		} else {
			systemUpdateFlag($invalidResponse);
		}
	}

	if (isset($_GET['remove'])) {
		if (validateResourceAccess($inputToken)) {
			$data = array('featured' => '0');	
			$hasil = update('tbl_category', $data, "WHERE cid = '".$_GET['remove']."'");
			if ($hasil > 0) {
				$_SESSION['msg'] = 'Success removed from featured category...';
				header( "Location:category.php");
				exit;	
			}
		} else {
			systemUpdateFlag($invalidResponse);
		}
	}

    if (isset($_GET['page']) && isset($_GET['disable'])) {
    	if (validateResourceAccess($inputToken)) {
			$data = array('category_status' => '0');	
			$update = update('tbl_category', $data, "WHERE cid = '".$_GET['disable']."'");
			if ($update > 0) {
		        $_SESSION['msg'] = "Category successfully disabled";
		        header('Location:category.php?page='.$_GET['page']);
				exit;
			}
		} else {
			systemUpdateFlag($invalidResponse);
		}
    }

    if (isset($_GET['page']) && isset($_GET['enable'])) {
    	if (validateResourceAccess($inputToken)) {
			$data = array('category_status' => '1');	
			$update = update('tbl_category', $data, "WHERE cid = '".$_GET['enable']."'");
			if ($update > 0) {
		        $_SESSION['msg'] = "Category successfully enabled";
		        header("Location:category.php?page=".$_GET['page']);
				exit;
			}
		} else {
			systemUpdateFlag($invalidResponse);
		}
    }		

?>

<section class="content">

	<ol class="breadcrumb">
		<li><a href="dashboard.php">Dashboard</a></li>
		<li class="active">Manage Category</a></li>
	</ol>

	<div class="container-fluid">

		<div class="row clearfix">
			<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
				<div class="card corner-radius">
					<div class="header">
						<h2>MANAGE CATEGORY</h2>
						<div class="header-dropdown m-r--5">
							<a href="category-add.php"><button type="button" class="button button-rounded btn-offset waves-effect waves-float">ADD NEW CATEGORY</button></a>
						</div>
					</div>

					<div style="margin-top: -10px;" class="body table-responsive">

						<?php if(isset($_SESSION['msg'])) { ?>
						<div class='alert alert-info alert-dismissible corner-radius bottom-offset' role='alert'>
							<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span>&nbsp;&nbsp;</button>
							<?php echo $_SESSION['msg']; ?>
						</div>
						<?php unset($_SESSION['msg']); } ?>

						<form method="get" id="form_validation">
							<table class='table'>
								<tr>
									<td>
										<div class="form-group form-float">
											<div class="form-line">
												<input type="text" class="form-control" name="keyword" placeholder="Search..." required>
											</div>
										</div>
									</td>
									<td width="1%"><a href="category.php"><button type="button" class="button button-rounded waves-effect waves-float">RESET</button></a></td>
									<td width="1%"><button type="submit" class="btn bg-blue btn-circle waves-effect waves-circle waves-float"><i class="material-icons">search</i></button></td>
								</tr>
							</table>
						</form>

						<?php if ($tcount == 0) { ?>
							<p align="center" style="font-size: 110%;">There are no categories.</p>
						<?php } else { ?>							

						<table class='table table-hover table-striped table-offset'>
							<thead>
								<tr>
									<th>Category Name</th>
									<th>Image</th>
									<th><center>Featured</center></th>
									<th width="15%">Action</th>
								</tr>
							</thead>
							<?php
							while(($count < $rpp) && ($i < $tcount)) {
								mysqli_data_seek($result, $i);
								$data = mysqli_fetch_array($result);
								?>
								<tr>
									<td style="vertical-align: middle;"><?php if ($data['category_status'] == '1') { echo $data['category_name']; } else { echo '<strike>'.$data['category_name'].'</strike>'; } ?></td>
									
									<td style="vertical-align: middle;"><img style="object-fit:cover;" class="<?php if ($data['category_status'] == '1') { echo 'img-rounded';} else {echo 'img-rounded img-grayscale';} ?>" src="upload/category/<?php echo $data['category_image']; ?>" width="60px" height="60px"/></td>

									<td align="center" style="vertical-align: middle;">
										<?php if ($data['featured'] == '0') { ?>
										<a href="category.php?add=<?php echo $data['cid'];?>" onclick="return confirm('Add to featured category?')" ><i class="material-icons" style="color:grey">lens</i></a>
										<?php } else { ?>
										<a href="category.php?remove=<?php echo $data['cid'];?>" onclick="return confirm('Remove from featured category?')" ><i class="material-icons" style="color:#4bae4f">lens</i></a>
										<?php } ?>
									</td>

									<td style="vertical-align: middle;">
										<?php if ($data['category_status'] == '1') { ?>
										<a href="category.php?page=<?php echo $page; ?>&disable=<?php echo $data['cid'];?>" onclick="return confirm('Disabling this category means all the radios in this category will not be visible too, are you sure?')">
											<i class="material-icons">visibility</i>
										</a>
										<?php } else { ?>
										<a href="category.php?page=<?php echo $page; ?>&enable=<?php echo $data['cid'];?>" onclick="return confirm('Are you sure want to enable this category?')">
											<i class="material-icons">visibility_off</i>
										</a>
										<?php } ?>

										<a href="category-edit.php?id=<?php echo $data['cid'];?>">
											<i class="material-icons">mode_edit</i>
										</a>

										<a href="category-delete.php?id=<?php echo $data['cid'];?>" onclick="return confirm('Are you sure want to delete this category?')" >
											<i class="material-icons">delete</i>
										</a>
									</td>
								</tr>
								<?php
								$i++; 
								$count++;
							}
							?>
						</table>

						<?php } ?>

						<?php if ($tcount > $postPerPage) { echo pagination($reload, $page, $keyword, $tpages); } ?>

					</div>

				</div>
			</div>
		</div>
	</div>
</section>

<?php include_once('includes/footer.php'); ?>