<?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);
$component = trim((string) ($_GET['component'] ?? 'epf'));
$mode = trim((string) ($_GET['mode'] ?? 'view'));
$validComponents = ['epf', 'esi', 'pt', 'lwf', 'bonus'];
if (!in_array($component, $validComponents, true)) {
    $component = 'epf';
}

$tabLabels = [
    'epf' => 'EPF',
    'esi' => 'ESI',
    'pt' => 'Professional Tax',
    'lwf' => 'Labour Welfare Fund',
    'bonus' => 'Statutory Bonus',
];

$config = settings_get_statutory_component($conn, $orgId, $component);
$isEnabled = !empty($config['enabled']);
$errors = [];
$handledToggle = false;

if (isset($_GET['action'], $_GET['component']) && $_GET['action'] === 'toggle-enabled') {
    $targetComponent = trim((string) $_GET['component']);
    if (in_array($targetComponent, $validComponents, true)) {
        $toggleConfig = settings_get_statutory_component($conn, $orgId, $targetComponent);
        $toggleConfig['enabled'] = !empty($toggleConfig['enabled']) ? false : true;
        if ($targetComponent === 'epf') {
            $toggleConfig['button_label'] = !empty($toggleConfig['enabled']) ? 'Update EPF' : 'Enable EPF';
        } elseif ($targetComponent === 'esi') {
            $toggleConfig['button_label'] = !empty($toggleConfig['enabled']) ? 'Update ESI' : 'Enable ESI';
        } elseif ($targetComponent === 'bonus') {
            $toggleConfig['button_label'] = !empty($toggleConfig['enabled']) ? 'Update Statutory Bonus' : 'Enable Statutory Bonus';
        } elseif ($targetComponent === 'lwf') {
            $toggleConfig['button_label'] = !empty($toggleConfig['enabled']) ? 'Disable' : 'Enable';
        }
        settings_save_statutory_component($conn, $orgId, $targetComponent, $toggleConfig);
        header('Location: statutory_components.php?component=' . urlencode($targetComponent) . '&mode=view&toggled=1');
        exit();
    }
}

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['toggle_enabled'])) {
    $targetComponent = trim((string) ($_POST['toggle_component'] ?? $component));
    if (!in_array($targetComponent, $validComponents, true)) {
        $targetComponent = 'epf';
    }

    $toggleConfig = settings_get_statutory_component($conn, $orgId, $targetComponent);
    $toggleConfig['enabled'] = !empty($toggleConfig['enabled']) ? false : true;
    if ($targetComponent === 'epf') {
        $toggleConfig['button_label'] = !empty($toggleConfig['enabled']) ? 'Update EPF' : 'Enable EPF';
    } elseif ($targetComponent === 'esi') {
        $toggleConfig['button_label'] = !empty($toggleConfig['enabled']) ? 'Update ESI' : 'Enable ESI';
    } elseif ($targetComponent === 'bonus') {
        $toggleConfig['button_label'] = !empty($toggleConfig['enabled']) ? 'Update Statutory Bonus' : 'Enable Statutory Bonus';
    } elseif ($targetComponent === 'lwf') {
        $toggleConfig['button_label'] = !empty($toggleConfig['enabled']) ? 'Disable' : 'Enable';
    }
    settings_save_statutory_component($conn, $orgId, $targetComponent, $toggleConfig);
    header('Location: statutory_components.php?component=' . urlencode($targetComponent) . '&mode=view&toggled=1');
    exit();
}

if (!$handledToggle && $_SERVER['REQUEST_METHOD'] === 'POST') {
    if ($component === 'epf') {
        $config['enabled'] = true;
        $config['form']['epf_number'] = trim((string) ($_POST['epf_number'] ?? ''));
        $config['form']['deduction_cycle'] = trim((string) ($_POST['deduction_cycle'] ?? 'Monthly'));
        $config['form']['employee_contribution_rate'] = trim((string) ($_POST['employee_contribution_rate'] ?? ''));
        $config['form']['employer_contribution_rate'] = trim((string) ($_POST['employer_contribution_rate'] ?? ''));
        $config['form']['include_employer_contribution'] = isset($_POST['include_employer_contribution']);
        $config['form']['include_edli'] = isset($_POST['include_edli']);
        $config['form']['include_admin_charges'] = isset($_POST['include_admin_charges']);
        $config['form']['override_employee_level'] = isset($_POST['override_employee_level']);
        $config['form']['prorate_restricted_pf_wage'] = isset($_POST['prorate_restricted_pf_wage']);
        $config['form']['consider_salary_components'] = isset($_POST['consider_salary_components']);
    } elseif ($component === 'esi') {
        $config['enabled'] = true;
        $config['form']['esi_number'] = trim((string) ($_POST['esi_number'] ?? ''));
        $config['form']['deduction_cycle'] = trim((string) ($_POST['deduction_cycle'] ?? 'Monthly'));
        $config['form']['employee_contribution'] = trim((string) ($_POST['employee_contribution'] ?? ''));
        $config['form']['employer_contribution'] = trim((string) ($_POST['employer_contribution'] ?? ''));
        $config['form']['include_employer_contribution'] = isset($_POST['include_employer_contribution']);
    } elseif ($component === 'bonus') {
        $config['enabled'] = true;
        $config['form']['payment_frequency'] = trim((string) ($_POST['payment_frequency'] ?? 'Monthly'));
        $config['form']['bonus_percentage'] = trim((string) ($_POST['bonus_percentage'] ?? ''));
        $config['form']['state'] = trim((string) ($_POST['state'] ?? 'Tamil Nadu'));
    }

    if (settings_save_statutory_component($conn, $orgId, $component, $config)) {
        $redirectMode = in_array($component, ['epf', 'esi', 'bonus'], true) ? 'view' : $mode;
        header('Location: statutory_components.php?component=' . urlencode($component) . '&mode=' . urlencode($redirectMode));
        exit();
    }

    $errors[] = 'Unable to save this statutory component.';
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Statutory Components | 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>
    .tabs{display:flex;gap:34px;border-bottom:1px solid var(--border);margin:10px 0 24px;}
    .tab-link{padding:0 0 14px;color:#2563eb;text-decoration:none;font-size:16px;}
    .tab-link.active{color:#111827;border-bottom:3px solid #3b82f6;}
    .empty-state{display:flex;flex-direction:column;align-items:center;justify-content:center;min-height:520px;text-align:center;padding:24px;}
    .empty-ill{width:200px;height:160px;border-radius:32px;background:linear-gradient(135deg,#dbeafe,#eef2ff);margin-bottom:26px;display:flex;align-items:center;justify-content:center;color:#6366f1;font-size:76px;}
    .empty-title{font-size:24px;font-weight:700;max-width:720px;}
    .empty-copy{max-width:720px;margin-top:14px;font-size:17px;line-height:1.6;color:#111827;}
    .empty-action{margin-top:28px;}
    .edit-layout{display:grid;grid-template-columns:minmax(0,1fr) 520px;gap:28px;align-items:start;}
    .field-line{margin-top:22px;}
    .field-line label{display:block;font-size:16px;font-weight:600;margin-bottom:10px;}
    .input-row{display:grid;grid-template-columns:1fr 280px;gap:18px;align-items:end;}
    .hint-copy{font-size:13px;color:#6b7280;margin-top:8px;}
    .checkbox-stack{display:grid;gap:14px;margin-top:16px;}
    .checkbox-stack label,
    .check-item{
      display:grid;
      grid-template-columns:18px minmax(0,1fr);
      gap:12px;
      align-items:flex-start;
      font-size:15px;
      color:#111827;
      line-height:1.55;
      margin-bottom:0;
      font-weight:400;
    }
    .checkbox-stack label input,
    .check-item input{
      width:18px;
      height:18px;
      margin:2px 0 0;
      accent-color:#3b82f6;
    }
    .checkbox-stack label span,
    .check-item span{
      display:block;
      min-width:0;
    }
    .checkbox-stack label[style*="margin-left"],
    .check-item.child{
      margin-left:40px !important;
      position:relative;
    }
    .checkbox-stack label[style*="margin-left"]::before,
    .check-item.child::before{
      content:"";
      position:absolute;
      left:-18px;
      top:12px;
      width:12px;
      height:1px;
      background:#cbd5e1;
    }
    .side-panel{background:#fff7e7;border:1px solid #f7d898;border-radius:16px;padding:28px 30px;}
    .calc-box{background:#fff;border:1px solid #e5e7eb;border-radius:16px;padding:24px 30px;margin-top:18px;}
    .calc-row{display:flex;justify-content:space-between;gap:20px;padding:16px 0;border-bottom:1px solid #e5e7eb;font-size:15px;}
    .calc-row:last-child{border-bottom:none;font-size:17px;font-weight:700;}
    .inline-radio{display:flex;gap:24px;align-items:center;margin:8px 0 10px;flex-wrap:wrap;}
    .inline-radio label{display:flex;gap:10px;align-items:center;font-size:15px;}
    .inline-radio input{width:18px;height:18px;margin:0;accent-color:#3b82f6;}
    .note-box{background:#fff7e7;border:1px solid #f7d898;border-radius:14px;padding:18px 24px;margin-top:18px;font-size:15px;line-height:1.6;}
    .info-card{max-width:560px;border:1px solid var(--border);border-radius:16px;background:#fff;padding:24px 26px;box-shadow:0 10px 22px rgba(15,23,42,.05);}
    .info-card h3{margin:0 0 18px;font-size:18px;}
    .info-grid{display:grid;grid-template-columns:180px 1fr;gap:18px 24px;}
    .warn-box{margin-top:18px;border:1px dashed #d1d5db;border-radius:16px;padding:28px 24px;text-align:center;}
    .warn-box .icon{font-size:28px;color:#fb923c;margin-bottom:12px;}
    .page-actions{display:flex;gap:14px;margin-top:26px;padding-top:24px;border-top:1px solid var(--border);}
    .cancel-btn{padding:10px 18px;border:1px solid #d1d5db;border-radius:8px;color:#374151;text-decoration:none;background:#fff;font-weight:600;}
    .toggle-form{display:inline-flex;align-items:center;margin-left:6px;}
    .toggle-button{border:none;background:transparent;padding:0;color:#2563eb;cursor:pointer;font:inherit;font-weight:600;}
  </style>
</head>
<body class="orgprofile-page">
  <?php settings_render_header($_SESSION['org_name'] ?? 'demo', 'dashboard.php', 'fa-landmark'); ?>

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

    <section class="profile-main">
      <div class="profile-head">
        <h1>Statutory Components</h1>
      </div>

      <div class="profile-card">
        <div class="tabs">
          <?php foreach ($tabLabels as $code => $label): ?>
            <?php $tabClass = $code === $component ? 'tab-link active' : 'tab-link'; ?>
            <a class="<?php echo $tabClass; ?>" href="statutory_components.php?component=<?php echo urlencode($code); ?>&mode=<?php echo urlencode($code === $component ? $mode : 'view'); ?>">
              <?php echo htmlspecialchars($label); ?>
            </a>
          <?php endforeach; ?>
        </div>

        <?php if ($errors): ?>
          <div class="alert-err">
            <ul>
              <?php foreach ($errors as $error): ?>
                <li><?php echo htmlspecialchars($error); ?></li>
              <?php endforeach; ?>
            </ul>
          </div>
        <?php endif; ?>

        <?php if (($component === 'epf' || $component === 'esi' || $component === 'bonus') && $mode === 'edit'): ?>
          <form method="post">
            <?php if ($component === 'epf'): ?>
              <h2 style="font-size:24px;"><?php echo htmlspecialchars((string) $config['title']); ?></h2>
              <div class="edit-layout">
                <div>
                  <div class="input-row">
                    <div class="field-line">
                      <label for="epf_number">EPF Number</label>
                      <input id="epf_number" name="epf_number" type="text" placeholder="AA/AAA/0000000/XXX" value="<?php echo htmlspecialchars((string) $config['form']['epf_number']); ?>">
                      <div class="hint-copy">Format: AA/AAA/0000000/XXX</div>
                    </div>
                    <div class="field-line">
                      <label for="deduction_cycle">Deduction Cycle <i class="fa-regular fa-circle-question"></i></label>
                      <select id="deduction_cycle" name="deduction_cycle">
                        <option <?php echo $config['form']['deduction_cycle'] === 'Monthly' ? 'selected' : ''; ?>>Monthly</option>
                      </select>
                    </div>
                  </div>

                  <div class="field-line">
                    <label for="employee_contribution_rate">Employee Contribution Rate</label>
                    <select id="employee_contribution_rate" name="employee_contribution_rate">
                      <option <?php echo $config['form']['employee_contribution_rate'] === '12% of Actual PF Wage' ? 'selected' : ''; ?>>12% of Actual PF Wage</option>
                    </select>
                  </div>

                  <div class="field-line">
                    <label for="employer_contribution_rate">Employer Contribution Rate <a href="support.php?topic=epf-splitup" class="config-link" style="margin-left:10px;">View Splitup</a></label>
                    <select id="employer_contribution_rate" name="employer_contribution_rate">
                      <option <?php echo $config['form']['employer_contribution_rate'] === '12% of Actual PF Wage' ? 'selected' : ''; ?>>12% of Actual PF Wage</option>
                    </select>
                  </div>

                  <div class="checkbox-stack">
                    <label><input type="checkbox" name="include_employer_contribution" <?php echo !empty($config['form']['include_employer_contribution']) ? 'checked' : ''; ?>><span>Include employer's contribution in employee’s salary structure.</span></label>
                    <label style="margin-left:28px;"><input type="checkbox" name="include_edli" <?php echo !empty($config['form']['include_edli']) ? 'checked' : ''; ?>><span>Include employer's EDLI contribution in employee’s salary structure.</span></label>
                    <label style="margin-left:28px;"><input type="checkbox" name="include_admin_charges" <?php echo !empty($config['form']['include_admin_charges']) ? 'checked' : ''; ?>><span>Include admin charges in employee’s salary structure.</span></label>
                    <label><input type="checkbox" name="override_employee_level" <?php echo !empty($config['form']['override_employee_level']) ? 'checked' : ''; ?>><span>Override PF contribution rate at employee level</span></label>
                  </div>

                  <div class="field-line">
                    <label>PF Configuration when LOP Applied</label>
                    <div class="checkbox-stack">
                      <label><input type="checkbox" name="prorate_restricted_pf_wage" <?php echo !empty($config['form']['prorate_restricted_pf_wage']) ? 'checked' : ''; ?>><span>Pro-rate Restricted PF Wage<br><span class="muted">PF contribution will be pro-rated based on the number of days worked by the employee.</span></span></label>
                      <label><input type="checkbox" name="consider_salary_components" <?php echo !empty($config['form']['consider_salary_components']) ? 'checked' : ''; ?>><span>Consider all applicable salary components if PF wage is less than ₹15,000 after Loss of Pay<br><span class="muted">PF wage will be computed using the salary earned in that particular month rather than the actual amount in the salary structure.</span></span></label>
                    </div>
                  </div>
                </div>

                <div class="side-panel">
                  <h2 style="margin:0;font-size:18px;">Sample EPF Calculation</h2>
                  <div style="margin-top:12px;font-size:17px;line-height:1.6;">Let's assume the PF wage is ₹ 20,000. The breakup of contribution will be:</div>
                  <div class="calc-box">
                    <div style="font-size:18px;font-weight:700;margin-bottom:8px;">Employee's Contribution</div>
                    <div class="calc-row"><span>EPF (12% of 20000)</span><strong>₹ 2400</strong></div>
                    <div style="font-size:18px;font-weight:700;margin:20px 0 8px;">Employer's Contribution</div>
                    <div class="calc-row"><span>EPS (8.33% of 20000 (Max of ₹ 15,000))</span><strong>₹ 1250</strong></div>
                    <div class="calc-row"><span>EPF (12% of 20000 - EPS)</span><strong>₹ 1150</strong></div>
                    <div class="calc-row"><span>Total</span><strong>₹ 2400</strong></div>
                  </div>
                  <div style="margin-top:18px;font-size:15px;line-height:1.6;color:#374151;">Do you want to preview EPF calculation for multiple cases, based on the preferences you have configured ?</div>
                  <a class="config-link" href="support.php?topic=epf-calculation" style="margin-top:16px;display:inline-block;">Preview EPF Calculation</a>
                </div>
              </div>
            <?php elseif ($component === 'esi'): ?>
              <h2 style="font-size:24px;"><?php echo htmlspecialchars((string) $config['title']); ?></h2>
              <div style="max-width:760px;">
                <div class="input-row">
                  <div class="field-line">
                    <label for="esi_number">ESI Number</label>
                    <input id="esi_number" name="esi_number" type="text" placeholder="00-00-000000-000-0000" value="<?php echo htmlspecialchars((string) $config['form']['esi_number']); ?>">
                    <div class="hint-copy">Format: 00-00-000000-000-0000</div>
                  </div>
                  <div class="field-line">
                    <label for="deduction_cycle">Deduction Cycle <i class="fa-regular fa-circle-question"></i></label>
                    <select id="deduction_cycle" name="deduction_cycle">
                      <option <?php echo $config['form']['deduction_cycle'] === 'Monthly' ? 'selected' : ''; ?>>Monthly</option>
                    </select>
                  </div>
                </div>

                <div class="field-line">
                  <label for="employee_contribution">Employees' Contribution</label>
                  <div style="display:flex;align-items:center;gap:16px;">
                    <input id="employee_contribution" name="employee_contribution" type="text" style="max-width:240px;" value="<?php echo htmlspecialchars((string) $config['form']['employee_contribution']); ?>">
                    <div style="font-size:16px;">of Gross Pay</div>
                  </div>
                </div>

                <div class="field-line">
                  <label for="employer_contribution">Employer's Contribution</label>
                  <div style="display:flex;align-items:center;gap:16px;">
                    <input id="employer_contribution" name="employer_contribution" type="text" style="max-width:240px;" value="<?php echo htmlspecialchars((string) $config['form']['employer_contribution']); ?>">
                    <div style="font-size:16px;">of Gross Pay</div>
                  </div>
                </div>

                <div class="checkbox-stack">
                  <label><input type="checkbox" name="include_employer_contribution" <?php echo !empty($config['form']['include_employer_contribution']) ? 'checked' : ''; ?>><span>Include employer's contribution in employee’s salary structure.</span></label>
                </div>

                <div class="note-box">
                  Note: ESI deductions will be made only if the employee’s monthly salary is less than or equal to ₹21,000. If the employee gets a salary revision which increases their monthly salary above ₹21,000, they would have to continue making ESI contributions till the end of the contribution period in which the salary was revised (April-September or October-March).
                </div>
              </div>
            <?php elseif ($component === 'bonus'): ?>
              <h2 style="font-size:24px;">Statutory Bonus</h2>
              <div style="max-width:880px;">
                <div class="field-line">
                  <label>Payment Frequency</label>
                  <div class="inline-radio">
                    <label><input type="radio" name="payment_frequency" value="Monthly" <?php echo $config['form']['payment_frequency'] === 'Monthly' ? 'checked' : ''; ?>> Monthly</label>
                    <label><input type="radio" name="payment_frequency" value="Yearly" <?php echo $config['form']['payment_frequency'] === 'Yearly' ? 'checked' : ''; ?>> Yearly</label>
                  </div>
                </div>

                <div class="field-line">
                  <label for="bonus_percentage">Monthly Percentage of Bonus <span class="req">*</span></label>
                  <div style="display:flex;align-items:center;gap:0;max-width:620px;">
                    <input id="bonus_percentage" name="bonus_percentage" type="text" value="<?php echo htmlspecialchars((string) $config['form']['bonus_percentage']); ?>" style="border-radius:8px 0 0 8px;">
                    <span style="height:40px;padding:0 18px;border:1px solid var(--border);border-left:none;border-radius:0 8px 8px 0;display:inline-flex;align-items:center;background:#fff;font-size:16px;">%</span>
                    <span style="margin-left:16px;font-size:16px;">of salary or minimum wage earned this year</span>
                  </div>
                  <div class="hint-copy" style="font-size:15px;margin-top:16px;">Statutory Bonus rate should be in-between 8.33% and 20%, based on the Statutory Bonus Act.</div>
                </div>

                <div class="note-box">
                  <div style="font-weight:700;margin-bottom:10px;">NOTE:</div>
                  <div>The payment frequency of this statutory bonus is monthly and taxable.</div>
                  <div style="margin-top:10px;">Once you've associated the statutory bonus with an employee, you can change the bonus percentage only at the beginning of the next fiscal year.</div>
                </div>

                <div style="margin-top:28px;">
                  <h2 style="font-size:18px;margin-bottom:8px;">Minimum Wage in Each State</h2>
                  <div class="muted" style="font-size:15px;">Statutory Bonus is a percentage of either the minimum wage or Basic + DA (whichever is higher).</div>
                  <div class="info-card" style="margin-top:18px;">
                    <h3><?php echo htmlspecialchars((string) $config['form']['state']); ?></h3>
                    <div class="warn-box">
                      <div class="icon"><i class="fa-solid fa-circle-exclamation"></i></div>
                      <div style="font-size:16px;line-height:1.5;">Minimum wage details are not added for <?php echo htmlspecialchars((string) $config['form']['state']); ?></div>
                      <a class="save-btn" href="support.php?topic=minimum-wage" style="margin-top:16px;background:#eef4ff;color:#2563eb;">Add Minimum Wage</a>
                    </div>
                  </div>
                </div>
              </div>
            <?php endif; ?>

            <div class="page-actions">
              <button class="save-btn" type="submit"><?php echo $component === 'bonus' ? 'Save' : 'Enable'; ?></button>
              <a class="cancel-btn" href="statutory_components.php?component=<?php echo urlencode($component); ?>">Cancel</a>
            </div>
          </form>
        <?php else: ?>
          <?php if (($component === 'epf' || $component === 'esi' || $component === 'bonus') && !empty($config['enabled'])): ?>
            <div>
              <div style="display:flex;align-items:center;gap:12px;justify-content:space-between;flex-wrap:wrap;">
                <div>
                  <h2 style="font-size:24px;margin-bottom:6px;"><?php echo htmlspecialchars((string) $config['title']); ?></h2>
                  <div style="font-size:17px;max-width:860px;line-height:1.6;"><?php echo htmlspecialchars((string) $config['description']); ?></div>
                </div>
                <a class="config-link" href="statutory_components.php?component=<?php echo urlencode($component); ?>&mode=edit" style="font-size:16px;">Edit</a>
              </div>

              <?php if ($component === 'epf'): ?>
                <div class="edit-layout" style="margin-top:26px;">
                  <div class="info-card">
                    <h3><?php echo htmlspecialchars((string) $config['title']); ?></h3>
                    <div class="info-grid">
                      <div class="muted">EPF Number</div>
                      <div><?php echo htmlspecialchars((string) $config['form']['epf_number']); ?></div>

                      <div class="muted">Deduction Cycle</div>
                      <div><?php echo htmlspecialchars((string) $config['form']['deduction_cycle']); ?></div>

                      <div class="muted">Employee Contribution Rate</div>
                      <div><?php echo htmlspecialchars((string) $config['form']['employee_contribution_rate']); ?></div>

                      <div class="muted">Employer Contribution Rate</div>
                      <div><?php echo htmlspecialchars((string) $config['form']['employer_contribution_rate']); ?></div>

                      <div class="muted">Contribution Preferences</div>
                      <div>
                        <?php echo !empty($config['form']['include_employer_contribution']) ? "Employer's PF contribution" : 'Employer contribution not included'; ?><br>
                        <?php echo !empty($config['form']['include_edli']) ? 'EDLI contribution' : 'EDLI contribution not included'; ?><br>
                        <?php echo !empty($config['form']['include_admin_charges']) ? 'Admin charges' : 'Admin charges not included'; ?>
                      </div>

                      <div class="muted">Allow Employee level Override</div>
                      <div><?php echo !empty($config['form']['override_employee_level']) ? 'Yes' : 'No'; ?></div>

                      <div class="muted">Pro-rate Restricted PF Wage</div>
                      <div><?php echo !empty($config['form']['prorate_restricted_pf_wage']) ? 'Yes' : 'No'; ?></div>

                      <div class="muted">Consider applicable salary components based on LOP</div>
                      <div><?php echo !empty($config['form']['consider_salary_components']) ? 'Yes' : 'No'; ?></div>

                      <div class="muted">Status</div>
                      <div style="<?php echo $isEnabled ? 'color:#16a34a;' : 'color:#ef4444;'; ?>">
                        <?php echo $isEnabled ? 'Enabled' : 'Disabled'; ?>
                        <form class="toggle-form" method="post" action="statutory_components.php?component=epf&mode=view">
                          <input type="hidden" name="toggle_component" value="epf">
                          <button class="toggle-button" type="submit" name="toggle_enabled" value="1"><?php echo $isEnabled ? '(Disable)' : '(Enable)'; ?></button>
                        </form>
                      </div>
                    </div>
                  </div>

                  <div class="side-panel">
                    <h2 style="margin:0;font-size:18px;">Sample EPF Calculation</h2>
                    <div style="margin-top:12px;font-size:17px;line-height:1.6;">Let's assume the PF wage is â‚¹ 20,000. The breakup of contribution will be:</div>
                    <div class="calc-box">
                      <div style="font-size:18px;font-weight:700;margin-bottom:8px;">Employee's Contribution</div>
                      <div class="calc-row"><span>EPF (12% of 20000)</span><strong>â‚¹ 2400</strong></div>
                      <div style="font-size:18px;font-weight:700;margin:20px 0 8px;">Employer's Contribution</div>
                      <div class="calc-row"><span>EPS (8.33% of 20000 (Max of â‚¹ 15,000))</span><strong>â‚¹ 1250</strong></div>
                      <div class="calc-row"><span>EPF (12% of 20000 - EPS)</span><strong>â‚¹ 1150</strong></div>
                      <div class="calc-row"><span>Total</span><strong>â‚¹ 2400</strong></div>
                    </div>
                    <div style="margin-top:18px;font-size:15px;line-height:1.6;color:#374151;">Do you want to preview EPF calculation for multiple cases, based on the preferences you have configured ?</div>
                    <a class="config-link" href="support.php?topic=epf-calculation" style="margin-top:16px;display:inline-block;">Preview EPF Calculation</a>
                  </div>
                </div>
              <?php elseif ($component === 'esi'): ?>
                <div class="edit-layout" style="margin-top:26px;grid-template-columns:minmax(0,1fr) 420px;">
                  <div class="info-card">
                    <h3><?php echo htmlspecialchars((string) $config['title']); ?></h3>
                    <div class="info-grid">
                      <div class="muted">ESI Number</div>
                      <div><?php echo htmlspecialchars((string) $config['form']['esi_number']); ?></div>

                      <div class="muted">Deduction Cycle</div>
                      <div><?php echo htmlspecialchars((string) $config['form']['deduction_cycle']); ?></div>

                      <div class="muted">Employees' Contribution</div>
                      <div><?php echo htmlspecialchars((string) $config['form']['employee_contribution']); ?> of Gross Pay</div>

                      <div class="muted">Employer's Contribution</div>
                      <div><?php echo htmlspecialchars((string) $config['form']['employer_contribution']); ?> of Gross Pay</div>

                      <div class="muted">Other Details</div>
                      <div><?php echo !empty($config['form']['include_employer_contribution']) ? "Employer's contribution is included in employee's salary structure." : 'Employer contribution is not included.'; ?></div>
                    </div>
                  </div>

                  <div class="note-box" style="margin-top:0;background:#fff7e7;">
                    <div style="font-size:18px;font-weight:700;margin-bottom:12px;">Status</div>
                    <div style="<?php echo $isEnabled ? 'color:#16a34a;font-weight:600;' : 'color:#ef4444;font-weight:600;'; ?>">
                      <?php echo $isEnabled ? 'Enabled' : 'Disabled'; ?>
                      <form class="toggle-form" method="post" action="statutory_components.php?component=esi&mode=view">
                        <input type="hidden" name="toggle_component" value="esi">
                        <button class="toggle-button" type="submit" name="toggle_enabled" value="1"><?php echo $isEnabled ? '(Disable)' : '(Enable)'; ?></button>
                      </form>
                    </div>
                    <div style="margin-top:14px;line-height:1.6;">
                      ESI deductions will be made only if the employee’s monthly salary is less than or equal to ₹21,000.
                    </div>
                  </div>
                </div>
              <?php elseif ($component === 'bonus'): ?>
                <div style="margin-top:26px;">
                  <h2 style="font-size:24px;">Statutory Bonus</h2>
                  <div class="info-card" style="margin-top:18px;">
                    <div class="info-grid" style="grid-template-columns:220px 1fr;">
                      <div class="muted">Payment Frequency</div>
                      <div><?php echo htmlspecialchars((string) $config['form']['payment_frequency']); ?></div>

                      <div class="muted">Monthly Percentage of Bonus</div>
                      <div><?php echo htmlspecialchars((string) $config['form']['bonus_percentage']); ?>%</div>

                      <div class="muted">State</div>
                      <div><?php echo htmlspecialchars((string) $config['form']['state']); ?></div>

                      <div class="muted">Status</div>
                    <div style="<?php echo $isEnabled ? 'color:#16a34a;' : 'color:#ef4444;'; ?>">
                      <?php echo $isEnabled ? 'Enabled' : 'Disabled'; ?>
                      <form class="toggle-form" method="post" action="statutory_components.php?component=bonus&mode=view">
                        <input type="hidden" name="toggle_component" value="bonus">
                        <button class="toggle-button" type="submit" name="toggle_enabled" value="1"><?php echo $isEnabled ? '(Disable)' : '(Enable)'; ?></button>
                      </form>
                    </div>
                    </div>
                  </div>
                </div>
              <?php endif; ?>
            </div>
          <?php elseif ($component === 'pt'): ?>
            <div>
              <h2 style="font-size:24px;"><?php echo htmlspecialchars((string) $config['title']); ?></h2>
              <div style="font-size:17px;max-width:880px;line-height:1.6;margin-top:8px;"><?php echo htmlspecialchars((string) $config['description']); ?></div>
              <div class="info-card" style="margin-top:28px;">
                <h3><?php echo htmlspecialchars((string) $config['card']['office']); ?></h3>
                <div class="info-grid">
                  <div class="muted">PT Number</div>
                  <div><a class="config-link" href="statutory_components.php?component=pt&mode=view"><?php echo htmlspecialchars((string) $config['button_label']); ?></a></div>

                  <div class="muted">State</div>
                  <div><?php echo htmlspecialchars((string) $config['card']['state']); ?></div>

                  <div class="muted">Deduction Cycle</div>
                  <div><?php echo htmlspecialchars((string) $config['card']['deduction_cycle']); ?></div>

                  <div class="muted">PT Slabs</div>
                  <div><a class="config-link" href="#"><?php echo htmlspecialchars((string) $config['card']['slabs_link']); ?></a></div>
                </div>
              </div>
            </div>
          <?php elseif ($component === 'lwf'): ?>
            <div>
              <h2 style="font-size:24px;"><?php echo htmlspecialchars((string) $config['title']); ?></h2>
              <div style="font-size:17px;max-width:860px;line-height:1.6;margin-top:8px;"><?php echo htmlspecialchars((string) $config['description']); ?></div>
              <div class="info-card" style="margin-top:28px;">
                <h3><?php echo htmlspecialchars((string) $config['card']['state']); ?></h3>
                <div class="info-grid">
                  <div class="muted">Employees' Contribution</div>
                  <div>₹ <?php echo htmlspecialchars((string) $config['card']['employee_contribution']); ?></div>

                  <div class="muted">Employer's Contribution</div>
                  <div>₹ <?php echo htmlspecialchars((string) $config['card']['employer_contribution']); ?></div>

                  <div class="muted">Deduction Cycle</div>
                  <div><?php echo htmlspecialchars((string) $config['card']['deduction_cycle']); ?></div>

                <div class="muted">Status</div>
                <div style="<?php echo $isEnabled ? 'color:#16a34a;' : 'color:#ef4444;'; ?>">
                  <?php echo $isEnabled ? 'Enabled' : 'Disabled'; ?>
                  <form class="toggle-form" method="post" action="statutory_components.php?component=lwf&mode=view">
                    <input type="hidden" name="toggle_component" value="lwf">
                    <button class="toggle-button" type="submit" name="toggle_enabled" value="1"><?php echo $isEnabled ? '(Disable)' : '(Enable)'; ?></button>
                  </form>
                </div>
                </div>
              </div>
            </div>
          <?php else: ?>
            <div class="empty-state">
              <div class="empty-ill">
                <i class="fa-solid <?php echo $component === 'bonus' ? 'fa-sack-dollar' : 'fa-hand-holding-dollar'; ?>"></i>
              </div>
              <div class="empty-title"><?php echo htmlspecialchars((string) ($config['component_question'] ?? '')); ?>
                <?php if ($component === 'epf'): ?>Are you registered for EPF?<?php endif; ?>
                <?php if ($component === 'esi'): ?>Configure Employees' State Insurance?<?php endif; ?>
                <?php if ($component === 'bonus'): ?>Are your employees eligible to receive statutory bonus?<?php endif; ?>
              </div>
              <div class="empty-copy"><?php echo htmlspecialchars((string) $config['description']); ?></div>
              <div class="empty-action">
                <form method="post">
                  <input type="hidden" name="toggle_component" value="<?php echo htmlspecialchars($component); ?>">
                  <button class="save-btn" type="submit" name="toggle_enabled" value="1"><?php echo htmlspecialchars((string) $config['button_label']); ?></button>
                </form>
              </div>
            </div>
          <?php endif; ?>
        <?php endif; ?>
      </div>
    </section>
  </main>
</body>
</html>
