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

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

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


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

  $get_blog_comments=$retrieveObj->retriever("blog
  		JOIN blog_comments ON blog.b_id=blog_comments.b_id
  	","*","blog.b_id",$b_id,"fetchAll");
  $get_blog=$retrieveObj->retriever("blog","*","blog.b_id",$b_id,"fetch");

?>

<h4 class="page-title">
	<span class="fas fa-blog"></span>
	Blog
</h4>
<p><a href="#" class="bold" onclick="blog_view()">Blog</a> <span class="fas fa-angle-right"></span> Blog post comments : <i><?php echo $get_blog->b_title; ?> </i> </p>
<div class="row">
	<div class="col-md-12 mb-3">
		<table class="table table-head-bg-primary mt-4" id="blogs_table">
			<thead>
				<tr>
                  <th scope="col">Author</th>
                  <th scope="col">Date posted</th>
                  <th scope="col">Comments</th>
                  <th scope="col">Replies</th>
                  <th scope="col">Options</th>
                </tr>
			</thead>
			<tbody>
				<?php
		          if($get_blog_comments!="1020"){
		            $element_id="element_id";
		              foreach ($get_blog_comments as $blog_comment) {
		                $element_id=$element_id."Ot";
		        ?>  
		            <tr id="<?php echo $element_id ?>">  
		            <?php
		            	if(!empty($blog_comment->user_id)){//comment from admin or editor
		            		//get admin or editor details
		            		$get_ae_details=$retrieveObj->retriever("users
		            			JOIN blog_comments ON blog_comments.user_id=users.user_id
		            			WHERE users.user_id='".$blog_comment->user_id."'
		            			","*","fetch");
		                ?>
		                	<td>
				              	<span class="fas fa-user"></span><br>
				                <?php 
				                  echo $get_ae_details->firstname." ".$get_ae_details->lastname."<br>".$get_ae_details->email;
				                ?>
				              </td>
				              <td>
				                <?php 
				                  echo date("d-m-Y H:i:s", strtotime($blog_comment->bc_time));
				                ?>
				              </td>
				              <td>
				              	
				                <?php 
				                  echo $blog_comment->bc_text;
				                ?>
				              </td>
				              <td>
				              	<?php
				              		//get replies count
				              		echo $comments_count=$rowcountObj->rowcounter("blog_comment_replies","*","bc_id",$blog_comment->bc_id);
				              	?>
				            </td>
		                <?php
		                }else{
		                	//comment from visitor
		                ?>
		                	<td>
				              	<span class="fas fa-user"></span><br>
				                <?php 
				                  echo $blog_comment->bc_fullname."<br>".$blog_comment->bc_email;
				                ?>
				              </td>
				              <td>
				                <?php 
				                  echo date("d-m-Y H:i:s", strtotime($blog_comment->bc_time));
				                ?>
				              </td>
				              <td>
				              	
				                <?php 
				                  echo $blog_comment->bc_text;
				                ?>
				              </td>
				              <td>
				              	<?php
				              		//get replies count
				              		echo $comments_count=$rowcountObj->rowcounter("blog_comment_replies","*","bc_id",$blog_comment->bc_id);
				              	?>
				            </td>
		                <?php
		                }
		            ?>       
				              
		              <td id="<?php echo $element_id.'Dbtn' ?>">
		                    <div class="">
	                          <button title="Reply" class="btn btn-success hand_cursor" onclick="blog_post_comment_reply_view(<?php echo $blog_comment->bc_id ?>)"><span class="fas fa-reply"></span> Reply </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_note" value="<?php echo $blog_comment->bc_id; ?>">
	                              <button id="<?php echo $element_id.'Rbtn' ?>" onclick="delete_blog_post_comment('<?php echo 'R'.$element_id.'form' ?>','<?php echo $element_id; ?>','r_blog_response')" type="submit" title="Delete" class="btn btn-danger hand_cursor"><span class="fas fa-times "></span> Delete</button>
	                          </form>
	                          
	                        </div>             
		              </td>
		            </tr>
		          <?php
		              }
		            }
		          ?>
			</tbody>
		</table>
	</div>
</div>
<script type="text/javascript">
  $(document).ready(function () {
    var table = $('#blogs_table').DataTable({
      columnDefs: [
        {bSortable: false, targets: [4]} 
      ]
    });
  })
</script>