<?php
session_start();
include('includes/config.php');
error_reporting(0);

// Ensure only logged-in Admin or Staff can view profile
if (!isset($_SESSION['alogin']) && !isset($_SESSION['staff_login'])) {
  header('location:index.php');
  exit;
}

// Get staff ID from GET
$sid = isset($_GET['sid']) ? $_GET['sid'] : '';

// Fetch staff details along with department, designation, gender, and degree
$sql = "SELECT st.*, 
               d.department_name, 
               des.designation_name, 
               g.gender_name, 
               deg.degree_name
        FROM tblstaff st
        LEFT JOIN tbldepartment d ON st.department_id=d.department_id
        LEFT JOIN tblstaffdesignation des ON st.designation_id=des.designation_id
        LEFT JOIN tblgender g ON st.gender_id=g.gender_id
        LEFT JOIN tbldegree deg ON st.degree_id=deg.degree_id
        WHERE st.StaffId = :sid";

$query = $dbh->prepare($sql);
$query->bindParam(':sid', $sid, PDO::PARAM_STR);
$query->execute();
$result = $query->fetch(PDO::FETCH_OBJ);

// Define default photo
$defaultPhoto = "uploads/photos/default-profile.jpg";
$finalPhoto = (!empty($result->Photo) && file_exists($result->Photo)) ? $result->Photo : $defaultPhoto;
?>

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>View Staff Profile | Library System</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="../<?php echo $_SESSION['logo']; ?>">

  <!-- Bootstrap 4.6 + FontAwesome -->
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css" rel="stylesheet">
  <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">

  <style>
    .main-content {
      margin-left: 180px;
      margin-top: 50px;
      padding: 20px;
    }

    .card-profile-img {
      width: 100px;
      height: 100px;
      object-fit: cover;
      border-radius: 4px;
    }

    .header-line {
      color: #2258d5;
      font-size: 22px;
      font-weight: 600;
    }
  </style>
</head>

<body>
  <?php include('includes/header.php'); ?>

  <div class="main-content container">
    <div class="row mb-4">
      <div class="col-md-12 d-flex justify-content-between align-items-center">
        <h5 class="header-line" style="color:#0d6efd;"><i class="fas fa-user-circle mr-2"></i>Staff Profile</h5>
      </div>
    </div>

    <?php if ($result): ?>
      <div class="row justify-content-center">
        <div class="col-md-8">
          <div class="card shadow-sm">
            <div class="card-header bg-secondary text-white d-flex justify-content-between align-items-center">
              <div>
                <i class="fas fa-info-circle mr-2"></i>Staff Information
              </div>
              <a href="reg-staffs.php" class="btn btn-light btn-sm">
                <i class="fa fa-arrow-left"></i> Back
              </a>
            </div>
            <div class="card-body">
              <div class="text-center mb-4">
                <?php
                // if (!empty($result->Photo) && file_exists($result->Photo)) {
                echo '<img src="../staff/' . htmlentities($result->Photo) . '" class="card-profile-img" alt="Staff Photo">';
                // } else {
                // echo '<p class="text-muted">Photo Not Available</p>';
                // }
                ?>
                <h5 class="mt-2"><?= htmlentities($result->FullName); ?></h5>
              </div>

              <table class="table table-bordered table-striped">
                <tr>
                  <th>Staff ID:</th>
                  <td><?= htmlentities($result->StaffId); ?></td>
                </tr>
                <tr>
                  <th>Email:</th>
                  <td><?= htmlentities($result->EmailId); ?></td>
                </tr>
                <tr>
                  <th>Mobile No:</th>
                  <td><?= htmlentities($result->MobileNumber); ?></td>
                </tr>
                <tr>
                  <th>Address:</th>
                  <td><?= htmlentities($result->Address); ?></td>
                </tr>
                <tr>
                  <th>Department:</th>
                  <td><?= htmlentities($result->department_name ?? 'N/A'); ?></td>
                </tr>
                <tr>
                  <th>Designation:</th>
                  <td><?= htmlentities($result->designation_name ?? 'N/A'); ?></td>
                </tr>
                <tr>
                  <th>Qualification:</th>
                  <td><?= htmlentities($result->Qualification ?? 'N/A'); ?></td>
                </tr>
                <tr>
                  <th>Degree:</th>
                  <td><?= htmlentities($result->degree_name ?? 'N/A'); ?></td>
                </tr>
                <tr>
                  <th>Job Type:</th>
                  <td class="text-capitalize"><?= htmlentities($result->jobtype ?? 'N/A'); ?></td>
                </tr>
                <tr>
                  <th>Shift:</th>
                  <td>
                    <?= ($result->shift == 1 ? 'I' : ($result->shift == 2 ? 'II' : 'N/A')) ?>
                  </td>
                </tr>
                <tr>
                  <th>Joining Date:</th>
                  <td><?= htmlentities($result->JoiningDate); ?></td>
                </tr>
                <tr>
                  <th>Gender:</th>
                  <td><?= htmlentities($result->gender_name ?? 'N/A'); ?></td>
                </tr>
                <!-- <tr>
                  <th>Registration Date:</th>
                  <td><?= htmlentities($result->RegDate); ?></td>
                </tr> -->
                <!-- <?php if ($result->UpdationDate): ?>
                  <tr>
                    <th>Last Updated:</th>
                    <td><?= htmlentities($result->UpdationDate); ?></td>
                  </tr>
                <?php endif; ?> -->
                <tr>
                  <th>Status:</th>
                  <td>
                    <?php
                    if ($result->Status == 1) {
                      echo '<span class="text-success">Active</span>';
                    } elseif ($result->Status == 0) {
                      echo '<span class="text-danger">Blocked</span>';
                    } elseif ($result->Status == 2) {
                      echo '<span class="text-warning">Pending</span>';
                    }
                    ?>
                  </td>
                </tr>
                <?php if (!empty($result->IDProof)): ?>
                  <tr>
                    <th>ID Proof:</th>
                    <?php
                    $idProofPath = "../staff/" . $result->IDProof;
                    if (!empty($result->IDProof) && file_exists($idProofPath)) {
                      echo '<td><a href="' . $idProofPath . '" target="_blank">View ID Proof</a></td>';
                    } else {
                      echo '<td class="text-muted">Not Available</td>';
                    }
                    ?>

                  </tr>
                <?php endif; ?>
              </table>
            </div>
          </div>
        </div>
      </div>
    <?php else: ?>
      <div class="alert alert-danger text-center">
        Staff profile not found.
      </div>
    <?php endif; ?>
  </div>

  <?php include('includes/footer.php'); ?>

  <!-- Scripts -->
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js"></script>
</body>

</html>