<?php
defined('_FEXEC') or die();

/**
 * Requires in Admin:
 *  - SHAREPAGE (full URL)
 *  - SHAREPAGE_TITLE (short title/message)
 *
 * Optional:
 *  - TEXT_SHARE, TEXT_COPY_LINK, TEXT_COPIED, TEXT_GET_DIRECTIONS, etc.
 */

$shareUrl   = trim((string)SHAREPAGE);
$shareTitle = trim((string)SHAREPAGE_TITLE);

// Safety: if not configured, do nothing
if ($shareUrl === '') {
  return;
}

// Build message text for platforms that allow text
$shareText = $shareTitle !== '' ? $shareTitle : $shareUrl;

// Share endpoints
$xUrl    = 'https://twitter.com/intent/tweet?text=' . rawurlencode($shareText) . '&url=' . rawurlencode($shareUrl);
$fbUrl   = 'https://www.facebook.com/sharer/sharer.php?u=' . rawurlencode($shareUrl);
$liUrl   = 'https://www.linkedin.com/sharing/share-offsite/?url=' . rawurlencode($shareUrl);
$waUrl   = 'https://wa.me/?text=' . rawurlencode($shareText . ' ' . $shareUrl);
$mailUrl = 'mailto:?subject=' . rawurlencode($shareText) . '&body=' . rawurlencode($shareText . "\n\n" . $shareUrl);

// For Copy Link button
$copyValue = htmlspecialchars($shareUrl, ENT_QUOTES);
?>

<div class="border rounded p-3 mt-3">
  <div class="d-flex flex-wrap align-items-center justify-content-between gap-2 mb-2">
    <div class="fw-semibold">
      <i class="bi bi-share me-1"></i>
      <?php echo defined('TEXT_SHARE') ? TEXT_SHARE : 'Share'; ?>
    </div>
    <!--<div class="small text-muted">
      <?php //echo htmlspecialchars($shareUrl, ENT_QUOTES); ?>
    </div>-->
  </div>

  <div class="d-flex flex-wrap gap-2 align-items-center">
    <a class="btn btn-outline-success btn-sm d-inline-flex align-items-center gap-2"
       href="<?php echo htmlspecialchars($waUrl, ENT_QUOTES); ?>" target="_blank" rel="noopener">
      <i class="bi bi-whatsapp"></i><span>WhatsApp</span>
    </a>

    <a class="btn btn-outline-primary btn-sm d-inline-flex align-items-center gap-2"
       href="<?php echo htmlspecialchars($fbUrl, ENT_QUOTES); ?>" target="_blank" rel="noopener">
      <i class="bi bi-facebook"></i><span>Facebook</span>
    </a>

    <a class="btn btn-outline-dark btn-sm d-inline-flex align-items-center gap-2"
       href="<?php echo htmlspecialchars($xUrl, ENT_QUOTES); ?>" target="_blank" rel="noopener">
      <i class="bi bi-twitter-x"></i><span>X</span>
    </a>

    <a class="btn btn-outline-primary btn-sm d-inline-flex align-items-center gap-2"
       href="<?php echo htmlspecialchars($liUrl, ENT_QUOTES); ?>" target="_blank" rel="noopener">
      <i class="bi bi-linkedin"></i><span>LinkedIn</span>
    </a>

    <a class="btn btn-outline-secondary btn-sm d-inline-flex align-items-center gap-2"
       href="<?php echo htmlspecialchars($mailUrl, ENT_QUOTES); ?>">
      <i class="bi bi-envelope"></i><span>Email</span>
    </a>

    <button type="button"
            class="btn btn-primary btn-sm d-inline-flex align-items-center gap-2"
            data-osc-copy="<?php echo $copyValue; ?>">
      <i class="bi bi-clipboard"></i>
      <span><?php echo defined('TEXT_COPY_LINK') ? TEXT_COPY_LINK : 'Copy link'; ?></span>
    </button>

    <span class="small text-success d-none" data-osc-copied>
      <i class="bi bi-check2-circle me-1"></i>
      <?php echo defined('TEXT_COPIED') ? TEXT_COPIED : 'Copied!'; ?>
    </span>
  </div>
</div>

<script>
(function(){
  const btn = document.querySelector('[data-osc-copy]');
  if(!btn) return;

  const copied = document.querySelector('[data-osc-copied]');
  const value = btn.getAttribute('data-osc-copy') || '';

  btn.addEventListener('click', async function(){
    try{
      await navigator.clipboard.writeText(value);
      if(copied){
        copied.classList.remove('d-none');
        window.setTimeout(() => copied.classList.add('d-none'), 1800);
      }
    }catch(e){
      // Fallback: select + copy
      const ta = document.createElement('textarea');
      ta.value = value;
      ta.style.position = 'fixed';
      ta.style.left = '-9999px';
      document.body.appendChild(ta);
      ta.select();
      try { document.execCommand('copy'); } catch(e2) {}
      document.body.removeChild(ta);
      if(copied){
        copied.classList.remove('d-none');
        window.setTimeout(() => copied.classList.add('d-none'), 1800);
      }
    }
  });
})();
</script>
