<?php
/**
 * Premium Share Store — QR code + download + social share.
 */
require_once __DIR__ . '/_init.php';

$base_url = defined('SITE_URL') ? rtrim(SITE_URL, '/') . '/' : '/';
$__sg_share_urls = function_exists('sg_seller_resolve_store_urls')
    ? sg_seller_resolve_store_urls($seller_slug, null, (int)($seller_id ?? 0))
    : ['public' => $base_url . rawurlencode((string)$seller_slug)];
$store_url = (string)($__sg_share_urls['public'] ?? ($base_url . rawurlencode((string)$seller_slug)));
$share_text = 'Shop my store on ShopGlowsy: ' . $store_url;
$host_label = (string)(parse_url($store_url, PHP_URL_HOST) ?: 'shopglowsy.com');
$path_label = trim((string)(parse_url($store_url, PHP_URL_PATH) ?: ''), '/');
$display_url = $path_label !== '' ? ($host_label . '/' . $path_label) : $host_label;
$qr_img_url = '/seller/qr-image.php?size=280&data=' . rawurlencode($store_url);

$seller_page_title = 'Share Store';
$seller_current_page = 'share-store';
$sg_webapp_shell = true;
$sg_product_create_shell = true;
$sg_hide_app_header = true;
require_once __DIR__ . '/header.php';
?>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@500;600;700;800&display=swap" rel="stylesheet">
<link rel="stylesheet" href="/assets/css/sg-share-store.css?v=3">

<div class="sg-ss" id="sgShareStore"
     data-store-url="<?php echo htmlspecialchars($store_url); ?>"
     data-store-name="<?php echo htmlspecialchars((string)$store_name); ?>"
     data-share-text="<?php echo htmlspecialchars($share_text); ?>">

  <header class="sg-ss-top">
    <a href="index.php" class="sg-ss-back" data-sg-back aria-label="Back">
      <i class="fas fa-chevron-left" aria-hidden="true"></i>
    </a>
    <div class="sg-ss-brand">
      <img src="/assets/img/branding/app_icon.png" alt="" width="28" height="28" decoding="async">
      <span>Shop<span>Glowsy</span></span>
    </div>
    <span class="sg-ss-top-spacer" aria-hidden="true"></span>
  </header>

  <section class="sg-ss-hero">
    <p class="sg-ss-kicker">Grow your sales</p>
    <h1>Share your store</h1>
    <p class="sg-ss-lead">Send your link, show your QR code, or download it for flyers and packaging.</p>
    <p style="margin:14px 0 0;"><a href="notifications.php" style="font-weight:700;color:#9b4d66;text-decoration:none;">Open Push Notifications →</a></p>
  </section>

  <section class="sg-ss-card sg-ss-store-card">
    <div class="sg-ss-store-row">
      <div class="sg-ss-avatar" aria-hidden="true"><?php echo htmlspecialchars(mb_strtoupper(mb_substr((string)$store_name, 0, 1))); ?></div>
      <div class="sg-ss-store-meta">
        <strong><?php echo htmlspecialchars((string)$store_name); ?></strong>
        <span><?php echo htmlspecialchars($display_url); ?></span>
      </div>
    </div>
    <div class="sg-ss-link-row">
      <input type="text" id="sgSsUrl" readonly value="<?php echo htmlspecialchars($store_url); ?>" aria-label="Store link">
      <button type="button" class="sg-ss-btn sg-ss-btn--wine" id="sgSsCopy">Copy</button>
    </div>
    <p class="sg-ss-copied" id="sgSsCopied" hidden>Link copied</p>
  </section>

  <section class="sg-ss-card sg-ss-qr-card">
    <div class="sg-ss-qr-frame">
      <img id="sgSsQrImg"
           src="<?php echo htmlspecialchars($qr_img_url); ?>"
           width="220"
           height="220"
           alt="QR code for <?php echo htmlspecialchars((string)$store_name); ?>"
           decoding="async">
    </div>
    <p class="sg-ss-qr-caption">Scan to open your store</p>
    <button type="button" class="sg-ss-btn sg-ss-btn--primary sg-ss-btn--block" id="sgSsDownloadQr">
      <i class="fas fa-download" aria-hidden="true"></i> Download QR Image
    </button>
  </section>

  <section class="sg-ss-actions">
    <button type="button" class="sg-ss-action" id="sgSsNative" hidden>
      <span class="sg-ss-action-ico"><i class="fas fa-share-nodes"></i></span>
      <span><strong>Share</strong><small>Use your phone share sheet</small></span>
    </button>
    <a class="sg-ss-action" href="https://wa.me/?text=<?php echo rawurlencode($share_text); ?>" target="_blank" rel="noopener">
      <span class="sg-ss-action-ico wa"><i class="fab fa-whatsapp"></i></span>
      <span><strong>WhatsApp</strong><small>Share with customers</small></span>
    </a>
    <a class="sg-ss-action" href="https://www.facebook.com/sharer/sharer.php?u=<?php echo rawurlencode($store_url); ?>" target="_blank" rel="noopener">
      <span class="sg-ss-action-ico fb"><i class="fab fa-facebook-f"></i></span>
      <span><strong>Facebook</strong><small>Post to your page</small></span>
    </a>
    <a class="sg-ss-action" href="<?php echo htmlspecialchars($store_url); ?>" target="_blank" rel="noopener">
      <span class="sg-ss-action-ico store"><i class="fas fa-external-link-alt"></i></span>
      <span><strong>Open Store</strong><small>Preview your live storefront</small></span>
    </a>
  </section>
</div>

<script src="/assets/js/sg-share-store.js?v=3" defer></script>
<?php require_once __DIR__ . '/footer.php'; ?>
