<?php
/**
 * IBVTE College - Certificate of Authorization
 * Professional Landscape Design based on Reference
 */

require_once __DIR__ . '/../config.php';
require_once __DIR__ . '/../includes/db.php';
require_once __DIR__ . '/../includes/auth.php';

if (!checkDBConnection()) exit;
Auth()->requireRole(['college']);

$db = DB();
$college = Auth()->getUser();

// If address is missing, use a placeholder
$address = $college['address'] ?: 'Official Registered Address';
$city = $college['city'] ?: 'City';
$state = $college['state'] ?: 'State';
$fullAddress = $address . ', ' . $city . ', ' . $state;

// Validity (Default 1 year from now or fixed period)
$validFrom = date('F d, Y', strtotime($college['created_at']));
$validTo = date('F d, Y', strtotime($college['created_at'] . ' + 1 year'));

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Authorization Certificate - <?= htmlspecialchars($college['institution_name']) ?></title>
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Montserrat:wght@400;500;600;700&family=Playfair+Display:wght@700;900&family=Roboto:wght@700&display=swap');

        * { box-sizing: border-box; }
        body { margin: 0; padding: 0; background: #222; font-family: 'Montserrat', sans-serif; }

        .print-btn {
            position: fixed; top: 20px; right: 20px; padding: 12px 25px;
            background: #d4af37; color: #000; border: none; border-radius: 5px;
            cursor: pointer; font-weight: bold; z-index: 999; box-shadow: 0 4px 10px rgba(0,0,0,0.3);
        }

        .certificate-container {
            width: 297mm; height: 210mm;
            background: #fff; margin: 20px auto; position: relative;
            box-shadow: 0 0 50px rgba(0,0,0,0.8); overflow: hidden;
            border: 18px solid #0a2351;
            display: flex; flex-direction: column;
        }

        /* Star Pattern Background */
        .bg-pattern {
            position: absolute; inset: 0;
            background-image: url("data:image/svg+xml,%3Csvg width='80' height='80' viewBox='0 0 80 80' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M40 5l4 12 12 4-12 4-4 12-4-12-12-4 12-4 4-12z' fill='%230a2351' fill-opacity='0.02'/%3E%3C/svg%3E");
            z-index: 1;
        }

        .inner-red-border {
            position: relative; z-index: 5; flex: 1; border: 3px solid #b4142d;
            margin: 6px; padding: 25px 50px; display: flex; flex-direction: column; align-items: center;
        }

        /* Header Branding - More compact */
        .header-branding { text-align: center; margin-bottom: 10px; }
        .logo-img { height: 75px; margin-bottom: 10px; }
        .inst-title {
            font-family: 'Playfair Display', serif; font-size: 34px; color: #0a2351;
            margin: 0; font-weight: 900; letter-spacing: 0.5px; text-transform: uppercase;
        }
        .inst-subline {
            font-weight: 700; font-size: 13px; color: #b4142d; margin-top: 5px;
            letter-spacing: 2px;
        }

        /* Certificate Title Section */
        .title-box { text-align: center; margin: 15px 0; }
        .main-cert-label {
            font-family: 'Roboto', sans-serif; font-size: 36px; color: #b4142d;
            font-weight: 700; margin: 0; text-transform: uppercase;
            letter-spacing: 1.5px; border-bottom: 3px double #b4142d; padding-bottom: 2px;
        }
        .this-certifies { font-size: 18px; color: #555; margin-top: 10px; font-style: italic; }

        /* College Info - Dynamic Scaling */
        .college-info-block { text-align: center; flex-grow: 1; display: flex; flex-direction: column; justify-content: center; }
        .college-name-text {
            font-family: 'Playfair Display', serif; font-size: 38px; color: #0a2351;
            margin: 10px 0; font-weight: 700; line-height: 1.2;
        }
        .college-loc { font-size: 17px; font-weight: 600; color: #444; margin-bottom: 15px; }

        .auth-desc {
            max-width: 800px; text-align: center; font-size: 16px; line-height: 1.5;
            color: #666; font-weight: 500; margin: 0 auto;
        }

        .valid-box {
            margin-top: 20px; font-weight: 700; font-size: 20px; color: #000;
            border: 1px solid #ddd; padding: 5px 25px; border-radius: 4px;
        }

        /* Footer - No Absolute Positioning */
        .certificate-footer {
            width: 100%; margin-top: auto; padding-top: 30px;
            display: flex; justify-content: space-between; align-items: flex-end;
        }
        
        .seal-decoration {
            width: 120px; height: 120px; border: 4px double #d4af37; border-radius: 50%;
            display: flex; flex-direction: column; align-items: center; justify-content: center;
            color: #b4142d; font-weight: 900; font-size: 24px; line-height: 1;
            transform: rotate(-10deg); background: #fff;
        }
        .seal-decoration span { font-size: 12px; color: #333; margin-top: 3px; }

        .sig-container { text-align: center; min-width: 200px; }
        .sig-hr { border-top: 2px solid #0a2351; margin-bottom: 8px; }
        .sig-lbl { font-size: 13px; font-weight: 700; color: #0a2351; text-transform: uppercase; }

        .qr-container { text-align: center; }
        .qr-box { width: 85px; height: 85px; border: 1px solid #eee; padding: 3px; background: #fff; }
        .qr-link { font-size: 10px; margin-top: 5px; font-weight: 700; color: #0a2351; }

        @media print {
            body { background: none; padding: 0; }
            .print-btn { display: none; }
            .certificate-container { margin: 0; box-shadow: none; width: 297mm; height: 210mm; }
        }
        /* Watermark */
        .watermark {
            position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);
            opacity: 0.04; width: 450px; z-index: 2; pointer-events: none;
        }
    </style>
</head>
<body>

    <button class="print-btn" onclick="window.print()">
        <i class="fas fa-print me-2"></i>PRINT CERTIFICATE
    </button>

    <div class="certificate-container">
        <div class="bg-pattern"></div>
        <img src="../assets/images/logo.jpeg" class="watermark" alt="Watermark">
        
        <div class="inner-red-border">
            <div class="header-branding">
                <img src="../assets/images/logo.jpeg" class="logo-img" alt="Logo">
                <h1 class="inst-title">Indian Board of Vocational and Technical Education</h1>
                <div class="inst-subline">NEW DELHI, INDIA - AN ISO 9001:2015 CERTIFIED ORGANIZATION</div>
            </div>

            <div class="title-box">
                <h2 class="main-cert-label">Certificate of Authorization</h2>
                <div class="this-certifies">This is to certify that</div>
            </div>
            
            <div class="college-info-block">
                <div class="college-name-text"><?= htmlspecialchars($college['institution_name']) ?></div>
                <div class="college-loc">situated at <strong><?= htmlspecialchars($fullAddress) ?></strong></div>
                
                <div class="auth-desc">
                    is a <strong>Certified Academic Partner</strong> authorized by the Indian Board of Vocational and Technical Education to carry out Admissions (Online/Offline), Skill Training, and related Educational Services in India on behalf of IBVTE Council.
                </div>

                <div style="display: flex; justify-content: center;">
                    <div class="valid-box">
                        Valid from <?= $validFrom ?> to <?= $validTo ?>
                    </div>
                </div>
            </div>

            <div class="certificate-footer">
                <div class="seal-decoration">
                    IBVTE
                    <span>AUTHORIZED</span>
                </div>

                <div class="sig-container">
                    <div class="sig-hr"></div>
                    <div class="sig-lbl">Authorized Signatory</div>
                    <div style="font-size: 10px; color: #555;">IBVTE Council Verification Dept.</div>
                </div>

                <div class="qr-container">
                    <img src="https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=<?= urlencode(APP_URL . "verify-college.php?code=" . $college['institution_code']) ?>" class="qr-box" alt="QR">
                    <div class="qr-link">www.ibvte.org</div>
                </div>

                <div class="sig-container">
                    <div class="sig-hr"></div>
                    <div class="sig-lbl">Chairman / Director</div>
                    <div style="font-size: 10px; color: #555;">Indian Board of Vocational & Tech. Ed.</div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/js/all.min.js"></script>
</body>
</html>
