<?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 h_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 h_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 h_subject='$tf_rows[sc_id]' ";

    }



    $sql = "SELECT * FROM homework  WHERE h_admin_2 = '$_SESSION[uref]' AND h_class='$cid'  $AndWhere ORDER BY h_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["homework"];



           $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);





           $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[h_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='viewDetails'  data-target='#modalNotes' data-title='$rows[h_title]'  data-note_script='$rows[homework_script]' data-img='$rows[homework]'>



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

                        

                        <br>

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

                        &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[h_date] 

                    </a>

                        <br>  

                        <a href='view-student-homework.php?h_ref=$rows[h_ref]' class='btn btn-primary btn-sm' >Submitted Homework </a>

                    

                    </div>



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

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

                        <br>

                        <a href='view-homework.php?del_id=$rows[h_id]&homework=$rows[homework]' 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 title = $(this).data('title');

              //console.log(title);

                $('#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);



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

                console.log(img);

                imgCode = "<img src='../upload/homework/"+img+"' width='100%'>";

                console.log(imgCode);

                $('#modalIMG').html(imgCode);

                

            });

        });

    </script>