<?php
/**
 * ============================================================================
 *  SETTINGS > ACCOUNT MANAGEMENT > ACCOUNT DEFAULTS  (settings-account-defaults.php)
 * ----------------------------------------------------------------------------
 *  Mirrors the HubSpot "Account Defaults" page.
 *  Tabs: General | User Defaults | Notification Profiles | Currency | Feature Releases
 *
 *  - Forms with data-settings-form show the bottom save bar when changed.
 *  - Currency + notification profile dialogs add rows in the browser only.
 *  - DUMMY values below; wire saving to your DB in assets/js/settings.js (save()).
 * ============================================================================
 */
require_once __DIR__ . '/includes/config.php';
require_once __DIR__ . '/includes/settings-ui.php';
require_login();

$user = current_user();

/* DUMMY account values */
$ACCOUNT = array(
  'name'     => $user['account'],
  'timezone' => 'Asia/Kolkata',
  'fiscal'   => 'April - March',
  'company'  => $user['account'],
  'domain'   => '',
  'address'  => '',
  'address2' => '',
  'city'     => '',
  'state'    => '',
  'zip'      => '',
  'country'  => 'India',
  'industry' => '',
);

$TIMEZONES = array(
  'Pacific/Honolulu'    => 'UTC -10:00 Honolulu',
  'America/Los_Angeles' => 'UTC -07:00 Los Angeles',
  'America/Denver'      => 'UTC -06:00 Denver',
  'America/Chicago'     => 'UTC -05:00 Chicago',
  'America/New_York'    => 'UTC -04:00 New York',
  'UTC'                 => 'UTC +00:00 UTC',
  'Europe/London'       => 'UTC +01:00 London',
  'Europe/Berlin'       => 'UTC +02:00 Berlin',
  'Asia/Dubai'          => 'UTC +04:00 Dubai',
  'Asia/Kolkata'        => 'UTC +05:30 Kolkata',
  'Asia/Singapore'      => 'UTC +08:00 Singapore',
  'Asia/Tokyo'          => 'UTC +09:00 Tokyo',
  'Australia/Sydney'    => 'UTC +10:00 Sydney',
);

$FISCAL = array('January - December', 'February - January', 'March - February', 'April - March', 'May - April',
                'June - May', 'July - June', 'August - July', 'September - August', 'October - September',
                'November - October', 'December - November');

$INDUSTRIES = array('Accounting', 'Advertising & Marketing', 'Automotive', 'Banking & Financial Services', 'Computer Software',
                    'Consulting', 'E-commerce', 'Education', 'Healthcare', 'Hospitality', 'Information Technology',
                    'Insurance', 'Logistics & Supply Chain', 'Manufacturing', 'Media & Publishing', 'Non-profit',
                    'Real Estate', 'Retail', 'Telecommunications', 'Travel & Tourism', 'Other');

/* Date & number formats: value => [label, example] */
$FORMATS = array(
  'en-IN' => array('India',          '01/10/2026 and 1,234.56'),
  'en-US' => array('United States',  '10/01/2026 and 1,234.56'),
  'en-GB' => array('United Kingdom', '01/10/2026 and 1,234.56'),
  'de-DE' => array('Germany',        '01.10.2026 and 1.234,56'),
  'fr-FR' => array('France',         '01/10/2026 and 1 234,56'),
  'ja-JP' => array('Japan',          '2026/10/01 and 1,234.56'),
);

$LANGUAGES = array('English', 'Hindi', 'Español', 'Français', 'Deutsch', 'Português', '日本語');
$FONTS     = array('Sans Serif' => 'Arial, Helvetica, sans-serif', 'Serif' => 'Georgia, serif', 'Arial' => 'Arial, sans-serif',
                   'Georgia' => 'Georgia, serif', 'Tahoma' => 'Tahoma, sans-serif', 'Verdana' => 'Verdana, sans-serif',
                   'Courier New' => '"Courier New", monospace');
$SIZES     = array(9, 10, 11, 12, 13, 14, 16, 18);

/* Currencies. code => [name, symbol]; first entry in $CURRENCIES is the company currency */
$CURRENCY_LIST = array('USD' => array('US Dollar', '$'), 'INR' => array('Indian Rupee', '₹'), 'EUR' => array('Euro', '€'),
                       'GBP' => array('British Pound', '£'), 'AED' => array('UAE Dirham', 'AED'), 'SGD' => array('Singapore Dollar', 'S$'),
                       'AUD' => array('Australian Dollar', 'A$'), 'JPY' => array('Japanese Yen', '¥'), 'CAD' => array('Canadian Dollar', 'C$'));
$CURRENCIES = array(
  array('code' => 'USD', 'rate' => null, 'format' => '$123,456.78', 'updated' => ''),
);

/** <option> list helper: $selected compared to value. */
function opts($list, $selected, $useKeys = true) {
  $h = '';
  foreach ($list as $k => $v) {
    $val = $useKeys ? $k : $v;
    $h .= '<option value="' . e($val) . '"' . ((string) $val === (string) $selected ? ' selected' : '') . '>' . e($v) . '</option>';
  }
  return $h;
}

/** Label with a hover info icon. */
function info_label($for, $text, $tip) {
  return '<label class="field__label set-label" for="' . e($for) . '">' . e($text)
       . ' <span class="set-info" tabindex="0" data-tip-text="' . e($tip) . '" aria-label="' . e($tip) . '">' . icon('info', 'iconSize-16') . '</span></label>';
}

$page_title      = 'Account Defaults';
$page_css        = array('dropdown', 'settings', 'settings-pages');
$page_js         = array('settings', 'settings-pages');
$layout          = 'app';
$active_nav      = '';
$settings_active = 'account-defaults';
include __DIR__ . '/includes/header.php';
?>

<div class="settings">
  <?php include __DIR__ . '/includes/settings-nav.php'; ?>

  <div class="settings__main">
    <header class="set-head">
      <h1 class="set-head__title">Account Defaults</h1>
    </header>

    <nav class="set-tabs set-tabs--boxed" data-tabs role="tablist" aria-label="Account defaults">
      <button type="button" class="set-tab active" role="tab" aria-selected="true"  data-tab-target="#general">General</button>
      <button type="button" class="set-tab"        role="tab" aria-selected="false" data-tab-target="#user-defaults">User Defaults</button>
      <!-- <button type="button" class="set-tab"        role="tab" aria-selected="false" data-tab-target="#notification-profiles">Notification Profiles</button> -->
      <button type="button" class="set-tab"        role="tab" aria-selected="false" data-tab-target="#currency">Currency</button>
      <!-- <button type="button" class="set-tab"        role="tab" aria-selected="false" data-tab-target="#feature-releases">Feature Releases</button> -->
    </nav>

    <!-- ============================================================ GENERAL -->
    <section id="general" role="tabpanel">
      <p class="set-intro">These defaults will be applied to the entire account.</p>

      <form class="set-stack" data-settings-form novalidate>
        <div class="field">
          <?php echo info_label('ad-name', 'Account name', 'The name shown in the account menu and when you switch between accounts.'); ?>
          <input class="input" id="ad-name" name="account_name" value="<?php echo e($ACCOUNT['name']); ?>" required maxlength="100">
          <p class="field__error">Enter an account name</p>
        </div>

        <div class="field">
          <?php echo info_label('ad-tz', 'Time zone', 'Used for reports, scheduled emails, workflows and anywhere a user has not set their own time zone.'); ?>
          <select class="input" id="ad-tz" name="timezone"><?php echo opts($TIMEZONES, $ACCOUNT['timezone']); ?></select>
        </div>

        <div class="field">
          <?php echo info_label('ad-fy', 'Fiscal year', 'Sets the start month for fiscal-quarter and fiscal-year date ranges in reports and forecasts.'); ?>
          <select class="input" id="ad-fy" name="fiscal_year"><?php echo opts($FISCAL, $ACCOUNT['fiscal'], false); ?></select>
        </div>

        <div class="set-block">
          <h2 class="set-block__title">Company Information</h2>
          <p class="set-block__sub">This information will be used as a default where needed. If you're looking to update your company information for billing, visit <a class="link" href="account-billing.php">Account &amp; Billing</a>.</p>
        </div>

        <div class="field">
          <label class="field__label" for="co-name">Company name</label>
          <input class="input" id="co-name" name="company_name" value="<?php echo e($ACCOUNT['company']); ?>">
        </div>
        <div class="field">
          <label class="field__label" for="co-domain">Company domain</label>
          <input class="input" id="co-domain" name="company_domain" value="<?php echo e($ACCOUNT['domain']); ?>" placeholder="yourcompany.com">
        </div>
        <div class="field">
          <label class="field__label" for="co-addr">Company address</label>
          <input class="input" id="co-addr" name="address" value="<?php echo e($ACCOUNT['address']); ?>">
        </div>
        <div class="field">
          <label class="field__label" for="co-addr2">Company address line 2</label>
          <input class="input" id="co-addr2" name="address2" value="<?php echo e($ACCOUNT['address2']); ?>">
        </div>
        <div class="field">
          <label class="field__label" for="co-city">City</label>
          <input class="input" id="co-city" name="city" value="<?php echo e($ACCOUNT['city']); ?>">
        </div>
        <div class="field">
          <label class="field__label" for="co-state">State</label>
          <input class="input" id="co-state" name="state" value="<?php echo e($ACCOUNT['state']); ?>">
        </div>
        <div class="field">
          <label class="field__label" for="co-zip">Zip</label>
          <input class="input" id="co-zip" name="zip" value="<?php echo e($ACCOUNT['zip']); ?>" inputmode="numeric">
        </div>
        <div class="field">
          <label class="field__label" for="co-country">Country</label>
          <input class="input" id="co-country" name="country" value="<?php echo e($ACCOUNT['country']); ?>">
        </div>
        <div class="field">
          <?php echo info_label('co-industry', 'Industry', 'Helps tailor templates, benchmarks and recommendations to your business.'); ?>
          <select class="input" id="co-industry" name="industry">
            <option value="">Select an industry</option>
            <?php echo opts($INDUSTRIES, $ACCOUNT['industry'], false); ?>
          </select>
        </div>
      </form>
    </section>

    <!-- ====================================================== USER DEFAULTS -->
    <section id="user-defaults" class="crm-d-none" role="tabpanel">
      <p class="set-intro">Set defaults for users in your account. To add or edit user permissions go to <a class="link" href="settings-users.php">Users &amp; Teams</a>.</p>

      <form class="set-stack" data-settings-form novalidate>
        <p class="crm-border crm-p-3 crm-rounded-3 crm-w-100">To edit these settings, you must be an authorized user.</p>
        <div class="set-block">
          <h2 class="set-block__title">New user defaults</h2>
          <p class="set-block__sub">Set defaults for users added to your account. They can update this in user preferences.</p>
        </div>
        <div class="field">
          <?php echo info_label('ud-lang', 'Language', 'The interface language new users start with.'); ?>
          <select class="input" id="ud-lang" name="language"><?php echo opts($LANGUAGES, 'English', false); ?></select>
        </div><br>
        <div class="field">
          <?php echo info_label('ud-format', 'Date and number format', 'How dates and numbers are displayed for new users.'); ?>
          <p class="field__help set-format-preview" data-format-preview>Format: <?php echo e($FORMATS['en-US'][1]); ?></p>
          <select class="input" id="ud-format" name="format" data-format-select>
            <?php foreach ($FORMATS as $k => $f) { ?>
            <option value="<?php echo e($k); ?>" data-example="<?php echo e($f[1]); ?>"<?php echo $k === 'en-US' ? ' selected' : ''; ?>><?php echo e($f[0]); ?></option>
            <?php } ?>
          </select>
        </div>

        <div class="set-block">
          <h2 class="set-block__title">Notifications</h2>
          <p class="set-block__sub">Set notification defaults for all users in your account. They can update this in user preferences.</p>
          <a class="link crm-fw-semibold crm-d-inline-block crm-mt-3" href="settings-notifications.php">Manage account defaults</a>
          <p class="set-block__sub">Set up the default notifications for all users in your account who are not included in a profile.</p>
        </div>

        <div class="set-block">
          <h2 class="set-block__title">Email</h2>
          <p class="set-block__sub">This default will apply to outgoing emails from the CRM and Inbox email editors for all users of this account.</p>
        </div>
        <div class="field">
          <span class="field__label">Font</span>
          <div class="font-row">
            <select class="input" name="email_font" aria-label="Font" data-font-family>
              <?php foreach ($FONTS as $name => $stack) { ?><option value="<?php echo e($name); ?>" data-stack="<?php echo e($stack); ?>"<?php echo $name === 'Sans Serif' ? ' selected' : ''; ?>><?php echo e($name); ?></option><?php } ?>
            </select>
            <select class="input font-row__size" name="email_size" aria-label="Font size" data-font-size>
              <?php foreach ($SIZES as $s) { ?><option value="<?php echo $s; ?>"<?php echo $s === 11 ? ' selected' : ''; ?>><?php echo $s; ?> pt</option><?php } ?>
            </select>
            <input type="color" class="font-row__color" name="email_color" value="#444444" aria-label="Font color" data-font-color>
          </div>
          <p class="font-preview" data-font-preview>Hi Penelope, thanks for your time today. Here is the proposal we discussed.</p>
        </div>
      </form>
    </section>

    <!-- ============================================== NOTIFICATION PROFILES -->
    <section id="notification-profiles" class="crm-d-none" role="tabpanel">
      <p class="set-intro">These defaults will be used for user defaults and presets.</p>
      <div class="set-block">
        <h2 class="set-block__title">Notification Profiles</h2>
        <p class="set-block__sub">Set notification defaults for a group of users within a preset. To add or edit Presets go to <a class="link" href="settings-users.php">Presets</a>.</p>
      </div>
      <ul class="profile-list" data-profile-list></ul>
      <button type="button" class="btn-crm btn-crm--outline crm-mt-3" data-dialog-open="#profile-dialog">Create notification profile</button>
    </section>

    <!-- =========================================================== CURRENCY -->
    <section id="currency" class="crm-d-none" role="tabpanel">
      <p class="set-intro">These defaults will be used for deals and properties.</p>
 <p class="crm-border crm-p-3 crm-rounded-3 crm-w-100 crm-mb-3">To edit these settings, you must be an authorized user.</p>
      <div class="card set-card fx-card">
        <div>
          <p class="crm-fw-semibold">Schedule automatic exchange rate updates. <button type="button" class="link" data-help="exchange-rates">Learn more</button></p>
          <button type="button" class="btn-crm btn-crm--outline btn-crm--sm crm-mt-2" data-dialog-open="#ratelog-dialog">View history</button>
        </div>
        <label class="switch-labeled">
          <span data-switch-text>OFF</span>
          <span class="switch"><input type="checkbox" data-fx-toggle aria-label="Schedule automatic exchange rate updates"><span class="switch__track"></span></span>
        </label>
      </div>

      <div class="set-card__head">
        <h2 class="set-block__title">All Currencies</h2>
        <div class="set-head__actions crm-align-items-center">
          <span class="crm-text-body-secondary crm-fs-7">Currencies used: <span data-currency-count><?php echo count($CURRENCIES); ?></span> of 200</span>
          <button type="button" class="btn-crm btn-crm--outline btn-crm--sm" data-dialog-open="#ratelog-dialog">Exchange rate log</button>
          <button type="button" class="btn-crm btn-crm--primary btn-crm--sm" data-dialog-open="#currency-dialog">Add Currency</button>
        </div>
      </div>

      <div class="card set-table-card">
        <table class="set-table">
          <thead><tr><th>Name</th><th>Exchange rate</th><th>Format</th><th>Last updated</th><th class="col-actions"><span class="crm-visually-hidden">Actions</span></th></tr></thead>
          <tbody data-currency-rows>
            <?php foreach ($CURRENCIES as $i => $c) { $info = $CURRENCY_LIST[$c['code']]; ?>
            <tr data-code="<?php echo e($c['code']); ?>">
              <td class="crm-fw-semibold"><?php echo e($info[0] . ' (' . $c['code'] . ') ' . $info[1]); ?></td>
              <td><?php echo $i === 0 ? '<span class="badge badge--brand">Company Currency</span>' : e($c['rate']); ?></td>
              <td><?php echo e($c['format']); ?></td>
              <td class="crm-text-body-secondary"><?php echo e($c['updated'] !== '' ? $c['updated'] : '—'); ?></td>
              <td class="col-actions"><?php if ($i === 0) { ?><button type="button" class="link" data-cur-edit>Edit</button><?php } else { ?><div class="su-row-actions"><button type="button" class="link" data-cur-edit>Edit</button><button type="button" class="link su-danger" data-remove-currency>Remove</button></div><?php } ?></td>
            </tr>
            <?php } ?>
          </tbody>
        </table>
      </div>
    </section>

    <!-- =================================================== FEATURE RELEASES -->
    <section id="feature-releases" class="crm-d-none" role="tabpanel">
      <p class="set-intro">Set defaults related to Feature Releases.</p>
      <form data-settings-form>
        <div class="set-block set-block--wide">
          <h2 class="set-block__title">Gradual Releases</h2>
          <p class="set-block__sub">When new features and tools are released, you can opt in to get them at the end of the gradual release. This gives you more time to test changes and prepare users. Upcoming release dates can be found on the <button type="button" class="link" data-help="product-updates">Product Updates page</button>.</p>
          <label class="check-row">
            <span class="check"><input type="checkbox" name="late_releases"><span class="check__box" aria-hidden="true"></span></span>
            <span>Put this account at the end of gradual feature releases</span>
          </label>
          <p class="field__help check-row__note">Note: Opting into this setting will <strong>only</strong> apply to all future releases. Changes to this preference apply to the entire account.</p>
        </div>
        <div class="set-block set-block--wide">
          <h2 class="set-block__title">Email Notifications</h2>
          <p class="set-block__sub">Get weekly emails about the latest product updates. Changes to this preference will only apply to you.</p>
          <label class="check-row">
            <span class="check"><input type="checkbox" name="release_emails"><span class="check__box" aria-hidden="true"></span></span>
            <span>Turn on email notifications</span>
          </label>
        </div>
      </form>
    </section>
  </div>
</div>

<!-- Add currency -->
<dialog class="set-modal set-modal--drawer" id="currency-dialog" aria-labelledby="cur-title">
  <form id="currency-form" novalidate>
    <div class="set-modal__head">
      <h2 class="set-modal__title" id="cur-title">Add currency</h2>
      <button type="button" class="icon-btn icon-btn--ghost" data-dialog-close aria-label="Close"><?php echo icon('x', 'iconSize-20'); ?></button>
    </div>
    <div class="set-modal__body">
      <div class="field">
        <label class="field__label" for="cur-code">Currency</label>
        <select class="input" id="cur-code" name="code">
          <?php foreach ($CURRENCY_LIST as $code => $c) { if ($code === $CURRENCIES[0]['code']) continue; ?>
          <option value="<?php echo e($code); ?>" data-label="<?php echo e($c[0] . ' (' . $code . ') ' . $c[1]); ?>" data-symbol="<?php echo e($c[1]); ?>"><?php echo e($c[0] . ' (' . $code . ')'); ?></option>
          <?php } ?>
        </select>
      </div>
      <div class="field">
        <label class="field__label" for="cur-rate">Exchange rate</label>
        <input class="input" id="cur-rate" name="rate" type="number" step="any" min="0" required placeholder="e.g. 0.012">
        <p class="field__help">1 <?php echo e($CURRENCIES[0]['code']); ?> = ? of the new currency</p>
        <p class="field__error">Enter an exchange rate</p>
      </div>
    </div>
    <div class="set-modal__foot">
      <button type="button" class="btn-crm btn-crm--outline" data-dialog-close>Cancel</button>
      <button type="submit" class="btn-crm btn-crm--primary">Add currency</button>
    </div>
  </form>
</dialog>

<!-- Edit currency -->
<?php echo su_drawer('cur-edit-dialog', 'Edit currency',
  '<p class="crm-fw-semibold crm-mb-3" data-ce-name></p>'
  . su_field('Exchange rate', '<input class="input" id="ce-rate" name="rate" type="number" step="any" min="0" required>', '1 ' . e($CURRENCIES[0]['code']) . ' = ? of this currency. The company currency is always 1.', false, 'ce-rate')
  . su_field('Number format', '<select class="input" id="ce-format" name="format"><option value="1,234.56">1,234.56</option><option value="1.234,56">1.234,56</option><option value="1 234,56">1 234,56</option></select>', 'Used when amounts in this currency are shown.', false, 'ce-format'),
  'Save', 'Currency updated', 'id="cur-edit-form" data-keep'); ?>

<!-- Exchange rate log -->
<dialog class="set-modal set-modal--drawer" id="ratelog-dialog" aria-labelledby="rl-title">
  <div>
    <?php echo su_dialog_head('Exchange rate log', 'rl-title'); ?>
    <div class="set-modal__body">
      <p class="field__help crm-mb-3">Every change to an exchange rate in the last 12 months.</p>
      <div class="card set-table-card">
        <table class="set-table su-table">
          <thead><tr><th>Date</th><th>Currency</th><th>Old rate</th><th>New rate</th><th>Changed by</th></tr></thead>
          <tbody id="ratelog-rows">
            <tr><td>2 Oct 2026</td><td>EUR</td><td>0.91</td><td>0.92</td><td>Ashley Smith</td></tr>
            <tr><td>2 Oct 2026</td><td>GBP</td><td>0.78</td><td>0.79</td><td>Ashley Smith</td></tr>
            <tr><td>4 Sep 2026</td><td>INR</td><td>83.10</td><td>83.40</td><td>Victor Roberts</td></tr>
            <tr><td>1 Aug 2026</td><td>EUR</td><td>0.93</td><td>0.91</td><td>Automatic update</td></tr>
          </tbody>
        </table>
      </div>
    </div>
    <div class="set-modal__foot">
      <button type="button" class="btn-crm btn-crm--outline" data-download="exchange-rate-log.csv" data-download-table="#ratelog-dialog table">Download CSV</button>
      <button type="button" class="btn-crm btn-crm--primary" data-dialog-close>Done</button>
    </div>
  </div>
</dialog>

<!-- Create notification profile -->
<dialog class="set-modal set-modal--drawer" id="profile-dialog" aria-labelledby="np-title">
  <form id="profile-form" novalidate>
    <div class="set-modal__head">
      <h2 class="set-modal__title" id="np-title">Create notification profile</h2>
      <button type="button" class="icon-btn icon-btn--ghost" data-dialog-close aria-label="Close"><?php echo icon('x', 'iconSize-20'); ?></button>
    </div>
    <div class="set-modal__body">
      <div class="field">
        <label class="field__label" for="np-name">Profile name</label>
        <input class="input" id="np-name" name="name" required maxlength="80" placeholder="e.g. Sales reps">
        <p class="field__error">Enter a profile name</p>
      </div>
      <div class="field">
        <label class="field__label" for="np-preset">Apply to preset</label>
        <select class="input" id="np-preset" name="preset"><option>Sales</option><option>Marketing</option><option>Support</option><option>Admins</option></select>
      </div>
    </div>
    <div class="set-modal__foot">
      <button type="button" class="btn-crm btn-crm--outline" data-dialog-close>Cancel</button>
      <button type="submit" class="btn-crm btn-crm--primary">Create</button>
    </div>
  </form>
</dialog>

<div class="save-bar" data-save-bar role="region" aria-label="Unsaved changes">
  <span class="save-bar__text">You have unsaved changes</span>
  <button type="button" class="btn-crm btn-crm--outline" data-cancel>Cancel</button>
  <button type="button" class="btn-crm btn-crm--primary" data-save><span class="btn-crm__spinner" aria-hidden="true"></span>Save</button>
</div>

<?php include __DIR__ . '/includes/footer.php'; ?>
