<?php 
  ob_start();
  include "../../db.php";
  date_default_timezone_set('Asia/Kolkata');
  $currentCoreDate = date("Y-m-d");
  session_start();

if (isset($_POST['start'])) {
    
    $start= mysqli_real_escape_string($con,$_POST['start']);
     $AndWhere ='';

     if (!empty($_POST['First1FilterValue']) && !empty($_POST['First2FilterValue'])) {
        $startDate = date('Y-m-d', strtotime($_POST['First1FilterValue']));
        $endDate = date('Y-m-d', strtotime($_POST['First2FilterValue']));
    
        $AndWhere .= "AND STR_TO_DATE(uploadDT, '%b %d, %Y %h:%i %p') BETWEEN '$startDate' AND '$endDate'";
    }
    
    if (!empty($_POST['First1FilterValue']) && empty($_POST['First2FilterValue'])) {
        $startDate = date('Y-m-d', strtotime($_POST['First1FilterValue']));
    
        $AndWhere .= "AND DATE(STR_TO_DATE(uploadDT, '%b %d, %Y %h:%i %p')) = '$startDate'";
    }
    
    if (empty($_POST['First1FilterValue']) && !empty($_POST['First2FilterValue'])) {
        echo "<div style='text-align:center;font-size:20px;color:red;padding:15px;background:#f2f2f2;'>
                You Have To Enter Starting Date First.
              </div><br>";
        die();
    }

    if ( !empty($_POST['SecondFilterValue']) ) {

         $AndWhere .= "";
    }

    if (!empty($_POST['ThirdFilterValue'])) {

        $AndWhere .= "AND st_name = '$_POST[ThirdFilterValue]'";
    }

/*---- count ---------*/
  $st_sql = "SELECT * FROM status WHERE s_by='$_SESSION[uref]' ORDER BY s_pos ASC";
  $st_res = mysqli_query($con,$st_sql);
  $st_output = "<div class='col-md-12 text-center'>";
  while ($st_rows = mysqli_fetch_assoc($st_res)) {

      $count_sql = "SELECT * FROM student_details WHERE st_status = '$st_rows[s_id]' AND `flag_status` ='D' $AndWhere";
      $count_res = mysqli_query($con,$count_sql);
      $count_total_rows = mysqli_num_rows($count_res);

      $st_output .="
                    <b><i class='fa fa-check-square' aria-hidden='true' style='color:green;'></i> $st_rows[s_name] : $count_total_rows &nbsp; &nbsp;</b>
                ";
  }
  $st_output .= "<hr></div>";
  echo $st_output;

/*---------------*/

$sql = "SELECT * FROM student_details WHERE `flag_status` ='D' $AndWhere ORDER BY st_no DESC LIMIT $start,25";

    $res = mysqli_query($con,$sql);
    if ($total_rows=mysqli_num_rows($res)>0) {
        $s_output="";
          $c = $start+25;
        while ($rows=mysqli_fetch_assoc($res)) {

          /* show upload admin Start*/
          if(!empty($rows["st_by"])){
            $adm_sql = "SELECT uname FROM users  WHERE uid = '$rows[st_by]'";
            $adm_run = mysqli_query($con, $adm_sql);
            $adm_rows = mysqli_fetch_assoc($adm_run);
            $admin = "<b style='color:#3333ff;'><i class='fa fa-user' aria-hidden='true'></i> ".$adm_rows['uname']."</b>";
          }
          /* show upload admin End*/

          /* show status Start*/
          $st_show = '';
          if(!empty($rows["ustd_status"])){
            $st_show_sql = "SELECT * FROM status WHERE s_id = '$rows[ustd_status]' AND s_by='$_POST[uref]'";
            $st_show_run = mysqli_query($con, $st_show_sql);
            $st_show_row = mysqli_fetch_assoc($st_show_run);
            $st_show = $st_show_row['s_name'];
          }
          /* show status End*/

          /*status select option Start*/
            $st_loop ="<option selected disable>select</option>";
            $st_sql = "SELECT * FROM status WHERE s_by='$_SESSION[uref]'  ORDER BY s_pos ASC";
            $st_run = mysqli_query($con, $st_sql);
            while ( $st_rows = mysqli_fetch_assoc($st_run) ) {
              
                $st_loop .= "<option value='".$st_rows['s_id']."'>".$st_rows['s_name']."</option>";
            }

          /*status select option End*/
          /*latest remark Start*/
            $latest_remark = "";
            $r_run = mysqli_query($con, "SELECT * FROM remark WHERE r_sid = '$rows[st_no]' ORDER BY r_id DESC LIMIT 1");
            while ( $r_row = mysqli_fetch_assoc($r_run) ) {
              $latest_remark .= "<b style='color:#3333ff;'><i class='fas fa-comments'></i> ".$r_row['remark']." </b>(".$r_row['uploadDate']." )";
             }
          /*latest remark End*/

          /*latest Adminremark Start*/
            $last_admin_remark = "";
            $r_run = mysqli_query($con, "SELECT * FROM admin_remark WHERE r_sid = '$rows[st_no]' ORDER BY r_id DESC LIMIT 1");
            while ( $r_row = mysqli_fetch_assoc($r_run) ) {
              $last_admin_remark .= "A<i class='fas fa-comments'></i> ".$r_row['remark']." (".$r_row['uploadDate']." )";
             }
          /*latest Admin remark End*/
            ?>
<li class='clearfix' style='float: none;'>
    <div class="single-dis-admin mb-2">
        <span id="status-msg-<?= $rows['st_no']; ?>" class="text-center"></span>
        <div class="row">
            <div class="col-md-9 col-xs-7 second-item">
                <i class="fa fa-user-circle" aria-hidden="true" style="color:orange;font-size:18px;"></i>
                <b style="color:black;font-size:17px;"><?= $rows['st_name'] ?></b>
                ( <b><?= $st_show ?></b> )
                <br>

                <?= $admin ?>
                <br>

                <!-- Uncomment below lines if you need phone details -->
                <!-- 
                <i class="fa fa-phone fa-rotate-90"></i> <?= $rows['st_ph'] ?> &nbsp;
                <i class="fab fa-whatsapp"></i> <?= $rows['st_wp_no'] ?>
                <br>
                -->

                <i class="fas fa-map-marker-alt"></i> <?= $rows['st_address'] ?>
                <br>

                <!-- Uncomment below lines if you need qualification, laptop, or job status -->
                <!-- 
                <i class="fa fa-graduation-cap" aria-hidden="true"></i> <?= $rows['st_qualification'] ?>
                <i class="fas fa-laptop"></i> <?= $rows['st_laptop'] ?>
                <i class="fas fa-briefcase"></i> <?= $rows['st_current_status'] ?>
                <br>
                -->

                <b>S<i class="far fa-clock"></i>: </b> <?= $rows['uploadDT'] ?>
                <br>

                <?= $latest_remark ?><br>
                <?= $last_admin_remark ?>
            </div>

            <div class="col-md-3 col-xs-5 third-item">
                <button class="m-2 btn btn-success btn-sm reloadFromRecycleBinBtn" id="<?= $rows['st_no'] ?>"
                    title="Reload from RecycleBin" style="width:50px;">
                    <i class="fa fa-recycle" aria-hidden="true" style="font-size:15px;"></i>
                </button>
            </div>
        </div>
    </div>

</li>
<?php
$c++;
}
echo $s_output;
}

}
?>

<script>
$(document).ready(function() {

    $('.reloadFromRecycleBinBtn').on('click', function() {

        var sid = $(this).attr('id');

        $.ajax({
            url: "action-Ajax.php",
            method: "POST",
            data: {
                action: 'reloadFromRecycle',
                sid: sid
            },
            cache: false,
            success: function(result) {
                $("#status-msg-" + sid).html(result);
            }
        });


    });
});
</script>