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

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

// Get student ID from session or GET
$sid = isset($_GET['sid']) ? $_GET['sid'] : $_SESSION['stdid'];

// Fetch student details along with gender, department, and degree
$sql = "SELECT s.*, 
               g.gender_name, 
               d.department_name, 
               deg.degree_name 
        FROM tblstudents s 
        LEFT JOIN tblgender g ON s.gender_id = g.gender_id
        LEFT JOIN tbldepartment d ON s.department_id = d.department_id
        LEFT JOIN tbldegree deg ON s.degree_id = deg.degree_id
        WHERE s.StudentId = :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 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">
        <h5 class="header-line" style="color:#0d6efd;"><i class="fas fa-user-circle mr-2"></i>Student 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>Student Information
              </div>
              <a href="reg-students.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
                $photoWebPath = "../" . $result->Photo;
                if (!empty($result->Photo) && file_exists($photoWebPath)) {
                  echo '<img src="' . htmlentities($photoWebPath) . '" class="card-profile-img" alt="Student 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>Student ID:</th>
                  <td><?= htmlentities($result->StudentId); ?></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>Batch:</th>
                  <td><?= htmlentities($result->passed_out_year ?? 'N/A'); ?></td>
                </tr>
                <tr>
                  <th>Gender:</th>
                  <td><?= htmlentities($result->gender_name ?? 'N/A'); ?></td>
                </tr>
                <tr>
                  <th>Department:</th>
                  <td><?= htmlentities($result->department_name ?? 'N/A'); ?></td>
                </tr>
                <tr>
                  <th>Shift:</th>
                  <td>
                    <?php if ($result->shift == 1) {
                      echo "I";
                    } elseif ($result->shift == 2) {
                      echo "II";
                    } else {
                      echo "N/A";
                    } ?>
                  </td>
                </tr>
                <tr>
                  <th>Degree:</th>
                  <td><?= htmlentities($result->degree_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>
                <tr>
                  <th>ID Proof:</th>
                  <?php if (!empty($result->IDProof)) { ?>

                    <?php
                    $idProofPath = "../" . $result->IDProof;
                    if (file_exists($idProofPath)) {
                      echo '<td><a href="' . htmlentities($idProofPath) . '" target="_blank">View ID Proof</a></td>';
                    } else {
                      echo '<td class="text-muted">Not Available</td>';
                    }
                    ?>
                  </tr>
                <?php } else {
                    echo '<td class="text-muted">Not Available</td>';
                  } ?>
              </table>
            </div>
          </div>
        </div>
      </div>
    <?php else: ?>
      <div class="alert alert-danger text-center">
        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>