<?php
// admin/view-certificate.php - Realistic Certificate View with 5 Themes
require_once '../includes/config.php';

$cert_id = $_GET['id'] ?? 0;
$stmt = $pdo->prepare("SELECT c.*, t.* FROM certificates c LEFT JOIN certificate_templates t ON c.template_style = t.id WHERE c.id = ?");
$stmt->execute([$cert_id]);
$cert = $stmt->fetch();
if(!$cert) die("Certificate not found");

$bg = $cert['bg_color'] ?? '#fffff0';
$border = $cert['border_color'] ?? '#c9a84c';
$text = $cert['text_color'] ?? '#2d1f00';
$accent = $cert['accent_color'] ?? '#c9a84c';
$theme = $cert['name'] ?? 'ক্লাসিক সোনালী';

// Ornamental border patterns based on theme
$ornaments = [
    'ক্লাসিক সোনালী' => ['corner' => '✦', 'pattern' => 'repeating-linear-gradient(45deg, transparent, transparent 10px, rgba(201,168,76,0.1) 10px, rgba(201,168,76,0.1) 20px)'],
    'মডার্ন ব্লু' => ['corner' => '◆', 'pattern' => 'none'],
    'এলিগ্যান্ট রেড' => ['corner' => '❦', 'pattern' => 'none'],
    'প্রফেশনাল গ্রে' => ['corner' => '●', 'pattern' => 'none'],
    'ন্যাচারাল গ্রীন' => ['corner' => '❧', 'pattern' => 'none'],
];
$ornament = $ornaments[$theme] ?? $ornaments['ক্লাসিক সোনালী'];
?>
<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <title>Certificate - <?=htmlspecialchars($cert['student_name'])?></title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Noto+Serif+Bengali:wght@400;700&family=Playfair+Display:wght@400;700&family=Poppins:wght@300;600&display=swap');
        *{margin:0;padding:0;box-sizing:border-box}
        body{display:flex;justify-content:center;align-items:center;min-height:100vh;background:#e8e8e8;padding:20px}
        
        .certificate{
            width:900px;min-height:600px;padding:30px;position:relative;
            background:<?=$bg?>;font-family:'Noto Serif Bengali','Playfair Display',serif;
            color:<?=$text?>;box-shadow:0 20px 60px rgba(0,0,0,0.3);
            <?=$ornament['pattern']?>
        }
        .cert-border{position:absolute;top:15px;left:15px;right:15px;bottom:15px;border:3px solid <?=$border?>;pointer-events:none}
        .cert-border::before{content:'<?=$ornament['corner']?>';position:absolute;top:-12px;left:30px;font-size:20px;color:<?=$accent?>;background:<?=$bg?>;padding:0 8px}
        .cert-border::after{content:'<?=$ornament['corner']?>';position:absolute;top:-12px;right:30px;font-size:20px;color:<?=$accent?>;background:<?=$bg?>;padding:0 8px}
        
        .cert-inner{padding:40px 50px;text-align:center;position:relative;z-index:1}
        .cert-logo{font-size:1.5rem;color:<?=$accent?>;margin-bottom:10px}
        .cert-header{border-bottom:2px solid <?=$accent?>;padding-bottom:15px;margin-bottom:25px}
        .cert-title{font-size:2.8rem;font-weight:700;letter-spacing:5px;color:<?=$border?>;font-family:'Playfair Display',serif;text-transform:uppercase}
        .cert-subtitle{font-size:1rem;color:<?=$text?>;opacity:0.7;letter-spacing:3px}
        
        .cert-body{font-size:1.1rem;line-height:2;margin:20px 0}
        .cert-name{font-size:2.2rem;font-weight:700;color:<?=$accent?>;margin:10px 0;text-decoration:underline;text-underline-offset:8px;text-decoration-color:<?=$accent?>;font-family:'Playfair Display',serif}
        .cert-course{font-size:1.3rem;color:<?=$border?>;font-weight:700;margin:5px 0}
        
        .cert-footer{display:flex;justify-content:space-between;align-items:flex-end;margin-top:40px;padding-top:20px;border-top:1px solid <?=$accent?>44}
        .cert-date{text-align:left;font-size:0.9rem}
        .cert-sign{text-align:center}
        .sign-line{width:180px;border-top:2px solid <?=$text?>;margin:40px auto 5px}
        .cert-seal{width:80px;height:80px;border:3px solid <?=$accent?>;border-radius:50%;display:flex;align-items:center;justify-content:center;font-size:0.7rem;color:<?=$accent?>;transform:rotate(-15deg);opacity:0.8;position:absolute;bottom:60px;right:70px}
        
        .cert-number{position:absolute;bottom:20px;left:30px;font-size:0.7rem;color:<?=$text?>88}
        .cert-verify{position:absolute;bottom:20px;right:30px;font-size:0.7rem;color:<?=$text?>88}
        
        .watermark{position:absolute;top:50%;left:50%;transform:translate(-50%,-50%)rotate(-30deg);font-size:8rem;color:<?=$accent?>;opacity:0.03;pointer-events:none;font-weight:700;white-space:nowrap}
        
        .print-btn{position:fixed;top:20px;right:20px;background:<?=$accent?>;color:white;border:none;padding:10px 25px;border-radius:50px;cursor:pointer;font-weight:600;z-index:1000}
        .print-btn:hover{opacity:0.9}
        
        @media print{body{background:white}.print-btn{display:none}.certificate{box-shadow:none}}
    </style>
</head>
<body>
<button class="print-btn" onclick="window.print()"><i class="fas fa-print me-1"></i> প্রিন্ট / PDF</button>

<div class="certificate">
    <div class="cert-border"></div>
    <div class="watermark">CERTIFIED</div>
    
    <div class="cert-inner">
        <div class="cert-logo"><i class="fas fa-graduation-cap"></i> বাংলা LMS</div>
        
        <div class="cert-header">
            <div class="cert-title">Certificate</div>
            <div class="cert-subtitle">OF COMPLETION</div>
        </div>
        
        <div class="cert-body">
            <p>এতদ্বারা প্রত্যয়ন করা যাচ্ছে যে</p>
            <div class="cert-name"><?=htmlspecialchars($cert['student_name'])?></div>
            <p>সফলভাবে সম্পন্ন করেছেন</p>
            <div class="cert-course">"<?=htmlspecialchars($cert['course_name'])?>"</div>
            <p>গ্রেড: <strong><?=$cert['grade']?></strong></p>
        </div>
        
        <div class="cert-footer">
            <div class="cert-date">
                <strong>ইস্যুর তারিখ</strong><br>
                <?=date('d F Y',strtotime($cert['completion_date']))?>
            </div>
            <div class="cert-sign">
                <div class="sign-line"></div>
                <strong><?=htmlspecialchars($cert['instructor_name'])?></strong><br>
                <small>ইন্সট্রাক্টর</small>
            </div>
        </div>
    </div>
    
    <div class="cert-seal">
        <div>CERTIFIED<br>LMS</div>
    </div>
    <div class="cert-number">সার্টিফিকেট নং: <?=$cert['certificate_number']?></div>
    <div class="cert-verify">ভেরিফাই: onesbd.com/LMS/verify-certificate.php?code=<?=$cert['verification_code']?></div>
</div>
</body>
</html>