<?php

require_once __DIR__ . '/includes/settings_helpers.php';

settings_require_auth();
settings_ensure_tables($conn);

$org = settings_get_current_org($conn);
$orgId = (int) ($org['id'] ?? 0);
$schedule = settings_get_pay_schedule($conn, $orgId);
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
<base href="<?php echo billing_escape(billing_base_href()); ?>">
  <title>Pay Schedule | Settings</title>
  <link rel="stylesheet" href="assets/fontawesome/css/all.min.css">
  <link rel="stylesheet" href="assets/font-inter.css">
  <link rel="stylesheet" href="orgprofile.css">
  <style>
    .note-banner{display:flex;align-items:center;gap:12px;background:#fff8e1;border:1px solid #fde7a9;border-radius:12px;padding:14px 18px;color:#111827;margin-bottom:18px;}
    .schedule-card{display:grid;grid-template-columns:1.1fr .9fr;gap:28px;border:1px solid var(--border);border-radius:16px;background:#fff;padding:26px 30px;box-shadow:0 10px 22px rgba(15,23,42,.05);}
    .schedule-title{font-size:18px;font-weight:700;margin:0 0 24px;}
    .schedule-meta{display:grid;grid-template-columns:190px 1fr;gap:22px 28px;}
    .schedule-meta .label{color:#6b7280;font-size:15px;}
    .schedule-meta .value{font-size:16px;color:#111827;}
    .upcoming{padding-left:32px;border-left:1px solid var(--border);}
    .upcoming h3{margin:8px 0 22px;font-size:18px;}
    .upcoming-item{position:relative;padding-left:22px;margin-bottom:22px;}
    .upcoming-item::before{content:"";position:absolute;left:0;top:8px;width:10px;height:10px;border-radius:50%;background:#d9dbf5;}
    .upcoming-month{font-size:18px;font-weight:700;margin-bottom:6px;}
    .upcoming-date{font-size:15px;color:#374151;}
  </style>
</head>
<body class="orgprofile-page">
  <?php settings_render_header($_SESSION['org_name'] ?? 'demo', 'dashboard.php', 'fa-calendar-days'); ?>

  <main class="profile-layout">
    <?php settings_render_sidebar('pay_schedule'); ?>

    <section class="profile-main">
      <div class="profile-head">
        <h1>Pay Schedule</h1>
      </div>

      <div class="profile-card">
        <div class="note-banner">
          <span style="font-size:24px;color:#f59e0b;"><i class="fa-regular fa-lightbulb"></i></span>
          <div><strong>Note:</strong> Pay Schedule cannot be edited once you process the first pay run.</div>
        </div>

        <div class="schedule-card">
          <div>
            <div class="schedule-title">This Organisation's payroll runs on this schedule. <span style="margin-left:10px;color:#94a3b8;"><i class="fa-regular fa-pen-to-square"></i></span></div>
            <div class="schedule-meta">
              <div class="label">Pay Frequency</div>
              <div class="value"><?php echo htmlspecialchars((string) $schedule['pay_frequency']); ?></div>

              <div class="label">Working Days</div>
              <div class="value"><?php echo htmlspecialchars(implode(', ', $schedule['working_days'])); ?></div>

              <div class="label">Pay Day</div>
              <div class="value"><?php echo htmlspecialchars((string) $schedule['pay_day_label']); ?></div>

              <div class="label">First Pay Period</div>
              <div class="value"><?php echo htmlspecialchars((string) $schedule['first_pay_period']); ?></div>

              <div class="label">First Pay Date</div>
              <div class="value"><?php echo htmlspecialchars(date('d/m/Y', strtotime((string) $schedule['first_pay_date']))); ?></div>

              <div class="label">No. of Working Days</div>
              <div class="value"><?php echo (int) $schedule['working_days_count']; ?></div>
            </div>
          </div>

          <div class="upcoming">
            <h3>Upcoming Payrolls</h3>
            <?php foreach ($schedule['upcoming_payrolls'] as $item): ?>
              <div class="upcoming-item">
                <div class="upcoming-month"><?php echo htmlspecialchars((string) ($item['label'] ?? '')); ?></div>
                <div class="upcoming-date">Pay Date : <?php echo htmlspecialchars(date('d/m/Y', strtotime((string) ($item['pay_date'] ?? 'now')))); ?></div>
              </div>
            <?php endforeach; ?>
          </div>
        </div>
      </div>
    </section>
  </main>
</body>
</html>

