<?php
include ("../../../inc/includes.php");
require_once __DIR__ . "/../inc/guard.php";

use GlpiPlugin\Wbstore\Config;
use GlpiPlugin\Wbstore\Notifications;
use GlpiPlugin\Wbstore\Utils;

Session::checkRight('config', UPDATE);

function wbstore_e($value): string {
   return htmlspecialchars((string)$value, ENT_QUOTES, 'UTF-8');
}

if (($_SERVER['REQUEST_METHOD'] ?? '') === 'POST') {
   if (isset($_POST['mark_read']) && isset($_POST['id'])) {
      Notifications::markRead((int)$_POST['id']);
   }
   if (isset($_POST['clear_read'])) {
      Notifications::clearRead();
   }
   Html::redirect(Plugin::getWebDir('wbstore') . '/front/notifications.php');
   exit;
}

Html::header('WB Store - Avisos', $_SERVER['PHP_SELF'], 'config', 'plugins');

$iframe = Utils::portalNoticesIframeUrl(['embed' => 'notifications']);
$catalogLink = Plugin::getWebDir('wbstore') . '/front/catalog.php';
$configLink = Plugin::getWebDir('wbstore') . '/front/config.php';
$storeUrl = rtrim((string)Config::get('store_url', 'https://wbuarque.com.br'), '/');
?>
<style>
:root{
   --wb-bg:#f4f7fb;
   --wb-card:#ffffff;
   --wb-border:#dbe5f0;
   --wb-text:#16324f;
   --wb-muted:#66788a;
   --wb-primary:#0f4c81;
   --wb-primary-2:#1b8fd9;
   --wb-primary-3:#1aa5a3;
   --wb-accent:#72d692;
   --wb-gold:#f2bf41;
   --wb-shadow:0 16px 35px rgba(15, 76, 129, .10);
}
.wb-notify{max-width:1340px;margin:0 auto;color:var(--wb-text)}
.wb-top{display:flex;justify-content:space-between;gap:16px;align-items:stretch;padding:22px;border-radius:22px;background:linear-gradient(135deg, var(--wb-primary) 0%, #5c48d8 38%, var(--wb-primary-2) 68%, var(--wb-primary-3) 100%);color:#fff;box-shadow:0 20px 42px rgba(17,56,122,.22)}
.wb-top-left{flex:1;min-width:280px}
.wb-top-left h2{margin:10px 0 8px;font-size:28px;font-weight:900;line-height:1.08}
.wb-top-left p{margin:0;max-width:760px;color:rgba(255,255,255,.95)}
.wb-badge{display:inline-flex;align-items:center;gap:8px;padding:7px 12px;border-radius:999px;background:rgba(255,255,255,.16);font-size:12px;font-weight:900;backdrop-filter:blur(6px)}
.wb-actions{display:flex;flex-wrap:wrap;gap:10px;align-items:flex-start;justify-content:flex-end}
.wb-btn{display:inline-flex;align-items:center;gap:8px;padding:12px 16px;border-radius:14px;font-weight:900;text-decoration:none;border:1px solid transparent;cursor:pointer;transition:.18s ease}
.wb-btn:hover{transform:translateY(-1px)}
.wb-btn-light{background:rgba(255,255,255,.16);color:#fff;border-color:rgba(255,255,255,.22)}
.wb-btn-gold{background:var(--wb-gold);color:#473100}
.wb-card{margin-top:18px;background:var(--wb-card);border:1px solid var(--wb-border);border-radius:22px;box-shadow:var(--wb-shadow);overflow:hidden}
.wb-card-head{padding:18px 20px;border-bottom:1px solid var(--wb-border);display:flex;justify-content:space-between;gap:14px;align-items:center;background:linear-gradient(180deg, #ffffff 0%, #f8fbff 100%)}
.wb-card-head h3{margin:0;font-size:24px;font-weight:900}
.wb-card-head p{margin:4px 0 0;color:var(--wb-muted);font-weight:700}
.wb-body{padding:18px}
.wb-frame{border:1px solid var(--wb-border);border-radius:18px;overflow:hidden;background:#f8fbff;min-height:70vh}
.wb-frame iframe{width:100%;height:70vh;border:0;display:block}
.wb-empty{padding:24px;color:var(--wb-muted);font-weight:800}
@media (max-width: 900px){
   .wb-top{flex-direction:column}
   .wb-actions{justify-content:flex-start}
   .wb-top-left h2{font-size:24px}
}
</style>
<div class="wb-notify">
   <section class="wb-top">
      <div class="wb-top-left">
         <span class="wb-badge"><i class="fas fa-bell"></i> Avisos WBUARQUE</span>
         <h2>Central de avisos da WB Store.</h2>
         <p>Aqui ficam os comunicados do portal, atualizações e recados operacionais ligados à sua instalação.</p>
      </div>
      <div class="wb-actions">
         <a class="wb-btn wb-btn-light" href="<?= wbstore_e($catalogLink) ?>"><i class="fas fa-puzzle-piece"></i> Voltar ao catálogo</a>
         <a class="wb-btn wb-btn-light" href="<?= wbstore_e($configLink) ?>"><i class="fas fa-cog"></i> Configurações</a>
         <a class="wb-btn wb-btn-gold" href="<?= wbstore_e($storeUrl) ?>" target="_blank" rel="noopener"><i class="fas fa-external-link-alt"></i> Abrir portal</a>
      </div>
   </section>

   <section class="wb-card">
      <div class="wb-card-head">
         <div>
            <h3>Notificações</h3>
            <p>Conteúdo carregado diretamente do portal autorizado pela WB Store.</p>
         </div>
      </div>
      <div class="wb-body">
         <?php if ($iframe === ''): ?>
            <div class="wb-empty">Avisos indisponíveis. Configure a URL da Store antes de usar esta tela.</div>
         <?php else: ?>
            <div class="wb-frame">
               <iframe src="<?= wbstore_e($iframe) ?>" loading="lazy"></iframe>
            </div>
         <?php endif; ?>
      </div>
   </section>
</div>
<?php Html::footer();
