<?php
require_once __DIR__ . '/../../admin/_nav.php';
$pageTitle  = 'Teachers';
$sidebarNav = adminNav();
$u = function($p) { return \Core\View::url($p); };
?>
<div class="page-header">
  <div><h1><i class="bi bi-person-badge"></i> Teachers</h1><p>Manage teaching staff</p></div>
  <button onclick="openModal('addTeacher')" class="btn btn-primary"><i class="bi bi-plus-lg"></i> Add Teacher</button>
</div>

<!-- Filters -->
<div class="card" style="margin-bottom:16px">
  <div class="card-body" style="padding:12px 16px">
    <form method="GET" style="display:flex;gap:8px;flex-wrap:wrap;align-items:center">
      <input type="text" name="search" value="<?= htmlspecialchars($filters['search']) ?>" placeholder="Name, email, TSC, emp no..." style="width:200px">
      <select name="employment_type" style="width:140px">
        <option value="">All Types</option>
        <option value="permanent" <?= $filters['empType']==='permanent'?'selected':'' ?>>Permanent</option>
        <option value="contract"  <?= $filters['empType']==='contract' ?'selected':'' ?>>Contract</option>
        <option value="bom"       <?= $filters['empType']==='bom'      ?'selected':'' ?>>BOM</option>
      </select>
      <select name="status" style="width:120px">
        <option value="">All Status</option>
        <option value="1" <?= $filters['statusF']==='1'?'selected':'' ?>>Active</option>
        <option value="0" <?= $filters['statusF']==='0'?'selected':'' ?>>Inactive</option>
      </select>
      <select name="class_teacher" style="width:150px">
        <option value="">Class Teacher?</option>
        <option value="1" <?= $filters['classTeacher']==='1'?'selected':'' ?>>Assigned</option>
        <option value="0" <?= $filters['classTeacher']==='0'?'selected':'' ?>>Not Assigned</option>
      </select>
      <?php if(!empty($departments)): ?>
      <select name="department_id" style="width:150px">
        <option value="">All Departments</option>
        <?php foreach($departments as $d): ?>
        <option value="<?= $d['id'] ?>" <?= $filters['deptId']==$d['id']?'selected':'' ?>><?= htmlspecialchars($d['name']) ?></option>
        <?php endforeach; ?>
      </select>
      <?php endif; ?>
      <button type="submit" class="btn btn-primary btn-sm"><i class="bi bi-search"></i> Filter</button>
      <a href="<?= $u('admin/teachers') ?>" class="btn btn-secondary btn-sm"><i class="bi bi-x"></i> Clear</a>
    </form>
  </div>
</div>

<!-- Teachers Table -->
<div class="card">
  <div class="table-wrap">
    <table>
      <thead>
        <tr>
          <th>Teacher</th>
          <th>TSC No</th>
          <th>Employment</th>
          <th>Class Teacher Of</th>
          <th>Status</th>
          <th>Last Login</th>
          <th>Actions</th>
        </tr>
      </thead>
      <tbody>
        <?php foreach($teachers['data'] as $t): ?>
        <tr>
          <td>
            <div style="display:flex;align-items:center;gap:10px">
              <div class="avatar" style="width:36px;height:36px;font-size:14px"><?= strtoupper(substr($t['name'],0,1)) ?></div>
              <div>
                <div style="font-weight:500"><?= htmlspecialchars($t['name']) ?></div>
                <div style="font-size:12px;color:var(--gray)"><?= htmlspecialchars($t['email']) ?></div>
                <?php if($t['phone']): ?><div style="font-size:11px;color:var(--gray)"><?= htmlspecialchars($t['phone']) ?></div><?php endif; ?>
              </div>
            </div>
          </td>
          <td style="font-size:13px"><?= htmlspecialchars($t['tsc_no'] ?? '—') ?></td>
          <td><span class="badge badge-info"><?= ucfirst($t['employment_type'] ?? 'permanent') ?></span></td>
          <td>
            <?php if($t['class_name']): ?>
            <span class="badge badge-success"><i class="bi bi-people"></i> <?= htmlspecialchars($t['class_name'].' '.$t['stream_name']) ?></span>
            <?php else: ?>
            <span style="color:var(--gray);font-size:12px">Not assigned</span>
            <?php endif; ?>
          </td>
          <td><span class="badge <?= $t['is_active']?'badge-success':'badge-danger' ?>"><?= $t['is_active']?'Active':'Inactive' ?></span></td>
          <td style="font-size:12px;color:var(--gray)"><?= $t['last_login_at'] ? date('d M Y', strtotime($t['last_login_at'])) : 'Never' ?></td>
          <td style="display:flex;gap:6px">
            <a href="<?= $u('admin/teachers/'.$t['id']) ?>" class="btn btn-sm btn-primary"><i class="bi bi-eye"></i> View</a>
            <form method="POST" action="<?= $u('admin/teachers/'.$t['id'].'/toggle') ?>" style="display:inline">
              <?= \Core\View::csrf() ?>
              <button class="btn btn-sm btn-secondary" title="<?= $t['is_active']?'Deactivate':'Activate' ?>">
                <i class="bi bi-<?= $t['is_active']?'pause-circle':'play-circle' ?>"></i>
              </button>
            </form>
          </td>
        </tr>
        <?php endforeach; ?>
        <?php if(empty($teachers['data'])): ?>
        <tr><td colspan="7" style="text-align:center;padding:40px;color:var(--gray)">
          <i class="bi bi-person-badge" style="font-size:32px;display:block;margin-bottom:8px"></i>
          No teachers found. <button onclick="openModal('addTeacher')" class="btn btn-sm btn-primary" style="margin-left:8px">Add First Teacher</button>
        </td></tr>
        <?php endif; ?>
      </tbody>
    </table>
  </div>
  <div style="padding:14px"><?= \Core\View::paginate($teachers, $u('admin/teachers')) ?></div>
</div>

<!-- Add Teacher Modal -->
<div class="modal" id="addTeacher">
  <div class="modal-box" style="max-width:580px">
    <div class="card-header">
      <h5><i class="bi bi-person-plus"></i> Add New Teacher</h5>
      <button onclick="closeModal('addTeacher')" style="background:none;border:none;cursor:pointer;font-size:20px">&times;</button>
    </div>
    <div class="card-body">
      <form method="POST" action="<?= $u('admin/teachers/store') ?>">
        <?= \Core\View::csrf() ?>
        <div class="grid g2">
          <div class="form-group"><label>Full Name *</label><input type="text" name="name" required placeholder="e.g. John Kamau"></div>
          <div class="form-group"><label>Email Address *</label><input type="email" name="email" required placeholder="teacher@school.ac.ke"></div>
          <div class="form-group"><label>Phone</label><input type="text" name="phone" placeholder="+254..."></div>
          <div class="form-group"><label>TSC Number</label><input type="text" name="tsc_no" placeholder="TSC/..."></div>
          <div class="form-group"><label>Employee Number</label><input type="text" name="employee_no"></div>
          <div class="form-group">
            <label>Employment Type *</label>
            <select name="employment_type">
              <option value="permanent">Permanent</option>
              <option value="contract">Contract</option>
              <option value="bom">BOM</option>
            </select>
          </div>
          <div class="form-group"><label>Joining Date</label><input type="date" name="joining_date"></div>
        </div>
        <div class="grid g2">
          <div class="form-group"><label>Password *</label><input type="password" name="password" required minlength="8"></div>
          <div class="form-group"><label>Confirm Password *</label><input type="password" name="password_confirmation" required></div>
        </div>
        <p style="font-size:12px;color:var(--gray);margin-bottom:12px"><i class="bi bi-info-circle"></i> Teacher will use email + password to login to their portal.</p>
        <button type="submit" class="btn btn-primary"><i class="bi bi-person-plus"></i> Add Teacher</button>
      </form>
    </div>
  </div>
</div>
