<?php
/*
  Released under the GNU General Public License
*/
// Check to ensure this file is included in osConcert!
defined('_FEXEC') or die();

if (CLOSE_LOGIN == 'no') {
  include(DIR_WS_INCLUDES . 'general.js');
  include(DIR_WS_INCLUDES . 'javascript/http.js');
  include(DIR_WS_INCLUDES . 'password_strength.js.php');
  include(DIR_WS_INCLUDES . 'javascript/customer_account.js');

  // ----------------------------
  // GUEST / PWA flags
  // ----------------------------
  $pwa_enabled = (PURCHASE_WITHOUT_ACCOUNT == 'yes' || PURCHASE_NO_ACCOUNT == 'yes');
  $is_guest    = $pwa_enabled && (isset($_GET['guest']) || isset($_POST['guest']));

  // Carry guest param to the create page only when needed
  $guest_logon = $is_guest ? 'guest=guest' : '';

  echo tep_draw_form(
    'account',
    tep_href_link(FILENAME_CREATE_ACCOUNT_NEW, $guest_logon, 'SSL'),
    'POST',
    'enctype="multipart/form-data"'
  ) . tep_draw_hidden_field('action', 'process');

  // ----------------------------
  // Sign-up visibility
  // - Hide if seatplan login is enforced and user is not logged in
  // - But show if HIDE_SIGNUP == 'false'
  // ----------------------------
  // NOTE: adapt this check to your session API if needed.
  $is_logged_in = !empty($FSESSION->customer_id) || !empty($customer_id);
  $signup_blocked = (SEATPLAN_LOGIN_ENFORCED == 'true' && !$is_logged_in);

  $show_signup = true;
  if ($signup_blocked) $show_signup = false;
  if (HIDE_SIGNUP == 'false') $show_signup = true;

  $signup_class = $show_signup ? '' : 'd-none';

  // ----------------------------
  // Heading (normal vs guest/PWA)
  // ----------------------------
  ?>
  <div class="section-header">
    <h2><?php echo $is_guest ? HEADING_TITLE_PWA : HEADING_TITLE; ?></h2>
  </div>

  <div class="index_static">
    <?php
    // Messages
    if ($messageStack->size('account') > 0) {
      echo $messageStack->output('account');
    }

    // Login prompt + required info (only when PURCHASE_NO_ACCOUNT == 'no')
 if (PURCHASE_NO_ACCOUNT == 'no'): ?>
  <div class="alert alert-warning d-flex justify-content-between align-items-center py-2 px-3 mb-2 small">
    
    <div class="mb-0">
      <strong>Note:</strong>
      <?php echo sprintf(TEXT_ORIGIN_LOGIN, tep_href_link(FILENAME_LOGIN, tep_get_all_get_params(), 'SSL')); ?>
    </div>

    <div class="text-nowrap">
      <span class="text-danger">*</span> <?php echo FORM_REQUIRED_INFORMATION; ?>
    </div>

  </div>
<?php endif; ?>


    <!-- Signup block -->
    <div class="<?php echo $signup_class; ?>">
      <?php
      // Render fields
      // If guest (PWA), show only required fields; otherwise show all.
      $only_required = ($is_guest && PURCHASE_WITHOUT_ACCOUNT == 'yes');

      for ($i = 0, $n = count($fieldsDesc); $i < $n; $i++) {
        $fieldDesc = &$fieldsDesc[$i];

        if (!isset($ACCOUNT[$fieldDesc['uniquename']])) {
          $ACCOUNT[$fieldDesc['uniquename']] = $fieldDesc['default_value'];
        }

        if ($only_required && ($fieldDesc['required'] !== 'Y')) {
          continue; // skip non-required fields in guest mode
        }

        // Choose row id (kept for compatibility, if used by your renderer)
        $row_id = ($fieldDesc['input_type'] == 'L') ? 'title_row' : 'rec_row';

        if (method_exists($customerAccount, "edit__" . $fieldDesc['uniquename'])) {
          $customerAccount->{"edit__" . $fieldDesc['uniquename']}($fieldDesc);
        } else {
          $customerAccount->commonInput($fieldDesc);
        }
      }
      ?>
    </div>

    <div class="mt-3 d-flex justify-content-end">
      <?php echo tep_template_image_button_val('', IMAGE_BUTTON_CONTINUE, ' onClick="javascript:validateForm();"'); ?>
    </div>

    </div><!-- /.index_static -->
  </form>
<?php } // CLOSE_LOGIN == 'no' ?>
