<?php
require_once __DIR__ . '/_nav.php';
$pageTitle  = 'Class Fee Status';
$sidebarNav = teacherNav();
$u = function($p) { return \Core\View::url($p); };
?>

<div class="page-header">
  <div><h1><i class="bi bi-cash-coin"></i> Class Fee Status</h1><p>View and manage fee payments for your class</p></div>
  <?php if(!empty($invoices)): ?>
  <div style="display:flex;gap:8px">
    <button onclick="openModal('reminderModal')" class="btn btn-primary"><i class="bi bi-send"></i> Send Reminders</button>
    <a href="?stream_id=<?= $streamId ?>&term_id=<?= $termId ?>&print=1" target="_blank" class="btn btn-secondary"><i class="bi bi-printer"></i> Print</a>
  </div>
  <?php endif; ?>
</div>

<?php if(empty($myStreams)): ?>
<div class="card"><div class="card-body" style="text-align:center;padding:60px;color:var(--gray)">
  <i class="bi bi-cash-coin" style="font-size:48px;display:block;margin-bottom:12px"></i>
  <h3 style="margin-bottom:8px">Not assigned as class teacher</h3>
  <p>Ask your admin to assign you as a class teacher to access fee information.</p>
</div></div>
<?php else: ?>

<!-- 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:flex-end">
      <?php if(count($myStreams) > 1): ?>
      <div class="form-group" style="margin:0">
        <label style="font-size:12px">Stream</label>
        <select name="stream_id" style="width:150px">
          <?php foreach($myStreams as $ms): ?>
          <option value="<?= $ms['id'] ?>" <?= $streamId==$ms['id']?'selected':'' ?>><?= htmlspecialchars($ms['full_name']) ?></option>
          <?php endforeach; ?>
        </select>
      </div>
      <?php else: ?>
      <input type="hidden" name="stream_id" value="<?= $streamId ?>">
      <?php endif; ?>
      <div class="form-group" style="margin:0">
        <label style="font-size:12px">Term</label>
        <select name="term_id" style="width:170px">
          <option value="">All Terms</option>
          <?php foreach($terms as $t): ?>
          <option value="<?= $t['id'] ?>" <?= $termId==$t['id']?'selected':'' ?>><?= htmlspecialchars($t['year_name'].' '.$t['name']) ?></option>
          <?php endforeach; ?>
        </select>
      </div>
      <div class="form-group" style="margin:0">
        <label style="font-size:12px">Status</label>
        <select name="status" style="width:120px">
          <option value="">All</option>
          <option value="unpaid"  <?= ($filters['status']??'')==='unpaid' ?'selected':'' ?>>Unpaid</option>
          <option value="partial" <?= ($filters['status']??'')==='partial'?'selected':'' ?>>Partial</option>
          <option value="paid"    <?= ($filters['status']??'')==='paid'   ?'selected':'' ?>>Paid</option>
        </select>
      </div>
      <button type="submit" class="btn btn-primary btn-sm"><i class="bi bi-search"></i> Filter</button>
    </form>
  </div>
</div>

<!-- Summary Cards -->
<?php if(!empty($summary)): ?>
<div class="grid g4" style="margin-bottom:20px">
  <div class="stat-card" style="">
    <div class="stat-icon si-blue"><i class="bi bi-cash-stack"></i></div>
    <div class="stat-info"><h3>KES <?= number_format($summary['expected']) ?></h3><p>Expected</p></div>
  </div>
  <div class="stat-card" style="">
    <div class="stat-icon si-green"><i class="bi bi-check-circle"></i></div>
    <div class="stat-info"><h3>KES <?= number_format($summary['collected']) ?></h3><p>Collected (<?= $summary['rate'] ?>%)</p></div>
  </div>
  <div class="stat-card" style="">
    <div class="stat-icon si-red"><i class="bi bi-exclamation-circle"></i></div>
    <div class="stat-info"><h3>KES <?= number_format($summary['outstanding']) ?></h3><p>Outstanding</p></div>
  </div>
  <div class="stat-card" style="">
    <div class="stat-icon si-amber"><i class="bi bi-people"></i></div>
    <div class="stat-info">
      <h3><?= $summary['total'] ?></h3>
      <p>
        <span style="color:var(--success)"><?= $summary['paid'] ?> paid</span> &bull;
        <span style="color:var(--warning)"><?= $summary['partial'] ?> partial</span> &bull;
        <span style="color:var(--danger)"><?= $summary['unpaid'] ?> unpaid</span>
      </p>
    </div>
  </div>
</div>

<!-- Collection rate bar -->
<div class="card" style="margin-bottom:20px">
  <div class="card-body" style="padding:14px 18px">
    <div style="display:flex;justify-content:space-between;font-size:13px;margin-bottom:6px">
      <span style="font-weight:600">Collection Rate</span>
      <span style="font-weight:700;color:<?= $summary['rate']>=80?'var(--success)':($summary['rate']>=50?'var(--warning)':'var(--danger)') ?>"><?= $summary['rate'] ?>%</span>
    </div>
    <div style="background:var(--light);border-radius:8px;height:10px">
      <div style="width:<?= min(100,$summary['rate']) ?>%;background:<?= $summary['rate']>=80?'var(--success)':($summary['rate']>=50?'var(--warning)':'var(--danger)') ?>;height:10px;border-radius:8px;transition:width .5s"></div>
    </div>
  </div>
</div>
<?php endif; ?>

<!-- Invoice Table -->
<div class="card">
  <div class="card-header">
    <h5><i class="bi bi-receipt"></i> Fee Invoices — <?php foreach($myStreams as $ms): if($ms['id']==$streamId): ?><?= htmlspecialchars($ms['full_name']) ?><?php endif; endforeach; ?></h5>
    <span style="font-size:12px;color:var(--gray)"><?= count($invoices) ?> students</span>
  </div>
  <div class="table-wrap"><table>
    <thead>
      <tr>
        <th>#</th>
        <th>Student</th>
        <th>Parent / Phone</th>
        <th>Invoice</th>
        <th>Paid</th>
        <th>Balance</th>
        <th>Status</th>
      </tr>
    </thead>
    <tbody>
      <?php foreach($invoices as $i => $inv):
        $bal = (float)$inv['outstanding']; ?>
      <tr>
        <td style="color:var(--gray);font-size:12px"><?= $i+1 ?></td>
        <td>
          <div style="font-weight:500"><?= htmlspecialchars($inv['student_name']) ?></div>
          <div style="font-size:11px;color:var(--gray)"><?= htmlspecialchars($inv['assessment_no']) ?></div>
        </td>
        <td style="font-size:12px">
          <?php if($inv['parent_name']): ?>
          <div><?= htmlspecialchars($inv['parent_name']) ?></div>
          <div style="color:var(--gray)"><?= htmlspecialchars($inv['parent_phone'] ?? '—') ?></div>
          <?php else: ?>
          <span style="color:var(--gray)">—</span>
          <?php endif; ?>
        </td>
        <td>KES <?= number_format($inv['total_amount'],2) ?></td>
        <td style="color:var(--success);font-weight:500">KES <?= number_format($inv['amount_paid'],2) ?></td>
        <td style="font-weight:600;color:<?= $bal>0?'var(--danger)':'var(--success)' ?>">KES <?= number_format($bal,2) ?></td>
        <td>
          <span class="badge <?= $inv['status']==='paid'?'badge-success':($inv['status']==='partial'?'badge-warning':'badge-danger') ?>">
            <?= ucfirst($inv['status']) ?>
          </span>
        </td>
      </tr>
      <?php endforeach; ?>
      <?php if(empty($invoices)): ?>
      <tr><td colspan="7" style="text-align:center;padding:40px;color:var(--gray)">
        <i class="bi bi-inbox" style="font-size:32px;display:block;margin-bottom:8px"></i>
        No invoices found
      </td></tr>
      <?php endif; ?>
    </tbody>
  </table></div>
</div>

<!-- Send Reminder Modal -->
<div class="modal" id="reminderModal"><div class="modal-box" style="max-width:500px">
  <div class="card-header">
    <h5><i class="bi bi-send"></i> Send Fee Reminders</h5>
    <button onclick="closeModal('reminderModal')" style="background:none;border:none;cursor:pointer;font-size:20px">&times;</button>
  </div>
  <div class="card-body">
    <form method="POST" action="<?= $u('teacher/class-fees/reminder') ?>">
      <?= \Core\View::csrf() ?>
      <input type="hidden" name="stream_id" value="<?= $streamId ?>">
      <input type="hidden" name="term_id" value="<?= $termId ?>">

      <div style="background:var(--light);border-radius:8px;padding:10px 14px;margin-bottom:14px;font-size:13px">
        <i class="bi bi-info-circle" style="color:var(--primary)"></i>
        SMS will be sent to parents of <strong><?= $summary['unpaid'] + $summary['partial'] ?></strong> students with outstanding fees.
        Use <code>{student}</code>, <code>{amount}</code>, <code>{stream}</code> as placeholders.
      </div>

      <div class="form-group">
        <label>Message *</label>
        <textarea name="message" rows="4" required maxlength="320"
          placeholder="Dear parent, kindly note that {student} from {stream} has an outstanding fee balance of {amount}. Please clear at your earliest convenience. Thank you."
        >Dear parent, kindly note that {student} from {stream} has an outstanding fee balance of {amount}. Please clear at your earliest convenience. Thank you.</textarea>
        <small style="color:var(--gray)">Max 320 characters. Placeholders: {student}, {amount}, {stream}</small>
      </div>

      <div style="display:flex;gap:8px">
        <button type="submit" class="btn btn-primary"><i class="bi bi-send"></i> Send SMS Reminders</button>
        <button type="button" onclick="closeModal('reminderModal')" class="btn btn-secondary">Cancel</button>
      </div>
    </form>
  </div>
</div></div>

<?php endif; ?>
