<?php
if (isset($_GET['sol']) && !empty($_GET['sol'])) {
  $solu = $_GET['sol'];
?>


<script>
  function del(iden,action){
    var dataString = "id="+iden+"&action="+action;
    var status = $("#status").val();
    var identity = $('#loadings')+iden;
    var confirms = confirm("Are you sure you want to continue?");
    if (confirms == true) {
    $.ajax({
        type: "POST",
        url: "control.php",
        cache : false,
        data : dataString,
        beforeSend : function(){
            $('#del'+iden).hide();
            $('#loadings'+iden).show();
        },
        success : function(response){
            $('#loadings'+iden).hide();
            document.getElementById('inputHere').innerHTML = response;
            $('#myModal').modal('show');
            window.location.reload(1);
        }
    });
}
}
</script>
<?php
if (isset($_POST['assign'])) {
  $score = $_POST['score'];
  $aid = $_POST['aid'];

  $in = array('score' => $score, );

  update('answer',$in,'id',$aid);
}
?>
<div class="x_content">
    <table id="datatable-responsive" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">
                            <thead>
                             <tr>
                                <th>S/N</th>
                                <th>Student Name</th>
                                <th>Matric</th>
                                <th>Date Submited</th>
                                <th>View Solution</th>
                                <th>Score</th>
                              </tr>
                            </thead>
                            <tbody>
                        <?php
                         $select = $conn->query("SELECT * FROM answer WHERE package = '$vlink2' AND assignment = '$solu'");
                            $fetch = $select->fetchAll(PDO::FETCH_OBJ);
                            $count = $select->rowCount();
                            $serial = 1;
                            if ($count > 0) {
                              foreach ($fetch as $value) {
                              $doceid = $value->id;
                              $stid = $value->iden;
                              $answer = $value->answer;
                              $score = $value->score;
                              $datess = $value->date;

                             $select2 = $conn->query("SELECT * FROM students WHERE id = '$stid' LIMIT 1");
                            $fetch2 = $select2->fetchAll(PDO::FETCH_OBJ);
                            $count2 = $select2->rowCount();
                            if ($count2 > 0) {
                              foreach ($fetch2 as $key) {
                                $stuname = $key->sname;
                                $stuoname = $key->oname;
                                $matric = $key->matric;
                               }
                             }

                            ?>
                              <tr>
                                <td><?php echo $serial; ?></td>
                                <td><?php echo "$stuname $stuoname"; ?></td>
                                <td><?php echo $matric; ?></td>
                                <td><?php echo $datess; ?></td>
                                <td><button class="btn btn-default btn-xs" data-toggle="modal" data-target="#edit<?php echo $doceid ?>" title="View Content"><i class="fa fa-eye"></i></button></td>
                                <td>
                                  <?php
                                  if (empty($score)) {
                                  ?>
                                  <form method="post">
                                    <div class="form-group">
                                      <input type="number" name="score" class="form-control">
                                      <input type="hidden" name="aid" value="<?php echo $doceid ?>">
                                      <span><button class="btn btn-info" name="assign" type="submit" required><i class="fa fa-plus"></i> </button></span>
                                    </div>
                                  </form>
                                  <?php
                                  }else{
                                    ?>
                                    <?php echo $score ?> <span> <button class="btn btn-default btn-xs" title="Refresh Score" id="del<?php echo $doceid?>" onclick= "del('<?php echo $doceid ?>', 'delassscore')"><i class="fa fa-refresh"></i></button><span id="loadings<?php echo $doceid ?>" style="display:none"><img src="img/loading.gif"></span></span>
                                    <?php
                                  }
                                   ?>
                                                                    
                                </td>


                                <div class="modal fade" id="edit<?php echo $doceid ?>" role="dialog">
                                    <div class="modal-dialog modal-lg">
                                      <div class="modal-content">
                                        <div class="modal-header">
                                          <button type="button" class="close" data-dismiss="modal">&times;</button>
                                          <h4 class="modal-title">Viewing Solution for <?php echo "$stuname $stuoname"; ?></h4>
                                        </div>
                                        <div class="modal-body">
                                        <p><?php echo "$answer"; ?></p>
                                        </div>
                                        </div>
                                      </div>
                                    </div>
                              </tr>
                              
                            <?php
                            $serial++;
                                 }
                            }
                        ?>
                            </tbody>
                          </table>                
</div>

                      


<div class="modal fade" id="myModal" role="dialog">
    <div class="modal-dialog modal-sm">
      <div class="modal-content">
        <div class="modal-body text-center">
        <div id="inputHere">

        </div>
        </div>
        </div>
      </div>
    </div>


<div class="modal fade" id="myModal2" role="dialog">
    <div class="modal-dialog modal-sm">
      <div class="modal-content">
        <div class="modal-body text-center">
        <?php  
        if (isset($mess)) {
        echo "$mess";
        }
        ?>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Ok</button>
        </div>
        </div>
      </div>
    </div>

<?php
if(isset($mess)){
?>
<script type="text/javascript">
   $(window).load(function(){
   $('#myModal2').modal('show');
   }); 
</script>
<?php
  }
}
?>