<?php
/* ============================================================
   MANAGER RESPONSIBILITY REVIEW — NEW FEATURE
   ------------------------------------------------------------
   Purpose: when someone is newly made a Manager, they should
   read & explicitly accept a manager-specific responsibilities
   note (separate from the normal employee "responsibilities"
   already in the file) before it stops nagging them on the
   dashboard.

   WHERE TO PASTE EACH PART:
   1. The two ensureColumn() lines -> put them next to the other
      ensureColumn(...) calls near the top of dashboard_manager.php
      (right after the existing offer/details/responsibilities ones).
   2. The "accept_manager_responsibilities" POST handler -> put it
      inside the big `if ($_SERVER['REQUEST_METHOD']==='POST' &&
      isset($_POST['action']))` block, as another `elseif`, next to
      accept_responsibilities.
   3. The HTML card -> paste it right after the existing
      "KPI & PERFORMANCE" card (or anywhere you like) in the page.
   ============================================================ */


/* ---------- 1. COLUMNS (put near the other ensureColumn calls) ---------- */

ensureColumn($conn, 'users', 'manager_responsibilities', "TEXT NULL DEFAULT NULL");
ensureColumn($conn, 'users', 'manager_responsibilities_accepted', "VARCHAR(20) DEFAULT NULL");
ensureColumn($conn, 'users', 'manager_responsibilities_accepted_at', "DATETIME DEFAULT NULL");


/* ---------- 2. FORM HANDLER (put as another `elseif` inside the action block) ---------- */

/*
elseif ($action === 'accept_manager_responsibilities'
        && columnExists($conn,'users','manager_responsibilities_accepted')) {

    $sql = "UPDATE users
            SET manager_responsibilities_accepted = 'Accepted',
                manager_responsibilities_accepted_at = ?
            WHERE id = ?";
    $stmt = $conn->prepare($sql);
    if ($stmt) {
        $stmt->bind_param("ss", $now, $employee_id);
        $stmt->execute();
        $stmt->close();
    }
    $_SESSION['success_message'] = "Manager responsibilities accepted!";
}
*/


/* ---------- 3. HTML CARD (paste into the page, manager-role only) ---------- */
?>

<?php
// Only show this card for users whose role is 'manager' AND who have
// manager-specific responsibility text set by admin/HR, AND haven't
// accepted it yet. Once accepted, it simply won't render again.
$showManagerResponsibilityCard =
    strtolower(trim($user['role'] ?? '')) === 'manager'
    && !empty($user['manager_responsibilities'])
    && ($user['manager_responsibilities_accepted'] ?? '') !== 'Accepted';
?>

<?php if ($showManagerResponsibilityCard): ?>
<div class="dash-card" id="section-manager-responsibilities" style="border:1px solid #fde68a">
  <div class="dash-card-header" style="background:#fffbeb">
    <div class="dash-card-title">
      <i class="fas fa-user-shield" style="color:var(--amber)"></i>
      Manager Responsibilities — Action Required
    </div>
    <span class="badge-custom badge-warning"><i class="fas fa-hourglass-half me-1"></i>Pending Review</span>
  </div>
  <div class="dash-card-body">
    <p style="font-size:12.5px;color:var(--muted);margin-bottom:10px">
      As a Manager, please read the responsibilities below and confirm you
      understand and accept them.
    </p>
    <div class="resp-box" style="background:#fffbeb;border-left-color:var(--amber)">
      <?= nl2br(h($user['manager_responsibilities'])) ?>
    </div>
    <form method="POST" class="mt-3">
      <input type="hidden" name="action" value="accept_manager_responsibilities">
      <button type="submit" class="btn btn-warning btn-sm"
              onclick="return confirm('I have read and accept the manager responsibilities.')">
        <i class="fas fa-check me-1"></i>I Accept These Responsibilities
      </button>
    </form>
  </div>
</div>
<?php elseif (strtolower(trim($user['role'] ?? '')) === 'manager'
           && !empty($user['manager_responsibilities'])
           && ($user['manager_responsibilities_accepted'] ?? '') === 'Accepted'): ?>
<!-- OPTIONAL: small confirmation chip once accepted, shown wherever you like -->
<div style="margin-bottom:10px">
  <span class="badge-custom badge-success">
    <i class="fas fa-check-circle me-1"></i>
    Manager Responsibilities Accepted
    <?php if (!empty($user['manager_responsibilities_accepted_at'])): ?>
      on <?= date('M d, Y', strtotime($user['manager_responsibilities_accepted_at'])) ?>
    <?php endif; ?>
  </span>
</div>
<?php endif; ?>

<?php
/* ---------- HOW TO SET THE TEXT FOR A NEW MANAGER ---------- */
// From an admin/HR screen (or directly in DB), when someone is promoted
// or hired as Manager, set:
//   UPDATE users
//   SET manager_responsibilities = 'Your manager-specific duties text here...',
//       manager_responsibilities_accepted = NULL
//   WHERE id = <new_manager_id>;
//
// The card above will then automatically appear on their dashboard the
// next time they log in, and disappear once they click Accept.