<?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);
$settings = settings_get_org_settings($conn, $orgId);
$flash = settings_flash_message();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $payload = [
        'date_format' => $settings['date_format'],
        'field_separator' => $settings['field_separator'],
        'primary_contact_name' => trim((string) ($_POST['primary_contact_name'] ?? '')),
        'primary_contact_email' => trim((string) ($_POST['primary_contact_email'] ?? '')),
        'sender_email' => trim((string) ($_POST['sender_email'] ?? '')),
        'logo_path' => $settings['logo_path'],
    ];

    if (settings_upsert_org_settings($conn, $orgId, $payload)) {
        settings_set_flash('success', 'Sender email preferences saved.');
    } else {
        settings_set_flash('error', 'Unable to save sender email preferences right now.');
    }

    header('Location: sender_email_preferences.php');
    exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
<base href="<?php echo billing_escape(billing_base_href()); ?>">
  <title>Sender Email Preferences | 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">
</head>
<body class="orgprofile-page">
  <?php settings_render_header($_SESSION['org_name'] ?? 'demo', 'dashboard.php', 'fa-paper-plane'); ?>
  <main class="profile-layout">
    <?php settings_render_sidebar('sender_email_preferences'); ?>
    <section class="profile-main">
      <div class="profile-head"><h1>Sender Email Preferences</h1></div>
      <div class="profile-card">
        <?php if ($flash['type'] === 'success'): ?>
          <div class="alert-ok"><?php echo htmlspecialchars($flash['message']); ?></div>
        <?php elseif ($flash['type'] === 'error'): ?>
          <div class="alert-error"><?php echo htmlspecialchars($flash['message']); ?></div>
        <?php endif; ?>

        <form method="post" class="profile-form-grid">
          <div class="profile-field">
            <label for="primary_contact_name">Sender Name</label>
            <input id="primary_contact_name" name="primary_contact_name" type="text" value="<?php echo htmlspecialchars((string) $settings['primary_contact_name']); ?>">
          </div>
          <div class="profile-field">
            <label for="primary_contact_email">Reply-To Email</label>
            <input id="primary_contact_email" name="primary_contact_email" type="email" value="<?php echo htmlspecialchars((string) $settings['primary_contact_email']); ?>">
          </div>
          <div class="profile-field profile-field-span">
            <label for="sender_email">From Email Address</label>
            <input id="sender_email" name="sender_email" type="email" value="<?php echo htmlspecialchars((string) $settings['sender_email']); ?>">
            <p class="profile-help">This address will be used for payroll notifications and template emails.</p>
          </div>
          <div class="profile-actions">
            <button class="save-btn" type="submit">Save Preferences</button>
          </div>
        </form>
      </div>
    </section>
  </main>
</body>
</html>

