<?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_comments
    ","*","b_id",$b_id,"fetchAll");

?>

      
  <?php
    if($get_blog_comments!="1020"){
  ?>
      <div class="comments-heading">
        <h3><?php echo $rowcountObj->rowcounter("blog_comments","*","b_id",$b_id); ?> comment/s</h3>
      </div>
      <div class="comments-list">
        <ul>
          <?php
            $reply_form_container_id="reply_form_container_id";
            foreach ($get_blog_comments as $blog_comment) {
              $reply_form_container_id.="I";
          ?>
              <li class="threaded-comments">
                <div class="comments-details">
                  <div class="comments-list-img">
                    <!--img src="img/blog/b02.jpg" alt="post-author"-->
                    <span class="fa fa-user fa-3x"></span>
                  </div>
                  <div class="comments-content-wrap">
                    <span>
                        <b>
                          <?php
                            if($blog_comment->user_id!=""){
                              //get user information
                              $get_user_info=$retrieveObj->retriever("users 
                                JOIN user_roles ON user_roles.user_id=users.user_id
                                JOIN user_role ON user_roles.role_id=user_role.role_id
                                WHERE users.user_id='".$blog_comment->user_id."'
                                ","*","fetch");

                              echo $get_user_info->username;
                              if($get_user_info->user_role=='admin' or $get_user_info->user_role=='su_admin'){
                                echo "(Admin)";
                              }
                              
                            }else{
                              echo $blog_comment->bc_fullname;
                            }
                          ?>
                        </b>
                        
                        <span class="post-time">
                          <?php
                            echo date("M j, Y", strtotime($blog_comment->bc_time)) ;
                            echo " @ ";
                            echo date("H:i A", strtotime($blog_comment->bc_time)) ;
                          ?>
                        </span>
                    <a href="#!" onclick="$('#<?php echo $reply_form_container_id ?>').show()">Reply</a>
                    </span>
                    <p><?php echo $blog_comment->bc_text; ?></p>
                    <div class="bg-light p-3" id="<?php echo $reply_form_container_id ?>" style="display: none;">
                      <p class="text-center"><b> Reply to this comment </b></p>
                      <span class="email-notes">Your email address will not be published. Required fields are marked *</span>
                      <form id="<?php echo $reply_form_container_id."_form" ?>">
                        <div class="row">
                          <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
                            <p>Name *</p>
                            <input class="form-control form-control-sm" style="border-radius: 0px;" type="text" name="full_name" />
                          </div>
                          <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
                            <p>Email *</p>
                            <input class="form-control form-control-sm" style="border-radius: 0px;" type="email" name="email" />
                          </div>
                          <div class="col-lg-12 col-md-12 col-sm-12 comment-form-comment">
                            <p>Message</p>
                            <textarea name="message" class="form-control form-control-sm" style="border-radius: 0px;" id="message-box"  rows="6"></textarea>
                          </div>
                          <div class="col-lg-12 col-md-12 col-sm-12 pt-2" id="<?php echo $reply_form_container_id.'_response' ?>"></div>
                          <div class="col-lg-12 col-md-12 col-sm-12 py-2 d-flex justify-content-between">
                            <input type="hidden" name="add_comment_reply_note" value="<?php echo $blog_comment->bc_id; ?>">
                            <button type="submit" class="btn py-2 px-3 btn-sm rounded-pill btn-outline-info" onclick=" add_blog_comment_reply('<?php echo $reply_form_container_id."_form" ?>' ,'<?php echo $reply_form_container_id.'_response' ?>' ,<?php echo $blog_comment->bc_id; ?>,'<?php echo $reply_form_container_id ?>','<?php echo $reply_form_container_id."_specific_replies_id" ?>')">Post reply</button>
                            <a class="btn py-2 px-3 btn-sm rounded-pill btn-outline-danger" href="#!" onclick="$('#<?php echo $reply_form_container_id."_form" ?>').trigger('reset');$('#<?php echo $reply_form_container_id ?>').hide()">Cancel</a>
                          </div>
                        </div>
                      </form>
                    </div>

                    <div id="<?php echo $reply_form_container_id.'_specific_replies_id' ?>">
                      <?php
                        $get_blog_comment_replies=$retrieveObj->retriever("blog_comment_replies","*","bc_id",$blog_comment->bc_id,"fetchAll");
                        if($get_blog_comment_replies!="1020"){
                      ?>
                        <div class="comments-list">
                          <ul>
                            <?php
                              
                              foreach ($get_blog_comment_replies as $bcr) {
                                
                            ?>
                                <li class="threaded-comments ml-0">
                                  <div class="comments-details">
                                    <div class="comments-list-img">
                                      <!--img src="img/blog/b02.jpg" alt="post-author"-->
                                      <span class="fa fa-user fa-2x text-secondary"></span>
                                    </div>
                                    <div class="comments-content-wrap">
                                      <span>
                                          <b class="text-primary">
                                            <?php
                                              if($bcr->user_id!=""){
                                                //get user information
                                                $get_user_info2=$retrieveObj->retriever("users 
                                                  JOIN user_roles ON user_roles.user_id=users.user_id
                                                  JOIN user_role ON user_roles.role_id=user_role.role_id
                                                  WHERE users.user_id='".$bcr->user_id."'
                                                  ","*","fetch");

                                                echo "<span class='text-primary'>";
                                                echo $get_user_info2->username."";
                                                if($get_user_info2->user_role=='admin' or $get_user_info2->user_role=='su_admin'){
                                                  echo "(Admin)";
                                                }
                                                echo "</span>";
                                                
                                              }else{
                                                echo $bcr->bcr_fullname;
                                              }
                                            ?>
                                          </b>
                                          
                                          <span class="post-time">
                                            <?php
                                              echo date("M j, Y", strtotime($bcr->bcr_time)) ;
                                              echo " @ ";
                                              echo date("H:i A", strtotime($bcr->bcr_time)) ;
                                            ?>
                                          </span>
                                      </span>
                                          
                                      <p><?php echo $bcr->bcr_text; ?></p>
                                    </div>
                              </li>
                            <?php
                              }
                            ?>
                                
                          </ul>
                        </div>
                      <?php
                        }
                      ?>
                    </div>
                  </div>
                </div>
              </li>
          <?php
            }
          ?>
              
        </ul>
      </div>
  <?php
    }
  ?>
      

