<?php
require_once __DIR__ . '/../../config/config.php';
require_once __DIR__ . '/../../includes/functions.php';
startSession(); requireLogin();
$db   = Database::getInstance();
$page = 'Stat Detail';

$type = get('type');
[$empOrgClauseE, $empOrgParamsE] = orgScopeClause($db, 'e.company_id');

$titles = [
    'active'           => 'Active Employees',
    'inactive'         => 'Inactive Employees',
    'joined_12m'       => 'Employees Joined — Last 12 Months',
    'separated_12m'    => 'Employees Separated — Last 12 Months',
    'present_today'    => 'Employees Present Today',
    'on_leave_today'   => 'Employees On Leave Today',
];
if (!isset($titles[$type])) { setFlash('error', 'Unknown report type.'); header('Location: ' . BASE_URL . '/dashboard.php'); exit; }
$title = $titles[$type];

$baseSelect = "SELECT e.id, e.employee_id, e.first_name, e.last_name, e.mobile, e.date_of_joining,
                      e.status, d.name AS dept, des.name AS designation
                 FROM employees e
            LEFT JOIN departments d ON d.id=e.department_id
            LEFT JOIN designations des ON des.id=e.designation_id";

switch ($type) {
    case 'active':
        $rows = $db->fetchAll("$baseSelect WHERE e.status='active' AND $empOrgClauseE ORDER BY e.first_name", $empOrgParamsE);
        break;

    case 'inactive':
        $rows = $db->fetchAll("$baseSelect WHERE e.status IN ('inactive','suspended','on_leave') AND $empOrgClauseE ORDER BY e.first_name", $empOrgParamsE);
        break;

    case 'joined_12m':
        $rows = $db->fetchAll(
            "$baseSelect WHERE e.date_of_joining >= DATE_SUB(CURDATE(), INTERVAL 12 MONTH) AND $empOrgClauseE ORDER BY e.date_of_joining DESC",
            $empOrgParamsE
        );
        break;

    case 'separated_12m':
        $rows = $db->fetchAll(
            "SELECT e.id, e.employee_id, e.first_name, e.last_name, e.mobile, e.date_of_joining,
                    e.status, d.name AS dept, des.name AS designation, s.last_working_date
               FROM separations s
               JOIN employees e ON e.id=s.employee_id
          LEFT JOIN departments d ON d.id=e.department_id
          LEFT JOIN designations des ON des.id=e.designation_id
              WHERE s.last_working_date >= DATE_SUB(CURDATE(), INTERVAL 12 MONTH) AND $empOrgClauseE
              ORDER BY s.last_working_date DESC",
            $empOrgParamsE
        );
        break;

    case 'present_today':
        $rows = $db->fetchAll(
            "SELECT e.id, e.employee_id, e.first_name, e.last_name, e.mobile, e.date_of_joining,
                    e.status, d.name AS dept, des.name AS designation, m.in_time, m.out_time
               FROM muster m
               JOIN employees e ON e.id=m.employee_id
          LEFT JOIN departments d ON d.id=e.department_id
          LEFT JOIN designations des ON des.id=e.designation_id
              WHERE m.muster_date=CURDATE() AND m.status='P' AND $empOrgClauseE
              ORDER BY e.first_name",
            $empOrgParamsE
        );
        break;

    case 'on_leave_today':
        $rows = $db->fetchAll(
            "SELECT e.id, e.employee_id, e.first_name, e.last_name, e.mobile, e.date_of_joining,
                    e.status, d.name AS dept, des.name AS designation
               FROM muster m
               JOIN employees e ON e.id=m.employee_id
          LEFT JOIN departments d ON d.id=e.department_id
          LEFT JOIN designations des ON des.id=e.designation_id
              WHERE m.muster_date=CURDATE() AND m.status='L' AND $empOrgClauseE
              ORDER BY e.first_name",
            $empOrgParamsE
        );
        break;

    default:
        $rows = [];
}

include __DIR__ . '/../../includes/header.php';
?>
<div class="pc-card">
  <div class="pc-card-title d-flex justify-content-between align-items-center">
    <span><?= clean($title) ?> (<?= count($rows) ?>)</span>
    <a href="<?= BASE_URL ?>/dashboard.php" class="small"><i class="ti ti-arrow-left me-1"></i>Back to Dashboard</a>
  </div>
  <div class="pc-table-wrap">
    <table class="pc-table">
      <thead>
        <tr>
          <th>Employee</th><th>Department</th><th>Designation</th><th>Mobile</th>
          <?php if ($type === 'joined_12m'): ?><th>Date of Joining</th><?php endif; ?>
          <?php if ($type === 'separated_12m'): ?><th>Last Working Date</th><?php endif; ?>
          <?php if ($type === 'present_today'): ?><th>In</th><th>Out</th><?php endif; ?>
          <th>Status</th>
        </tr>
      </thead>
      <tbody>
      <?php foreach ($rows as $r): ?>
        <tr>
          <td>
            <a href="<?= BASE_URL ?>/modules/employees/view.php?id=<?= $r['id'] ?>" class="fw-semibold text-decoration-none"><?= clean($r['first_name'].' '.$r['last_name']) ?></a>
            <div style="font-size:10px;color:var(--pc-text2)"><?= clean($r['employee_id']) ?></div>
          </td>
          <td><?= clean($r['dept'] ?? '—') ?></td>
          <td><?= clean($r['designation'] ?? '—') ?></td>
          <td><?= clean($r['mobile'] ?? '—') ?></td>
          <?php if ($type === 'joined_12m'): ?><td><?= fmtDate($r['date_of_joining']) ?></td><?php endif; ?>
          <?php if ($type === 'separated_12m'): ?><td><?= fmtDate($r['last_working_date']) ?></td><?php endif; ?>
          <?php if ($type === 'present_today'): ?>
            <td><?= clean($r['in_time'] ?? '—') ?></td>
            <td><?= clean($r['out_time'] ?? '—') ?></td>
          <?php endif; ?>
          <td><?= statusBadge($r['status']) ?></td>
        </tr>
      <?php endforeach; ?>
      <?php if (!$rows): ?>
        <tr><td colspan="8" class="text-center text-muted py-4">No records found</td></tr>
      <?php endif; ?>
      </tbody>
    </table>
  </div>
</div>
<?php include __DIR__ . '/../../includes/footer.php'; ?>