<?php 

  include "../db.php";

  session_start();



if (isset($_POST['start'])) {

    

    $start= mysqli_real_escape_string($con,$_POST['start']);

    $cid= mysqli_real_escape_string($con,$_POST['cid']);

    $AndWhere ='';



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



        $AndWhere .= "AND n_title='$_POST[FirstFilterValue]' ";



    }



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



        $sf_sql = "SELECT sid,sclass FROM class WHERE uploadedby = '$_SESSION[uref]' AND sid='$_POST[SecondFilterValue]'";

        $sf_run = mysqli_query($con, $sf_sql);

        $sf_rows = mysqli_fetch_assoc($sf_run);



        $AndWhere .= "AND n_class='$sf_rows[sid]' ";



    }



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



       $tf_sql = "SELECT sc_id,sc_name FROM subject WHERE uploadedby = '$_SESSION[uref]' AND sc_id='$_POST[ThirdFilterValue]'";

        $tf_run = mysqli_query($con, $tf_sql);

        $tf_rows = mysqli_fetch_assoc($tf_run);



        $AndWhere .= "AND n_subject='$tf_rows[sc_id]' ";

    }



    $sql = "SELECT * FROM notes  WHERE n_admin_2 = '$_SESSION[uref]' AND n_class='$cid'  $AndWhere ORDER BY n_id DESC LIMIT $start,10";



   



    $res = mysqli_query($con,$sql);

    if ($total_rows=mysqli_num_rows($res)>0) {

        $s_output='';

          $c = $start+1;

        while ($rows=mysqli_fetch_assoc($res)) {

            $uploadedImage = $rows["notes"];



           $up_sql = "SELECT uname, uref FROM users WHERE uref = '$rows[uploadedby]' ";

           $up_run = mysqli_query($con, $up_sql);

           $up_rows = mysqli_fetch_assoc($up_run);

//$sql = "SELECT  s.*, c.sclass FROM subject AS s LEFT JOIN class AS c ON s.sc_class_id = c.sid WHERE s.uploadedby = '$_SESSION[uref]' $AndWhere ORDER BY c.sclass DESC LIMIT $start,10";



           $sub_sql = "SELECT  s.*, c.sclass FROM subject AS s LEFT JOIN class AS c ON s.sc_class_id = c.sid  WHERE s.sc_id = '$rows[n_subject]' ";

           $sub_run = mysqli_query($con, $sub_sql);

           $sub_rows = mysqli_fetch_assoc($sub_run);

            $s_output .= 

                "

                <li class='clearfix' style='float: none;'>

                <div class='row'>

                    



                    <div class='col-md-10 col-xs-8 second-item'>

                    <a data-toggle='modal'  class='b tn bt n-pr imary b tn-s m viewDetails'  data-target='#modalNotes' data-ref='$rows[n_ref]' data-title='$rows[n_title]'  data-note_script='$rows[note_script]'>



                            <i class='fa fa-pencil-square-o' aria-hidden='true'style='color:orange;font-size:18px;'></i> <b style='color:black;font-size:17px;'>$rows[n_title]</b>  

                    

                        

                        <br>    

                        <b style='color:black;'><i class='fa fa-user'></i> </b>$up_rows[uname]

                        &nbsp;&nbsp;

                        <b style='color:black;'><i class='fa fa-book'></i> </b>$sub_rows[sc_name]

                        

                        

                        <br>

                        <i class='fa fa-clock-o text-primary'></i> $rows[n_date]

                        

                    </a>

                    

                    </div>



                    <div class='col-md-2 col-xs-4 third-item'>

                        <a href='edit-notes.php?edit_ref=$rows[n_ref]' class='m-2 btn btn-success btn-sm' style='width:60px;' title='Click to Edit'>Edit</a>

                        <br>

                        <a href='view-notes.php?del_id=$rows[n_id]&notes=$rows[notes]' class='delete m-2 btn btn-danger btn-sm' style='width:60px;' title='Click to Delete'>Delete </a>

                    </div>

                </div>

                </li>

                ";

          $c++;

        }



        mysqli_close($con);



        echo $s_output;

    }



}

?>

        <script>

        $(document).ready(function(){



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

                var n_ref = $(this).data('ref');

                var title = $(this).data('title');

                console.log(n_ref);

                $('#modalName').text(title);



                $('.modal-css').css({

                    'color' : 'black'

                });

                $('.modal-name').css({

                    'text-align' : 'center',

                    'color' : 'black',

                    'font-size' : '20px'

                });

                var note = $(this).data('note_script');

                $('#modalData').html(note);



                 $.ajax({

                    url:'noteImgDisplay-Ajax.php',

                    type:'post',

                    data:{

                      n_ref:n_ref

                    },

                    success:function(result){

                      $('#imageShow').html(result);

                    }

                });



                

                

            });

        });

    </script>