<?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'] ?? 'contractor'));
$mode = trim((string) ($_GET['mode'] ?? 'list'));
$validTabs = ['contractor' => 'Contractor', 'custom_field' => 'Custom Field', 'custom_button' => 'Custom Button', 'validation_rules' => 'Validation Rules', 'record_locking' => 'Record Locking', 'related_list' => 'Related List'];
if (!isset($validTabs[$tab])) {
    $tab = 'contractor';
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Employees & Contractors | 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:30px;flex-wrap:wrap;}
    .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;}
    .hero-wrap{min-height:460px;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-title{font-size:22px;font-weight:700;margin-bottom:12px;}
    .hero-copy{max-width:720px;font-size:16px;line-height:1.6;color:#374151;}
    .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;margin-top:24px;}
    .note-panel{max-width:760px;background:#f6f7ff;border:1px solid #e6e9f2;border-radius:18px;padding:28px 36px;margin-top:34px;text-align:left;}
    .note-panel h3{margin:0 0 18px;font-size:18px;}
    .note-panel ul{margin:0;padding-left:20px;display:grid;gap:18px;}
    .note-panel li{font-size:16px;line-height:1.6;}
    .top-tools{display:flex;justify-content:space-between;align-items:center;margin-bottom:18px;}
    .minor-link{color:#2563eb;text-decoration:none;font-size:15px;}
    .empty-art{display:flex;flex-direction:column;align-items:center;justify-content:center;min-height:470px;text-align:center;}
    .empty-art h2{margin:18px 0 10px;font-size:22px;}
    .empty-art 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;}
    .mini-form{max-width:520px;}
    .mini-form label{display:block;font-size:16px;font-weight:600;margin:22px 0 10px;}
    .mini-form input,.mini-form select{width:100%;height:50px;border:1px solid #d7dce7;border-radius:10px;padding:0 14px;font-size:15px;background:#fff;}
    .actions-line{display:flex;justify-content:space-between;align-items:center;border-top:1px solid var(--border);padding-top:22px;margin-top:26px;}
    .btn-group{display:flex;gap:16px;}
  </style>
</head>
<body class="orgprofile-page">
  <?php settings_render_header($_SESSION['org_name'] ?? 'demo', 'dashboard.php', 'fa-id-card'); ?>
  <main class="profile-layout">
    <?php settings_render_sidebar('employees_contractors'); ?>
    <section class="profile-main">
      <div class="profile-head"><h1>Employees & Contractors</h1></div>
      <div class="profile-card">
        <div class="inner-tabs">
          <?php foreach ($validTabs as $tabKey => $label): ?>
            <a class="<?php echo $tabKey === $tab ? 'active' : ''; ?>" href="employees_contractors.php?tab=<?php echo urlencode($tabKey); ?>"><?php echo htmlspecialchars($label); ?></a>
          <?php endforeach; ?>
        </div>
        <?php if ($tab === 'contractor'): ?>
          <div class="hero-wrap">
            <div class="hero-icon"><i class="fa-regular fa-user"></i></div>
            <div class="hero-title">Get your contractors onboard</div>
            <div class="hero-copy">Capture all necessary details about your contractors and manage their compensation in this module.</div>
            <a class="primary-btn" href="#">Enable Contractors Module</a>
            <div class="note-panel">
              <h3>When you enable the contractors module:</h3>
              <ul>
                <li><strong>Roles & Permissions</strong><br>User permissions configured for employees will also apply to contractors, but only those permissions relevant to managing contractors.</li>
                <li><strong>Workflows</strong><br>The existing workflows defined in Automation &gt; Workflow Rules will automatically apply to contractors as well.</li>
                <li><strong>Documents</strong><br>Documents uploaded in the organization folder for all employees will be shown to contractors.</li>
              </ul>
            </div>
          </div>
        <?php elseif ($tab === 'custom_field' && $mode === 'new'): ?>
          <h2 style="font-size:24px;margin:4px 0 24px;">New Custom Field</h2>
          <div class="mini-form">
            <label for="field_name">Field Name<span class="req">*</span></label>
            <input id="field_name" type="text">
            <label for="data_type">Data Type<span class="req">*</span></label>
            <select id="data_type"><option>Select</option><option>Text</option><option>Number</option><option>Date</option><option>Dropdown</option></select>
          </div>
          <div class="actions-line"><div class="btn-group"><a class="save-btn" href="employees_contractors.php?tab=custom_field">Save</a><a class="cancel-btn" href="employees_contractors.php?tab=custom_field">Cancel</a></div><div class="required-copy">* indicates mandatory fields</div></div>
        <?php elseif ($tab === 'custom_field'): ?>
          <div class="top-tools"><div class="minor-link">Custom Fields Usage: 0/59</div><a class="primary-btn" style="margin:0;" href="employees_contractors.php?tab=custom_field&mode=new">Create New</a></div>
          <div class="empty-art"><div class="hero-icon"><i class="fa-regular fa-rectangle-list"></i></div><h2>You haven't created any custom fields yet.</h2><p>Create employee-specific fields whenever you need to track additional information beyond the default profile details.</p><div class="button-row"><a class="primary-btn" style="margin:0;" href="employees_contractors.php?tab=custom_field&mode=new">Create Custom Field</a></div></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" style="margin:0;" href="employees_contractors.php?tab=custom_button">New <i class="fa-solid fa-angle-down" style="margin-left:8px;"></i></a></div>
          <div class="empty-art"><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" style="margin:0;" href="employees_contractors.php?tab=custom_button">+ Add Custom Buttons</a><a class="ghost-btn" href="employees_contractors.php?tab=custom_button">+ Add Custom Links</a></div></div>
        <?php elseif ($tab === 'validation_rules'): ?>
          <div class="top-tools"><div></div><a class="primary-btn" style="margin:0;" href="workflow_rules.php?mode=new">Create New</a></div>
          <div class="empty-art"><div class="hero-icon"><i class="fa-regular fa-file-code"></i></div><h2>You haven't created any Validation Rule yet</h2><p>With Validation Rules, you can automate actions based on the specific conditions and criteria that you set. Once you've set up a Validation Rule, it will trigger an action when the specified criteria are met, saving you time and reducing the risk of errors that can occur when performing manual tasks.</p><div class="button-row"><a class="primary-btn" style="margin:0;" href="workflow_rules.php?mode=new">+ Add Rule</a></div></div>
        <?php elseif ($tab === 'record_locking'): ?>
          <div class="top-tools"><div></div><a class="primary-btn" style="margin:0;" href="employees_contractors.php?tab=record_locking">+ New Lock Configuration</a></div>
          <div class="empty-art"><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" style="margin:0;" href="employees_contractors.php?tab=record_locking">+ New Lock Configuration</a></div></div>
        <?php else: ?>
          <div class="top-tools"><div></div><a class="primary-btn" style="margin:0;" href="employees_contractors.php?tab=related_list">Create New</a></div>
          <div class="empty-art"><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" style="margin:0;" href="employees_contractors.php?tab=related_list">New Related List</a></div></div>
        <?php endif; ?>
      </div>
    </section>
  </main>
</body>
</html>
