<?php
include 'auth.php';
include 'db.php';

// (ඉතිරි පැරණි කේත කොටස් සියල්ල පහළින් එලෙසම පවතී...)

// 1. ඔබගේ පරිගණකයේ IP අංකය සහ ෆෝල්ඩරයේ නම
$server_ip   = "10.207.11.111";
$folder_name = "ds_feedback";

// 2. ස්වයංක්‍රීයව සකස් වන Base URL එක
$base_url = "http://" . $server_ip . "/" . $folder_name . "/feedback.php";

// එක් නිලධාරියෙකුගේ ID එකක් පැමිණ ඇත්දැයි පරික්ෂා කිරීම
$officer_id = isset($_GET['id']) ? intval($_GET['id']) : 0;

if ($officer_id > 0) {
    // එක් නිලධාරියෙකුගේ පමණක් දත්ත ලබා ගැනීම
    $sql = "SELECT officers.*, divisions.name as division_name 
            FROM officers 
            LEFT JOIN divisions ON officers.division_id = divisions.id 
            WHERE status = 'ACTIVE' AND officers.id = $officer_id";
    $page_title = "නිලධාරී QR කේතය";
} else {
    // සියලුම නිලධාරීන්ගේ දත්ත ලබා ගැනීම
    $sql = "SELECT officers.*, divisions.name as division_name 
            FROM officers 
            LEFT JOIN divisions ON officers.division_id = divisions.id 
            WHERE status = 'ACTIVE' ORDER BY officers.id DESC";
    $page_title = "සියලුම නිලධාරීන්ගේ QR කේත පුවරු";
}

$result = $conn->query($sql);
?>

<!DOCTYPE html>
<html lang="si">
<head>
    <meta charset="UTF-8">
    <title><?php echo $page_title; ?> - මාතලේ ප්‍රාදේශීය ලේකම් කාර්යාලය</title>
    <style>
        body { font-family: 'Iskoola Pota', Segoe UI, sans-serif; background: #edf2f7; padding: 20px; margin: 0; }
        .no-print { margin-bottom: 20px; text-align: center; }
        .btn-print { background: #2b6cb0; color: white; border: none; padding: 10px 20px; font-size: 16px; font-weight: bold; border-radius: 5px; cursor: pointer; }
        .btn-back { background: #718096; color: white; text-decoration: none; padding: 10px 15px; font-size: 14px; border-radius: 5px; margin-right: 10px; font-weight: bold; }
        
        .grid-container { 
            display: grid; 
            grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); 
            gap: 20px; 
            justify-content: center;
        }

        /* එක් නිලධාරියෙකු පමණක් ඇති විට Card එක මැදට ගැනීම */
        .single-card-mode {
            display: flex;
            justify-content: center;
        }

        .qr-card { 
            background: white; 
            border: 2px solid #2b6cb0; 
            border-radius: 10px; 
            padding: 15px; 
            text-align: center; 
            box-shadow: 0 4px 6px rgba(0,0,0,0.05); 
            page-break-inside: avoid;
            width: 300px;
        }
        .card-header { font-size: 14px; font-weight: bold; color: #1a365d; border-bottom: 1px solid #e2e8f0; padding-bottom: 5px; margin-bottom: 10px; }
        .officer-name { font-size: 16px; font-weight: bold; color: #2d3748; margin: 5px 0; }
        .officer-desig { font-size: 13px; color: #4a5568; }
        .officer-sub { font-size: 12px; color: #718096; margin-top: 5px; }
        .qr-img { margin: 10px 0; }
        .qr-img img { width: 140px; height: 140px; }
        .scan-text { font-size: 12px; color: #e53e3e; font-weight: bold; }
        .officer-code { font-size: 11px; color: #a0aec0; margin-top: 5px; }

        @media print {
            .no-print { display: none; }
            body { background: white; padding: 0; }
            .grid-container, .single-card-mode { display: block; }
            .qr-card { border: 1.5px solid #000; box-shadow: none; margin: auto; }
        }
    </style>
</head>
<body>

    <div class="no-print">
        <a href="officers_list.php" class="btn-back">⬅️ නිලධාරී නාමාවලියට</a>
        <button onclick="window.print()" class="btn-print">🖨️ මෙම QR Card එක Print කරන්න</button>
    </div>

    <div class="<?php echo ($officer_id > 0) ? 'single-card-mode' : 'grid-container'; ?>">
        <?php 
        if ($result->num_rows > 0):
            while($officer = $result->fetch_assoc()): 
                $feedback_url = $base_url . "?id=" . $officer['id'];
                $qr_api_url = "https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=" . urlencode($feedback_url);
        ?>
            <div class="qr-card">
                <div class="card-header">මාතලේ ප්‍රාදේශීය ලේකම් කාර්යාලය</div>
                <div class="officer-name"><?php echo $officer['name']; ?></div>
                <div class="officer-desig"><?php echo $officer['designation']; ?> (<?php echo $officer['division_name']; ?>)</div>
                
                <?php if(!empty($officer['gn_division'])): ?>
                    <div class="officer-sub">වසම: <?php echo $officer['gn_division']; ?></div>
                <?php endif; ?>

                <div class="qr-img">
                    <img src="<?php echo $qr_api_url; ?>" alt="QR Code">
                </div>

                <div class="scan-text">මෙම QR කේතය Scan කර ඔබගේ සේවා තෘප්තිමත්භාවය සටහන් කරන්න</div>
                <div class="officer-code">පද්ධති අංකය: <?php echo $officer['officer_code']; ?></div>
            </div>
        <?php 
            endwhile;
        else:
            echo "<p style='text-align:center; color:red;'>අදාළ නිලධාරියා හමු නොවීය.</p>";
        endif; 
        ?>
    </div>

</body>
</html>