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

// Allowed roles
$allowed_roles = ['store manager', 'admin', 'project manager'];
if (!isset($_SESSION['user_id']) || !in_array($_SESSION['role'], $allowed_roles)) {
    header("Location: ../login.php");
    exit();
}

$srn_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if ($srn_id <= 0) {
    die("<h2 style='color:#ef4444;text-align:center;padding:120px;'>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:120px;'>Supply Return Note not found.</h2>");
}

// Fetch SRN 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']) ?> - View</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
    <style>
        :root {
            --primary: #a855f7;
            --glass: rgba(255,255,255,0.09);
            --border: rgba(255,255,255,0.18);
        }
        * { margin:0; padding:0; box-sizing:border-box; }
        body {
            font-family: 'Inter', system-ui, sans-serif;
            background: linear-gradient(rgba(0,0,0,0.85), rgba(0,0,0,0.95)), url('../background.jpeg') center/cover fixed;
            color: #f1f5f9;
            padding: 20px 15px;
            min-height: 100vh;
        }
        .container { max-width: 1300px; margin: 0 auto; }

        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 30px;
            flex-wrap: wrap;
            gap: 15px;
        }
        .back-btn {
            color: #c4b5fd;
            text-decoration: none;
            font-weight: 600;
            display: flex;
            align-items: center;
            gap: 8px;
        }
        .print-btn {
            background: linear-gradient(135deg, var(--primary), #c084fc);
            color: white;
            padding: 12px 28px;
            border: none;
            border-radius: 14px;
            font-weight: 600;
            text-decoration: none;
            display: inline-flex;
            align-items: center;
            gap: 10px;
        }

        .glass-card {
            background: var(--glass);
            backdrop-filter: blur(20px);
            border: 1px solid var(--border);
            border-radius: 24px;
            padding: 40px;
            box-shadow: 0 25px 60px rgba(0,0,0,0.6);
        }

        .info-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
            gap: 25px;
            margin-bottom: 40px;
        }
        .info-item label {
            font-size: 0.95rem;
            opacity: 0.75;
            margin-bottom: 6px;
            display: block;
        }
        .info-item p {
            font-size: 1.15rem;
            font-weight: 600;
        }

        h2 {
            margin: 35px 0 20px;
            color: #ddd6fe;
            border-bottom: 2px solid rgba(168,85,247,0.3);
            padding-bottom: 10px;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin: 25px 0;
            background: rgba(255,255,255,0.03);
            border-radius: 16px;
            overflow: hidden;
        }
        th, td {
            padding: 16px 14px;
            text-align: left;
            border-bottom: 1px solid var(--border);
        }
        th {
            background: rgba(168,85,247,0.25);
            font-weight: 600;
        }
        tr:hover { background: rgba(255,255,255,0.06); }

        .grand-total {
            font-size: 1.45rem;
            font-weight: 700;
            background: rgba(168,85,247,0.2);
            color: #e0bbff;
        }

        .footer {
            text-align: center;
            margin-top: 50px;
            opacity: 0.7;
            font-size: 0.95rem;
        }

        @media (max-width: 768px) {
            .glass-card { padding: 25px; }
        }
    </style>
</head>
<body>
<?php include '../config/header.php'; ?>
<div class="container">

    <div class="header">
        <a href="srn_list.php" class="back-btn">
            <i class="fas fa-arrow-left"></i> Back to SRN List
        </a>
        <!-- <a href="srn_print.php?id=<?= $srn_id ?>" target="_blank" class="print-btn">
            <i class="fas fa-print"></i> Print SRN
        </a> -->
    </div>

    <h1>Supply Return Note #<?= htmlspecialchars($srn['srn_code']) ?></h1>

    <div class="glass-card">

        <!-- Header Information -->
        <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>SRN Date</label>
                <p><?= date('d M Y', strtotime($srn['srn_date'])) ?></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><span style="padding:6px 16px;background:#eab308;color:black;border-radius:30px;font-weight:600;">
                    <?= htmlspecialchars($srn['status']) ?>
                </span></p>
            </div>
            <div class="info-item">
                <label>Created By</label>
                <p><?= htmlspecialchars($srn['created_by_name'] ?? 'System') ?></p>
            </div>
            <div class="info-item">
                <label>Created On</label>
                <p><?= date('d M Y h:i A', strtotime($srn['created_at'])) ?></p>
            </div>
        </div>

        <!-- Returned Items -->
        <h2><i class="fas fa-undo"></i> Returned Items</h2>
        <table>
            <thead>
                <tr>
                    <th>Sr.</th>
                    <th>Item Name</th>
                    <th>Model No</th>
                    <th>Serial No</th>
                    <th>Quantity</th>
                    <th>UOM</th>
                    <th>Rate (₹)</th>
                    <th>Reason</th>
                    <th style="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><?= htmlspecialchars($item['serial_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:700;">₹ <?= number_format($line_total, 2) ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
            <tfoot>
                <tr class="grand-total">
                    <td colspan="8" style="text-align:right;">Grand Total</td>
                    <td style="text-align:right;">₹ <?= number_format($grand_total, 2) ?></td>
                </tr>
            </tfoot>
        </table>

        <?php if (!empty($srn['remarks'])): ?>
        <div style="margin-top:40px; padding:25px; background:rgba(168,85,247,0.1); border-left:5px solid var(--primary); border-radius:16px;">
            <strong>Remarks / Reason for Return:</strong><br>
            <?= nl2br(htmlspecialchars($srn['remarks'])) ?>
        </div>
        <?php endif; ?>

    </div>

    <div class="footer">
        Generated on <?= date('d M Y \a\t h:i A') ?> • Technocool HVAC Engineers
    </div>

</div>

</body>
</html>

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