<?php session_start(); error_reporting(0); 
if(!isset($_SESSION['j_admin'])){ $url = urlencode("http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");header('location:../login/?url='.$url.''); } ?>
<!doctype html>
<html lang="en"> 
	<head>
		<?php
		require_once("../j_conn/url.php");
		require_once("../j_conn/dbc.php");
		require_once("../j_incs/functions.php");
		$page_name="News Center";
		$page_desc="$company_desc";
		$page_img="".$dir."j_files/j_img/logo/logo-meta.png";
		$page_author="$company";
		$page_keywords="".$company.", $page_name, $page_desc, ".$company_motto.", $company_desc";
		$page_url="".$dir."blog/";
		require_once("../j_incs/metas.php");
		require_once("../j_incs/scripts-top.php");
		?>
	</head>
	<body>
		<div class="container-scroller">
			<?php require_once("../j_incs/header.php"); ?>
			<div class="container-fluid page-body-wrapper">
				<?php require_once("../j_incs/nav.php"); ?>
				<div class="main-panel">
					<div class="content-wrapper">
						<div class="page-header">
							<h3 class="page-title">
							  <?php echo $page_name; ?>
							</h3>
							<nav aria-label="breadcrumb">
							  <ol class="breadcrumb">
								<li class="breadcrumb-item"><a href="<?php echo $dir; ?>">Dashboard</a></li>
								<li class="breadcrumb-item active" aria-current="page"><?php echo $page_name; ?></li>
							  </ol>
							</nav>
						</div>
						<div class="row grid-margin">
							<div class="col-12">
								
								<div class="card">
									<div class="card-body">
										
										
										<?php 
										$sql4="SELECT * from j_blog WHERE j_deleted < 1";  
										$result4 = runSQL($sql4);
										$total = mysqli_num_rows($result4);
										
										echo "<h4>Count: <b>".number_format($total)."</b></h4><br/>";
										
										
										/*
										$limit =100;
										$offset = RMC($_GET['m']);
										if($offset<1){
											$offset = 0;
											$num = 1;
										}else{
											$num=$offset+1;
										}
										*/
										
										/*
										$sql4="SELECT * from j_users WHERE j_deleted < 1 AND j_status < 1 order by j_id desc limit $limit offset $offset"; //check 
										$result4 = runSQL($sql4);
										$count = mysqli_num_rows($result4); 
										*/
										if($total>0){
	
										?>
							
										<div class="w-100">
											
											
											
											
											
									
									
											<?php 
											if(isset($_GET['country'])){ $s_country = urldecode($_GET['country']); $s_country = ($s_country); }else{ $s_country = ""; }
											if(isset($_GET['state'])){ $s_state = urldecode($_GET['state']); $s_state = ($s_state); }else{ $s_state = ""; }
											if(isset($_GET['city'])){ $s_city = urldecode($_GET['city']); $s_city = ($s_city); }else{ $s_city = ""; }
											
											if(isset($_GET['u'])){ $s_user = urldecode($_GET['u']); $s_user = ($s_user); }else{ $s_user = ""; }
											
											if(isset($_GET['industry'])){ $s_industry = urldecode($_GET['industry']); $s_industry = ($s_industry); }else{ $s_industry = ""; }
											
											if(isset($_GET['status'])){ $s_status = urldecode($_GET['status']); $s_status = ($s_status); }else{ $s_status = ""; }
											
											if(isset($_GET['date'])){ $s_date = urldecode($_GET['date']); $s_date = ($s_date); }else{ $s_date = ""; }
											
											if(isset($_GET['order'])){ $s_order = urldecode($_GET['order']); $s_order = trim($s_order); }else{ $s_order = ""; }
											if(isset($_GET['search'])){ $s_search = urldecode($_GET['search']); $s_search = ($s_search); }else{ $s_search = ""; }
											

											$double_q="' '";
											$single_q="'";
											$single_dq='"';
											$apos="'";

											if(empty($s_country)){
												$and_s_country="";	
											}else{
												$and_s_country="AND j_country=".$single_q."".$s_country."".$single_q."";		
											}
											
											if(empty($s_state)){
												$and_s_state="";	
											}else{
												$and_s_state="AND j_state=".$single_q."".$s_state."".$single_q."";		
											}
											
											
											if(empty($s_city)){
												$and_s_city="";	
											}else{
												$and_s_city="AND j_city=".$single_q."".$s_city."".$single_q."";		
											}
											

											if(empty($s_industry)){
												$and_s_industry="";	
											}else{
												$and_s_industry="AND j_category=".$single_q."".$s_industry."".$single_q."";		
											}
											
											
											if(empty($s_status)){
												$and_s_status="";	
											}else{
												
												if($s_status =="Unapproved"){ $s_status_title = "0"; }
												if($s_status =="Approved"){ $s_status_title = "1"; }
												
												$and_s_status="AND j_m_approve=".$single_q."".$s_status_title."".$single_q."";		
												
												/*
												$and_s_status="
												AND j_id IN (SELECT j_user_id FROM j_pays WHERE j_deleted < 1 AND j_paid=".$single_q."".$s_status_title."".$single_q.")
												";
												*/
												
											}
											
											if(empty($s_date)){
												$and_s_date="";	
											}else{
												$and_s_date="AND j_date LIKE ".$single_q."%".$s_date."%".$single_q."";		
											}
											
											
											
											if(empty($s_user)){
												$and_s_user="";	
											}else{
												//get user id
												$sqlxx = "SELECT * FROM j_users where j_deleted < 1 AND j_blocked < 1 AND j_username='".$s_user."' AND j_account_type='Partner'"; //s_user
												$resultxx = runSQL($sqlxx);
												$dataxx = mysqli_fetch_array($resultxx);
												$s_user_id = $dataxx['j_id'];
												$s_user_username = $dataxx['j_username'];
												$s_user_firstname = $dataxx['j_firstname'];

												$and_s_user="AND j_id=".$single_q."".$s_user_id."".$single_q."";		
											}
											

											if(empty($s_order)){
												$and_s_order="order by j_id desc";
												$s_order = "Newest to Oldest";
											}else{
												if($s_order == "Newest to Oldest"){ $and_s_order="order by j_id desc"; }
												if($s_order == "Oldest to Newest"){ $and_s_order="order by j_id asc"; }
												if($s_order == "Alphabetical"){ $and_s_order="order by j_title asc"; }
											}
											
											
											
											if(!empty($s_search)){
												$s_search = RMC2($s_search);
											}
											
											
											
											$limit =20;
											$offset = RMC($_GET['m']);
											if($offset<1){
												$offset = 0;
												$num = 1;
											}else{
												$num=$offset+1;
											}
											
											
											
											
											
											if(empty($s_search)){
												$sql25="SELECT * from j_blog WHERE 
												j_deleted < 1
												$and_s_country $and_s_state $and_s_city $and_s_industry  $and_s_user $and_s_date $and_s_status 
												$and_s_order
												"; //  
											}else{
												$sql25="SELECT * from j_blog WHERE 
												j_deleted < 1 AND j_title LIKE '%".$s_search."%'
												|| j_deleted < 1 AND j_content LIKE '%".$s_search."%'
												|| j_deleted < 1 AND j_content_sm LIKE '%".$s_search."%'
												|| j_deleted < 1 AND j_img_cover LIKE '%".$s_search."%'
												|| j_deleted < 1 AND j_date LIKE '%".$s_search."%'
												
												
												";  	
											}
											$result25 = runSQL($sql25);
											$total = mysqli_num_rows($result25);
											 
											/*
											|| j_deleted < 1 AND j_status < 1 AND j_phone LIKE '%".$s_search."%' 
												|| j_deleted < 1  AND j_status < 1 AND j_id IN (SELECT j_user_id FROM j_pays WHERE j_deleted < 1 AND j_transact_id LIKE '%".$s_search."%')
												*/
											
											
											if(empty($s_search)){
												$sql25="SELECT * from j_blog WHERE 
												j_deleted < 1 
												$and_s_country $and_s_state $and_s_city $and_s_industry $and_s_user $and_s_date $and_s_status 
												$and_s_order
												limit $limit offset $offset
												"; //  
											}else{
												$sql25="SELECT * from j_blog WHERE 
												j_deleted < 1 AND j_title LIKE '%".$s_search."%'
												|| j_deleted < 1 AND j_content LIKE '%".$s_search."%'
												|| j_deleted < 1 AND j_content_sm LIKE '%".$s_search."%'
												|| j_deleted < 1 AND j_img_cover LIKE '%".$s_search."%'
												|| j_deleted < 1 AND j_date LIKE '%".$s_search."%'
												order by j_id desc limit $limit offset $offset
												";  	
											}
											$result4 = runSQL($sql25);
											
											/*
											|| j_deleted < 1  AND j_status < 1 AND j_id IN (SELECT j_user_id FROM j_pays WHERE j_deleted < 1 AND j_transact_id LIKE '%".$s_search."%' order by j_id asc limit 1) 
											*/
					
					
					
											?>
											
											
											
											<div class="w-100" >
												<form method="GET" action="" id="form_browse">
													<div class=" row" style="">
														<div class="col-lg-8" style="">
															<div class="input-group mb-3">
																<?php /*
																<div class="input-group-prepend text-center" style="width:40px; min-height:50px; padding-top:12px !important; padding-left:12px !important; background-color:#fff !important; border:1px solid #efefef !important; border-right:0px solid #efefef !important; border-radius:0px !important;">
																	<i class="prefix-icon fas fa-search" style="font-size:20px;"></i>
																</div>
																*/
																?>
																
																<input value="<?php if(isset($_GET['search']) && !empty($_GET['search']) ){ echo $s_search; } ?>" 
																autocomplete="off" style="width:calc(100% - 40px);min-height:50px; padding-left:10px !important; background-color:#fff !important; border:1px solid #efefef !important; border-top-left-radius:12px; border-bottom-left-radius:12px; border-left:1px solid #efefef !important;" type="text" class="myform2" name="search" placeholder="Search for news	"/>
																<div class="form_search_btn input-group-prepend text-center" style="cursor:pointer; width:40px; min-height:50px; padding-top:12px !important; padding-left:6px; background-color:#fff !important; border:1px solid #efefef !important; border-left:0px solid #efefef !important; border-top-right-radius:12px; border-bottom-right-radius:12px;">
																	<i class="prefix-icon fas fa-search text-danger" style="font-size:20px;"></i>
																</div>
															</div>
															
														</div>
													</div>
													
													
													
													
													
													<div class="w-100" style="">	
														<div class="row">	
															<?php /*
															<div class="col-6 col-md-4">
																<div class="form-group">	
																	<select  name="status" title="Select Store" style="min-height:40px; padding-top:7px; padding-left:40px; background-color:#fff !important; border-radius:8px !important; border:1px solid #efefef !important;" type="text" class="w-100 myform2" data-hide-disabled="true" data-live-search="true">
																		
																		<?php if(empty($s_status)){ ?>
																		<option value=""> Status</option>
																		<?php }else{ 
																		
																		?>
																		<option value="<?php echo $s_status; ?>"><?php echo $s_status; ?></option>
																		<?php } ?>
																		
																		<option value=""> All</option>
																		<option value="Approved"> Approved</option>
																		 
																		<option value="Unapproved"> Unapproved</option>
																		
																		 
																	</select>
																</div>
															</div>
															*/
															?>
															
															
															<div class="col-7 col-md-5">
																<div class="form-group">	
																	<input  name="date" placeholder="Select date" style="min-height:40px; padding-top:7px; padding-left:40px; background-color:#fff !important; border-radius:8px !important; border:1px solid #efefef !important;" type="date" class="w-100 myform2" data-hide-disabled="true" data-live-search="true"
																	value="<?php if(!empty($s_date)){ echo $s_date; } ?>"
																	/>
																		
																		 
																</div>
															</div>
															
															 
															
														</div>
													</div>
									
													
												</form>	
												
												<div> 
													<h6>Search result: 
														<b class="text-danger"><?php echo number_format($total); ?></b> 
													</h6>
												</div>
											</div>
											
											
											<div class="table-responsive my-content" id="my-content">
												<table id="" class="table table-hover"> <!--order-listing-->
													<thead>
														<tr>
															<th>S/N</th>
															<th>Menu</th>
															<th>Title</th>
															<th>Content</th>
															<th>Date</th>
														</tr>
													</thead>
													<tbody>
														<?php
														$date = YMD($CurrentDate);
														while ($row = mysqli_fetch_array($result4)){
														?>

														
														<tr id="i<?php echo $row['j_id']; ?>">
															<td id="nums<?php echo $row['j_id']; ?>"><?php echo $num++; ?></td>
															
															<td>
																
																<div class="btn-group dropup">
																	<a href="javascript:void()" class="dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
																		Options
																	</a>
																	<div class="dropdown-menu">
																
																		
																		<?php if(!empty($row['j_img_cover'])){ ?>
																		<a class="dropdown-item r_img" href="javascript:void()" url="<?php echo $row['j_img_cover']; ?>">
																			<i class="fa fa-image"></i>
																			<span class="m-l-10">View Image</span>
																		</a>
																		<div class="dropdown-divider"></div>
																		<?php } ?>
																		
																		
																		<a class="dropdown-item" href="<?php echo $dir; ?>blog-edit/?r=<?php echo $row['j_id'];?>">
																			<i class="fa fa-edit"></i>
																			<span class="m-l-10">Edit</span>
																		</a>
																		<div class="dropdown-divider"></div>
																		
																		<a class="dropdown-item r_del" href="javascript:void()" num="<?php echo $row['j_id']; ?>">
																			<i class="fa fa-trash"></i>
																			<span class="m-l-10">Delete</span>
																		</a>
																	</div>
																</div>
										
																
															</td>
															
															<td> 
																<?php echo DRMC($row['j_title']); ?>
															</td>
															<td> 
																<?php echo mb_substr(DRMC($row['j_content']),0,20); ?>...
															</td>
															<td> <?php echo FULL_DATE($row['j_date']); ?></td>
															
														</tr>
														<?php } ?>
														
														
														
														
														
														
														
														
														
														

													</tbody>
												</table>
												
												
												<br/>
												<?php
												$load_more = "".$dir."blog/?
												search=".urlencode($s_search)."
												&u=".urlencode($s_user)."
												&industry=".urlencode($s_industry)."
												&date=".urlencode($s_date)."
												&status=".urlencode($s_status)."
												&country=".urlencode($s_country)."
												&state=".urlencode($s_state)."
												&city=".urlencode($s_city)."
												&order=".urlencode($s_order)."
												";
												?>
												<p align="center">
													<?php if($offset+$limit < $total){ ?>
													<a class="app_btn m-b-10" href="<?php echo $load_more; ?>&m=<?php echo $offset+$limit; ?>"> Next <i class="fas fa-chevron-right text-2 ml-2"></i></a>
													<?php } ?>
													&nbsp; &nbsp;
													<?php if($offset > 0){ ?>
													<a class="app_btn bg-danger m-b-10" href="<?php echo $load_more; ?>&m=<?php echo $offset-$limit; ?>"><i class="fas fa-chevron-left text-2 ml-2"></i>  Previous</a>
													<?php } ?>
												</p>
												
												
												
											</div>
											
											
										</div>
										



										<?php }else{ //no record ?> 
										<div class="">
											<div class="container">
												<div class="row">
													 <div class="col-md-12" style="text-align:center;">
														<div class="avatar avatar-image avatar-lg avatar-red">
															<i class="fa fa-times"></i>
														 </div>
														 <p class="text-center">No record found.</p>

													 </div>
												</div>

											</div>
										</div>
										<?php } ?>
										
							
							
										
									</div>
								</div>
							</div>
						</div>
					</div>
					<?php require_once("../j_incs/footer.php"); ?>
					
					
					
					<div class="modal fade" id="modal">
						<div class="modal-dialog modal-md">
							<div class="modal-content">
								<div class="modal-header">
									<h5 class="modal-title" id="exampleModalLongTitle">DELETE </h5>
									<button type="button" class="close" data-dismiss="modal">
										<i class="fa fa-times"></i>
									</button>
								</div>
								<div class="modal-body">
									<h6 style="font-size:1.2rem; line-height:2rem;" class="del_div">
										Are you sure you want to delete row #<span class="m_num"></span>
									</h6>
									<div class="row">
										<div class="col-6 del_div">
											<a href="javascript:void()"  data-dismiss="modal" class="btn btn-md bg_red w-100">LATER</a>
										</div>
										<div class="col-6 del_div">
											<form id="form" method="post">
												<button type="submit" id="submit" class="btn btn-md btn-primary w-100">DELETE</button>
												<input class="form-control" name="r_id" style="display:none;">
											</form>
										</div>
										<div class="col-12">
											<div id="formresult"></div>
										</div>
									</div>
											
								</div>
							</div>
						</div>
					</div>
					
					
					
					<div class="modal fade" id="modal_img">
						<div class="modal-dialog modal-md">
							<div class="modal-content">
								<div class="modal-header">
									<h5 class="modal-title" id="exampleModalLongTitle">IMAGE</h5>
									<button type="button" class="close" data-dismiss="modal">
										<i class="fa fa-times"></i>
									</button>
								</div>
								<div class="modal-body">
									<img src="" alt="Image" id="r_img" class="img-responsive" style="width:100%; height:auto;"/>
								</div>
							</div>
						</div>
					</div>
		
		
			
		
				</div> <!-- main-panel ends -->
			</div> <!-- page-body-wrapper ends -->
		</div>
		<!-- container-scroller -->
		<?php require_once("../j_incs/scripts-bottom.php"); ?>
		
		<script>
			
		$(".adv_search_btn").click(function(){
			$(".adv_search_box").slideToggle();
		});
		
		$("[name=u],[name=status],[name=date],[name=industry],[name=country],[name=state],[name=city],[name=order]").change(function(){
			$("[name=search]").val('');
			$("#form_browse").submit();
		});
		
		$(".form_search_btn").click(function(){
			$("#form_browse").submit();
		});
		
		 
	</script>
	
	
		<script>
			$(".r_del").click(function(){
				var r_num = $(this).attr("num");
				$("[name=r_id]").val(r_num);
				$("#submit").removeAttr('disabled');
				$("#submit").html('DELETE');
				var num = $("#nums"+r_num+"").text();
				//$("#i"+r_num+"").hide();
				$(".m_num").text(num);
				$("#modal").modal('show');
				$(".del_div").show();
				$("#formresult").html("");
			});
			
			$(".r_img").click(function(){
				var img_file = $(this).attr("url");
				var img_url_link = "<?php echo $dir; ?>j_files/j_img/blog/"+img_file+"";
				$("#modal_img").modal('show');
				$("#r_img").attr('src',''+img_url_link+'');
			});
			
			
			var form = "#form";
			var button = "#submit";
			var button_val = $(button).html();
			var result = "#formresult";
			var loader = ".j_loader";
			$(form).on('submit',(function(e) {
			e.preventDefault();
			$(button).attr('disabled', '');
			$(button).html('<span class="fa fa-circle-notch fa-spin fa-1x fa-fw"></span>');
			$(loader).show('slow');
			 $(result).html("");
			var xhr = $.ajax({
			url: "save.php",
			type: "POST",
			data: new FormData(this),
			contentType: false,
			cache: false,
			processData:false,
			success: function(data)
			{
			if(data){
			 $(button).removeAttr('disabled');
			$(button).html(button_val);
			$(result).html(data);
			}else{
			$(result).html("Error occurred.");
			$(button).removeAttr('disabled');
			$(button).html(button_val);
			}
			}
			});
				setTimeout(function(){
					xhr.abort();
					$(button).removeAttr('disabled');
					$(button).html(button_val);
					//$(result).html("");
				}, 10000);
			}));
		</script>
	</body>
</html>