<?php
$s = is_array($settings ?? null) ? $settings : [];
?>

<section class="card">
  <h2 class="section-title"><?= $e($t('settings.company.title')) ?></h2>
  <p class="muted mt-12"><?= $e($t('settings.company.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">
  <form action="/settings/company/save" method="post" novalidate>
    <input type="hidden" name="_token" value="<?= $e($csrfToken ?? '') ?>">

    <h3 class="section-title"><?= $e($t('settings.company.section_address')) ?></h3>

    <div class="form-grid-2 mt-12">
      <label class="form-field">
        <span class="field-label"><?= $e($t('settings.company.fields.company_name')) ?></span>
        <input class="form-control" type="text" name="company_name" maxlength="200" value="<?= $e((string) ($s['company_name'] ?? '')) ?>">
      </label>
      <label class="form-field">
        <span class="field-label"><?= $e($t('settings.company.fields.person_name')) ?></span>
        <input class="form-control" type="text" name="person_name" maxlength="200" value="<?= $e((string) ($s['person_name'] ?? '')) ?>">
      </label>
    </div>

    <label class="form-field mt-0">
      <span class="field-label"><?= $e($t('settings.company.fields.sender_contact_person')) ?></span>
      <input class="form-control" type="text" name="sender_contact_person" maxlength="200" value="<?= $e((string) ($s['sender_contact_person'] ?? '')) ?>">
    </label>

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

    <div class="form-grid-3 mt-0">
      <label class="form-field">
        <span class="field-label"><?= $e($t('settings.company.fields.postal_code')) ?></span>
        <input class="form-control" type="text" name="postal_code" maxlength="16" value="<?= $e((string) ($s['postal_code'] ?? '')) ?>">
      </label>
      <label class="form-field">
        <span class="field-label"><?= $e($t('settings.company.fields.city')) ?></span>
        <input class="form-control" type="text" name="city" maxlength="128" value="<?= $e((string) ($s['city'] ?? '')) ?>">
      </label>
      <label class="form-field">
        <span class="field-label"><?= $e($t('settings.company.fields.country_code')) ?></span>
        <input class="form-control" type="text" name="country_code" maxlength="2" value="<?= $e((string) ($s['country_code'] ?? 'PL')) ?>">
      </label>
    </div>

    <div class="form-grid-2">
      <label class="form-field">
        <span class="field-label"><?= $e($t('settings.company.fields.phone')) ?></span>
        <input class="form-control" type="tel" name="phone" maxlength="64" value="<?= $e((string) ($s['phone'] ?? '')) ?>">
      </label>
      <label class="form-field">
        <span class="field-label"><?= $e($t('settings.company.fields.email')) ?></span>
        <input class="form-control" type="email" name="email" maxlength="128" value="<?= $e((string) ($s['email'] ?? '')) ?>">
      </label>
    </div>

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

    <div class="form-grid-3 mt-0">
      <label class="form-field">
        <span class="field-label">REGON</span>
        <input class="form-control" type="text" name="regon" maxlength="14" value="<?= $e((string) ($s['regon'] ?? '')) ?>">
      </label>
      <label class="form-field">
        <span class="field-label">Numer BDO</span>
        <input class="form-control" type="text" name="bdo_number" maxlength="20" value="<?= $e((string) ($s['bdo_number'] ?? '')) ?>">
      </label>
      <label class="form-field">
        <span class="field-label">KRS / Wpis do ewidencji</span>
        <input class="form-control" type="text" name="court_register" maxlength="128" value="<?= $e((string) ($s['court_register'] ?? '')) ?>">
      </label>
    </div>

    <label class="form-field">
      <span class="field-label">Logo firmy (sciezka)</span>
      <input class="form-control" type="text" name="logo_path" maxlength="255" placeholder="np. uploads/logo.png" value="<?= $e((string) ($s['logo_path'] ?? '')) ?>">
    </label>

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

    <div class="form-grid-2 mt-12">
      <label class="form-field">
        <span class="field-label"><?= $e($t('settings.company.fields.bank_account')) ?></span>
        <input class="form-control" type="text" name="bank_account" maxlength="64" value="<?= $e((string) ($s['bank_account'] ?? '')) ?>">
      </label>
      <label class="form-field">
        <span class="field-label"><?= $e($t('settings.company.fields.bank_owner_name')) ?></span>
        <input class="form-control" type="text" name="bank_owner_name" maxlength="200" value="<?= $e((string) ($s['bank_owner_name'] ?? '')) ?>">
      </label>
    </div>

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

    <div class="form-grid-4 mt-12">
      <label class="form-field">
        <span class="field-label"><?= $e($t('settings.company.fields.length_cm')) ?></span>
        <input class="form-control" type="number" name="default_package_length_cm" step="0.1" min="0.1" value="<?= $e((string) ($s['default_package_length_cm'] ?? '25')) ?>">
      </label>
      <label class="form-field">
        <span class="field-label"><?= $e($t('settings.company.fields.width_cm')) ?></span>
        <input class="form-control" type="number" name="default_package_width_cm" step="0.1" min="0.1" value="<?= $e((string) ($s['default_package_width_cm'] ?? '20')) ?>">
      </label>
      <label class="form-field">
        <span class="field-label"><?= $e($t('settings.company.fields.height_cm')) ?></span>
        <input class="form-control" type="number" name="default_package_height_cm" step="0.1" min="0.1" value="<?= $e((string) ($s['default_package_height_cm'] ?? '8')) ?>">
      </label>
      <label class="form-field">
        <span class="field-label"><?= $e($t('settings.company.fields.weight_kg')) ?></span>
        <input class="form-control" type="number" name="default_package_weight_kg" step="0.001" min="0.001" value="<?= $e((string) ($s['default_package_weight_kg'] ?? '1')) ?>">
      </label>
    </div>

    <label class="form-field">
      <span class="field-label"><?= $e($t('settings.company.fields.label_format')) ?></span>
      <select class="form-control" name="default_label_format">
        <option value="PDF"<?= ((string) ($s['default_label_format'] ?? 'PDF')) === 'PDF' ? ' selected' : '' ?>>PDF</option>
        <option value="ZPL"<?= ((string) ($s['default_label_format'] ?? 'PDF')) === 'ZPL' ? ' selected' : '' ?>>ZPL</option>
      </select>
    </label>

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