<?php
include 'db_conn_class2.php';

$blogid = $_POST['blogid'];

//echo "blogid = ".$blogid;
$row = $_POST['row'];
$rowperpage = 1;	
	
	//counting total number of posts
	 //$allcount_query = "SELECT count(*) as allcount from tbl_comment_det where status='ACTIVE' and deleted = 0 and  blog_id=".$blogid;
	 //$allcount_result = mysqli_query($conn,$allcount_query);
	 //$allcount_fetch = mysqli_fetch_array($allcount_result);
     //$allcount = $allcount_fetch['allcount'];
	
	 $sql_comment = "select comment_id,comment,comment_dt,login_user_id,first_name from tbl_comment_det cd inner join tbl_login l on l.login_id = cd.login_user_id where cd.status='ACTIVE' and cd.deleted = 0 and cd.reference_comment_id = 0 and cd.blog_id=".$blogid." order by cd.comment_dt desc" ;
	 //limit ".$rowperpage; 
	//echo $sql_comment;
	// return;
	$result = mysqli_query($conn,$sql_comment);
	$numrows = mysqli_num_rows($result);
	 if($numrows>0){
		foreach($result as $row)
		{
		 $output .= '
		 <div class="panel panel-default">
			 <div class="userlogo" style="float: left;
				background-color: #2e3192;
				color: #fff;
				padding: 3px 11px;
				margin:10px 8px;
				border-radius: 50%;
				text-align: center;
				height: 30px;
				width: 30px;">
			  <span style="text-transform:capitalize;">'.substr($row["first_name"], 0, 1).'</span>&nbsp;
		 	</div>
		  <div class="panel-heading"> <b>'.$row["first_name"].'</b> <br> <i>'. date('jS F Y',strtotime($row["comment_dt"])).'</i></div>
		  <div class="panel-body" style="margin-left:30px;padding:5px 20px 10px; ">'.$row["comment"].'
		  </div>	
		<div class="panel-footer" align="right"><button type="button" class="btn btn-sm btn-primary reply" id="'.$row["comment_id"].'">Reply</button></div>	  

		  </div>
		 
		 ';
		 $output .= get_reply_comment($conn, $row["comment_id"],$blogid);
		
		}
	
	echo $output;
	
	
	//echo "<center><a href='#' id='loadMore'><button class='btn btn-sm btn-info'>Load More</button></a><br>
		//<input type='hidden' id='row' value='0'>
       // <input type='hidden' id='all' value='".$allcount."'>	</center>	
		//";
	}
	
	

	function get_reply_comment($conn, $parentId = 0,$blogid, $marginLeft = 0) 
	{
		$query  = "select comment_id,comment,comment_dt,login_user_id,first_name from tbl_comment_det cd inner join tbl_login l on l.login_id = cd.login_user_id where cd.status='ACTIVE' and cd.deleted = 0 and cd.reference_comment_id = '".$parentId."' and blog_id= ".$blogid;
			$output = '';
		//echo $query ;
		
		
		$commentsResult = mysqli_query($conn, $query);
		$commentsCount = mysqli_num_rows($commentsResult);
		if($parentId == 0) {
			$marginLeft = 0;
		} else {
			$marginLeft = $marginLeft + 48;
		}
		if($commentsCount > 0) {
			foreach($commentsResult as $row){
				$output .= '
					<div class="panel panel-default" style="margin-left:'.$marginLeft.'px">
					 
					 <div class="userlogo" style="float: left;
						background-color: #5bc0de;
						color: #fff;
						padding: 3px 11px;
						margin:10px 8px;
						border-radius: 50%;
						text-align: center;
						height: 30px;
						width: 30px;">
					  <span style="text-transform:capitalize;">'.substr($row["first_name"], 0, 1).'</span>&nbsp;
					</div>
		  			<div class="panel-heading"> <b>'.$row["first_name"].'</b> <br> <i>'. date('jS F Y',strtotime($row["comment_dt"])).'</i></div>
					<div class="panel-body" style="margin-left:30px;padding:5px 20px 10px; ">'.$row["comment"].'<br>
					<button type="button" class="btn btn-sm btn-info reply pull-right" id="'.$row["comment_id"].'">Reply</button>
					
					
					
					</div>
					
					</div>
					';
				$output .= get_reply_comment($conn, $row["comment_id"],$blogid);
			}
		}
		return $output;
	}
	
?>