<?php
// view_statement.php
$token = $_GET['token'] ?? '';
if (empty($token)) {
    die("<h2 style='text-align:center; color:red; margin-top:50px; font-family:sans-serif;'>رابط غير صالح</h2>");
}

try {
    require_once 'db_config.php';
    $stmt = $db->prepare("SELECT html_content, created_at FROM shared_statements WHERE token = ?");
    $stmt->execute([$token]);
    $row = $stmt->fetch(PDO::FETCH_ASSOC);
    
    if (!$row) {
        die("<h2 style='text-align:center; color:red; margin-top:50px; font-family:sans-serif;'>كشف الحساب غير موجود أو انتهت صلاحية الرابط</h2>");
    }
    
    $html = $row['html_content'];
} catch (Exception $e) {
    die("<h2 style='text-align:center; color:red; margin-top:50px; font-family:sans-serif;'>خطأ في قاعدة البيانات</h2>");
}
?>
<!DOCTYPE html>
<html lang='ar' dir='rtl'>
<head>
<meta charset='UTF-8'>
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
<title>كشف حساب - مراجعة العميل</title>
<style>
body { font-family: 'Cairo', sans-serif; padding: 20px; background: #f4f6f9; }
.statement-container { max-width: 1000px; margin: 0 auto; background: #fff; padding: 30px; border-radius: 10px; box-shadow: 0 5px 15px rgba(0,0,0,0.1); }
table { width: 100%; border-collapse: collapse; margin-top: 20px; font-size: 14px; }
th, td { padding: 12px; border: 1px solid #dee2e6; text-align: right; }
th { background: #0056b3; color: white; }
tr:nth-child(even) { background-color: #f8f9fa; }
.no-print { display: none !important; }
@media print {
    body { background: white; padding: 0; }
    .statement-container { box-shadow: none; padding: 0; }
    .action-buttons { display: none !important; }
}
/* Force table layout to prevent huge squished cells */
.table-responsive { overflow-x: auto; }
</style>
<link href='https://fonts.googleapis.com/css2?family=Cairo:wght@400;600;700;800&display=swap' rel='stylesheet'>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
</head>
<body>
<div class='statement-container'>
<div class='action-buttons' style='text-align:center; margin-bottom: 30px; display: flex; justify-content: center; gap: 15px;'>
    <button onclick='window.print()' style='background:#27ae60; color:white; border:none; padding:12px 25px; border-radius:6px; font-size:16px; cursor:pointer; font-family:Cairo; font-weight:bold; box-shadow: 0 4px 6px rgba(0,0,0,0.1); display: flex; align-items: center; gap: 8px;'>
        <i class="fas fa-print"></i> طباعة / حفظ كـ PDF
    </button>
</div>

<!-- Statement Content Injected Here -->
<div style="direction: rtl;">
<?php echo $html; ?>
</div>
<!-- End Statement Content -->

<div style='text-align: center; color: #555; font-size: 15px; margin-top: 40px; font-weight: bold; border-top: 2px dashed #ccc; padding-top: 20px;'>
    <span style='color: #27ae60; font-size: 20px;'>✔</span> مستخرج آلياً بواسطة نظام <strong>Easy ERP</strong><br>
    <small style="font-weight:normal; color: #777;">تاريخ إصدار الرابط: <span dir="ltr"><?php echo $row['created_at']; ?></span></small>
</div>
</div>
</body>
</html>
