<?php include_once($_SERVER['DOCUMENT_ROOT'].'/_ps2020/_global/dir_root.php'); ?>
<?php include_once($_SERVER['DOCUMENT_ROOT'].'/_ps2020/config/cfg_loggedUser.php'); ?>
<?php include($_SERVER['DOCUMENT_ROOT'] ."/_ps2020/config/connect.php"); ?>

<?php
    ob_start();

    // if(session_id() == '') { session_start(); }

    // Include config files

    include($_SERVER['DOCUMENT_ROOT'] ."/_ps2020/config/connect.php");

    // $sqlQuery = "SELECT c.user_id AS 'ID', c.username AS 'Username', COUNT(c.user_id) AS 'Total-Students', SUM(c.completed_surveys) AS 'Completed-Surveys' FROM school s, user_account c WHERE s.school_id = c.school_id GROUP BY s.school_id ";
    // $data = $conn->query($sqlQuery);
    // $tablesAndTheirData = array();
    // array_push($tablesAndTheirData, array(
    //     'fields' => $data->fetch_fields(),
    //     'data' => $data
    // ));

    // $result = $conn->query("SELECT u.user_id AS 'ID', u.username AS 'Username', c.class_name AS 'Class', SUM(u.completed_surveys) AS 'Completed Surveys (Total)' FROM user_account u, school_class c WHERE u.class_id = c.class_id AND c.teacher_id = '".$_SESSION['userID']."' ORDER BY u.user_id ASC;");

    // $result = $conn->query("SELECT user_id AS 'ID', username AS 'Username', SUM(completed_surveys) AS 'Completed Surveys (Total)' FROM user_account WHERE teacher_id = '".$_SESSION['userID']."' ORDER BY user_id ASC;");

    $db_sid = 0;
    $result = $conn->query("SELECT school_id FROM user_account WHERE user_id = '".$_SESSION['userID']."'");
		if($result) {
			while ( $row = $result->fetch_assoc() ){
				$db_sid = $row['school_id'];
			}
		}

    if ($_SESSION['accountType'] == 2){	// Teacher
    	$result = $conn->query("SELECT u.user_id AS 'ID', u.username AS 'Username', c.class_name AS 'Class', u.completed_demographic AS 'Completed Demographic', u.completed_surveys AS 'Completed Surveys (Total)' FROM school_class c, user_account u WHERE c.class_id = u.class_id AND c.teacher_id = '".$_SESSION['userID']."' GROUP BY u.user_id ");
    }
    else if ($_SESSION['accountType'] == 3){	// School-Admin
    	$result = $conn->query("SELECT u.user_id AS 'ID', u.username AS 'Username', c.class_name AS 'Class', u.completed_demographic AS 'Completed Demographic', u.completed_surveys AS 'Completed Surveys (Total)' FROM school_class c, user_account u WHERE (u.class_id = c.class_id OR u.class_id = 0) AND u.school_id = '".$db_sid."' GROUP BY u.user_id ");
    }
    else{
      $result = $conn->query("SELECT user_id AS 'ID', username AS 'Username', class_id AS 'Class', completed_demographic AS 'Completed Demographic', completed_surveys AS 'Completed Surveys (Total)' FROM user_account u GROUP BY user_id ");
    }

      $tablesAndTheirData = array();
        array_push($tablesAndTheirData, array(
            'fields' => $result->fetch_fields(),
            'data' => $result
        ));
?>

<!DOCTYPE html>
<html lang="en">
<html>
  <head>
    <title>Survey Completion</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />

    <?php include_once(dir_rootPHP.'_global/icon.php'); ?>

    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/bs4-4.1.1/jq-3.3.1/jszip-2.5.0/dt-1.10.21/af-2.3.5/b-1.6.3/b-colvis-1.6.3/b-flash-1.6.3/b-html5-1.6.3/b-print-1.6.3/cr-1.5.2/fc-3.3.1/fh-3.1.7/kt-2.5.2/r-2.2.5/rg-1.1.2/rr-1.2.7/sc-2.0.2/sb-1.0.0/sp-1.1.1/sl-1.3.1/datatables.min.css"/>

    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/pdfmake.min.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/vfs_fonts.js"></script>
    <script type="text/javascript" src="https://cdn.datatables.net/v/bs4-4.1.1/jq-3.3.1/jszip-2.5.0/dt-1.10.21/af-2.3.5/b-1.6.3/b-colvis-1.6.3/b-flash-1.6.3/b-html5-1.6.3/b-print-1.6.3/cr-1.5.2/fc-3.3.1/fh-3.1.7/kt-2.5.2/r-2.2.5/rg-1.1.2/rr-1.2.7/sc-2.0.2/sb-1.0.0/sp-1.1.1/sl-1.3.1/datatables.min.js"></script>

  <!-- libs -->
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" integrity="sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <!-- <link rel="stylesheet" href="<?php echo dir_root; ?>libs/fontAwesome/css/font-awesome.min.css" /> -->
    <!-- <link rel="stylesheet" href="<?php echo dir_root; ?>libs/animate.css" /> -->

    <!-- animation -->
      <link rel="stylesheet" href="<?php echo dir_root; ?>libs/Animate/animate.css" />
      <!-- <link rel="stylesheet" href="<?php echo dir_root; ?>libs/Animate/animate_bounce.css" /> -->
      <link rel="stylesheet" href="<?php echo dir_root; ?>libs/Animate/animate_fade.css" />
      <!-- <link rel="stylesheet" href="<?php echo dir_root; ?>libs/Animate/animate_flip.css" /> -->
      <!-- <link rel="stylesheet" href="<?php echo dir_root; ?>libs/Animate/animate_other.css" /> -->
      <!-- <link rel="stylesheet" href="<?php echo dir_root; ?>libs/Animate/animate_rotate.css" /> -->
      <link rel="stylesheet" href="<?php echo dir_root; ?>libs/Animate/animate_slide.css" />
      <!-- <link rel="stylesheet" href="<?php echo dir_root; ?>libs/Animate/animate_zoom.css" /> -->

  <!-- css -->
    <link rel="stylesheet" href="<?php echo dir_root; ?>css/template-hielo/main.css" />
    <link rel="stylesheet" href="<?php echo dir_root; ?>css/template-hielo/dataTable.css" />

  <!-- css (custom) -->
    <link rel="stylesheet" href="<?php echo dir_root; ?>css/henry_style.css" />
    <!-- <link rel="stylesheet" href="<?php echo dir_root; ?>css/tanuj_style.css" /> -->
    <!-- <link rel="stylesheet" href="<?php echo dir_root; ?>css/karl_style.css" /> -->
    <!-- <link rel="stylesheet" href="<?php echo dir_root; ?>css/harsh_style.css" /> -->

    <style type="text/css">
      .card{
        border-radius: 10px;
        box-shadow: 0px 0px 15px #e1e1e1;
      }
        .card-body{
          border-radius: 10px;
        }

      #data_table_wrapper{
        padding: 5px;
      }
    </style>

  </head>
  <!-- <body class="subpage"> -->
  <body>

    <!-- Header | Nav -->
      <?php include_once(dir_rootPHP.'_global/navbar/navbar.php'); ?>
      <?php include_once(dir_rootPHP.'_global/navbar/navbar-mobile.php'); ?>

    <!-- Page-Title -->
      <section id="One" class="wrapper style3" style="padding-top: 9rem;">
        <div class="inner">
          <header class="align-center">
            <p>Greenscape Environmental Experience & Learning Survey Experiments</p>
            <h2>Survey Completion</h2>
          </header>
        </div>
      </section>

    <!-- Content -->
      <section id="two" class="wrapper style2">
        <div class="inner">

          <div class="card"style="background:rgba(255,255,255,0.5);">
            <div class="card-body"style="background:rgba(255,255,255,0.5);">
                <div class="table-responsive" style="padding-left:15px; margin-top:25px;">
                <?php foreach($tablesAndTheirData as $table): ?>
                        <?php if($table['data']->num_rows):?>
                            <table id="data_table" class="table table-striped" style="width:100%;">

                                <thead class="animated fadeInUp">
                                    <tr>
                                    <?php foreach($table['fields'] as $field): ?>

                                        <th><?php echo $field->name;?></th>

                                    <?php endforeach; ?>
                                    </tr>
                                </thead>
                                <tbody>
                            <?php while($row = $table['data']->fetch_assoc()): ?>
                                <tr>
                                    <?php foreach($row as $key => $value):?>
                                        <td>
                                          <?php
                                            if ($key == 'Class'){

                                              if ($value == 0){
                                                echo 'Not Assigned';
                                              }
                                              else{
                                                $result = $conn->query("SELECT class_name FROM school_class WHERE class_id = ".$value);
                                                  if($result) {
                                                    $row = $result->fetch_assoc();
                                                    echo $row['class_name'];
                                                  }
                                              }

                                            }
                                            else
                                              echo $value;
                                          ?>
                                        </td>
                                    <?php endforeach; ?>
                                </tr>
                            <?php endwhile;?>
                                </tbody>
                            </table>
                        <?php else:?>
                            <p>Table does not have any data</p>
                        <?php endif;?>
                    <?php endforeach;
                ?>
                </div>
            </div>
          </div>

        </div>
      </section>


    <!-- Footer -->
      <?php include_once(dir_rootPHP.'_global/footer/footer.php'); ?>

<script type="text/javascript">
        $(document).ready(function() {
            $('#data_table').DataTable( {
                dom: 'frtip'
            } );
        } );
    </script>
</body>
</html>
