<?php
session_start();
require_once '../../vippanneladmin56329803/config.php';

if (!function_exists('displayText')) {
    function displayText($text) {
        if ($text === null) return '';
        return htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
    }
}

if (!isset($_SESSION['agent_id'])) {
    header("Location: ../agent.php");
    exit;
}

$stmt = $pdo->prepare("SELECT * FROM agents WHERE id = ?");
$stmt->execute([$_SESSION['agent_id']]);
$agent = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$agent) {
    session_destroy();
    header("Location: ../agent.php");
    exit;
}

$announcement = $pdo->query("SELECT * FROM announcements WHERE status = 1 ORDER BY id DESC LIMIT 1")->fetch(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html dir="rtl" lang="fa">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
    <title>اطلاعیه‌ها | رهگذر</title>
    <link rel="stylesheet" href="../style.css">
</head>
<body>
<div class="container">
    <div class="admin-header">
        <div class="brand">
            <img src="../picture/rahgozar.png" alt="نمایندگی" class="brand-logo">
            <span class="brand-name">اطلاعیه‌ها</span>
        </div>
        <div class="admin-info">
            <div class="admin-welcome">خوش آمدید، <strong><?= displayText($agent['name']) ?></strong></div>
            <a href="../agent.php" class="logout-btn">بازگشت به داشبورد</a>
            <a href="../agent.php?logout=1" class="logout-btn">خروج</a>
        </div>
    </div>

    <?php if($announcement): ?>
        <div class="content-card">
            <div class="section-header">
                <div class="section-title">
                    <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5">
                        <path d="M18 8A6 6 0 0 0 6 8c0 7-3 9-3 9h18s-3-2-3-9"/>
                        <path d="M13.73 21a2 2 0 0 1-3.46 0"/>
                    </svg>
                    <?= displayText($announcement['title']) ?>
                </div>
                <div class="admin-welcome" style="font-size: 12px;">
                    تاریخ: <?= date('Y-m-d H:i', strtotime($announcement['created_at'])) ?>
                </div>
            </div>
            
            <div style="margin-bottom: 24px; padding: 20px; background: var(--glass-inset); border-radius: var(--r-md);">
                <?= nl2br(displayText($announcement['preview'])) ?>
            </div>
            
            <div style="padding: 24px; background: var(--glass-inset); border-radius: var(--r-md); line-height: 2;">
                <?= nl2br(displayText($announcement['content'])) ?>
            </div>
        </div>
    <?php else: ?>
        <div class="content-card" style="text-align: center; padding: 60px 20px;">
            <svg width="60" height="60" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1" style="margin-bottom: 16px; opacity: 0.5;">
                <path d="M18 8A6 6 0 0 0 6 8c0 7-3 9-3 9h18s-3-2-3-9"/>
                <path d="M13.73 21a2 2 0 0 1-3.46 0"/>
            </svg>
            <p>هیچ اطلاعیه فعالی وجود ندارد</p>
        </div>
    <?php endif; ?>
</div>
</body>
</html>