<?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>Workflow Rules | 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;}
    .page-tip{padding:22px 28px;border:1px solid #f3d8b6;background:#fff8ef;border-radius:20px;margin-bottom:26px;}
    .stats{display:flex;gap:18px;flex-wrap:wrap;font-size:16px;margin-bottom:18px;}
    .stat-pill{padding:12px 16px;border-radius:14px;background:#f8fafc;border:1px solid #e5e7eb;}
    .empty-state{min-height:320px;display:flex;flex-direction:column;align-items:center;justify-content:center;text-align:center;}
    .empty-state h2{margin:0 0 12px;font-size:22px;}
    .empty-state p{max-width:720px;font-size:16px;line-height:1.6;color:#4b5563;}
    .modal-card{max-width:720px;padding:28px;border:1px solid var(--border);border-radius:20px;background:#fff;}
    .modal-card label{display:block;font-size:16px;font-weight:600;margin:18px 0 8px;}
    .modal-card input,.modal-card textarea,.modal-card select{width:100%;border:1px solid #d7dce7;border-radius:12px;padding:14px 16px;font-size:15px;}
    .modal-card textarea{min-height:92px;resize:vertical;}
    .modal-actions{display:flex;gap:14px;margin-top:22px;}
  </style>
</head>
<body class="orgprofile-page">
  <?php settings_render_header($_SESSION['org_name'] ?? 'demo', 'dashboard.php', 'fa-bolt'); ?>
  <main class="profile-layout">
    <?php settings_render_sidebar('workflow_rules'); ?>
    <section class="profile-main">
      <div class="profile-head"><h1>Workflow Rules</h1></div>
      <div class="profile-card">
        <div class="top-tools"><a class="save-btn" href="workflow_rules.php?mode=new"><i class="fa-solid fa-plus" style="margin-right:8px;"></i>Add New</a></div>
        <?php if ($mode === 'new'): ?>
          <div class="modal-card">
            <h2 style="margin:0;">New Workflow Rule</h2>
            <label for="rule_name">Workflow Rule Name</label>
            <input id="rule_name" type="text">
            <label for="rule_description">Description</label>
            <textarea id="rule_description"></textarea>
            <label for="rule_module">Module</label>
            <select id="rule_module"><option>Select</option><option>Employee</option><option>Pay Runs</option><option>Loan</option><option>Reimbursement Claim</option></select>
            <div class="modal-actions">
              <a class="save-btn" href="workflow_rules.php">Next</a>
              <a class="cancel-btn" href="workflow_rules.php">Cancel</a>
            </div>
          </div>
        <?php else: ?>
          <div class="page-tip">
            <div style="font-size:13px;font-weight:700;letter-spacing:.12em;color:#f97316;margin-bottom:10px;">PAGE TIPS</div>
            <div style="font-size:18px;font-weight:700;margin-bottom:8px;">Automate actions and processes</div>
            <div style="font-size:16px;color:#4b5563;">With Workflow Rules, you can increase efficiency and productivity by freeing up time that would otherwise be spent on manual tasks.</div>
          </div>
          <div class="stats">
            <div class="stat-pill">Custom Functions: <strong>0 / 1000</strong></div>
            <div class="stat-pill">Webhooks: <strong>0 / 1000</strong></div>
            <div class="stat-pill">Email Alerts: <strong>0 / 500</strong></div>
          </div>
          <div class="empty-state">
            <h2>You haven't created any Workflow Rules yet</h2>
            <p>With Workflow Rules, you can automate actions based on the specific conditions and criteria that you set. Once you've set up a Workflow Rule, it will trigger an action when the specified criteria are met.</p>
            <a class="save-btn" href="workflow_rules.php?mode=new" style="margin-top:22px;">+ Add Workflow Rule</a>
          </div>
        <?php endif; ?>
      </div>
    </section>
  </main>
</body>
</html>
