<?php /* payroll/index.php */ ?>

<div class="d-flex justify-content-between align-items-start mb-3 flex-wrap gap-2">
  <div>
    <h5 class="fw-bold mb-1 d-flex align-items-center gap-2">
      <i class="bi bi-cash-coin text-success fs-4"></i>Payroll Management
    </h5>
    <small class="text-muted">Manage employee salaries, payslips and payroll periods</small>
  </div>
  <div class="d-flex gap-2 flex-wrap">
    <a href="<?= APP_URL ?>/payroll/advances" class="btn btn-sm btn-outline-warning fw-semibold">
      <i class="bi bi-wallet2 me-1"></i>Advances
    </a>
    <a href="<?= APP_URL ?>/payroll/employees" class="btn btn-sm btn-outline-secondary fw-semibold">
      <i class="bi bi-people me-1"></i>Employees
    </a>
    <a href="<?= APP_URL ?>/payroll/report" class="btn btn-sm btn-outline-info fw-semibold">
      <i class="bi bi-bar-chart me-1"></i>Reports
    </a>
  </div>
</div>

<?= flashMessages() ?>

<!-- KPI cards -->
<div class="row g-2 mb-3">
  <div class="col-6 col-md-3">
    <div class="card text-center py-3 border-primary h-100">
      <div class="fw-bold fs-3 text-primary"><?= $stats['active_emp'] ?></div>
      <small class="text-muted">Active Employees</small>
    </div>
  </div>
  <div class="col-6 col-md-3">
    <div class="card text-center py-3 border-success h-100">
      <div class="fw-bold text-success"><?= currency($stats['monthly_payroll']) ?></div>
      <small class="text-muted">Monthly Payroll</small>
    </div>
  </div>
  <div class="col-6 col-md-3">
    <div class="card text-center py-3 h-100">
      <div class="fw-bold fs-3"><?= count($periods) ?></div>
      <small class="text-muted">Total Periods</small>
    </div>
  </div>
  <div class="col-6 col-md-3">
    <div class="card text-center py-3 <?= $currentPeriod ? 'border-'.['draft'=>'secondary','processing'=>'warning','approved'=>'info','paid'=>'success'][$currentPeriod['status']??'draft'] : '' ?> h-100">
      <div class="fw-bold"><?= $currentPeriod ? ucfirst($currentPeriod['status']) : '—' ?></div>
      <small class="text-muted"><?= date('F Y') ?> Status</small>
    </div>
  </div>
</div>

<div class="row g-3">
  <!-- Create Period -->
  <div class="col-lg-4">
    <div class="card h-100">
      <div class="card-header fw-semibold small bg-transparent">
        <i class="bi bi-plus-circle me-1 text-primary"></i>Create Payroll Period
      </div>
      <div class="card-body">
        <?php if($currentPeriod):?>
        <div class="alert alert-info py-2 small mb-3">
          <i class="bi bi-info-circle me-1"></i>
          <?= $currentPeriod['period_name'] ?> period exists —
          <strong><?= ucfirst($currentPeriod['status']) ?></strong>
          <a href="<?= APP_URL ?>/payroll/process/<?= $currentPeriod['id'] ?>"
             class="ms-1 fw-bold">Open →</a>
        </div>
        <?php endif;?>
        <form method="POST" action="<?= APP_URL ?>/payroll/create-period">
          <?= Auth::csrfField() ?>
          <div class="row g-2 mb-3">
            <div class="col-6">
              <label class="form-label small fw-semibold">Month</label>
              <select name="month" class="form-select form-select-sm">
                <?php for($m=1;$m<=12;$m++):?>
                <option value="<?=$m?>" <?=$m==(int)date('n')?'selected':''?>>
                  <?= date('F',mktime(0,0,0,$m,1)) ?>
                </option>
                <?php endfor;?>
              </select>
            </div>
            <div class="col-6">
              <label class="form-label small fw-semibold">Year</label>
              <select name="year" class="form-select form-select-sm">
                <?php for($y=date('Y')+1;$y>=date('Y')-3;$y--):?>
                <option value="<?=$y?>" <?=$y==(int)date('Y')?'selected':''?>><?=$y?></option>
                <?php endfor;?>
              </select>
            </div>
          </div>
          <button type="submit" class="btn btn-primary w-100 fw-semibold">
            <i class="bi bi-plus-circle me-1"></i>Create & Process Payroll
          </button>
        </form>
      </div>
    </div>
  </div>

  <!-- Periods list -->
  <div class="col-lg-8">
    <div class="card">
      <div class="card-header fw-semibold small bg-dark text-white d-flex justify-content-between">
        <span><i class="bi bi-calendar3 me-1"></i>Payroll Periods</span>
        <span class="badge bg-secondary"><?= count($periods) ?></span>
      </div>
      <div class="card-body p-0">
        <table class="table table-sm table-hover mb-0">
          <thead class="table-secondary">
            <tr><th>Period</th><th class="text-center">Employees</th><th class="text-end">Total Net</th><th class="text-center">Status</th><th>Action</th></tr>
          </thead>
          <tbody>
            <?php foreach($periods as $p):
              $badgeCls=['draft'=>'bg-secondary','processing'=>'bg-warning text-dark','approved'=>'bg-info text-dark','paid'=>'bg-success'][$p['status']]??'bg-secondary';
            ?>
            <tr>
              <td class="fw-semibold small"><?= e($p['period_name']) ?></td>
              <td class="text-center small"><?= $p['emp_count'] ?></td>
              <td class="text-end fw-semibold small text-success"><?= currency($p['total_net']) ?></td>
              <td class="text-center"><span class="badge <?= $badgeCls ?>" style="font-size:.62rem"><?= ucfirst($p['status']) ?></span></td>
              <td>
                <a href="<?= APP_URL ?>/payroll/process/<?= $p['id'] ?>"
                   class="btn btn-sm btn-outline-primary py-0 px-2">
                  <i class="bi bi-eye"></i>
                </a>
              </td>
            </tr>
            <?php endforeach;?>
            <?php if(empty($periods)):?><tr><td colspan="5" class="text-center text-muted py-4">No payroll periods yet.</td></tr><?php endif;?>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</div>