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

settings_require_auth();
settings_ensure_tables($conn);
settings_get_current_org($conn);

$mode = trim((string) ($_GET['mode'] ?? 'list'));
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Schedules | 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>
    .top-tools{display:flex;justify-content:flex-end;margin-bottom:18px;}
    .empty-state{min-height:380px;display:flex;flex-direction:column;align-items:center;justify-content:center;text-align:center;}
    .empty-state i{font-size:96px;color:#c7d2fe;margin-bottom:16px;}
    .empty-state h2{margin:0 0 12px;font-size:22px;}
    .empty-state p{max-width:700px;font-size:16px;line-height:1.6;color:#4b5563;}
    .schedule-form label{display:block;font-size:16px;font-weight:600;margin:18px 0 8px;}
    .schedule-form input,.schedule-form select,.schedule-form textarea{width:100%;border:1px solid #d7dce7;border-radius:12px;padding:14px 16px;font-size:15px;}
    .schedule-form textarea{min-height:340px;font-family:Consolas,monospace;white-space:pre-wrap;}
    .grid-two{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:18px;}
    @media (max-width: 860px){.grid-two{grid-template-columns:1fr;}}
  </style>
</head>
<body class="orgprofile-page">
  <?php settings_render_header($_SESSION['org_name'] ?? 'demo', 'dashboard.php', 'fa-clock'); ?>
  <main class="profile-layout">
    <?php settings_render_sidebar('schedules'); ?>
    <section class="profile-main">
      <div class="profile-head"><h1>Schedules</h1></div>
      <div class="profile-card">
        <div class="top-tools"><a class="save-btn" href="schedules.php?mode=new"><i class="fa-solid fa-plus" style="margin-right:8px;"></i>Add New</a></div>
        <?php if ($mode === 'new'): ?>
          <div class="schedule-form">
            <label for="schedule_name">Name</label>
            <input id="schedule_name" type="text">
            <label for="schedule_frequency">Frequency</label>
            <select id="schedule_frequency"><option>Daily</option><option>Weekly</option><option>Monthly</option></select>
            <div class="grid-two">
              <div>
                <label for="schedule_start_date">Start Date</label>
                <input id="schedule_start_date" type="text" value="24/03/2026">
              </div>
              <div>
                <label for="schedule_start_time">Start Time</label>
                <select id="schedule_start_time"><option>10:30</option><option>11:00</option></select>
              </div>
            </div>
            <label for="schedule_expires">Expires After</label>
            <select id="schedule_expires"><option>Never Expires</option></select>
            <label for="schedule_script">Deluge Script</label>
            <textarea id="schedule_script">Void My_Custom_Scheduler(Map organization) {
/*
This is a sample function. Uncomment to execute or make changes to this function.
organizationID = organization.get("organization_id");
*/
}</textarea>
            <div style="display:flex;gap:14px;margin-top:22px;"><a class="save-btn" href="schedules.php">Save</a><a class="save-btn" href="schedules.php" style="background:#7aa7ff;">Save and Execute</a><a class="cancel-btn" href="schedules.php">Cancel</a></div>
          </div>
        <?php else: ?>
          <div class="empty-state">
            <i class="fa-regular fa-clock"></i>
            <h2>You haven't scheduled any tasks yet</h2>
            <p>Automate repetitive payroll tasks with Schedule Tasks. Create predefined tasks using a simple deluge script and schedule them to run at specified time intervals.</p>
            <a class="save-btn" href="schedules.php?mode=new" style="margin-top:22px;">+ Add Schedule</a>
          </div>
        <?php endif; ?>
      </div>
    </section>
  </main>
</body>
</html>
