<?php
session_start();
if (empty($_SESSION['authenticated'])) {
    header("Location: signin.php");
    exit();
}

require_once __DIR__ . '/db.php';
require_once __DIR__ . '/includes/poi_module.php';

ensure_poi_schema($conn);
$orgContext = poi_org_context($conn);
$orgName = $orgContext['org_name'];
$orgId = $orgContext['org_id'];
poi_ensure_settings_row($conn, $orgId);
$months = ['January','February','March','April','May','June','July','August','September','October','November','December'];

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $payload = [
        'release_enabled' => isset($_POST['release_enabled']) ? 1 : 0,
        'payroll_from_month' => $_POST['payroll_from_month'] ?? 'March',
        'allow_switch_tax_regime' => isset($_POST['allow_switch_tax_regime']) ? 1 : 0,
        'allow_tds_modification' => isset($_POST['allow_tds_modification']) ? 1 : 0,
        'mandate_attachments' => isset($_POST['mandate_attachments']) ? 1 : 0,
        'mandate_reviewer_comments' => isset($_POST['mandate_reviewer_comments']) ? 1 : 0,
    ];
    if (isset($_POST['release_now'])) {
        $payload['release_enabled'] = 1;
    }
    poi_save_settings($conn, $orgId, $payload);
    header('Location: poi_settings.php?saved=1');
    exit();
}

$settings = poi_get_settings($conn, $orgId);
$unsubmittedCount = count(poi_unsubmitted_employees($conn, $orgId, poi_fiscal_years()[0]));
$saved = isset($_GET['saved']) && $_GET['saved'] === '1';
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>POI Preference</title>
  <link rel="stylesheet" href="poi.css">
  <link rel="stylesheet" href="assets/font-inter.css">
  <link rel="stylesheet" href="assets/fontawesome/css/all.min.css">
</head>
<body class="poi-settings-page">
  <header class="poi-settings-top">
    <div class="poi-settings-brand">
      <div class="poi-settings-logo"><i class="fa-regular fa-file-lines"></i></div>
      <div>
        <div class="poi-settings-title">All Settings</div>
        <div class="poi-settings-sub"><?php echo htmlspecialchars($orgName); ?></div>
      </div>
    </div>
    <div class="poi-settings-search">
      <i class="fa-solid fa-magnifying-glass"></i>
      <input type="text" placeholder="Search settings ( / )">
    </div>
    <a class="poi-settings-close" href="proof_of_investments.php">Close Settings <span style="color:#ef4444;font-weight:800;">×</span></a>
  </header>

  <div class="poi-settings-layout">
    <aside class="poi-settings-sidebar">
      <div class="poi-sidebar-group-title">Organisation Settings</div>
      <nav class="poi-settings-nav">
        <a class="poi-settings-link" href="orgprofile.php"><i class="fa-solid fa-chevron-right"></i>Organisation</a>
        <a class="poi-settings-link" href="#"><i class="fa-solid fa-chevron-right"></i>Users and Roles</a>
        <a class="poi-settings-link" href="#"><i class="fa-solid fa-chevron-right"></i>Taxes</a>
      </nav>

      <div class="poi-settings-parent active"><i class="fa-solid fa-chevron-down"></i>Setup &amp; Configurations</div>
      <div class="poi-settings-subnav">
        <a href="#">Pay Schedule</a>
        <a href="#">Statutory Components</a>
        <a href="#">Salary Components</a>
        <a href="#">Employee Portal</a>
        <a href="#" class="active">Claims and Declarations</a>
      </div>

      <div class="poi-sidebar-group-title">Module Settings</div>
      <nav class="poi-settings-nav">
        <a class="poi-settings-link" href="#"><i class="fa-solid fa-chevron-right"></i>General</a>
        <a class="poi-settings-link" href="#"><i class="fa-solid fa-chevron-right"></i>Payments</a>
      </nav>
    </aside>

    <main class="poi-settings-main">
      <div class="poi-settings-card">
        <div class="poi-settings-card-head">
          <h1>Claims and Declarations</h1>
          <div class="poi-inline-note"><i class="fa-solid fa-circle-info"></i><a href="poi_unsubmitted.php" style="color:#2463d3;text-decoration:none;">View Employees</a> yet to submit POI</div>
        </div>

        <div class="poi-settings-tabs">
          <a href="#">Flexible Benefit Plan</a>
          <a href="reimbursements.php">Reimbursement Claims</a>
          <a href="#">Income Tax Declaration</a>
          <a href="poi_settings.php" class="active">Proof Of Investments</a>
        </div>

        <div class="poi-settings-body">
          <?php if ($saved): ?>
            <div style="margin-bottom:18px;padding:12px 14px;border-radius:12px;background:#effdf5;border:1px solid #bbf7d0;color:#166534;">POI settings saved successfully.</div>
          <?php endif; ?>

          <p style="font-size:16px;line-height:1.55;margin:0 0 18px;">Employees can submit the necessary supporting documents for their declared investments through the employee portal once you enable this option.</p>
          <p style="margin:0 0 10px;color:#374151;"><i class="fa-regular fa-lightbulb" style="color:#fb923c;"></i> Learn how to manage investment proofs. <a href="#" style="color:#2563eb;text-decoration:none;">POI Help Document</a></p>
          <p style="margin:0 0 20px;color:#374151;">Help your employees submit POI on time. <a href="#" style="color:#2563eb;text-decoration:none;">Download and share</a> this POI ebook.</p>

          <?php if (!(int) $settings['release_enabled']): ?>
            <div class="poi-locked-card">
              <div class="poi-lock-illus" aria-hidden="true"></div>
              <h3>POI is Locked</h3>
              <p>You are yet to enable submission of investment proofs for your employees through their respective portals. Release POI or submit it on their behalf under Employees &gt; Employee profile &gt; Investments &gt; Proof of Investments.</p>
              <form method="POST">
                <input type="hidden" name="release_enabled" value="1">
                <input type="hidden" name="payroll_from_month" value="<?php echo htmlspecialchars($settings['payroll_from_month']); ?>">
                <input type="hidden" name="allow_switch_tax_regime" value="<?php echo (int) $settings['allow_switch_tax_regime']; ?>">
                <input type="hidden" name="allow_tds_modification" value="<?php echo (int) $settings['allow_tds_modification']; ?>">
                <input type="hidden" name="mandate_attachments" value="<?php echo (int) $settings['mandate_attachments']; ?>">
                <input type="hidden" name="mandate_reviewer_comments" value="<?php echo (int) $settings['mandate_reviewer_comments']; ?>">
                <button class="poi-outline-btn" type="submit" name="release_now" value="1">Release Proof Of Investments</button>
              </form>
            </div>
          <?php else: ?>
            <form method="POST">
              <input type="hidden" name="release_enabled" value="1">
              <div class="poi-config-grid">
                <div class="poi-config-copy">
                  <strong>Process payroll with approved POI amount from</strong><br><br>
                  <span class="good">The approved POI amount will be considered for the payroll from <mark style="background:#fff1b8;padding:0 4px;border-radius:4px;"><?php echo htmlspecialchars($settings['payroll_from_month']); ?></mark> onwards to calculate and deduct income tax amount in subsequent payrolls.</span>
                </div>
                <div class="poi-config-select">
                  <select name="payroll_from_month">
                    <?php foreach ($months as $month): ?>
                      <option value="<?php echo htmlspecialchars($month); ?>" <?php echo $settings['payroll_from_month'] === $month ? 'selected' : ''; ?>><?php echo htmlspecialchars($month); ?></option>
                    <?php endforeach; ?>
                  </select>
                </div>
                <div style="font-size:16px;color:#1f2937;">for upcoming years.</div>
              </div>

              <hr style="border:none;border-top:1px solid #edf1f6;margin:20px 0;">
              <div style="font-size:18px;font-weight:600;margin-bottom:18px;">Other Configurations</div>
              <div class="poi-check-list">
                <label><input type="checkbox" name="allow_switch_tax_regime" value="1" <?php echo !empty($settings['allow_switch_tax_regime']) ? 'checked' : ''; ?>> Allow employees to switch tax regimes</label>
                <label><input type="checkbox" name="allow_tds_modification" value="1" <?php echo !empty($settings['allow_tds_modification']) ? 'checked' : ''; ?>> Allow TDS modification during Payroll</label>
                <label><input type="checkbox" name="mandate_attachments" value="1" <?php echo !empty($settings['mandate_attachments']) ? 'checked' : ''; ?>> Mandate investment proof attachments for POI submission</label>
                <label><input type="checkbox" name="mandate_reviewer_comments" value="1" <?php echo !empty($settings['mandate_reviewer_comments']) ? 'checked' : ''; ?>> Mandate reviewer comments for partial investment amount approval</label>
              </div>

              <hr style="border:none;border-top:1px solid #edf1f6;margin:20px 0 26px;">
              <button class="poi-primary-btn" type="submit">Save</button>
            </form>
          <?php endif; ?>
        </div>
      </div>
    </main>
  </div>
</body>
</html>
