<?php
session_start();
include('includes/config.php');
// ini_set('disperror_level: lay_errors', 1);
// ini_set('display_startutimezoneId: p_errors', 1);
error_reporting(E_ALL);
// Allow only admin or staff to access
if (!isset($_SESSION['alogin']) && !isset($_SESSION['staff_login'])) {
  header('location:index.php');
  exit;
}

// Determine staff ID to edit: admin can use ?sid=... else staff edits own
$sid = isset($_GET['sid']) ? $_GET['sid'] : (isset($_SESSION['staffid']) ? $_SESSION['staffid'] : null);

if (!$sid) {
  echo "<script>alert('Invalid staff ID'); window.location.href='index.php';</script>";
  exit;
}

// Fetch staff details
$sql = "SELECT s.*,
               g.gender_name,
               d.department_name,
               des.designation_name,
               deg.degree_name
        FROM tblstaff 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 tblstaffdesignation des ON s.designation_id = des.designation_id
        LEFT JOIN tbldegree deg ON s.degree_id = deg.degree_id
        WHERE s.StaffId = :sid";

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

if (!$result) {
  echo "<script>alert('Staff not found'); window.location.href='staff-profile.php';</script>";
  exit;
}


// Default photo fallback
$defaultPhoto = "uploads/photos/default-profile.jpg";

$idProofAbsPath = !empty($result->IDProof) ? __DIR__ . '/' . $result->IDProof : '';
$photoAbsPath = !empty($result->Photo) ? __DIR__ . '/' . $result->Photo : '';

$idProofUrl = !empty($result->IDProof) ? $result->IDProof : '';
$photoUrl = !empty($result->Photo) ? $result->Photo : '';
?>
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>My 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 5 -->
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
  <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" rel="stylesheet">

  <style>
    body {
      background-color: #f4f6f9;
    }

    .profile-card {
      border-radius: 12px;
      overflow: hidden;
    }

    .profile-header {
      background:
        <?php echo $_SESSION['theme_color'] ?? '#3498db'; ?>
      ;
      color: #fff;
      padding: 30px;
      text-align: center;
    }

    .profile-header img {
      width: 120px;
      height: 120px;
      object-fit: cover;
      border-radius: 50%;
      margin-bottom: 10px;
    }

    .profile-body {
      padding: 25px;
    }

    .info-label {
      font-size: 13px;
      color: #6c757d;
      margin-bottom: 3px;
    }

    .info-value {
      font-weight: 600;
      font-size: 15px;
    }

    .info-box {
      padding: 12px;
      background: #f8f9fa;
      border-radius: 8px;
      margin-bottom: 15px;
    }

    .section-title {
      font-weight: 600;
      margin-bottom: 15px;
      color: #0d6efd;
    }

    .doc-link {
      text-decoration: none;
      font-weight: 500;
    }

    .profile-photo-wrapper {
      width: 120px;
      height: 120px;
      border-radius: 50%;
      overflow: hidden;
      border: 4px solid #fff;
      background: #fff;
    }

    .profile-photo-wrapper img {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
  </style>
</head>

<body>

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

  <div class="container my-5">

    <div class="row justify-content-center">
      <div class="col-lg-9">

        <div class="card shadow profile-card">

          <!-- PROFILE HEADER -->
          <div class="profile-header">
            <?php
            $photoPath = (!empty($photoUrl) && file_exists($photoAbsPath))
              ? $photoUrl
              : "https://ui-avatars.com/api/?name=" . urlencode($result->FullName) . "&background=ffffff&color=0d6efd&size=200";
            ?>
            <div class="d-flex justify-content-center mb-2">
              <div class="profile-photo-wrapper">
                <img src="<?= htmlentities($photoPath); ?>" alt="Profile Photo">
              </div>
            </div>

            <h5 class="mb-0"><?= htmlentities($result->FullName); ?></h5>
            <small>Staff ID: <?= htmlentities($result->StaffId); ?></small>
          </div>

          <!-- PROFILE BODY -->
          <div class="profile-body">

            <h6 class="section-title"><i class="fa-solid fa-user me-2"></i>Personal Information</h6>

            <div class="row">
              <div class="col-md-6">
                <div class="info-box">
                  <div class="info-label">Email</div>
                  <div class="info-value">
                    <?= !empty($result->EmailId) ? htmlentities($result->EmailId) : '-' ?>
                  </div>
                </div>
              </div>
              <div class="col-md-3">
                <div class="info-box">
                  <div class="info-label">Mobile Number</div>
                  <div class="info-value">
                    <?= !empty($result->MobileNumber) ? htmlentities($result->MobileNumber) : '-'; ?>
                  </div>
                </div>
              </div>
              <div class="col-md-3">
                <div class="info-box">
                  <div class="info-label">Gender</div>
                  <div class="info-value">
                    <?= !empty($result->gender_name) ? htmlentities($result->gender_name) : '-'; ?>
                  </div>
                </div>
              </div>
              <div class="col-md-12">
                <div class="info-box">
                  <div class="info-label">Address</div>
                  <div class="info-value">
                    <?= !empty($result->Address) ? htmlentities($result->Address) : '-' ?>
                  </div>
                </div>
              </div>
            </div>

            <h6 class="section-title mt-4"><i class="fa-solid fa-graduation-cap me-2"></i>Professional Details</h6>

            <div class="row">
              <div class="col-md-6">
                <div class="info-box">
                  <div class="info-label">Designation</div>
                  <div class="info-value">
                    <?= !empty($result->designation_name) ? htmlentities($result->designation_name) : '-'; ?>
                  </div>
                </div>
              </div>

              <div class="col-md-6">
                <div class="info-box">
                  <div class="info-label">Qualification</div>
                  <div class="info-value">
                    <?= !empty($result->Qualification) ? htmlentities($result->Qualification) : '-'; ?>
                  </div>
                </div>
              </div>
              <div class="col-md-4">
                <div class="info-box">
                  <div class="info-label">Department</div>
                  <div class="info-value">
                    <?= !empty($result->department_name) ? htmlentities($result->department_name) : '-'; ?>
                  </div>
                </div>
              </div>

              <div class="col-md-4">
                <div class="info-box">
                  <div class="info-label">Degree</div>
                  <div class="info-value">
                    <?= !empty($result->degree_name) ? htmlentities($result->degree_name) : '-'; ?>
                  </div>
                </div>
              </div>

              <div class="col-md-4">
                <div class="info-box">
                  <div class="info-label">Shift</div>
                  <div class="info-value">
                    <?php echo ($result->shift == 1) ? 'Day' : 'Evening'; ?>
                  </div>
                </div>
              </div>
              <div class="col-md-4">
                <div class="info-box">
                  <div class="info-label">Joining Date</div>
                  <div class="info-value">
                    <?= !empty($result->JoiningDate) ? htmlentities($result->JoiningDate) : '-'; ?>
                  </div>
                </div>
              </div>


            </div>

            <h6 class="section-title mt-4"><i class="fa-solid fa-file-lines me-2"></i>Documents</h6>

            <div class="info-box">
              <?php if (!empty($idProofUrl) && file_exists($idProofAbsPath)) { ?>
                <a href="<?= htmlentities($idProofUrl); ?>" target="_blank" class="doc-link">
                  <i class="fa-solid fa-id-card me-2"></i>View ID Proof
                </a>
              <?php } else { ?>
                <span class="text-muted">ID Proof not uploaded</span>
              <?php } ?>
            </div>

          </div>
        </div>

      </div>
    </div>
  </div>

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

  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
</body>

</html>