<?php
$s = is_array($settings ?? null) ? $settings : [];
$hasToken = (bool) ($s['has_api_token'] ?? false);
$env = (string) ($s['environment'] ?? 'sandbox');
$dispatchMethod = (string) ($s['default_dispatch_method'] ?? 'pop');
$lockerSize = (string) ($s['default_locker_size'] ?? 'small');
$labelFormat = (string) ($s['label_format'] ?? 'Pdf');
?>

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

  <?php if (!empty($errorMessage)): ?>
    <div class="alert alert--danger mt-12" role="alert"><?= $e((string) $errorMessage) ?></div>
  <?php endif; ?>

  <?php if (!empty($successMessage)): ?>
    <div class="alert alert--success mt-12" role="status"><?= $e((string) $successMessage) ?></div>
  <?php endif; ?>
</section>

<section class="card mt-16">
  <h3 class="section-title"><?= $e($t('settings.inpost.config.title')) ?></h3>
  <form action="/settings/integrations/inpost/save" method="post" novalidate>
    <input type="hidden" name="_token" value="<?= $e($csrfToken ?? '') ?>">

    <label class="form-field mt-16">
      <span class="field-label"><?= $e($t('settings.inpost.fields.api_token')) ?></span>
      <input class="form-control" type="password" name="api_token" autocomplete="new-password">
      <span class="muted"><?= $e($hasToken ? $t('settings.inpost.api_token.saved') : $t('settings.inpost.api_token.missing')) ?></span>
    </label>

    <label class="form-field">
      <span class="field-label"><?= $e($t('settings.inpost.fields.organization_id')) ?></span>
      <input class="form-control" type="text" name="organization_id" value="<?= $e((string) ($s['organization_id'] ?? '')) ?>">
    </label>

    <label class="form-field">
      <span class="field-label"><?= $e($t('settings.inpost.fields.environment')) ?></span>
      <select class="form-control" name="environment">
        <option value="sandbox"<?= $env === 'sandbox' ? ' selected' : '' ?>>Sandbox</option>
        <option value="production"<?= $env === 'production' ? ' selected' : '' ?>>Production</option>
      </select>
    </label>

    <h4 class="section-title mt-16"><?= $e($t('settings.inpost.sections.dispatch')) ?></h4>

    <label class="form-field mt-12">
      <span class="field-label"><?= $e($t('settings.inpost.fields.default_dispatch_method')) ?></span>
      <select class="form-control" name="default_dispatch_method">
        <option value="pop"<?= $dispatchMethod === 'pop' ? ' selected' : '' ?>><?= $e($t('settings.inpost.dispatch_methods.pop')) ?></option>
        <option value="parcel_locker"<?= $dispatchMethod === 'parcel_locker' ? ' selected' : '' ?>><?= $e($t('settings.inpost.dispatch_methods.parcel_locker')) ?></option>
        <option value="courier"<?= $dispatchMethod === 'courier' ? ' selected' : '' ?>><?= $e($t('settings.inpost.dispatch_methods.courier')) ?></option>
      </select>
    </label>

    <label class="form-field">
      <span class="field-label"><?= $e($t('settings.inpost.fields.default_dispatch_point')) ?></span>
      <input class="form-control" type="text" name="default_dispatch_point" value="<?= $e((string) ($s['default_dispatch_point'] ?? '')) ?>" placeholder="np. RZE14N">
    </label>

    <h4 class="section-title mt-16"><?= $e($t('settings.inpost.sections.locker')) ?></h4>

    <label class="form-field mt-12">
      <span class="field-label"><?= $e($t('settings.inpost.fields.default_locker_size')) ?></span>
      <select class="form-control" name="default_locker_size">
        <option value="small"<?= $lockerSize === 'small' ? ' selected' : '' ?>>A (8 x 38 x 64 cm)</option>
        <option value="medium"<?= $lockerSize === 'medium' ? ' selected' : '' ?>>B (19 x 38 x 64 cm)</option>
        <option value="large"<?= $lockerSize === 'large' ? ' selected' : '' ?>>C (41 x 38 x 64 cm)</option>
      </select>
    </label>

    <label class="form-field">
      <span class="field-label"><?= $e($t('settings.inpost.fields.default_insurance')) ?></span>
      <input class="form-control" type="number" name="default_insurance" step="0.01" min="0" value="<?= $e($s['default_insurance'] !== null ? (string) $s['default_insurance'] : '') ?>" placeholder="<?= $e($t('settings.inpost.fields.insurance_placeholder')) ?>">
    </label>

    <h4 class="section-title mt-16"><?= $e($t('settings.inpost.sections.courier')) ?></h4>

    <div class="form-grid-3 mt-12">
      <label class="form-field">
        <span class="field-label"><?= $e($t('settings.inpost.fields.courier_length')) ?></span>
        <input class="form-control" type="number" name="default_courier_length" min="1" value="<?= $e((string) ($s['default_courier_length'] ?? 20)) ?>">
        <span class="muted">cm</span>
      </label>

      <label class="form-field">
        <span class="field-label"><?= $e($t('settings.inpost.fields.courier_width')) ?></span>
        <input class="form-control" type="number" name="default_courier_width" min="1" value="<?= $e((string) ($s['default_courier_width'] ?? 15)) ?>">
        <span class="muted">cm</span>
      </label>

      <label class="form-field">
        <span class="field-label"><?= $e($t('settings.inpost.fields.courier_height')) ?></span>
        <input class="form-control" type="number" name="default_courier_height" min="1" value="<?= $e((string) ($s['default_courier_height'] ?? 8)) ?>">
        <span class="muted">cm</span>
      </label>
    </div>

    <h4 class="section-title mt-16"><?= $e($t('settings.inpost.sections.other')) ?></h4>

    <label class="form-field mt-12">
      <span class="field-label"><?= $e($t('settings.inpost.fields.label_format')) ?></span>
      <select class="form-control" name="label_format">
        <option value="Pdf"<?= $labelFormat === 'Pdf' ? ' selected' : '' ?>>PDF A6</option>
        <option value="Zpl"<?= $labelFormat === 'Zpl' ? ' selected' : '' ?>>ZPL</option>
        <option value="Epl"<?= $labelFormat === 'Epl' ? ' selected' : '' ?>>EPL</option>
      </select>
    </label>

    <div class="mt-12">
      <label class="form-field form-field--inline">
        <input type="checkbox" name="weekend_delivery" value="1"<?= !empty($s['weekend_delivery']) ? ' checked' : '' ?>>
        <span class="field-label"><?= $e($t('settings.inpost.fields.weekend_delivery')) ?></span>
      </label>

      <label class="form-field form-field--inline">
        <input type="checkbox" name="auto_insurance_value" value="1"<?= !empty($s['auto_insurance_value']) ? ' checked' : '' ?>>
        <span class="field-label"><?= $e($t('settings.inpost.fields.auto_insurance_value')) ?></span>
      </label>

      <label class="form-field form-field--inline">
        <input type="checkbox" name="multi_parcel" value="1"<?= !empty($s['multi_parcel']) ? ' checked' : '' ?>>
        <span class="field-label"><?= $e($t('settings.inpost.fields.multi_parcel')) ?></span>
      </label>
    </div>

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