<?php
$printOptions = is_array($print_options ?? null) ? $print_options : [];
$showSignatures = !array_key_exists('show_signatures', $printOptions) || !empty($printOptions['show_signatures']);
$printedAt = date('Y-m-d H:i');
$footerNote = trim((string)($footer['note'] ?? ''));
$signatureStyle = trim((string)($footer['signature_style'] ?? ''));
$signatures = $footer['signatures'] ?? [
  ['label' => 'اعتماد الشركة'],
  ['label' => 'توقيع العميل'],
];
if ($signatures === []) {
    $signatures = [
        ['label' => 'اعتماد الشركة'],
        ['label' => 'توقيع العميل'],
    ];
}

$companyName = trim((string)($company['name'] ?? ''));
$companyAddress = trim((string)($company['address'] ?? ''));
$companyPhone = trim((string)($company['phone'] ?? ''));
$companyEmail = trim((string)($company['email'] ?? ''));
$companyWebsite = trim((string)($company['website'] ?? ''));
$companyContactParts = array_values(array_filter([
    $companyPhone,
    $companyEmail,
    $companyWebsite,
], static fn($value) => trim((string)$value) !== ''));
$footerSections = print_sections_ordered([
    'signatures' => ['enabled' => $showSignatures, 'title' => 'التوقيعات', 'order' => 10],
    'company_strip' => ['enabled' => true, 'title' => 'بيانات الشركة', 'order' => 20],
    'footer_note' => ['enabled' => true, 'title' => 'ملاحظات ختامية', 'order' => 30],
], $printOptions, 'footer');
?>
<?php $isReportPrint = !empty($report_mode) || !empty(($print_options['report_mode'] ?? false)); ?>
<?php if ($isReportPrint): ?>
<div class="report-print-footer">
  <div class="report-footer-note"><?= e($footerNote !== '' ? $footerNote : 'هذا التقرير صادر من مركز التقارير ويطابق نسخة الطباعة ونسخة PDF.') ?></div>
  <div class="report-footer-line">
    <?php if ($companyName !== ''): ?><span><?= e($companyName) ?></span><?php endif; ?>
    <?php if ($companyAddress !== ''): ?><span><?= e($companyAddress) ?></span><?php endif; ?>
    <?php foreach ($companyContactParts as $part): ?><span><?= e($part) ?></span><?php endforeach; ?>
    <span>تاريخ الطباعة: <?= e($printedAt) ?></span>
  </div>
</div>
<?php elseif (($print_format ?? '') === 'receipt'): ?>
<div class="print-footer receipt-footer">
  <div class="receipt-footer-bottom">
    <div>تاريخ الطباعة: <?= e($printedAt) ?></div>
  </div>
</div>
<?php elseif (($print_template ?? '') === 'quotation_v2'): ?>
<div class="print-footer quotation-print-footer">
  <?php foreach ($footerSections as $sectionKey => $section): if (empty($section['enabled'])) { continue; } ?>
    <?php if ($sectionKey === 'signatures' && $showSignatures): ?>
      <div class="qpf-signatures">
        <div class="qpf-sign-item">
          <div class="qpf-sign-label"><?= e(print_section_title($printOptions, 'footer', 'signatures', 'اعتماد الشركة')) ?></div>
          <div class="qpf-sign-line"></div>
        </div>
        <div class="qpf-sign-item">
          <div class="qpf-sign-label">اعتماد العميل</div>
          <div class="qpf-sign-line"></div>
        </div>
      </div>
    <?php elseif ($sectionKey === 'company_strip' && ($companyName !== '' || $companyAddress !== '' || $companyContactParts !== [])): ?>
      <div class="qpf-company-line">
        <?php if ($companyName !== ''): ?><span class="qpf-company-name"><?= e($companyName) ?></span><?php endif; ?>
        <?php if ($companyAddress !== ''): ?><span><?= e($companyAddress) ?></span><?php endif; ?>
        <?php foreach ($companyContactParts as $part): ?><span><?= e($part) ?></span><?php endforeach; ?>
      </div>
    <?php elseif ($sectionKey === 'footer_note'): ?>
      <div class="qpf-bottom-note">
        <div><?= e($footerNote !== '' ? $footerNote : 'شكراً لتعاملكم معنا.') ?></div>
        <div><?= e($printedAt) ?></div>
      </div>
    <?php endif; ?>
  <?php endforeach; ?>
</div>
<?php else: ?>
<div class="print-footer">
  <?php foreach ($footerSections as $sectionKey => $section): if (empty($section['enabled'])) { continue; } ?>
    <?php if ($sectionKey === 'signatures' && $showSignatures): ?>
      <?php if ($signatureStyle === 'voucher'): ?>
        <div class="voucher-signature-cards">
          <?php foreach ($signatures as $s): ?>
            <div class="voucher-signature-card">
              <div class="voucher-signature-title"><?= e($s['label'] ?? '') ?></div>
              <div class="voucher-signature-line"></div>
            </div>
          <?php endforeach; ?>
        </div>
      <?php else: ?>
        <div class="pf-signatures">
          <?php foreach ($signatures as $s): ?>
            <div class="pf-sign">
              <div class="pf-sign-title"><?= e($s['label'] ?? '') ?></div>
              <div class="pf-line"></div>
            </div>
          <?php endforeach; ?>
        </div>
      <?php endif; ?>
    <?php elseif ($sectionKey === 'company_strip' && ($companyName !== '' || $companyAddress !== '' || $companyContactParts !== [])): ?>
      <div class="pf-company-strip">
        <?php if ($companyName !== ''): ?><div class="pf-company-name"><?= e($companyName) ?></div><?php endif; ?>
        <?php if ($companyAddress !== ''): ?><div class="pf-company-address"><?= e($companyAddress) ?></div><?php endif; ?>
        <?php if ($companyContactParts !== []): ?><div class="pf-company-contact"><?= e(implode(' | ', $companyContactParts)) ?></div><?php endif; ?>
      </div>
    <?php elseif ($sectionKey === 'footer_note'): ?>
      <div class="pf-bottom">
        <div><?= e($footerNote !== '' ? $footerNote : 'هذا المستند صادر من النظام.') ?></div>
        <div class="pf-bottom-right"><?= e($printedAt) ?></div>
      </div>
    <?php endif; ?>
  <?php endforeach; ?>
</div>
<?php endif; ?>
