<?php
include 'db.php';
include 'auth.php';

// Check if user is logged in
if (!isset($_SESSION['user_id'])) {
    die('Unauthorized access');
}

// Get void ID
$void_id = $_GET['void_id'] ?? null;

if (!$void_id) {
    die('Void ID is required');
}

try {
    // Get void order details
    $stmt = $conn->prepare("
        SELECT 
            vo.*,
            s.invoice_number,
            s.customer_name,
            s.sale_date,
            s.total_amount,
            s.net_amount,
            vr.reason_name as void_reason,
            u.username as voided_by_user
        FROM void_orders vo
        LEFT JOIN sales s ON vo.sale_id = s.id
        LEFT JOIN void_reasons vr ON vo.void_reason_id = vr.id
        LEFT JOIN users u ON vo.voided_by = u.id
        WHERE vo.id = ?
    ");
    
    $stmt->bind_param("i", $void_id);
    $stmt->execute();
    $result = $stmt->get_result();
    
    if ($result->num_rows === 0) {
        die('Void order not found');
    }
    
    $void_order = $result->fetch_assoc();
    
    // Get sale items
    $stmt = $conn->prepare("
        SELECT 
            si.*,
            i.name as item_name,
            i.retail_price
        FROM sale_items si
        LEFT JOIN items i ON si.item_id = i.id
        WHERE si.sale_id = ?
    ");
    
    $stmt->bind_param("i", $void_order['sale_id']);
    $stmt->execute();
    $items_result = $stmt->get_result();
    
    $items = [];
    while ($item = $items_result->fetch_assoc()) {
        $items[] = $item;
    }
    
} catch (Exception $e) {
    die('Error loading void order details');
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Void Receipt - <?= $void_order['invoice_number'] ?></title>
    <style>
        @media print {
            .no-print { display: none !important; }
            body { margin: 0; padding: 10px; }
        }
        
        body {
            font-family: 'Courier New', monospace;
            font-size: 12px;
            line-height: 1.4;
            margin: 0;
            padding: 20px;
            background: #f5f5f5;
        }
        
        .receipt {
            max-width: 300px;
            margin: 0 auto;
            background: white;
            padding: 20px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
            border-radius: 5px;
        }
        
        .header {
            text-align: center;
            border-bottom: 2px dashed #ccc;
            padding-bottom: 10px;
            margin-bottom: 15px;
        }
        
        .store-name {
            font-size: 18px;
            font-weight: bold;
            margin-bottom: 5px;
        }
        
        .receipt-title {
            font-size: 16px;
            font-weight: bold;
            color: #d32f2f;
            margin: 10px 0;
        }
        
        .info-row {
            display: flex;
            justify-content: space-between;
            margin-bottom: 5px;
        }
        
        .label {
            font-weight: bold;
        }
        
        .value {
            text-align: right;
        }
        
        .items-table {
            width: 100%;
            border-collapse: collapse;
            margin: 15px 0;
        }
        
        .items-table th,
        .items-table td {
            padding: 3px 0;
            text-align: left;
            border-bottom: 1px dotted #ccc;
        }
        
        .items-table th {
            font-weight: bold;
            border-bottom: 2px solid #333;
        }
        
        .total-row {
            border-top: 2px solid #333;
            font-weight: bold;
            font-size: 14px;
        }
        
        .footer {
            text-align: center;
            margin-top: 20px;
            padding-top: 10px;
            border-top: 2px dashed #ccc;
            font-size: 10px;
            color: #666;
        }
        
        .void-notice {
            background: #ffebee;
            border: 2px solid #d32f2f;
            padding: 10px;
            margin: 15px 0;
            text-align: center;
            font-weight: bold;
            color: #d32f2f;
        }
        
        .print-btn {
            position: fixed;
            top: 20px;
            right: 20px;
            background: #007bff;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
            font-size: 14px;
        }
        
        .print-btn:hover {
            background: #0056b3;
        }
    </style>
</head>
<body>
    <button class="print-btn no-print" onclick="window.print()">Print Receipt</button>
    
    <div class="receipt">
        <div class="header">
            <div class="store-name">ITRPOS SYSTEM</div>
            <div>Void Order Receipt</div>
            <div><?= date('Y-m-d H:i:s') ?></div>
        </div>
        
        <div class="void-notice">
            ⚠️ ORDER VOIDED ⚠️
        </div>
        
        <div class="info-row">
            <span class="label">Void ID:</span>
            <span class="value">#<?= $void_order['id'] ?></span>
        </div>
        
        <div class="info-row">
            <span class="label">Sale ID:</span>
            <span class="value">#<?= $void_order['sale_id'] ?></span>
        </div>
        
        <div class="info-row">
            <span class="label">Invoice:</span>
            <span class="value"><?= htmlspecialchars($void_order['invoice_number']) ?></span>
        </div>
        
        <div class="info-row">
            <span class="label">Customer:</span>
            <span class="value"><?= htmlspecialchars($void_order['customer_name']) ?></span>
        </div>
        
        <div class="info-row">
            <span class="label">Sale Date:</span>
            <span class="value"><?= date('M d, Y H:i', strtotime($void_order['sale_date'])) ?></span>
        </div>
        
        <div class="info-row">
            <span class="label">Void Date:</span>
            <span class="value"><?= date('M d, Y H:i', strtotime($void_order['voided_at'])) ?></span>
        </div>
        
        <div class="info-row">
            <span class="label">Void Reason:</span>
            <span class="value"><?= htmlspecialchars($void_order['void_reason']) ?></span>
        </div>
        
        <div class="info-row">
            <span class="label">Voided By:</span>
            <span class="value"><?= htmlspecialchars($void_order['voided_by_user']) ?></span>
        </div>
        
        <?php if (!empty($items)): ?>
        <table class="items-table">
            <thead>
                <tr>
                    <th>Item</th>
                    <th>Qty</th>
                    <th>Price</th>
                    <th>Total</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($items as $item): ?>
                <tr>
                    <td><?= htmlspecialchars($item['item_name']) ?></td>
                    <td><?= $item['quantity'] ?></td>
                    <td>Rs <?= number_format($item['unit_price'], 2) ?></td>
                    <td>Rs <?= number_format($item['total_price'], 2) ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
        <?php endif; ?>
        
        <div class="info-row total-row">
            <span class="label">Original Amount:</span>
            <span class="value">Rs <?= number_format($void_order['original_amount'], 2) ?></span>
        </div>
        
        <div class="info-row">
            <span class="label">Sale Total:</span>
            <span class="value">Rs <?= number_format($void_order['total_amount'], 2) ?></span>
        </div>
        
        <div class="info-row">
            <span class="label">Net Amount:</span>
            <span class="value">Rs <?= number_format($void_order['net_amount'], 2) ?></span>
        </div>
        
        <?php if (!empty($void_order['notes'])): ?>
        <div style="margin-top: 15px; padding: 10px; background: #f8f9fa; border-left: 3px solid #007bff;">
            <strong>Void Notes:</strong><br>
            <?= nl2br(htmlspecialchars($void_order['notes'])) ?>
        </div>
        <?php endif; ?>
        
        <div class="footer">
            <div>This order has been voided</div>
            <div>Void ID: #<?= $void_order['id'] ?></div>
            <div>Printed: <?= date('Y-m-d H:i:s') ?></div>
            <div>Thank you for using ITRPOS</div>
        </div>
    </div>
</body>
</html>
