<?php
/**
 * Sidebar version footer — "Version: X.Y", centred at the bottom of the sidebar.
 * The platform owner can double-click to edit it; everyone else just reads it.
 * Include once per page inside the sidebar (needs $baseUrl in scope).
 */
$__ver     = \PpPhp\Core\PlatformSettings::version();
$__isOwner = \PpPhp\Core\Security::IsOwner();
$__base    = rtrim((string) ($baseUrl ?? ''), '/');
?>
<div class="pp-version-wrap">
    <div class="pp-version<?= $__isOwner ? ' is-editable' : '' ?>" data-pp-version data-base="<?= htmlspecialchars($__base) ?>"
         <?= $__isOwner ? 'title="Double-click to edit the version"' : '' ?>>
        <span class="pp-version-label">Version:</span>
        <span class="pp-version-value" data-pp-version-value><?= htmlspecialchars($__ver) ?></span>
        <?php if ($__isOwner): ?><i class="bi bi-pencil pp-version-pencil"></i><?php endif; ?>
    </div>
</div>

<?php if (!defined('PP_VERSION_ASSETS_DONE')) { define('PP_VERSION_ASSETS_DONE', true); ?>
<style>
.pp-version-wrap{display:flex;justify-content:center;width:100%;
    padding-top:.7rem;border-top:1px solid var(--pp-border,rgba(148,163,184,.18));}
.pp-version{display:inline-flex;align-items:center;justify-content:center;gap:.32rem;padding:.35rem .6rem;
    border-radius:8px;font-size:.72rem;font-weight:600;color:var(--pp-text-muted,#94a3b8);letter-spacing:.01em;
    transition:background-color .15s ease,color .15s ease;}
.pp-version .pp-version-label{opacity:.8;}
.pp-version .pp-version-value{font-weight:750;color:var(--pp-text,#e2e8f0);}
.pp-version.is-editable{cursor:pointer;}
.pp-version.is-editable:hover{background:color-mix(in srgb, var(--pp-accent,#4f46e5) 12%, transparent);color:var(--pp-text,#e2e8f0);}
.pp-version-pencil{font-size:.62rem;opacity:0;transition:opacity .15s ease;}
.pp-version.is-editable:hover .pp-version-pencil{opacity:.7;}
.pp-version-input{width:5.5rem;padding:.1rem .35rem;border-radius:6px;border:1px solid var(--pp-accent,#6366f1);
    background:var(--pp-input-bg,#0f172a);color:var(--pp-text,#e2e8f0);font-size:.72rem;font-weight:750;text-align:center;}
.pp-version-input:focus{outline:none;box-shadow:0 0 0 .15rem color-mix(in srgb,var(--pp-accent,#6366f1) 30%,transparent);}
</style>
<script>
(function(){
  function wire(el){
    if (el._ppVerWired) return; el._ppVerWired = true;
    if (!el.classList.contains('is-editable')) return; // read-only for non-owners
    var valEl = el.querySelector('[data-pp-version-value]');
    var base  = el.getAttribute('data-base') || '';
    el.addEventListener('dblclick', function(){
      if (el.querySelector('.pp-version-input')) return;
      var current = valEl.textContent.trim();
      var pencil = el.querySelector('.pp-version-pencil');
      var input = document.createElement('input');
      input.type = 'text'; input.className = 'pp-version-input'; input.value = current;
      input.setAttribute('maxlength', '19');
      // Hide the static value (and pencil), show the inline editor.
      valEl.style.display = 'none'; if (pencil) pencil.style.display = 'none';
      el.appendChild(input);
      input.focus(); input.select();
      var done = false;
      function cancel(){ if (done) return; done = true; input.remove(); valEl.style.display=''; if(pencil) pencil.style.display=''; }
      function save(){
        if (done) return;
        var v = input.value.trim();
        if (v === '' || v === current){ cancel(); return; }
        if (!/^\d{1,4}(\.\d{1,4}){1,3}$/.test(v)){
          if (window.Swal) Swal.fire({toast:true,position:'top-end',timer:2600,showConfirmButton:false,icon:'error',title:'Use a number like 1.0, 1.2 or 2.1.3.'});
          input.focus(); return;
        }
        done = true;
        fetch(base + '/settings/version', {
          method:'POST', headers:{'Content-Type':'application/json',Accept:'application/json'},
          credentials:'same-origin', body: JSON.stringify({ version: v })
        }).then(function(r){ return r.json(); }).then(function(j){
          input.remove(); valEl.style.display=''; if(pencil) pencil.style.display='';
          if (j && j.success){
            valEl.textContent = j.version;
            document.querySelectorAll('[data-pp-version-value]').forEach(function(n){ n.textContent = j.version; });
          }
          if (window.Swal) Swal.fire({toast:true,position:'top-end',timer:2400,showConfirmButton:false,icon:(j&&j.success)?'success':'error',title:(j&&j.message)||'Could not update.'});
        }).catch(function(){
          input.remove(); valEl.style.display=''; if(pencil) pencil.style.display='';
        });
      }
      input.addEventListener('keydown', function(e){ if(e.key==='Enter'){e.preventDefault();save();} else if(e.key==='Escape'){e.preventDefault();cancel();} });
      input.addEventListener('blur', save);
    });
  }
  function init(){ document.querySelectorAll('[data-pp-version]').forEach(wire); }
  if (document.readyState !== 'loading') init(); else document.addEventListener('DOMContentLoaded', init);
})();
</script>
<?php } ?>
