<?php
session_start();
require_once("../config/config.php");

$srn_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if ($srn_id <= 0) {
    die("<h2 style='color:#ef4444;text-align:center;padding:100px;'>Invalid Supply Return Note ID</h2>");
}

// Fetch SRN Header
$query = "
    SELECT 
        srn.*,
        p.project_code,
        p.project_name,
        u.full_name AS created_by_name
    FROM supply_return_note srn
    LEFT JOIN project_master p ON srn.project_id = p.id
    LEFT JOIN users u ON srn.created_by = u.id
    WHERE srn.id = ?
";
$stmt = $conn->prepare($query);
$stmt->bind_param("i", $srn_id);
$stmt->execute();
$srn = $stmt->get_result()->fetch_assoc();

if (!$srn) {
    die("<h2 style='color:#ef4444;text-align:center;padding:100px;'>Supply Return Note not found.</h2>");
}

// Fetch Items
$items_query = "
    SELECT 
        sri.*,
        i.name AS item_name,
        i.model_no,
        u.uom_name
    FROM supply_return_note_items sri
    LEFT JOIN inventory_master i ON sri.item_id = i.id
    LEFT JOIN uom_tbl u ON i.uom = u.id
    WHERE sri.srn_id = ?
    ORDER BY sri.id ASC
";
$items_stmt = $conn->prepare($items_query);
$items_stmt->bind_param("i", $srn_id);
$items_stmt->execute();
$items = $items_stmt->get_result()->fetch_all(MYSQLI_ASSOC);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>SRN #<?= htmlspecialchars($srn['srn_code']) ?> | Technocool</title>
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');

        @page {
            size: A4 portrait;
            margin: 15mm;
        }

        body {
            font-family: 'Inter', Arial, sans-serif;
            font-size: 11pt;
            line-height: 1.5;
            color: #1f2937;
            background: #fff;
            margin: 0;
            padding: 0;
        }

        .container {
            max-width: 210mm;
            margin: 0 auto;
            padding: 10mm;
        }

        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            border-bottom: 4px solid #a855f7;
            padding-bottom: 20px;
            margin-bottom: 30px;
        }

        .logo {
            max-width: 180px;
            height: auto;
        }

        .company-info h1 {
            margin: 8px 0 4px 0;
            font-size: 24pt;
            color: #6b21a8;
            font-weight: 700;
        }

        .company-info p {
            margin: 0;
            font-size: 11pt;
            color: #64748b;
        }

        .doc-title {
            text-align: right;
        }
        .doc-title h2 {
            margin: 0;
            font-size: 22pt;
            color: #1e2937;
        }
        .doc-title p {
            margin: 5px 0 0 0;
            font-size: 13pt;
        }

        .info-grid {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 20px;
            margin: 30px 0;
        }
        .info-item {
            padding: 12px 0;
        }
        .info-item label {
            font-size: 10pt;
            color: #64748b;
            display: block;
            margin-bottom: 4px;
        }
        .info-item p {
            font-size: 11.5pt;
            font-weight: 600;
            margin: 0;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin: 30px 0;
        }
        th, td {
            border: 1px solid #cbd5e1;
            padding: 10px 12px;
            text-align: left;
            vertical-align: top;
        }
        th {
            background: #6b21a8;
            color: white;
            font-weight: 600;
        }

        .totals {
            width: 100%;
            margin-top: 40px;
            text-align: right;
        }
        .totals table {
            width: 45%;
            margin-left: auto;
            border-collapse: collapse;
        }
        .totals td {
            padding: 10px 15px;
            border: 1px solid #cbd5e1;
        }
        .totals .label {
            font-weight: 600;
            background: #f8fafc;
            text-align: right;
        }
        .totals .grand {
            font-size: 15pt;
            font-weight: 700;
            background: #6b21a8;
            color: white;
        }

        .footer {
            margin-top: 60px;
            text-align: center;
            font-size: 9.5pt;
            color: #64748b;
            border-top: 1px solid #e2e8f0;
            padding-top: 15px;
        }

        @media print {
            body { background: white; }
        }
    </style>
</head>
<body>

<div class="container">

    <!-- Header with Logo -->
    <div class="header">
        <div>
            <!-- Update path to your actual logo -->
            <img src="../technocool-logo.png" alt="Technocool Logo" class="logo"
                 onerror="this.src='https://via.placeholder.com/180x80/6b21a8/ffffff?text=TECHNOCOOL';">
            <div class="company-info">
                <h1>TECHNOCOOL HVAC ENGINEERS</h1>
                <p>Your Trusted Partner in HVAC Solutions</p>
            </div>
        </div>

        <div class="doc-title">
            <h2>SUPPLY RETURN NOTE</h2>
            <p># <?= htmlspecialchars($srn['srn_code']) ?></p>
            <p>Date: <?= date('d M Y', strtotime($srn['srn_date'])) ?></p>
        </div>
    </div>

    <!-- Info Section -->
    <div class="info-grid">
        <div class="info-item">
            <label>Project</label>
            <p><?= htmlspecialchars($srn['project_name'] ?? '—') ?> 
               (<?= htmlspecialchars($srn['project_code'] ?? '—') ?>)</p>
        </div>
        <div class="info-item">
            <label>Receiver Name</label>
            <p><?= htmlspecialchars($srn['receiver_name']) ?></p>
        </div>
        <div class="info-item">
            <label>Status</label>
            <p><?= htmlspecialchars($srn['status']) ?></p>
        </div>
        <div class="info-item">
            <label>Created By</label>
            <p><?= htmlspecialchars($srn['created_by_name'] ?? 'System') ?></p>
        </div>
    </div>

    <!-- Items Table -->
    <table>
        <thead>
            <tr>
                <th style="width:5%">Sr.</th>
                <th>Item Name</th>
                <th>Model No</th>
                <th>Quantity</th>
                <th>UOM</th>
                <th>Rate (₹)</th>
                <th>Reason</th>
                <th style="width:15%; text-align:right;">Line Total</th>
            </tr>
        </thead>
        <tbody>
            <?php 
            $sr = 1;
            $grand_total = 0;
            foreach ($items as $item): 
                $line_total = $item['quantity'] * $item['rate'];
                $grand_total += $line_total;
            ?>
            <tr>
                <td><?= $sr++ ?></td>
                <td><?= htmlspecialchars($item['item_name']) ?></td>
                <td><?= htmlspecialchars($item['model_no'] ?: '—') ?></td>
                <td><?= number_format($item['quantity'], 3) ?></td>
                <td><?= htmlspecialchars($item['uom_name'] ?: '—') ?></td>
                <td>₹ <?= number_format($item['rate'], 2) ?></td>
                <td><?= htmlspecialchars($item['reason'] ?: '—') ?></td>
                <td style="text-align:right; font-weight:600;">₹ <?= number_format($line_total, 2) ?></td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>

    <!-- Totals -->
    <div class="totals">
        <table>
            <tr>
                <td class="label">Basic Total</td>
                <td>₹ <?= number_format($grand_total, 2) ?></td>
            </tr>
            <tr class="grand">
                <td>Grand Total</td>
                <td>₹ <?= number_format($grand_total, 2) ?></td>
            </tr>
        </table>
    </div>

    <!-- Remarks -->
    <?php if (!empty($srn['remarks'])): ?>
    <div style="margin-top:30px; padding:20px; border:1px solid #cbd5e1; border-radius:8px;">
        <strong>Remarks:</strong><br>
        <?= nl2br(htmlspecialchars($srn['remarks'])) ?>
    </div>
    <?php endif; ?>

    <!-- Footer -->
    <div class="footer">
        This is a computer-generated Supply Return Note • Original Copy<br>
        Generated on <?= date('d M Y h:i A') ?> | Thank you for choosing Technocool HVAC Engineers
    </div>

</div>

</body>
</html>

<?php
$stmt->close();
$items_stmt->close();
$conn->close();
?>