<?php 

  include "../db.php";

  session_start();

    $class_sql = "SELECT sclass FROM users WHERE uref = '$_SESSION[uref]'";

    $class_result = mysqli_query($con, $class_sql);

    $class_row = mysqli_fetch_assoc($class_result);

    $userClass = $class_row["sclass"];



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

    

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

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

    $AndWhere ='';



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



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



    }



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



        $AndWhere .= "";

    }

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



        $AndWhere .= "";

    }





   // echo $sql = "SELECT h.* FROM homework AS h INNER JOIN users AS u ON h.h_admin_2 = u.admin_ref AND h.h_class = u.sclass WHERE h.h_class = '$userClass' $AndWhere  ORDER BY h.h_id DESC LIMIT $start,10";

   

    $sql = "SELECT * FROM homework WHERE h_class = '$userClass' AND h_subject = '$sid' $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"];

           //upload by view//

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

           

           //submitted homework count//

           $sub_homework_count_sql = "SELECT COUNT(hi_sl) as hi_count from homework_img WHERE hi_ref = '$rows[h_ref]';";

           $sub_homework_count_result = mysqli_query($con,$sub_homework_count_sql);

           $sub_homework_count_row = mysqli_fetch_assoc($sub_homework_count_result);

           if ($sub_homework_count_row['hi_count'] > 0) {

               $switch = "<a href='edit-homework.php?edit_ref=$rows[h_ref]' class='btn btn-warning btn-sm' style='width:70px;'> Edit </a>";

           }else{

                $switch = "<a href='submit-homework.php?h_ref=$rows[h_ref]' class='btn btn-success btn-sm' style='width:70px;'> Submit </a>";

           }

           

           echo '<br>';

            $s_output .= 

                "

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

                <div class='row'>

                    



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

                        <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;&nbsp;

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



                        <br>

                        <button data-toggle='modal'  class='btn btn-primary btn-sm viewDetails'  data-target='#modalNotes' data-title='$rows[h_title]'  data-note_script='$rows[homework_script]' data-img='$rows[homework]' style='width:70px;'> Collect </button>



                       



                        <button data-toggle='modal'  class='btn btn-primary btn-sm viewSHomework' data-target='#modalSHomework' data-ref='$rows[h_ref]' data-title='$rows[h_title]'> View </button>

                        

                        

                        $switch

                        

                        

                    

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

                

            });





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

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

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

                //console.log(n_ref);

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



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

                    'color' : 'black'

                });

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

                    'text-align' : 'center',

                    'color' : 'black',

                    'font-size' : '20px'

                });



                 $.ajax({

                    url:'submitHomeworkImgDisplay-Ajax.php',

                    type:'post',

                    data:{

                      h_ref:h_ref

                    },

                    success:function(result){

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

                    }

                });



                

                

            });



        });

    </script>

