<?php
	include 'functions.php';
	include 'fcm.php';
?>

<?php
    $setting_query = "SELECT * FROM tbl_settings where id = '1'";
    $setting_result = mysqli_query($connect, $setting_query);
    $setting_row = mysqli_fetch_assoc($setting_result);
?>

	<?php 
		// create object of functions class
		$function = new functions;
		
		// create array variable to store data from database
		$data = array();
		
		if(isset($_GET['keyword'])) {	
			// check value of keyword variable
			$keyword = $function->sanitize($_GET['keyword']);
			$bind_keyword = "%".$keyword."%";
		} else {
			$keyword = "";
			$bind_keyword = $keyword;
		}
			
		if (empty($keyword)) {
			$sql_query = "SELECT n.nid, n.news_title, n.news_image, n.news_video, c.category_name, n.content_type
					FROM tbl_news n 
						LEFT JOIN tbl_category c ON n.cat_id = c.cid
					GROUP BY n.nid  
					ORDER BY n.nid DESC";
		} else {
			$sql_query = "SELECT n.nid, n.news_title, n.news_image, n.news_video, c.category_name, n.content_type
					FROM tbl_news n 
						LEFT JOIN tbl_category c ON n.cat_id = c.cid
					WHERE n.news_title LIKE ? 
					GROUP BY n.nid  
					ORDER BY n.nid DESC";
		}
		
		
		$stmt = $connect->stmt_init();
		if ($stmt->prepare($sql_query)) {	
			// Bind your variables to replace the ?s
			if (!empty($keyword)) {
				$stmt->bind_param('s', $bind_keyword);
			}
			// Execute query
			$stmt->execute();
			// store result 
			$stmt->store_result();
			$stmt->bind_result( 
					$data['nid'],
					$data['news_title'],
					$data['news_image'],
					$data['news_video'],
					$data['category_name'],
					$data['content_type']
					);
			// get total records
			$total_records = $stmt->num_rows;
		}
			
		// check page parameter
		if (isset($_GET['page'])) {
			$page = $_GET['page'];
		} else {
			$page = 1;
		}
						
		// number of data that will be display per page		
		$offset = 10;
						
		//lets calculate the LIMIT for SQL, and save it $from
		if ($page) {
			$from 	= ($page * $offset) - $offset;
		} else {
			//if nothing was given in page request, lets load the first page
			$from = 0;	
		}	
		
		if (empty($keyword)) {
			$sql_query = "SELECT n.nid, n.news_title, n.news_image, n.news_video, c.category_name, n.content_type
					FROM tbl_news n 
						LEFT JOIN tbl_category c ON n.cat_id = c.cid
					GROUP BY n.nid  
					ORDER BY n.nid DESC LIMIT ?, ?";
		} else {
			$sql_query = "SELECT n.nid, n.news_title, n.news_image, n.news_video, c.category_name, n.content_type
					FROM tbl_news n 
						LEFT JOIN tbl_category c ON n.cat_id = c.cid
					WHERE n.news_title LIKE ? 
					GROUP BY n.nid  
					ORDER BY n.nid DESC LIMIT ?, ?";
		}
		
		$stmt_paging = $connect->stmt_init();
		if ($stmt_paging ->prepare($sql_query)) {
			// Bind your variables to replace the ?s
			if (empty($keyword)) {
				$stmt_paging ->bind_param('ss', $from, $offset);
			} else {
				$stmt_paging ->bind_param('sss', $bind_keyword, $from, $offset);
			}
			// Execute query
			$stmt_paging ->execute();
			// store result 
			$stmt_paging ->store_result();
			$stmt_paging->bind_result(
				$data['nid'],
				$data['news_title'],
				$data['news_image'],
				$data['news_video'],
				$data['category_name'],
				$data['content_type']
			);
			// for paging purpose
			$total_records_paging = $total_records; 
		}

		// if no data on database show "No Reservation is Available"
		if ($total_records_paging == 0) {
	
	?>

    <section class="content">

        <ol class="breadcrumb">
            <li><a href="dashboard.php">Dashboard</a></li>
            <li class="active">Manage Shayari</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">
                        <div class="header">
                            <h2>MANAGE NEWS</h2>
                            <div class="header-dropdown m-r--5">
                                <a href="add-news.php"><button type="button" class="btn bg-blue waves-effect">ADD NEW SHAYARI</button></a>
                            </div>
                        </div>

                        <div class="body table-responsive">
	                        
	                        <form method="get">
	                        	<div class="col-sm-10">
									<div class="form-group form-float">
										<div class="form-line">
											<input type="text" class="form-control" name="keyword" placeholder="Search by title...">
										</div>
									</div>
								</div>
								<div class="col-sm-2">
					                <button type="submit" name="btnSearch" class="btn bg-blue btn-circle waves-effect waves-circle waves-float"><i class="material-icons">search</i></button>
								</div>
							</form>
										
							<table class='table table-hover table-striped'>
								<thead>
									<tr>
										<th width="40%">Shayari</th>
										<th width="25%">Image</th>
										<th width="10%">Category</th>
										<th width="10%"><center>Type</center></th>
										<th width="15%"><center>Action</center></th>
									</tr>
								</thead>

								
							</table>

							<div class="col-sm-10">Wopps! No data found with the keyword you entered.</div>

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

	<?php 
		// otherwise, show data
		} else {
			$row_number = $from + 1;
	?>

    <section class="content">

        <ol class="breadcrumb">
            <li><a href="dashboard.php">Dashboard</a></li>
            <li class="active">Manage Shayari</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">
                        <div class="header">
                            <h2>MANAGE SHAYARI</h2>

                            <div class="header-dropdown m-r--5">
                                <a href="add-news.php"><button type="button" class="btn bg-blue waves-effect">ADD NEW SHAYARI</button></a>
                            </div>
                            <br>
                                <?php if(isset($_SESSION['msg'])) { ?>
                                    <div class='alert alert-info'>
                                        <?php echo $_SESSION['msg']; ?>
                                    </div>
                                <?php unset($_SESSION['msg']); }?>
                        </div>

                        <div class="body table-responsive">
	                        
	                        <form method="get">
	                        	<div class="col-sm-10">
									<div class="form-group form-float">
										<div class="form-line">
											<input type="text" class="form-control" name="keyword" placeholder="Search by title...">
										</div>
									</div>
								</div>
								<div class="col-sm-2">
					                <button type="submit" name="btnSearch" class="btn bg-blue btn-circle waves-effect waves-circle waves-float"><i class="material-icons">search</i></button>
								</div>
							</form>
										
							<table class='table table-hover table-striped'>
								<thead>
									<tr>
										<th width="60%">Shayari</th>
										<th width="15%">Category</th>
										<th width="10%"><center>Type</center></th>
										<th width="15%"><center>Action</center></th>
									</tr>
								</thead>

								<?php 
									while ($stmt_paging->fetch()) { ?>
										<tr>
										    <td style="color:#000;background:#<?php $vb = str_pad(dechex(mt_rand(0,255)),2,'0',STR_PAD_LEFT);$vbb = str_pad(dechex(mt_rand(0,255)),2,'0',STR_PAD_LEFT);$vvb = str_pad(dechex(mt_rand(0,255)),2,'0',STR_PAD_LEFT); echo $vb.$vbb.$vvb;?>;"><center>
                                                <?php 
                                                    if ($data['content_type'] == 'Text') {
                                                        echo '<textarea style="text-align:center;width:100%;height:200px;" disabled>'.$data['news_title'].'</textarea>';
                                                    } else if($data['content_type'] == 'Video') {
                                                        echo '<video width="100%" height="200" controls><source src="upload/'.$data['news_video'].'" type="video/mp4"><source src="upload/'.$data['news_video'].'" type="video/ogg"></video>';
                                                    } else {
                                                        echo '<img src="upload/'.$data['news_image'].'" height="200px" width="100%"/>';
                                                    }
                                                ?>	
											</center></td>
											<td><center><?php echo $data['category_name'];?></center></td>
											<td><center>
                                                <?php if ($data['content_type'] == 'Text') { ?>
                                                    <span class="label bg-green">Text</span>
                                                <?php } else if ($data['content_type'] == 'Photo') { ?>
                                                    <span class="label bg-yellow">Photo</span>
                                                <?php } else { ?>
                                                    <span class="label bg-red">Video</span>
                                                <?php } ?>	
											</center></td>
											<td><center>
											    <?php if($_SESSION['user']=='admin') { ?>
									            <a href="edit-news.php?id=<?php echo $data['nid'];?>">
									                <i class="material-icons">mode_edit</i>
									            </a>
									            <a href="delete-news.php?id=<?php echo $data['nid'];?>" onclick="return confirm('Are you sure want to delete this Shayari?')" >
									                <i class="material-icons">delete</i>
									            </a><?php }?></center>
									        </td>
										</tr>
								<?php 
									}
								?>
							</table>

							<h4><?php $function->doPages($offset, 'manage-news.php', '', $total_records, $keyword); ?></h4>
							<?php 
								}
							?>
						</div>
                    </div>
                </div>
            </div>
        </div>
    </section>