<?php
require_once __DIR__ . '/../../config/config.php';
require_once __DIR__ . '/../../includes/functions.php';
startSession(); requireLogin(); requireRole('superadmin','admin','hr','management');
$db = Database::getInstance();
$page = 'PF / ESI Setup';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    verifyCsrf();
    $action = post('action');
    if ($action === 'pf') {
        $exists = $db->fetchOne("SELECT id FROM pf_setup LIMIT 1");
        $data = [
            'establishment_id'   => post('establishment_id'),
            'establishment_name' => post('establishment_name'),
            'pf_employee_rate'   => post('pf_employee_rate'),
            'pf_employer_rate'   => post('pf_employer_rate'),
            'eps_rate'           => post('eps_rate'),
            'edli_rate'          => post('edli_rate'),
            'admin_rate'         => post('admin_rate'),
            'pf_wage_ceiling'    => post('pf_wage_ceiling'),
            'is_active'          => 1,
        ];
        if ($exists) $db->updateRow('pf_setup', $data, $exists['id']);
        else $db->insertRow('pf_setup', $data);
        setFlash('success', 'PF setup saved.');
    } elseif ($action === 'esi') {
        $exists = $db->fetchOne("SELECT id FROM esi_setup LIMIT 1");
        $data = [
            'establishment_code' => post('establishment_code'),
            'employee_rate'      => post('employee_rate'),
            'employer_rate'      => post('employer_rate'),
            'wage_ceiling'       => post('wage_ceiling'),
            'is_active'          => 1,
        ];
        if ($exists) $db->updateRow('esi_setup', $data, $exists['id']);
        else $db->insertRow('esi_setup', $data);
        setFlash('success', 'ESI setup saved.');
    }
    header('Location: ' . BASE_URL . '/modules/configuration/pf_esi_setup.php'); exit;
}

$pf  = $db->fetchOne("SELECT * FROM pf_setup LIMIT 1");
$esi = $db->fetchOne("SELECT * FROM esi_setup LIMIT 1");

include __DIR__ . '/../../includes/header.php';
?>
<div class="row g-3">
  <!-- PF Setup -->
  <div class="col-md-6">
    <div class="pc-card">
      <div class="pc-card-title"><i class="ti ti-shield-check me-2"></i>Provident Fund (PF) Setup</div>
      <form method="POST">
        <?=csrfField()?><input type="hidden" name="action" value="pf">
        <div class="row g-3">
          <div class="col-md-6"><label class="pc-label">Establishment ID</label>
            <input type="text" name="establishment_id" class="form-control form-control-sm" value="<?=clean($pf['establishment_id']??'')?>"></div>
          <div class="col-md-6"><label class="pc-label">Establishment Name</label>
            <input type="text" name="establishment_name" class="form-control form-control-sm" value="<?=clean($pf['establishment_name']??'')?>"></div>
          <div class="col-md-6"><label class="pc-label">Employee PF Rate (%)</label>
            <input type="number" step="0.01" name="pf_employee_rate" class="form-control form-control-sm" value="<?=$pf['pf_employee_rate']??12.00?>" required></div>
          <div class="col-md-6"><label class="pc-label">Employer PF Rate (%)</label>
            <input type="number" step="0.01" name="pf_employer_rate" class="form-control form-control-sm" value="<?=$pf['pf_employer_rate']??12.00?>" required></div>
          <div class="col-md-4"><label class="pc-label">EPS Rate (%)</label>
            <input type="number" step="0.01" name="eps_rate" class="form-control form-control-sm" value="<?=$pf['eps_rate']??8.33?>"></div>
          <div class="col-md-4"><label class="pc-label">EDLI Rate (%)</label>
            <input type="number" step="0.01" name="edli_rate" class="form-control form-control-sm" value="<?=$pf['edli_rate']??0.50?>"></div>
          <div class="col-md-4"><label class="pc-label">Admin Charges (%)</label>
            <input type="number" step="0.01" name="admin_rate" class="form-control form-control-sm" value="<?=$pf['admin_rate']??0.50?>"></div>
          <div class="col-md-6"><label class="pc-label">PF Wage Ceiling (₹)</label>
            <input type="number" step="0.01" name="pf_wage_ceiling" class="form-control form-control-sm" value="<?=$pf['pf_wage_ceiling']??15000?>" required></div>
        </div>
        <div class="mt-3"><button class="btn btn-pc btn-sm"><i class="ti ti-device-floppy me-1"></i>Save PF Setup</button></div>
      </form>
    </div>
  </div>

  <!-- ESI Setup -->
  <div class="col-md-6">
    <div class="pc-card">
      <div class="pc-card-title"><i class="ti ti-heart-rate-monitor me-2"></i>Employee State Insurance (ESI) Setup</div>
      <form method="POST">
        <?=csrfField()?><input type="hidden" name="action" value="esi">
        <div class="row g-3">
          <div class="col-12"><label class="pc-label">Establishment Code</label>
            <input type="text" name="establishment_code" class="form-control form-control-sm" value="<?=clean($esi['establishment_code']??'')?>"></div>
          <div class="col-md-6"><label class="pc-label">Employee Contribution (%)</label>
            <input type="number" step="0.01" name="employee_rate" class="form-control form-control-sm" value="<?=$esi['employee_rate']??0.75?>" required></div>
          <div class="col-md-6"><label class="pc-label">Employer Contribution (%)</label>
            <input type="number" step="0.01" name="employer_rate" class="form-control form-control-sm" value="<?=$esi['employer_rate']??3.25?>" required></div>
          <div class="col-md-6"><label class="pc-label">Wage Ceiling (₹/month)</label>
            <input type="number" step="0.01" name="wage_ceiling" class="form-control form-control-sm" value="<?=$esi['wage_ceiling']??21000?>" required></div>
        </div>
        <div class="mt-3"><button class="btn btn-pc btn-sm"><i class="ti ti-device-floppy me-1"></i>Save ESI Setup</button></div>
      </form>
    </div>

    <div class="pc-card mt-3">
      <div class="pc-card-title"><i class="ti ti-info-circle me-2"></i>How These Rates Are Used</div>
      <ul class="small mb-0" style="padding-left:1.2rem;color:var(--pc-text2)">
        <li>PF is deducted only if the employee's Basic salary is at or below the PF Wage Ceiling (unless voluntarily opted-in).</li>
        <li>ESI applies only to employees whose Gross Salary is at or below the ESI Wage Ceiling.</li>
        <li>Both employee and employer contributions are calculated automatically during Payroll Processing.</li>
        <li>Admin and EDLI charges are employer-only costs, added to the total CTC.</li>
      </ul>
    </div>
  </div>
</div>
<?php include __DIR__ . '/../../includes/footer.php'; ?>
