<?php
require_once __DIR__ . '/db/config.php';
require_once __DIR__ . '/includes/settings.php';
require_once __DIR__ . '/includes/permissions.php';

session_start();

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

if (!canView('meetings')) {
    die("ليس لديك صلاحية لعرض هذه الصفحة");
}

$id = $_GET['id'] ?? 0;

if (!$id) {
    die("رقم الاجتماع غير صحيح");
}

try {
    $db = db();
    $stmt = $db->prepare("SELECT m.*, u.username as created_by_name FROM meetings m LEFT JOIN users u ON m.created_by = u.id WHERE m.id = ?");
    $stmt->execute([$id]);
    $meeting = $stmt->fetch(PDO::FETCH_ASSOC);

    if (!$meeting) {
        die("الاجتماع غير موجود");
    }
} catch (PDOException $e) {
    die("خطأ في قاعدة البيانات");
}

$settings = get_settings();
// Fix: Use site_logo directly as it contains the full path, or fall back correctly
$logo_path = $settings['site_logo'] ?? '';

// Validation and fallback logic
if (empty($logo_path) || !file_exists($logo_path)) {
    // If it's just a filename without path, try adding the path
    if (!empty($logo_path) && file_exists('uploads/charity/' . $logo_path)) {
        $logo_path = 'uploads/charity/' . $logo_path;
    } else {
        // Try to find any logo in the directory if the specific one is missing
        $possible_logos = glob('uploads/charity/*logo*.*');
        if (!empty($possible_logos)) {
            $logo_path = $possible_logos[0];
        } else {
            $logo_path = '';
        }
    }
}

if ($logo_path) {
    $logo_html = '<img src="' . $logo_path . '" alt="Logo" style="max-height: 100px;">';
} else {
    $logo_html = '';
}

?>
<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
    <meta charset="UTF-8">
    <title>محضر اجتماع - <?= htmlspecialchars($meeting['title']) ?></title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: #fff;
            color: #000;
            margin: 0;
            padding: 20px;
            direction: rtl;
        }
        .container {
            max-width: 800px;
            margin: 0 auto;
            border: 1px solid #ddd;
            padding: 40px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
        }
        .header {
            text-align: center;
            border-bottom: 2px solid #333;
            padding-bottom: 20px;
            margin-bottom: 30px;
        }
        .header h1 {
            margin: 10px 0 5px;
            font-size: 24px;
        }
        .header h2 {
            margin: 5px 0;
            font-size: 18px;
            color: #555;
        }
        .header .meta {
            font-size: 14px;
            color: #777;
        }
        .section {
            margin-bottom: 25px;
        }
        .section-title {
            font-size: 16px;
            font-weight: bold;
            background: #f4f4f4;
            padding: 8px 15px;
            border-right: 4px solid #333;
            margin-bottom: 15px;
        }
        .info-grid {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 15px;
            margin-bottom: 20px;
        }
        .info-item {
            margin-bottom: 5px;
        }
        .info-label {
            font-weight: bold;
            color: #555;
        }
        .content-box {
            padding: 10px;
            border: 1px solid #eee;
            background: #fafafa;
            min-height: 50px;
        }
        /* Style for rich text content to ensure it looks good */
        .rich-text {
            overflow-wrap: break-word;
        }
        .rich-text p { margin-top: 0; }
        .rich-text ul, .rich-text ol { margin-right: 20px; padding-right: 0; }
        
        /* Quill Alignment Classes */
        .ql-align-center { text-align: center; }
        .ql-align-right { text-align: right; }
        .ql-align-justify { text-align: justify; }
        .ql-direction-rtl { direction: rtl; text-align: right; }
        .ql-direction-ltr { direction: ltr; text-align: left; }
        .ql-indent-1 { margin-right: 3em; } /* RTL indent */
        .ql-indent-2 { margin-right: 6em; }
        
        .signatures {
            margin-top: 50px;
            display: flex;
            justify-content: space-between;
        }
        .signature-box {
            width: 40%;
            text-align: center;
        }
        .signature-line {
            margin-top: 50px;
            border-top: 1px solid #000;
        }
        @media print {
            body {
                padding: 0;
                background: #fff;
            }
            .container {
                box-shadow: none;
                border: none;
                padding: 0;
                max-width: 100%;
            }
            .no-print {
                display: none;
            }
            button { display: none; }
        }
    </style>
</head>
<body>
    <div class="no-print" style="text-align: center; margin-bottom: 20px;">
        <button onclick="window.print()" style="padding: 10px 20px; font-size: 16px; cursor: pointer; background: #333; color: #fff; border: none; border-radius: 5px;">طباعة المحضر</button>
    </div>

    <div class="container">
        <div class="header">
            <?php if ($logo_html): ?>
                <div style="margin-bottom: 10px;"><?= $logo_html ?></div>
            <?php endif; ?>
            <h1><?= htmlspecialchars($settings['site_name']) ?></h1>
            <?php if ($settings['site_slogan']): ?>
                <h2><?= htmlspecialchars($settings['site_slogan']) ?></h2>
            <?php endif; ?>
            <div style="margin-top: 20px; font-size: 20px; font-weight: bold; text-decoration: underline;">محضر اجتماع رسمي</div>
        </div>

        <div class="section">
            <div class="info-grid">
                <div class="info-item">
                    <span class="info-label">عنوان الاجتماع:</span>
                    <?= htmlspecialchars($meeting['title']) ?>
                </div>
                <div class="info-item">
                    <span class="info-label">المنظم:</span>
                    <?= htmlspecialchars($meeting['created_by_name']) ?>
                </div>
                <div class="info-item">
                    <span class="info-label">التاريخ:</span>
                    <?= date('Y-m-d', strtotime($meeting['start_time'])) ?>
                </div>
                <div class="info-item">
                    <span class="info-label">الوقت:</span>
                    <?= date('H:i', strtotime($meeting['start_time'])) ?> - <?= date('H:i', strtotime($meeting['end_time'])) ?>
                </div>
                <div class="info-item" style="grid-column: span 2;">
                    <span class="info-label">المكان:</span>
                    <?= htmlspecialchars($meeting['location'] ?: 'غير محدد') ?>
                </div>
            </div>
        </div>

        <?php if ($meeting['agenda']): ?>
        <div class="section">
            <div class="section-title">جدول الأعمال</div>
            <div class="content-box rich-text"><?= $meeting['agenda'] ?></div>
        </div>
        <?php endif; ?>

        <div class="section">
            <div class="info-grid">
                <div>
                    <div class="section-title">الحضور</div>
                    <div class="content-box"><?= nl2br(htmlspecialchars($meeting['attendees'] ?: 'لا يوجد')) ?></div>
                </div>
                <div>
                    <div class="section-title">الغياب / الاعتذار</div>
                    <div class="content-box"><?= nl2br(htmlspecialchars($meeting['absentees'] ?: 'لا يوجد')) ?></div>
                </div>
            </div>
        </div>

        <?php if ($meeting['meeting_details']): ?>
        <div class="section">
            <div class="section-title">تفاصيل الاجتماع / المحضر</div>
            <div class="content-box rich-text" style="min-height: 150px;"><?= $meeting['meeting_details'] ?></div>
        </div>
        <?php endif; ?>

        <?php if ($meeting['description']): ?>
        <div class="section">
            <div class="section-title"> القرارات والإلتزامات</div>
            <div class="content-box rich-text"><?= $meeting['description'] ?></div>
        </div>
        <?php endif; ?>

        <div class="signatures">
            <div class="signature-box">
                <div>توقيع مقرر الاجتماع</div>
                <div class="signature-line"></div>
            </div>
            <div class="signature-box">
                <div>اعتماد المدير / الرئيس</div>
                <div class="signature-line"></div>
            </div>
        </div>
        
        <div style="margin-top: 30px; text-align: center; font-size: 12px; color: #999;">
            تم استخراج هذا المستند إلكترونياً من النظام بتاريخ <?= date('Y-m-d H:i') ?>
        </div>
    </div>
</body>
</html>