<?php
$settings = is_array($settings ?? null) ? $settings : [];
$authMethod = (string) ($settings['auth_method'] ?? 'token');
$sender = trim((string) ($settings['sender'] ?? ''));
$senderMode = (string) ($settings['sender_mode'] ?? 'text');
$senderPhone = trim((string) ($settings['sender_phone'] ?? ''));
$defaultFooter = trim((string) ($settings['default_footer'] ?? ''));
$hasApiToken = (bool) ($settings['has_api_token'] ?? false);
$hasApiKey = (bool) ($settings['has_api_key'] ?? false);
$hasApiPassword = (bool) ($settings['has_api_password'] ?? false);
$isActive = (bool) ($settings['is_active'] ?? true);
$clearPolish = (bool) ($settings['clear_polish'] ?? false);
$transactional = (bool) ($settings['transactional'] ?? false);
$lastTestAt = trim((string) ($settings['last_test_at'] ?? ''));
$lastTestStatus = trim((string) ($settings['last_test_status'] ?? ''));
$lastTestMessage = trim((string) ($settings['last_test_message'] ?? ''));
$lastTestHttpCode = $settings['last_test_http_code'] ?? null;
$lastMessageId = '';
if (str_starts_with($lastTestMessage, 'messageId:')) {
    $lastMessageId = trim(substr($lastTestMessage, strlen('messageId:')));
}
?>

<section class="card">
  <h2 class="section-title"><?= $e($t('settings.smsplanet.title')) ?></h2>
  <p class="muted mt-12"><?= $e($t('settings.smsplanet.description')) ?></p>

  <?php if (!empty($errorMessage)): ?>
    <div class="mt-12"><?php $type='danger'; $message=(string) $errorMessage; $dismissible=true; include dirname(__DIR__) . '/components/alert.php'; ?></div>
  <?php endif; ?>

  <?php if (!empty($successMessage)): ?>
    <div class="mt-12"><?php $type='success'; $message=(string) $successMessage; $dismissible=true; include dirname(__DIR__) . '/components/alert.php'; ?></div>
  <?php endif; ?>

  <?php if (!empty($testMessage)): ?>
    <div class="mt-12"><?php $type='info'; $message=(string) $testMessage; $dismissible=true; include dirname(__DIR__) . '/components/alert.php'; ?></div>
  <?php endif; ?>
</section>

<section class="card mt-16">
  <h3 class="section-title"><?= $e($t('settings.smsplanet.config.title')) ?></h3>

  <div class="muted mt-12">
    <?= $e($t('settings.smsplanet.status.secret')) ?>:
    <strong><?= $e(($hasApiToken || $hasApiPassword) ? $t('settings.smsplanet.status.saved') : $t('settings.smsplanet.status.missing')) ?></strong>
    |
    <?= $e($t('settings.smsplanet.status.active')) ?>:
    <strong><?= $e($isActive ? $t('settings.integrations_hub.active.yes') : $t('settings.integrations_hub.active.no')) ?></strong>
  </div>

  <form class="statuses-form mt-16" action="/settings/integrations/smsplanet/save" method="post" novalidate>
    <input type="hidden" name="_token" value="<?= $e($csrfToken ?? '') ?>">

    <fieldset class="integration-settings-checkboxes">
      <legend class="field-label"><?= $e($t('settings.smsplanet.fields.auth_method')) ?></legend>
      <div class="integration-settings-checkboxes__list">
        <label class="integration-settings-checkboxes__item">
          <input type="radio" name="auth_method" value="token"<?= $authMethod === 'token' ? ' checked' : '' ?>>
          <span><?= $e($t('settings.smsplanet.auth.token')) ?></span>
        </label>
        <label class="integration-settings-checkboxes__item">
          <input type="radio" name="auth_method" value="key_password"<?= $authMethod === 'key_password' ? ' checked' : '' ?>>
          <span><?= $e($t('settings.smsplanet.auth.key_password')) ?></span>
        </label>
      </div>
      <span class="muted"><?= $e($t('settings.smsplanet.hints.auth_method')) ?></span>
    </fieldset>

    <label class="form-field">
      <span class="field-label"><?= $e($t('settings.smsplanet.fields.api_token')) ?></span>
      <input class="form-control" type="password" name="api_token" autocomplete="new-password" placeholder="<?= $hasApiToken ? '********' : '' ?>">
      <span class="muted"><?= $e($hasApiToken ? $t('settings.smsplanet.token.saved') : $t('settings.smsplanet.token.missing')) ?></span>
    </label>

    <label class="form-field">
      <span class="field-label"><?= $e($t('settings.smsplanet.fields.api_key')) ?></span>
      <input class="form-control" type="password" name="api_key" autocomplete="new-password" placeholder="<?= $hasApiKey ? '********' : '' ?>">
      <span class="muted"><?= $e($hasApiKey ? $t('settings.smsplanet.key.saved') : $t('settings.smsplanet.key.missing')) ?></span>
    </label>

    <label class="form-field">
      <span class="field-label"><?= $e($t('settings.smsplanet.fields.api_password')) ?></span>
      <input class="form-control" type="password" name="api_password" autocomplete="new-password" placeholder="<?= $hasApiPassword ? '********' : '' ?>">
      <span class="muted"><?= $e($hasApiPassword ? $t('settings.smsplanet.password.saved') : $t('settings.smsplanet.password.missing')) ?></span>
    </label>

    <label class="form-field">
      <span class="field-label"><?= $e($t('settings.smsplanet.fields.sender')) ?></span>
      <input class="form-control" type="text" name="sender" maxlength="32" value="<?= $e($sender) ?>" required>
      <span class="muted"><?= $e($t('settings.smsplanet.hints.sender')) ?></span>
    </label>

    <fieldset class="integration-settings-checkboxes">
      <legend class="field-label"><?= $e($t('settings.smsplanet.fields.sender_mode')) ?></legend>
      <div class="integration-settings-checkboxes__list">
        <label class="integration-settings-checkboxes__item">
          <input type="radio" name="sender_mode" value="text"<?= $senderMode !== 'phone' ? ' checked' : '' ?>>
          <span><?= $e($t('settings.smsplanet.sender_modes.text')) ?></span>
        </label>
        <label class="integration-settings-checkboxes__item">
          <input type="radio" name="sender_mode" value="phone"<?= $senderMode === 'phone' ? ' checked' : '' ?>>
          <span><?= $e($t('settings.smsplanet.sender_modes.phone')) ?></span>
        </label>
      </div>
      <span class="muted"><?= $e($t('settings.smsplanet.hints.sender_mode')) ?></span>
    </fieldset>

    <label class="form-field smsplanet-sender-phone-field">
      <span class="field-label"><?= $e($t('settings.smsplanet.fields.sender_phone')) ?></span>
      <input class="form-control" type="tel" name="sender_phone" inputmode="tel" maxlength="32" value="<?= $e($senderPhone) ?>" placeholder="48600111222">
      <span class="muted"><?= $e($t('settings.smsplanet.hints.sender_phone')) ?></span>
    </label>

    <label class="form-field smsplanet-default-footer-field">
      <span class="field-label"><?= $e($t('settings.smsplanet.fields.default_footer')) ?></span>
      <textarea class="form-control" name="default_footer" rows="3" maxlength="300"><?= $e($defaultFooter) ?></textarea>
      <span class="muted"><?= $e($t('settings.smsplanet.hints.default_footer')) ?></span>
    </label>

    <fieldset class="integration-settings-checkboxes">
      <legend class="field-label"><?= $e($t('settings.smsplanet.fields.options')) ?></legend>
      <div class="integration-settings-checkboxes__list">
        <label class="integration-settings-checkboxes__item">
          <input type="checkbox" name="clear_polish" value="1"<?= $clearPolish ? ' checked' : '' ?>>
          <span><?= $e($t('settings.smsplanet.fields.clear_polish')) ?></span>
        </label>
        <label class="integration-settings-checkboxes__item">
          <input type="checkbox" name="transactional" value="1"<?= $transactional ? ' checked' : '' ?>>
          <span><?= $e($t('settings.smsplanet.fields.transactional')) ?></span>
        </label>
        <label class="integration-settings-checkboxes__item">
          <input type="checkbox" name="is_active" value="1"<?= $isActive ? ' checked' : '' ?>>
          <span><?= $e($t('settings.smsplanet.fields.is_active')) ?></span>
        </label>
      </div>
    </fieldset>

    <div class="form-actions mt-16">
      <button type="submit" class="btn btn--primary"><?= $e($t('settings.smsplanet.actions.save')) ?></button>
    </div>
  </form>
</section>

<section class="card mt-16">
  <h3 class="section-title"><?= $e($t('settings.smsplanet.test.title')) ?></h3>
  <p class="muted mt-12"><?= $e($t('settings.smsplanet.test.description')) ?></p>

  <form class="statuses-form mt-16" action="/settings/integrations/smsplanet/test" method="post" novalidate>
    <input type="hidden" name="_token" value="<?= $e($csrfToken ?? '') ?>">

    <label class="form-field">
      <span class="field-label"><?= $e($t('settings.smsplanet.fields.test_phone')) ?></span>
      <input class="form-control" type="tel" name="phone" inputmode="tel" placeholder="48600111222" required>
    </label>

    <label class="form-field">
      <span class="field-label"><?= $e($t('settings.smsplanet.fields.test_message')) ?></span>
      <textarea class="form-control" name="message" rows="4" maxlength="918" required>Test orderPRO SMSPLANET</textarea>
      <?php if ($defaultFooter !== ''): ?>
        <span class="muted"><?= $e($t('settings.smsplanet.hints.test_footer')) ?></span>
      <?php endif; ?>
    </label>

    <div class="form-actions mt-16">
      <button type="submit" class="btn btn--secondary"><?= $e($t('settings.smsplanet.actions.send_test')) ?></button>
    </div>
  </form>

  <?php if ($lastTestAt !== ''): ?>
    <div class="alert <?= $lastTestStatus === 'ok' ? 'alert--success' : 'alert--danger' ?> mt-16" role="status">
      <div>
        <strong><?= $e($t('settings.smsplanet.status.last_test')) ?>:</strong>
        <?= $e($lastTestAt) ?>
        <?php if ($lastTestStatus !== ''): ?>
          <span class="badge badge--<?= $lastTestStatus === 'ok' ? 'success' : 'muted' ?>"><?= $e(strtoupper($lastTestStatus)) ?></span>
        <?php endif; ?>
        <?php if ($lastTestHttpCode !== null): ?>
          <span class="badge badge--muted">HTTP <?= $e((string) $lastTestHttpCode) ?></span>
        <?php endif; ?>
      </div>
      <?php if ($lastMessageId !== ''): ?>
        <div class="mt-12">
          <?= $e($t('settings.smsplanet.status.message_id')) ?>:
          <code><?= $e($lastMessageId) ?></code>
        </div>
      <?php elseif ($lastTestMessage !== ''): ?>
        <div class="mt-12"><?= $e($lastTestMessage) ?></div>
      <?php endif; ?>
    </div>
  <?php endif; ?>
</section>
