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

// Check if user is logged in
session_start();
if (!isset($_SESSION['user_id'])) {
    die('Access Denied');
}

// Basic permission check
function isAdmin() {
    if (isset($_SESSION['is_super_admin']) && $_SESSION['is_super_admin'] == 1) return true;
    if (isset($_SESSION['user_role']) && strtolower($_SESSION['user_role']) === 'admin') return true;
    return false;
}

function canView($page) {
    if (isAdmin()) return true;
    return $_SESSION['permissions'][$page]['view'] ?? false;
}

if (!canView('inbound')) {
    die('Unauthorized access');
}

$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if (!$id) {
    die('Invalid ID');
}

// Fetch inbound mail details
$stmt = db()->prepare("SELECT * FROM inbound_mail WHERE id = ?");
$stmt->execute([$id]);
$mail = $stmt->fetch();

if (!$mail) {
    die('Mail not found');
}

$settings = get_settings();
$logo = !empty($settings['site_logo']) ? $settings['site_logo'] : '';
$site_name = $settings['site_name'];
$site_address = $settings['site_address'];
$site_slogan = $settings['site_slogan'];

/**
 * Convert Gregorian date to Hijri
 */
function gregorianToHijri($date) {
    if (!$date) return '';
    $time = strtotime($date);
    $m = date('m', $time);
    $d = date('d', $time);
    $y = date('Y', $time);

    if (($y > 1582) || (($y == 1582) && ($m > 10)) || (($y == 1582) && ($m == 10) && ($d > 14))) {
        $jd = (int)((1461 * ($y + 4800 + (int)(($m - 14) / 12))) / 4) +
              (int)((367 * ($m - 2 - 12 * ((int)(($m - 14) / 12)))) / 12) -
              (int)((3 * ((int)(($y + 4900 + (int)(($m - 14) / 12)) / 100))) / 4) +
              $d - 32075;
    } else {
        $jd = 367 * $y - (int)((7 * ($y + 5001 + (int)(($m - 9) / 7))) / 4) + (int)((275 * $m) / 9) + $d + 1729777;
    }

    $l = $jd - 1948440 + 10632;
    $n = (int)(($l - 1) / 10631);
    $l = $l - 10631 * $n + 354;
    $j = ((int)((10985 - $l) / 5316)) * ((int)((50 * $l) / 17719)) + ((int)($l / 5670)) * ((int)((43 * $l) / 15238));
    $l = $l - ((int)((30 - $j) / 15)) * ((int)((17719 * $j) / 50)) - ((int)($j / 16)) * ((int)((15238 * $j) / 43)) + 29;
    
    $month = (int)((24 * $l) / 709);
    $day = $l - (int)((709 * $month) / 24);
    $year = 30 * $n + $j - 30;

    $hijriMonths = [
        1 => "محرم", 2 => "صفر", 3 => "ربيع الأول", 4 => "ربيع الآخر",
        5 => "جمادى الأولى", 6 => "جمادى الآخرة", 7 => "رجب", 8 => "شعبان",
        9 => "رمضان", 10 => "شوال", 11 => "ذو القعدة", 12 => "ذو الحجة"
    ];

    return $day . ' ' . $hijriMonths[$month] . ' ' . $year . ' هـ';
}

$hijriDate = gregorianToHijri($mail['date_registered']);
?>
<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>طباعة بريد وارد - <?= htmlspecialchars($mail['ref_no']) ?></title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Cairo:wght@400;700&display=swap" rel="stylesheet">
    <style>
        @page {
            size: A4;
            margin: 0;
        }
        body {
            font-family: 'Cairo', 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: #f4f7f6;
            color: #333;
            margin: 0;
            padding: 0;
            line-height: 1.6;
        }
        
        .print-wrapper {
            width: 21cm;
            margin: 0 auto;
            background: #fff;
            padding: 1.5cm;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
            min-height: 29.7cm;
            position: relative;
            box-sizing: border-box;
            display: flex;
            flex-direction: column;
        }

        /* Repeating Header */
        .report-table {
            width: 100%;
            border-collapse: collapse;
            flex-grow: 1;
        }
        .report-header {
            display: table-header-group;
        }
        .report-footer {
            display: table-footer-group;
        }

        /* Header Style */
        .header-container {
            border-bottom: 3px double #00827F;
            padding-bottom: 5px;
            margin-bottom: 2px;
            display: flex;
            justify-content: space-between;
            align-items: flex-start;
        }
        .header-logo img {
            max-height: 80px;
        }
        .header-info { text-align: left; }
        .site-name { font-size: 24px; font-weight: bold; color: #00827F; margin-bottom: 2px; }
        .site-slogan { font-size: 14px; font-weight: bold; color: #00827F; margin-bottom: 2px; }

        /* Meta & Content */
        .mail-meta {
            margin-bottom: 10px;
            display: flex;
            justify-content: space-between;
            background: transparent;
            padding: 2px 0;
            border: none;
            font-size: 14px;
        }
        .mail-content {
            font-size: 18px;
            text-align: justify;
            color: #000;
            min-height: 12cm;
            padding-bottom: 30px;
        }

        /* Footer Style */
        .footer-container {
            border-top: 3px double #00827F;
            padding-top: 10px;
            text-align: center;
            font-size: 11px;
            width: 100%;
            background: #fff;
            margin-top: auto;
        }
        .contact-row {
            display: flex;
            justify-content: center;
            gap: 15px;
            margin-bottom: 5px;
            flex-wrap: wrap;
        }
        .page-num-display {
            font-size: 14px;
            font-weight: bold;
            margin-top: 5px;
        }
        
        /* Footer Spacer for Table */
        .footer-spacer {
            height: 80px;
            display: none;
        }

        /* UI Controls */
        .no-print {
            position: fixed;
            top: 20px;
            left: 20px;
            background: #00827F;
            color: #fff;
            padding: 10px 25px;
            border: none;
            border-radius: 30px;
            cursor: pointer;
            z-index: 1000;
            font-family: 'Cairo', sans-serif;
            font-weight: bold;
            box-shadow: 0 4px 10px rgba(0,0,0,0.2);
        }

        @media print {
            @page { 
                margin: 1cm 0;
            }
            body { background: #fff; }
            .no-print { display: none; }
            .print-wrapper {
                width: 100%;
                margin: 0;
                padding: 0 1.5cm;
                box-shadow: none;
                min-height: auto;
                display: block;
            }
            
            .footer-container {
                position: fixed;
                bottom: 0;
                left: 0;
                right: 0;
                padding: 10px 1.5cm;
                box-sizing: border-box;
                z-index: 100;
            }
            
            .footer-spacer {
                display: block;
            }
        }
    </style>
</head>
<body>

    <button class="no-print" onclick="window.print()">
        <i class="fas fa-print"></i> طباعة الوثيقة
    </button>

    <div class="print-wrapper">
        <table class="report-table">
            <thead class="report-header">
                <tr>
                    <td>
                        <div class="header-container">
                            <div class="header-logo">
                                <?php if ($logo): ?>
                                    <img src="<?= htmlspecialchars($logo) ?>" alt="Logo">
                                <?php else: ?>
                                    <div style="font-weight: bold; font-size: 26px; color: #00827F;"><?= htmlspecialchars($site_name) ?></div>
                                <?php endif; ?>
                            </div>
                            <div class="header-info">
                                <div class="site-name"><?= htmlspecialchars($site_name) ?></div>
                                <?php if (!empty($site_slogan)): ?>
                                    <div class="site-slogan"><?= htmlspecialchars($site_slogan) ?></div>
                                <?php endif; ?>
                                <div style="font-size: 12px; color: #666;"><?= htmlspecialchars($site_address) ?></div>
                            </div>
                        </div>
                    </td>
                </tr>
            </thead>
            
            <tbody>
                <tr>
                    <td>
                        <div class="mail-meta">
                            <div><strong>رقم القيد:</strong> <?= htmlspecialchars($mail['ref_no']) ?></div>
                            <div><strong>التاريخ:</strong> <?= htmlspecialchars($hijriDate) ?> | <?= htmlspecialchars($mail['date_registered']) ?>م</div>
                        </div>

                        <div class="mail-content">
                            <?= $mail['description'] ?>
                        </div>
                    </td>
                </tr>
            </tbody>
            
            <tfoot class="report-footer">
                <tr>
                    <td>
                        <div class="footer-spacer"></div>
                    </td>
                </tr>
            </tfoot>
        </table>

        <div class="footer-container">
            <div class="contact-row">
                <span><i class="fas fa-phone"></i> 99621515</span>
                <span><i class="fas fa-envelope"></i> ahlalhkair@gmail.com</span>
                <span><i class="fas fa-globe"></i> https://alkhairteam.net/</span>
                <span class="ms-3">
                    <i class="fab fa-instagram"></i>
                    <i class="fab fa-twitter"></i>
                    <i class="fab fa-facebook"></i>
                    alkhair_team
                </span>
            </div>
            <div class="page-num-display">
            </div>
        </div>
    </div>

</body>
</html>