<?php
if (!defined('ABSPATH')) exit;
if (!current_user_can('manage_options')) wp_die('Unauthorized');

require_once plugin_dir_path(__DIR__) . 'global-field-map-helpers.php';

$pat_id = isset($_GET['pat_id']) ? (int)$_GET['pat_id'] : 0;
$account = afb_gf_get_account($pat_id);

if (!$account) {
    echo '<div class="wrap"><h1>Global Field Map Editor</h1><div class="notice notice-error"><p>Invalid or missing PAT account.</p></div></div>';
    return;
}

$initial = [
    'pat_id' => $pat_id,
    'workspace_gid' => preg_replace('/\D+/', '', (string)($_GET['workspace_gid'] ?? '')),
    'target' => ((string)($_GET['target'] ?? 'workspace') === 'portfolio') ? 'portfolio' : 'workspace',
    'portfolio_gid' => preg_replace('/\D+/', '', (string)($_GET['portfolio_gid'] ?? '')),
];

$editor_cfg = [
  'ajaxUrl' => admin_url('admin-ajax.php'),
  'nonce' => wp_create_nonce('afb_gf_editor_nonce'),
  'patId' => $pat_id,
];
?>
<div class="wrap">
  <h1>Global Field Map Editor</h1>
  <p class="description" style="max-width: 980px; margin-top: 8px;">
    Account: <strong><?= esc_html($account['label'] !== '' ? $account['label'] : ('Account #' . $account['id'])) ?></strong>
    · ID <?= esc_html((string)$account['id']) ?>
    <?php if (!empty($account['portfolio_gid'])): ?>
      · Default Portfolio <?= esc_html((string)$account['portfolio_gid']) ?>
    <?php endif; ?>
  </p>

  <p style="margin-top: 10px;">
    <a href="<?= esc_url(admin_url('admin.php?page=afb-global-field-map')) ?>" class="button">Back To Account List</a>
  </p>

  <div id="afb-gf-editor" class="card" style="max-width: 1100px; margin-top: 12px; padding: 18px;">
    <table class="form-table" role="presentation" style="margin-top: 0;">
      <tr>
        <th scope="row"><label for="afb-gf-workspace">Workspace</label></th>
        <td>
          <select id="afb-gf-workspace" class="regular-text"><option value="">Loading workspaces...</option></select>
        </td>
      </tr>
      <tr>
        <th scope="row"><label for="afb-gf-target">Save mapping for</label></th>
        <td>
          <select id="afb-gf-target">
            <option value="workspace">Workspace default</option>
            <option value="portfolio">A specific portfolio</option>
          </select>
        </td>
      </tr>
      <tr id="afb-gf-portfolio-row">
        <th scope="row"><label for="afb-gf-portfolio">Portfolio</label></th>
        <td>
          <select id="afb-gf-portfolio" class="regular-text"><option value="">Loading portfolios...</option></select>
        </td>
      </tr>
    </table>

    <hr>

    <h2 style="margin-top: 0;">Identity Mapping</h2>
    <table class="form-table" role="presentation" style="margin-top: 0;">
      <tr>
        <th scope="row">First Name</th>
        <td>
          <select id="afb-gf-first-select" class="regular-text"><option value="">Loading field list...</option></select>
        </td>
      </tr>
      <tr>
        <th scope="row">Last Name</th>
        <td>
          <select id="afb-gf-last-select" class="regular-text"><option value="">Loading field list...</option></select>
        </td>
      </tr>
      <tr>
        <th scope="row">Email</th>
        <td>
          <select id="afb-gf-email-select" class="regular-text"><option value="">Loading field list...</option></select>
        </td>
      </tr>
    </table>

    <h2>Gateway Mapping</h2>
    <table class="form-table" role="presentation" style="margin-top: 0;">
      <tr>
        <th scope="row">Create Client Gateway</th>
        <td>
          <select id="afb-gf-create-select" class="regular-text"><option value="">Loading field list...</option></select>
        </td>
      </tr>
      <tr>
        <th scope="row">Client Gateway Link</th>
        <td>
          <select id="afb-gf-link-select" class="regular-text"><option value="">Loading field list...</option></select>
        </td>
      </tr>
      <tr>
        <th scope="row">Gateway PIN</th>
        <td>
          <select id="afb-gf-pin-select" class="regular-text"><option value="">Loading field list...</option></select>
        </td>
      </tr>
    </table>

    <p class="description" id="afb-gf-fields-note" style="margin-top: 8px;"></p>

    <p style="margin-top: 14px;">
      <button id="afb-gf-save" type="button" class="button button-primary" disabled>Save Mapping</button>
    </p>

    <div id="afb-gf-status" style="margin-top: 10px;"></div>
  </div>
</div>

<script>
window.afbGlobalFieldMapCfg = window.afbGlobalFieldMapCfg || <?= wp_json_encode($editor_cfg) ?>;
window.afbGlobalFieldMapInitial = <?= wp_json_encode($initial) ?>;
</script>
