<?php /** @var array $document */ /** @var array $errors */ /** @var array $nav_items */ /** @var string $active_page */ /** @var string $save_route */ ?>
<div class="container-fluid py-4">
  <div class="row">
    <div class="col-12">
      <?php if ($errors): ?>
        <div class="alert alert-danger rounded-4">
          <?php foreach ($errors as $error): ?><div><?= e((string)$error) ?></div><?php endforeach; ?>
        </div>
      <?php endif; ?>
      <form method="post" action="<?= e(url($save_route)) ?>" class="card border-0 shadow-sm rounded-4">
        <?= csrf_field() ?>
        <div class="card-header bg-white p-4 border-0">
          <div class="d-flex flex-wrap justify-content-between align-items-start gap-3">
            <div>
              <h4 class="mb-1">إعدادات طباعة <?= e((string)($document['label'] ?? 'المستند')) ?></h4>
              <div class="text-muted small">هذه الصفحة مستقلة لهذا المستند فقط: المقاس الافتراضي، القالب الافتراضي، وإدارة الأقسام داخل القوالب.</div>
            </div>
            <a href="<?= e(url('/admin/print-settings')) ?>" class="btn btn-outline-secondary rounded-pill px-4">رجوع للموديول</a>
          </div>
        </div>
        <div class="card-body p-4">
          <div class="row g-3 mb-4">
            <div class="col-lg-3">
              <label class="form-label">المقاس الافتراضي</label>
              <select class="form-select" name="document[default_format]">
                <?php foreach ((array)($document['allowed_formats'] ?? []) as $format): ?>
                  <option value="<?= e((string)$format) ?>" <?= (($document['default_format'] ?? '') === $format ? 'selected' : '') ?>><?= strtoupper((string)$format) ?></option>
                <?php endforeach; ?>
              </select>
            </div>
            <div class="col-lg-3">
              <label class="form-label">القالب الافتراضي</label>
              <select class="form-select" name="document[active_template]">
                <?php foreach ((array)($document['templates'] ?? []) as $templateKey => $template): ?>
                  <option value="<?= e((string)$templateKey) ?>" <?= (($document['active_template'] ?? 'standard') === $templateKey ? 'selected' : '') ?>><?= e((string)($template['label'] ?? $templateKey)) ?></option>
                <?php endforeach; ?>
              </select>
            </div>
            <div class="col-lg-6">
              <label class="form-label">Footer Note</label>
              <textarea class="form-control" rows="2" name="document[footer_note]" placeholder="نص سفلي خاص بهذا المستند"><?= e((string)($document['footer_note'] ?? '')) ?></textarea>
            </div>
          </div>

          <div class="row g-3 mb-4">
            <div class="col-md-6 col-xl-3"><input type="hidden" name="document[show_logo]" value="0"><div class="form-check form-switch mt-2"><input class="form-check-input" type="checkbox" role="switch" id="show_logo" name="document[show_logo]" value="1" <?= !empty($document['show_logo']) ? 'checked' : '' ?>><label class="form-check-label fw-semibold" for="show_logo">إظهار الشعار</label></div></div>
            <div class="col-md-6 col-xl-3"><input type="hidden" name="document[show_signatures]" value="0"><div class="form-check form-switch mt-2"><input class="form-check-input" type="checkbox" role="switch" id="show_signatures" name="document[show_signatures]" value="1" <?= !empty($document['show_signatures']) ? 'checked' : '' ?>><label class="form-check-label fw-semibold" for="show_signatures">إظهار التوقيعات</label></div></div>
            <div class="col-md-6 col-xl-3"><input type="hidden" name="document[show_tax_no]" value="0"><div class="form-check form-switch mt-2"><input class="form-check-input" type="checkbox" role="switch" id="show_tax_no" name="document[show_tax_no]" value="1" <?= !empty($document['show_tax_no']) ? 'checked' : '' ?>><label class="form-check-label fw-semibold" for="show_tax_no">إظهار الرقم الضريبي</label></div></div>
            <div class="col-md-6 col-xl-3"><input type="hidden" name="document[show_terms]" value="0"><div class="form-check form-switch mt-2"><input class="form-check-input" type="checkbox" role="switch" id="show_terms" name="document[show_terms]" value="1" <?= !empty($document['show_terms']) ? 'checked' : '' ?>><label class="form-check-label fw-semibold" for="show_terms">إظهار الشروط</label></div></div>
          </div>

          <div class="border rounded-4 bg-white p-3">
            <div class="fw-semibold mb-2">قوالب <?= e((string)($document['label'] ?? 'المستند')) ?></div>
            <div class="text-muted small mb-3">كل قالب داخل هذه الصفحة فقط، لتسهيل الإدارة والتجربة بدون تجميع كل المستندات معًا.</div>
            <div class="accordion" id="templateAccordionSingle">
              <?php $templates = (array)($document['templates'] ?? []); $sectionDefinitions = (array)($document['section_definitions'] ?? []); $templateIndex = 0; ?>
              <?php foreach ($templates as $templateKey => $template): $templateIndex++; $templateCollapse = 'tpl_' . ($document['key'] ?? 'doc') . '_' . $templateKey; $templateSections = is_array($template['sections'] ?? null) ? $template['sections'] : []; ?>
                <div class="accordion-item border rounded-4 mb-3 overflow-hidden">
                  <h2 class="accordion-header" id="heading_<?= e($templateCollapse) ?>">
                    <button class="accordion-button <?= $templateIndex === 1 ? '' : 'collapsed' ?>" type="button" data-bs-toggle="collapse" data-bs-target="#<?= e($templateCollapse) ?>" aria-expanded="<?= $templateIndex === 1 ? 'true' : 'false' ?>">
                      <span class="fw-semibold"><?= e((string)($template['label'] ?? $templateKey)) ?></span>
                      <span class="ms-2 badge text-bg-light"><?= e((string)$templateKey) ?></span>
                      <?php if (($document['active_template'] ?? 'standard') === $templateKey): ?><span class="ms-2 badge text-bg-primary">الافتراضي</span><?php endif; ?>
                    </button>
                  </h2>
                  <div id="<?= e($templateCollapse) ?>" class="accordion-collapse collapse <?= $templateIndex === 1 ? 'show' : '' ?>" data-bs-parent="#templateAccordionSingle">
                    <div class="accordion-body bg-light-subtle">
                      <div class="row g-3 mb-3">
                        <div class="col-lg-6">
                          <label class="form-label">اسم القالب</label>
                          <input class="form-control" type="text" name="document[templates][<?= e((string)$templateKey) ?>][label]" value="<?= e((string)($template['label'] ?? '')) ?>">
                        </div>
                        <div class="col-lg-6">
                          <div class="alert alert-info mb-0 small">هذا القالب خاص بـ <?= e((string)($document['label'] ?? 'المستند')) ?> فقط. الترتيب والعناوين هنا ينعكس مباشرة عند اعتماد هذا القالب.</div>
                        </div>
                      </div>

                      <?php foreach ($sectionDefinitions as $areaKey => $areaSections): ?>
                        <div class="border rounded-4 p-3 bg-white mb-3">
                          <div class="fw-semibold mb-2"><?= e($areaKey === 'header' ? 'Header Zones' : ($areaKey === 'body' ? 'Body Zones' : 'Footer Zones')) ?></div>
                          <div class="table-responsive">
                            <table class="table table-sm align-middle mb-0">
                              <thead>
                                <tr>
                                  <th style="width:90px">إظهار</th>
                                  <th>القسم</th>
                                  <th style="width:120px">الترتيب</th>
                                  <th>العنوان المخصص</th>
                                </tr>
                              </thead>
                              <tbody>
                                <?php foreach ($areaSections as $sectionKey => $sectionDef): $sectionData = is_array($templateSections[$areaKey][$sectionKey] ?? null) ? $templateSections[$areaKey][$sectionKey] : []; ?>
                                  <tr>
                                    <td><input type="hidden" name="document[templates][<?= e((string)$templateKey) ?>][sections][<?= e((string)$areaKey) ?>][<?= e((string)$sectionKey) ?>][enabled]" value="0"><div class="form-check form-switch"><input class="form-check-input" type="checkbox" role="switch" id="<?= e(($document['key'] ?? 'doc') . '_' . $templateKey . '_' . $areaKey . '_' . $sectionKey) ?>" name="document[templates][<?= e((string)$templateKey) ?>][sections][<?= e((string)$areaKey) ?>][<?= e((string)$sectionKey) ?>][enabled]" value="1" <?= !array_key_exists('enabled', $sectionData) || !empty($sectionData['enabled']) ? 'checked' : '' ?>></div></td>
                                    <td><label class="fw-semibold" for="<?= e(($document['key'] ?? 'doc') . '_' . $templateKey . '_' . $areaKey . '_' . $sectionKey) ?>"><?= e((string)($sectionDef['label'] ?? $sectionKey)) ?></label><div class="small text-muted">المفتاح: <?= e((string)$sectionKey) ?></div></td>
                                    <td><input class="form-control" type="number" min="1" max="99" name="document[templates][<?= e((string)$templateKey) ?>][sections][<?= e((string)$areaKey) ?>][<?= e((string)$sectionKey) ?>][order]" value="<?= e((string)($sectionData['order'] ?? ($sectionDef['order'] ?? 10))) ?>"></td>
                                    <td><input class="form-control" type="text" name="document[templates][<?= e((string)$templateKey) ?>][sections][<?= e((string)$areaKey) ?>][<?= e((string)$sectionKey) ?>][title]" value="<?= e((string)($sectionData['title'] ?? ($sectionDef['default_title'] ?? ''))) ?>" placeholder="عنوان اختياري لهذا القسم"></td>
                                  </tr>
                                <?php endforeach; ?>
                              </tbody>
                            </table>
                          </div>
                        </div>
                      <?php endforeach; ?>
                    </div>
                  </div>
                </div>
              <?php endforeach; ?>
            </div>
          </div>
        </div>
        <div class="card-footer bg-white p-3 d-flex justify-content-end gap-2 border-0">
          <button type="submit" class="btn btn-primary rounded-pill px-4">حفظ إعدادات <?= e((string)($document['label'] ?? 'المستند')) ?></button>
        </div>
      </form>
    </div>
  </div>
</div>
