<?php
require_once __DIR__ . '/includes/settings_helpers.php';
settings_require_auth();
settings_ensure_tables($conn);
settings_get_current_org($conn);
$tab = trim((string) ($_GET['tab'] ?? 'approvals'));
$validTabs = ['approvals' => 'Approvals', 'custom_button' => 'Custom Button', 'record_locking' => 'Record Locking', 'related_list' => 'Related List'];
if (!isset($validTabs[$tab])) {
    $tab = 'approvals';
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Pay Runs | 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>
    .inner-tabs{display:flex;gap:36px;border-bottom:1px solid var(--border);margin-bottom:28px;}
    .inner-tabs a{padding:0 0 14px;color:#2563eb;text-decoration:none;font-size:16px;}
    .inner-tabs a.active{color:#111827;border-bottom:3px solid #3b82f6;font-weight:600;}
    .approval-grid{display:grid;grid-template-columns:repeat(3,minmax(0,1fr));gap:24px;max-width:980px;}
    .approval-card{border:1px solid #dfe4ec;border-radius:16px;padding:18px 18px 22px;background:#fff;min-height:214px;}
    .approval-card label{display:flex;gap:10px;align-items:flex-start;font-size:17px;font-weight:600;}
    .approval-card input{width:18px;height:18px;accent-color:#3b82f6;margin-top:3px;}
    .approval-card p{margin:16px 0 0 27px;font-size:15px;line-height:1.75;color:#374151;max-width:220px;}
    .save-row{border-top:1px solid var(--border);margin-top:26px;padding-top:24px;}
    .top-tools{display:flex;justify-content:space-between;align-items:center;margin-bottom:18px;}
    .minor-link{color:#2563eb;text-decoration:none;font-size:15px;}
    .hero-wrap{min-height:470px;display:flex;flex-direction:column;align-items:center;justify-content:center;text-align:center;}
    .hero-icon{font-size:110px;color:#c7d2fe;line-height:1;margin-bottom:20px;}
    .hero-wrap h2{margin:18px 0 10px;font-size:22px;}
    .hero-wrap p{max-width:660px;font-size:16px;line-height:1.6;color:#374151;}
    .button-row{display:flex;gap:18px;justify-content:center;margin-top:24px;flex-wrap:wrap;}
    .ghost-btn{display:inline-flex;align-items:center;justify-content:center;background:#eef2ff;color:#3b82f6;text-decoration:none;border-radius:10px;padding:12px 18px;font-weight:600;}
    .primary-btn{display:inline-flex;align-items:center;justify-content:center;background:#3b82f6;color:#fff;text-decoration:none;border:none;border-radius:10px;padding:12px 18px;font-weight:600;}
  </style>
</head>
<body class="orgprofile-page">
  <?php settings_render_header($_SESSION['org_name'] ?? 'demo', 'dashboard.php', 'fa-money-bill-wave'); ?>
  <main class="profile-layout">
    <?php settings_render_sidebar('payrun_settings'); ?>
    <section class="profile-main">
      <div class="profile-head"><h1>Pay Runs</h1></div>
      <div class="profile-card">
        <div class="inner-tabs">
          <?php foreach ($validTabs as $tabKey => $label): ?>
            <a class="<?php echo $tabKey === $tab ? 'active' : ''; ?>" href="payrun_settings.php?tab=<?php echo urlencode($tabKey); ?>"><?php echo htmlspecialchars($label); ?></a>
          <?php endforeach; ?>
        </div>
        <?php if ($tab === 'approvals'): ?>
          <h2 style="font-size:18px;margin:0 0 22px;">Select Approval Workflow</h2>
          <div class="approval-grid">
            <div class="approval-card"><label><input type="radio" name="approval_mode"> <span>Simple Approval</span></label><p>In this approval flow, any approver with Pay Run approval permission can approve.</p></div>
            <div class="approval-card"><label><input type="radio" name="approval_mode"> <span>Multi-Level Approval</span></label><p>Set many levels of approval. The pay run will be approved only when all the approvers approve.</p></div>
            <div class="approval-card"><label><input type="radio" name="approval_mode"> <span>Custom Approval</span></label><p>You can set up an approval flow based on one or more criteria. These criteria can be chosen by you.</p></div>
          </div>
          <div class="save-row"><a class="save-btn" href="payrun_settings.php?tab=approvals">Save</a></div>
        <?php elseif ($tab === 'custom_button'): ?>
          <div class="top-tools"><div><a class="minor-link" href="workflow_logs.php?tab=custom_button"><i class="fa-regular fa-clock"></i> View logs</a></div><a class="primary-btn" href="payrun_settings.php?tab=custom_button">New <i class="fa-solid fa-angle-down" style="margin-left:8px;"></i></a></div>
          <div class="hero-wrap"><div class="hero-icon"><i class="fa-regular fa-hand-pointer"></i></div><h2>You haven't created any Customisations yet</h2><p>Boost efficiency with custom buttons and custom links, enabling users to trigger specific actions unique to your organization, or quickly navigate to external sites.</p><div class="button-row"><a class="primary-btn" href="payrun_settings.php?tab=custom_button">+ Add Custom Buttons</a><a class="ghost-btn" href="payrun_settings.php?tab=custom_button">+ Add Custom Links</a></div></div>
        <?php elseif ($tab === 'record_locking'): ?>
          <div class="top-tools"><div></div><a class="primary-btn" href="payrun_settings.php?tab=record_locking">+ New Lock Configuration</a></div>
          <div class="hero-wrap"><div class="hero-icon"><i class="fa-regular fa-file-lines"></i></div><h2>You haven't created any Record Locking yet</h2><p>Record Locking helps you control updates to records. You can specify which actions and field updates to allow or restrict after records are locked, and choose who can perform these actions. This is useful for protecting important information and preventing accidental changes.</p><div class="button-row"><a class="primary-btn" href="payrun_settings.php?tab=record_locking">+ New Lock Configuration</a></div></div>
        <?php else: ?>
          <div class="top-tools"><div></div><a class="primary-btn" href="payrun_settings.php?tab=related_list">Create New</a></div>
          <div class="hero-wrap"><div class="hero-icon"><i class="fa-regular fa-rectangle-list"></i></div><h2>You haven't created any related list yet.</h2><div class="button-row"><a class="primary-btn" href="payrun_settings.php?tab=related_list">New Related List</a></div></div>
        <?php endif; ?>
      </div>
    </section>
  </main>
</body>
</html>
