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

<?php

	error_reporting(0);

    // delete selected records
    if(isset($_POST['submit'])) {

        $arr = $_POST['chk_id'];
        $count = count($arr);
        if ($count > 0) {
            foreach ($arr as $nid) {

                $sql_image = "SELECT radio_image FROM tbl_radio WHERE id = $nid";
                $img_results = $connect->query($sql_image);

                $sql_delete = "DELETE FROM tbl_radio WHERE id = $nid";
                $delete = $connect->query($sql_delete);

                if ($delete) {
                    while ($row = mysqli_fetch_assoc($img_results)) {
                    	unlink('upload/' . $row['radio_image']);
                    }
                    $_SESSION['msg'] = "$count Selected radios deleted";
                } else {
                    $_SESSION['msg'] = "Error deleting record";
                }

            }
        } else {
            $_SESSION['msg'] = "Whoops! no radios selected to delete";
        }
        header("Location:radio.php");
        exit;
    }

	if (isset($_REQUEST['keyword']) && $_REQUEST['keyword']<>"") {
		$keyword = $_REQUEST['keyword'];
		$reload = "radio.php";
		$sql =  "SELECT r.*, c.category_name FROM tbl_radio r, tbl_category c WHERE r.category_id = c.cid AND c.category_status = 1 AND r.radio_name LIKE '%$keyword%'";
		$result = $connect->query($sql);
	} else {
		$reload = "radio.php";
		$sql =  "SELECT r.*, c.category_name FROM tbl_radio r, tbl_category c WHERE r.category_id = c.cid AND c.category_status = 1 ORDER BY r.id 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['page']) && isset($_GET['add'])) {
		if (validateResourceAccess($inputToken)) {
			$data = array('featured' => '1');	
			$hasil = update('tbl_radio', $data, "WHERE id = '".$_GET['add']."'");
			if ($hasil > 0) {
				$_SESSION['msg'] = 'Success added to featured radio...';
				header("Location:radio.php?page=".$_GET['page']);
				exit;	
			}
		} else {
			systemUpdateFlag($invalidResponse);
		}			
	}

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

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

    if (isset($_GET['page']) && isset($_GET['enable'])) {
    	if (validateResourceAccess($inputToken)) {
			$data = array('radio_status' => '1');	
			$update = update('tbl_radio', $data, "WHERE id = '".$_GET['enable']."'");
			if ($update > 0) {
		        $_SESSION['msg'] = "Radio successfully enabled";
		        header("Location:radio.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 Radio</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 RADIO</h2>
						<div class="header-dropdown m-r--5">
							<a href="radio-add.php"><button type="button" class="button button-rounded btn-offset waves-effect waves-float">ADD NEW RADIO</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="radio.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 radios.</p>
						<?php } else { ?>

						<form method="post" action="">

							<div style="margin-left: 8px; margin-top: -36px; margin-bottom: 10px;">
								<button type="submit" name="<?php if (validateResourceAccess($inputToken)) { echo 'submit'; } else { echo 'validate'; } ?>" id="submit" class="button button-rounded waves-effect waves-float" onclick="return confirm('Are you sure want to delete all selected radios?')">Delete selected items(s)</button>
							</div>				

							<table class='table table-hover table-striped'>
								<thead>
									<tr>
										<th width="1%">
											<div style="margin-bottom: -15px">
												<input id="chk_all" name="chk_all" type="checkbox" class="filled-in chk-col-blue" />
												<label for="chk_all"></label>
											</div>
										</th>
										<th width="25%">Radio Name</th>
										<th width="10%">Image</th>
										<th width="1%">Radio Url</th>
										<th width="10%"><center>Category</center></th>
										<th width="1%"><center>Type</center></th>
										<th width="10%"><center>Views</center></th>
										<th width="1%"><center>Featured</center></th>
										<th width="20%"><center>Action</center></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;" width="1%">
											<div style="margin-top: 10px;">
												<input type="checkbox" name="chk_id[]" id="<?php echo $data['id'];?>" class="chkbox filled-in chk-col-blue" value="<?php echo $data['id'];?>"/>
												<label for="<?php echo $data['id'];?>"></label>
											</div>
										</td>

										<td style="vertical-align: middle;"><?php if ($data['radio_status'] == '1') { echo $data['radio_name']; } else { echo '<strike>'.$data['radio_name'].'</strike>'; } ?></td>

										<td style="vertical-align: middle;"><img style="object-fit:cover;" class="<?php if ($data['radio_status'] == '1') { echo 'img-rounded';} else {echo 'img-rounded img-grayscale';} ?>" src="upload/<?php echo $data['radio_image']; ?>" width="60px" height="60px"/></td>

										<td style="vertical-align: middle;">
											<?php
                                                $value = $data['radio_url'];
                                                if (strlen($value) > 33)
                                                $value = substr($value, 0, 30) . '...';	
                                                if ($data['radio_status'] == '1') { echo $value; } else { echo '<strike>'.$value.'</strike>'; }			
											?>
										</td>

										<td style="vertical-align: middle;"><center><?php if ($data['radio_status'] == '1') { echo $data['category_name']; } else { echo '<strike>'.$data['category_name'].'</strike>'; } ?></center></td>
										<td style="vertical-align: middle;">
											<center>
												<?php if ($data['type'] == 'radio') { ?>
												<span class="label label-rounded bg-green">RADIO</span>
												<?php } else if ($data['type'] == 'mp3') { ?>
												<span class="label label-rounded bg-orange">&nbsp;MP3&nbsp;</span>
												<?php } else { ?>
												<span class="label label-rounded bg-grey">UNKNOWN</span>
												<?php } ?>
											</center>
										</td>

										<td style="vertical-align: middle;"><center><?php if ($data['radio_status'] == '1') { echo number_format($data['view_count']); } else { echo '<strike>'.number_format($data['view_count']).'</strike>'; } ?></center></td>

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

										<td style="vertical-align: middle;"><center>

											<?php if ($data['radio_status'] == '1') { ?>
											<a href="radio.php?page=<?php echo $page; ?>&disable=<?php echo $data['id'];?>" onclick="return confirm('Are you sure want to disable this radio?')">
												<i class="material-icons">visibility</i>
											</a>
											<?php } else { ?>
											<a href="radio.php?page=<?php echo $page; ?>&enable=<?php echo $data['id'];?>" onclick="return confirm('Are you sure want to enable this radio?')">
												<i class="material-icons">visibility_off</i>
											</a>
											<?php } ?>											

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

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

						</form>

						<?php } ?>

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

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

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