<?php
  require_once("../../session_check.php");
  require_once("../../../classes/main_classes.php");

  if(!isset($_POST["bc_id"])){
    die("<p class='alert alert-warning'>Invalid request</p>");
  }

  $bc_id=addslashes($_POST["bc_id"]);


  //OBJECTS
  $retrieveObj=new RETRIEVE();
  $rowcountObj=new ROWCOUNT();
  $updateObj=new UPDATE();

  $get_blog_comment_replies=$retrieveObj->retriever("blog_comment_replies","*","bc_id",$bc_id,"bcr_id","ASC","fetchAll");

?>


<h1 class="comments-title">Replies (<?php echo $rowcountObj->rowcounter("blog_comment_replies","*","bc_id",$bc_id); ?>)</h1>
<div id="r_blog_response"></div>

	<?php
		if($get_blog_comment_replies!="1020"){
				$element_id="element_id";
			foreach ($get_blog_comment_replies as $blog_comment_reply) {
				$element_id=$element_id."Ot";
				if(!empty($blog_comment_reply->user_id)){//comment from admin or editor
        		//get admin or editor details
        		$get_ae_details=$retrieveObj->retriever("users
        			JOIN blog_comment_replies ON blog_comment_replies.user_id=users.user_id
        			WHERE users.user_id='".$blog_comment_reply->user_id."'
        			","*","fetch");
        	?>
        		<div class="be-comment">
					<div class="be-img-comment">	
						<a href="blog-detail-2.html">
							<span class="fas fa-user fa-2x text-secondary"></span>
						</a>
					</div>
					<div class="be-comment-content">
						<span class="be-comment-name">
							<a href="blog-detail-2.html">
								<?php 
				                  echo $get_ae_details->firstname." ".$get_ae_details->lastname;
				                ?>
							</a>
						</span>
						<span class="be-comment-time">
							<i class="fa fa-clock-o"></i>
							<?php
								echo date("M j, Y", strtotime($blog_comment_reply->bcr_time)) ;
								echo " at ";
								echo date("H:i A", strtotime($blog_comment_reply->bcr_time)) ;
							?>
						</span>
						<p class="be-comment-text">
							<?php echo $blog_comment_reply->bcr_text ?>
						</p>
						
					</div>
					<div class="text-right" id="<?php echo $element_id.'Dbtn' ?>">
						<span>
						  <button title="Edit" class="btn btn-sm btn-success hand_cursor" onclick="edit_blog_post_comment_reply_view(<?php echo $blog_comment_reply->bcr_id ?>)"><span class="fas fa-edit"></span> </button>
                          <form id="<?php echo 'R'.$element_id.'form' ?>" class="mb-0 hand_cursor" style="display: inline;">
                              <input type="hidden" name="delete_blog_post_comment_reply_note" value="<?php echo $blog_comment_reply->bcr_id; ?>">
                              <button id="<?php echo $element_id.'Rbtn' ?>" onclick="delete_blog_post_comment_reply('<?php echo 'R'.$element_id.'form' ?>','<?php echo $element_id; ?>','r_blog_response')" type="submit" title="Delete" class="btn btn-sm btn-danger hand_cursor"><span class="fas fa-times "></span> </button>
                          </form>
						</span>
					</div>
					<hr>
				</div>
        	<?php
        	}else{
        	?>
        		<div class="be-comment">
					<div class="be-img-comment">	
						<a href="blog-detail-2.html">
							<span class="fas fa-user fa-2x text-secondary"></span>
						</a>
					</div>
					<div class="be-comment-content">
						<span class="be-comment-name">
							<a href="blog-detail-2.html"><?php echo $blog_comment_reply->bcr_fullname ?></a>
						</span>
						<span class="be-comment-time">
							<i class="fa fa-clock-o"></i>
							<?php
								echo date("M j, Y", strtotime($blog_comment_reply->bcr_time)) ;
								echo " at ";
								echo date("H:i A", strtotime($blog_comment_reply->bcr_time)) ;
							?>
						</span>
						<p class="be-comment-text" id="<?php echo $element_id.'Dbtn' ?>">
							<?php echo $blog_comment_reply->bcr_text ?>
							
						</p>
					</div>
					<div class="text-right" id="<?php echo $element_id.'Dbtn' ?>">
						<span>
						  <button title="Edit" class="btn btn-sm btn-success hand_cursor" onclick="edit_blog_post_comment_reply_view(<?php echo $blog_comment_reply->bcr_id ?>)"><span class="fas fa-edit"></span> </button>
                          <form id="<?php echo 'R'.$element_id.'form' ?>" class="mb-0 hand_cursor" style="display: inline;">
                              <input type="hidden" name="delete_blog_post_comment_reply_note" value="<?php echo $blog_comment_reply->bcr_id; ?>">
                              <button id="<?php echo $element_id.'Rbtn' ?>" onclick="delete_blog_post_comment_reply('<?php echo 'R'.$element_id.'form' ?>','<?php echo $element_id; ?>','r_blog_response')" type="submit" title="Delete" class="btn btn-sm btn-danger hand_cursor"><span class="fas fa-times "></span> </button>
                          </form>
						</span>
					</div>
					<hr>
				</div>
        	<?php
        	}
	?>
				
	<?php
			}
		}else{
			echo "<p class='alert alert-info text-center'>No replies found</p>";
		}
	?>
			
	
	<form class="form-block" id="add_blog_comment_reply_form">
		<div class="row">
			<div class="col-md-12" id="add_blog_comment_reply_response">
				
			</div>							
			<div class="col-md-12">									
				<div class="form-group">
					<label>Reply:</label>
					<textarea class="form-input" id="reply" name="reply" required="" placeholder="Your text"></textarea>
				</div>
			</div>
			<div class="col-md-12 text-center">	
				<input type="hidden" name="add_blog_comment_reply_note" value="<?php echo $bc_id ?>">
				<button type="submit" class="btn btn-primary" onclick="add_blog_comment_reply(<?php echo $bc_id ?>)">Submit</button>
			</div>
		</div>
	</form>

	<?php
		//check if there is a notofication
		$get_notification=$retrieveObj->retriever("admin_notifications JOIN admin_notification_targets ON admin_notifications.an_id=admin_notification_targets.an_id","*","(an_source='blog_comment' or an_source='blog_reply') and admin_notification_targets.ant_seen_status='0' and admin_notifications.an_source_id='$bc_id' and admin_notification_targets.user_id",$user_id,"fetch");
		if($get_notification!="1020"){
			$updateObj->updater("admin_notification_targets","ant_seen_status='1'","an_id",$get_notification->an_id);
		}
	?>

<script type="text/javascript">
$(document).ready(function () {
	refresh_notifications_count()
})
</script>