<?php
$page_title = isset($page_title) ? $page_title : 'Settings';
$font_options = isset($font_options) ? $font_options : array();
$current_font = isset($current_font) ? $current_font : 'ibm_plex';
$document_signatory = isset($document_signatory) ? $document_signatory : '';
$lac_signatory_left = isset($lac_signatory_left) ? $lac_signatory_left : '';
$lac_signatory_right = isset($lac_signatory_right) ? $lac_signatory_right : '';
$weed_out_signatory_left = isset($weed_out_signatory_left) ? $weed_out_signatory_left : '';
$weed_out_signatory_right = isset($weed_out_signatory_right) ? $weed_out_signatory_right : '';
$crv_number = isset($crv_number) ? $crv_number : '';

$preview_map = array();
$google_map = array();
foreach ($font_options as $key => $opt) {
    $preview_map[$key] = isset($opt['css']) ? $opt['css'] : 'inherit';
    $google_map[$key] = !empty($opt['google'])
        ? ('https://fonts.googleapis.com/css2?family=' . $opt['google'] . '&display=swap')
        : '';
}
?>
<div class="content-wrapper">
  <section class="content-header">
    <div class="container-fluid">
      <div class="row mb-2 align-items-center">
        <div class="col-sm-10">
          <h1><?php echo htmlspecialchars($page_title); ?></h1>
          <p class="report-lead mb-0">
            Configure panel fonts, document signatories (SO/CRV, LAC, Weed Out), and CRV number.
          </p>
        </div>
      </div>
    </div>
  </section>
  <section class="content">
    <div class="container-fluid">
      <?php if (!empty($flash_success)) { ?>
        <div class="alert alert-success"><?php echo htmlspecialchars($flash_success); ?></div>
      <?php } ?>
      <?php if (!empty($flash_error)) { ?>
        <div class="alert alert-danger"><?php echo htmlspecialchars($flash_error); ?></div>
      <?php } ?>

      <div class="row">
        <div class="col-lg-6 mb-3">
          <div class="card report-card h-100">
            <div class="card-header">
              <h3 class="card-title mb-0">Font family</h3>
            </div>
            <div class="card-body">
              <form method="post" action="<?php echo base_url('index.php/settings/save_font'); ?>">
                <div class="form-group">
                  <label for="ui_font_family">Font family</label>
                  <select name="ui_font_family" id="ui_font_family" class="form-control js-select2" required>
                    <?php foreach ($font_options as $key => $opt) { ?>
                      <option value="<?php echo htmlspecialchars($key); ?>" <?php echo ($current_font === $key) ? 'selected' : ''; ?>
                        data-font-css="<?php echo htmlspecialchars($opt['css'], ENT_QUOTES); ?>"
                        data-google="<?php echo htmlspecialchars(!empty($opt['google']) ? $opt['google'] : '', ENT_QUOTES); ?>">
                        <?php echo htmlspecialchars($opt['label']); ?>
                      </option>
                    <?php } ?>
                  </select>
                </div>

                <div class="form-group">
                  <label>Preview</label>
                  <div id="fontPreview" class="p-3 border rounded" style="font-size:1.05rem;line-height:1.55;">
                    The Knowledge Resource Centre (KRC) — DLRL Koha Reports<br>
                    <span class="text-muted">ABCDEFG abcdefg 0123456789</span>
                  </div>
                  <small class="text-muted d-block mt-2" id="fontPreviewLabel">
                    Selected: <strong><?php echo htmlspecialchars(isset($font_options[$current_font]['label']) ? $font_options[$current_font]['label'] : $current_font); ?></strong>
                  </small>
                </div>

                <button type="submit" class="btn btn-primary">
                  <i class="fas fa-save mr-1"></i> Save font
                </button>
              </form>
            </div>
          </div>
        </div>

        <div class="col-lg-6 mb-3">
          <div class="card report-card h-100">
            <div class="card-header">
              <h3 class="card-title mb-0">SO / CRV signatory</h3>
            </div>
            <div class="card-body">
              <form method="post" action="<?php echo base_url('index.php/settings/save_signatory'); ?>">
                <div class="form-group">
                  <label for="document_signatory">Name &amp; designation</label>
                  <textarea name="document_signatory" id="document_signatory" class="form-control" rows="4"
                    placeholder="e.g.&#10;G Chandrashekhar, Sc-F,&#10;Wing Head, KRC"><?php echo htmlspecialchars($document_signatory); ?></textarea>
                  <small class="text-muted d-block mt-2">
                    Shown on SO and CRV PDFs (Kind Attn and signature blocks).
                    Leave blank to hide the name completely.
                  </small>
                </div>
                <button type="submit" class="btn btn-primary">
                  <i class="fas fa-save mr-1"></i> Save signatory
                </button>
              </form>
            </div>
          </div>
        </div>

        <div class="col-lg-6 mb-3">
          <div class="card report-card h-100">
            <div class="card-header">
              <h3 class="card-title mb-0">LAC New Titles signatures</h3>
            </div>
            <div class="card-body">
              <form method="post" action="<?php echo base_url('index.php/settings/save_lac_signatory'); ?>">
                <div class="form-group">
                  <label for="lac_signatory_left">Left signature</label>
                  <textarea name="lac_signatory_left" id="lac_signatory_left" class="form-control" rows="4"
                    placeholder="e.g.&#10;G Chandrashekhar, Sc-F,&#10;Member Secretary, LAC"><?php echo htmlspecialchars($lac_signatory_left); ?></textarea>
                </div>
                <div class="form-group">
                  <label for="lac_signatory_right">Right signature</label>
                  <textarea name="lac_signatory_right" id="lac_signatory_right" class="form-control" rows="4"
                    placeholder="e.g.&#10;R Anand, Sc-G,&#10;Chairman, LAC"><?php echo htmlspecialchars($lac_signatory_right); ?></textarea>
                  <small class="text-muted d-block mt-2">
                    Shown on LAC New Titles PDF and Word exports (left and right signature blocks).
                  </small>
                </div>
                <button type="submit" class="btn btn-primary">
                  <i class="fas fa-save mr-1"></i> Save LAC signatures
                </button>
              </form>
            </div>
          </div>
        </div>

        <div class="col-lg-6 mb-3">
          <div class="card report-card h-100">
            <div class="card-header">
              <h3 class="card-title mb-0">Weed Out signatures</h3>
            </div>
            <div class="card-body">
              <form method="post" action="<?php echo base_url('index.php/settings/save_weed_out_signatory'); ?>">
                <div class="form-group">
                  <label for="weed_out_signatory_left">Left signature</label>
                  <textarea name="weed_out_signatory_left" id="weed_out_signatory_left" class="form-control" rows="4"
                    placeholder="e.g.&#10;Librarian&#10;KRC"><?php echo htmlspecialchars($weed_out_signatory_left); ?></textarea>
                </div>
                <div class="form-group">
                  <label for="weed_out_signatory_right">Right signature</label>
                  <textarea name="weed_out_signatory_right" id="weed_out_signatory_right" class="form-control" rows="4"
                    placeholder="e.g.&#10;Wing Head, KRC"><?php echo htmlspecialchars($weed_out_signatory_right); ?></textarea>
                  <small class="text-muted d-block mt-2">
                    Shown as a footer on every page of Weed Out PDF / Word / Excel exports.
                  </small>
                </div>
                <button type="submit" class="btn btn-primary">
                  <i class="fas fa-save mr-1"></i> Save Weed Out signatures
                </button>
              </form>
            </div>
          </div>
        </div>

        <div class="col-lg-6 mb-3">
          <div class="card report-card h-100">
            <div class="card-header">
              <h3 class="card-title mb-0">CRV number</h3>
            </div>
            <div class="card-body">
              <form method="post" action="<?php echo base_url('index.php/settings/save_crv_number'); ?>">
                <div class="form-group">
                  <label for="crv_number">CRV No</label>
                  <input type="text" name="crv_number" id="crv_number" class="form-control"
                    value="<?php echo htmlspecialchars($crv_number); ?>"
                    placeholder="e.g. CRV0001" maxlength="80">
                  <small class="text-muted d-block mt-2">
                    Displayed as <strong>CRV No :</strong> on the CRV report. Update this value when you need a new number (not auto-generated).
                  </small>
                </div>
                <button type="submit" class="btn btn-primary">
                  <i class="fas fa-save mr-1"></i> Save CRV number
                </button>
              </form>
            </div>
          </div>
        </div>
      </div>
    </div>
  </section>
</div>
<script>
(function () {
  var map = <?php echo json_encode($preview_map, JSON_UNESCAPED_SLASHES); ?>;
  var googleMap = <?php echo json_encode($google_map, JSON_UNESCAPED_SLASHES); ?>;
  var labels = <?php echo json_encode(array_map(function ($o) { return $o['label']; }, $font_options), JSON_UNESCAPED_UNICODE); ?>;

  function ensureGoogleFont(key) {
    var href = googleMap[key] || '';
    if (!href) return;
    var id = 'font-preview-google-' + key;
    if (document.getElementById(id)) return;
    var link = document.createElement('link');
    link.id = id;
    link.rel = 'stylesheet';
    link.href = href;
    document.head.appendChild(link);
  }

  function applyPreview() {
    var sel = document.getElementById('ui_font_family');
    var preview = document.getElementById('fontPreview');
    var labelEl = document.getElementById('fontPreviewLabel');
    if (!sel || !preview) return;

    var key = sel.value;
    var css = map[key] || 'inherit';
    ensureGoogleFont(key);
    preview.style.setProperty('font-family', css, 'important');
    var nodes = preview.querySelectorAll('*');
    for (var i = 0; i < nodes.length; i++) {
      nodes[i].style.setProperty('font-family', css, 'important');
    }
    if (labelEl) {
      labelEl.innerHTML = 'Selected: <strong>' + (labels[key] || key) + '</strong>';
    }
  }

  function bind() {
    var sel = document.getElementById('ui_font_family');
    if (!sel) return;
    sel.addEventListener('change', applyPreview);
    if (window.jQuery) {
      var $sel = jQuery(sel);
      $sel.off('change.fontPreview select2:select.fontPreview select2:close.fontPreview');
      $sel.on('change.fontPreview select2:select.fontPreview select2:close.fontPreview', applyPreview);
    }
    applyPreview();
  }

  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', function () {
      setTimeout(bind, 50);
    });
  } else {
    setTimeout(bind, 50);
  }
  window.addEventListener('load', function () {
    setTimeout(bind, 100);
  });
})();
</script>
