<?php
   include('session.php');
   include('header1.php');
   include('ietpartial.php');
   include('calculations.php');

// start role specific 
$user_role = $_SESSION['role'];

if ($_SERVER["REQUEST_METHOD"] == "GET") {

  $departmentid = $_GET['departmentid'];

  $sqlViewDepartment = "SELECT *
  FROM departments
  WHERE departmentid = $departmentid;";

  $resultDepartment = mysqli_query($db,$sqlViewDepartment);

  $countDepartment = mysqli_num_rows($resultDepartment);

  if(isset($_GET['updated'])) {

    $updated = $_GET['updated'];

  }

    $sqlView = "SELECT *
          FROM positions
          WHERE departmentid = $departmentid;";

  
    $result = mysqli_query($db,$sqlView);

    $count = mysqli_num_rows($result);



    echo "<main role=\"main\" class=\"col-md-9 ml-sm-auto col-lg-10 px-4\">";

    echo "<div class=\"d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom\">";

    if ($countDepartment !== 0) { // COUNT EMPLOYEES START HERE
      
      while ($rowDepartment = mysqli_fetch_array($resultDepartment)) { 
        $departmentReturnId = $rowDepartment['departmentid'];
         echo "<h1 class=\"h2\">" . $rowDepartment['departmentname'] . "</h1>";
      }

      echo "<div class=\"btn-toolbar mb-2 mb-md-0\">"; // TOOLBAR STARTS HERE

          echo "<div class=\"btn-group mr-2\">";
                echo "<a href=\"addPosition.php?departmentid=" . $departmentid ."\" class=\"btn btn-sm btn-outline-secondary\" role=\"button\">Add Position</a>";
                echo "<a href=\"departments.php\" class=\"btn btn-sm btn-outline-secondary\" role=\"button\">Back</a>";
            echo "</div>";


          echo "</div>";

    echo "</div>";

    
if ($count !== 0) {

    // TABLE STARTS HERE

  echo "<table class = \"table\" border=\"1\">";

  echo "<thead>";
  echo "<td>Position Name</td>";
  echo "<td>Edit</td>";
  echo "<td>Delete</td>";
  echo "</thead>";
  echo "<tbody>";

  while ($rowposition = mysqli_fetch_array($result)) { // FETCHING EMPLOYEE DATA START
    echo "<tr>";
    echo "<td>" . $rowposition['positionname'] . "</td>";
    echo "<td><a href =\"editPosition.php?positionid=" . $rowposition['positionid'] . "&departmentid=" . $departmentReturnId . "\">Edit</td>";
    echo "<td><a href =\"deletePosition.php?positionid=" . $rowposition['positionid'] . "&departmentid=" . $departmentReturnId . "\">Delete</td>";
    echo "</tr>";


  } // FETCHING EMPLOYEE DATA END

  echo "</tbody>";
  echo "</table>";

    // TABLE ENDS HERE
} else {echo "No positions for this department yet..";}

    } // COUNT EMPLOYEES END HERE

} // GET ENDS HERE

include('footer1.php');

?>