<?php
require 'core/connection.php';
$dateh = date('H') + 1;
$datei = date('i');
$datenow = date('l M d Y');
$datenow = "$datenow $dateh-$datei";
$filename = "Books-$datenow";


if (isset($_POST['getb'])) {
  $dept = $_POST['dep'];
  if (empty($dept)) {
    echo "This file is empty.... Please input a valid value next time";
  }else{
  ?>
  <style type="text/css">
    table {
  border-collapse: collapse;
}

table, th, td {
  border: 1px solid black;
}
th {
  text-align: left;
}
td {
  height: 50px;
  vertical-align: top;
}
  </style>
  <div style="text-align: center; text-transform: uppercase; font-weight: bold;">
    List of E-library books in <?php echo $dept; ?><br>
    Document generated on <?php echo $datenow; ?><br><br>

  </div>

                    <table border="1" cellpadding="5" cellspacing="0">
                             <tr>
                                <th>S/N</th>
                                <th>Title</th>
                                <th>Author</th>
                                <th>Description</th>
                                <th>Date of Publication</th>
                                <th>Edition</th>
                                <th>Publisher</th>
                                <th>Place Of Publication</th>
                                <th>Number of Pages</th>
                                <th>Course</th>
                                <th>Department</th>
                                <th>By</th>
                                <th>Date Added</th>
                              </tr>
                        <?php
                         $select = $conn->query("SELECT * FROM books WHERE underd = '$dept'");
                            $fetch = $select->fetchAll(PDO::FETCH_OBJ);
                            $count = $select->rowCount();
                            $serial = 1;
                            if ($count > 0) {
                              foreach ($fetch as $value) {
                                $iden = $value->id;
                                $name = $value->name;
                                $author = $value->author;
                                $frontpage = $value->frontpage;
                                $description = $value->description;
                                $dop = $value->dop;
                                $edition = $value->edition;
                                $publisher = $value->publisher;
                                $nop = $value->nop;
                                $pop = $value->pop;
                                $file = $value->file;
                                $course = $value->course;
                                $underd = $value->underd;
                                $by = $value->by;
                                $date = $value->date;
                                if ($course == "journal" || $course == "paper") {
                                  $usi = "yes";
                                }else{
                                  $usi = "no";
                                }
                            ?>
                              <tr>
                                <td><?php echo $serial; ?></td>
                                <td><?php echo $name; ?></td>
                                <td><?php echo $author; ?></td>
                                <td><?php echo $description; ?></td>
                                <td><?php echo $dop; ?></td>
                                <td><?php echo $edition; ?></td>
                                <td><?php echo $publisher; ?></td>
                                <td><?php echo $pop; ?></td>
                                <td><?php echo $nop; ?></td>
                                <td>
                                  <?php
                                  if ($course == "journal" || $course == "paper") 
                                  {
                                   echo $course;
                                  }else{
                                    $select2 = $conn->query("SELECT * FROM course WHERE id = '$course' ");
                                  $fetch2 = $select2->fetchAll(PDO::FETCH_OBJ);
                                  $count2 = $select2->rowCount();
                                  if ($count2 > 0) {
                                    foreach ($fetch2 as $value2) {
                                      $name2 = $value2->name;
                                      echo "$name2";
                                    }
                                  } 

                                  }
                                  
                                    
                                  ?>
                                  </td>
                                <td><?php echo $underd; ?></td>
                                <td><?php echo $by; ?></td>
                                <td><?php echo $date; ?></td>
                              </tr>
                              
                            <?php
                            $serial++;
                              }
                              ?>
                            </table><br>
                            Total = <?php echo $count; ?>
                              <?php
                            }
                        ?>
                          

  <?php
  }
}

 /*mkdir("testing");*/


header("Content-Type: application/doc");
header("Content-Disposition: attachment; filename=$filename.doc");

?>