<?php
session_start();
include("../db.php");

if (!isset($_GET['transfer_id'])) {
    echo "Transfer ID required";
    exit;
}

$transfer_id = intval($_GET['transfer_id']);

// Get transfer details
$transfer_query = "
    SELECT st.*, 
           fs.store_name as from_store_name, fs.store_code as from_store_code, fs.address as from_address,
           ts.store_name as to_store_name, ts.store_code as to_store_code, ts.address as to_address,
           u1.username as requested_by_name,
           u2.username as approved_by_name,
           u3.username as received_by_name
    FROM stock_transfers st
    LEFT JOIN stores fs ON st.from_store_id = fs.id
    LEFT JOIN stores ts ON st.to_store_id = ts.id
    LEFT JOIN users u1 ON st.requested_by = u1.id
    LEFT JOIN users u2 ON st.approved_by = u2.id
    LEFT JOIN users u3 ON st.received_by = u3.id
    WHERE st.id = ?
";

$stmt = $conn->prepare($transfer_query);
$stmt->bind_param("i", $transfer_id);
$stmt->execute();
$transfer = $stmt->get_result()->fetch_assoc();
$stmt->close();

if (!$transfer) {
    echo "Transfer not found";
    exit;
}

// Get transfer items
$items_query = "SELECT * FROM stock_transfer_items WHERE transfer_id = ? ORDER BY item_name";
$stmt = $conn->prepare($items_query);
$stmt->bind_param("i", $transfer_id);
$stmt->execute();
$items_result = $stmt->get_result();
$stmt->close();
?>

<div class="row">
    <div class="col-md-6">
        <div class="card">
            <div class="card-header">
                <h6 class="card-title">Transfer Information</h6>
            </div>
            <div class="card-body">
                <table class="table table-borderless">
                    <tr>
                        <td><strong>Transfer #:</strong></td>
                        <td><?= htmlspecialchars($transfer['transfer_number']) ?></td>
                    </tr>
                    <tr>
                        <td><strong>Date:</strong></td>
                        <td><?= date('M d, Y h:i A', strtotime($transfer['transfer_date'])) ?></td>
                    </tr>
                    <tr>
                        <td><strong>Status:</strong></td>
                        <td>
                            <?php
                            $status_class = '';
                            $status_text = '';
                            switch ($transfer['status']) {
                                case 'pending':
                                    $status_class = 'bg-warning';
                                    $status_text = 'Pending';
                                    break;
                                case 'approved':
                                    $status_class = 'bg-info';
                                    $status_text = 'Approved';
                                    break;
                                case 'in_transit':
                                    $status_class = 'bg-primary';
                                    $status_text = 'In Transit';
                                    break;
                                case 'received':
                                    $status_class = 'bg-success';
                                    $status_text = 'Received';
                                    break;
                                case 'cancelled':
                                    $status_class = 'bg-danger';
                                    $status_text = 'Cancelled';
                                    break;
                            }
                            ?>
                            <span class="badge <?= $status_class ?>"><?= $status_text ?></span>
                        </td>
                    </tr>
                    <tr>
                        <td><strong>Total Items:</strong></td>
                        <td><?= $transfer['total_items'] ?> items</td>
                    </tr>
                    <tr>
                        <td><strong>Total Quantity:</strong></td>
                        <td><?= number_format($transfer['total_quantity'], 2) ?> units</td>
                    </tr>
                    <?php if ($transfer['notes']): ?>
                    <tr>
                        <td><strong>Notes:</strong></td>
                        <td><?= htmlspecialchars($transfer['notes']) ?></td>
                    </tr>
                    <?php endif; ?>
                </table>
            </div>
        </div>
    </div>
    
    <div class="col-md-6">
        <div class="card">
            <div class="card-header">
                <h6 class="card-title">Store Information</h6>
            </div>
            <div class="card-body">
                <div class="row">
                    <div class="col-md-6">
                        <h6 class="text-danger">From Store</h6>
                        <p><strong><?= htmlspecialchars($transfer['from_store_name']) ?></strong></p>
                        <p><small class="text-muted"><?= htmlspecialchars($transfer['from_store_code']) ?></small></p>
                        <p><small class="text-muted"><?= htmlspecialchars($transfer['from_address']) ?></small></p>
                    </div>
                    <div class="col-md-6">
                        <h6 class="text-success">To Store</h6>
                        <p><strong><?= htmlspecialchars($transfer['to_store_name']) ?></strong></p>
                        <p><small class="text-muted"><?= htmlspecialchars($transfer['to_store_code']) ?></small></p>
                        <p><small class="text-muted"><?= htmlspecialchars($transfer['to_address']) ?></small></p>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="card mt-3">
            <div class="card-header">
                <h6 class="card-title">User Information</h6>
            </div>
            <div class="card-body">
                <table class="table table-borderless">
                    <tr>
                        <td><strong>Requested By:</strong></td>
                        <td><?= htmlspecialchars($transfer['requested_by_name'] ?? 'N/A') ?></td>
                    </tr>
                    <?php if ($transfer['approved_by_name']): ?>
                    <tr>
                        <td><strong>Approved By:</strong></td>
                        <td><?= htmlspecialchars($transfer['approved_by_name']) ?></td>
                    </tr>
                    <?php endif; ?>
                    <?php if ($transfer['received_by_name']): ?>
                    <tr>
                        <td><strong>Received By:</strong></td>
                        <td><?= htmlspecialchars($transfer['received_by_name']) ?></td>
                    </tr>
                    <?php endif; ?>
                </table>
            </div>
        </div>
    </div>
</div>

<div class="card mt-3">
    <div class="card-header">
        <h6 class="card-title">Transfer Items</h6>
    </div>
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-bordered">
                <thead>
                    <tr>
                        <th>Item Name</th>
                        <th>Quantity</th>
                        <th>Cost Price</th>
                        <th>Total Value</th>
                        <th>Remarks</th>
                    </tr>
                </thead>
                <tbody>
                    <?php 
                    $total_value = 0;
                    while ($item = $items_result->fetch_assoc()): 
                        $item_value = $item['quantity'] * $item['cost_price'];
                        $total_value += $item_value;
                    ?>
                        <tr>
                            <td>
                                <strong><?= htmlspecialchars($item['item_name']) ?></strong>
                            </td>
                            <td>
                                <span class="badge bg-info"><?= number_format($item['quantity'], 2) ?></span>
                            </td>
                            <td>
                                <?= number_format($item['cost_price'], 2) ?>
                            </td>
                            <td>
                                <strong><?= number_format($item_value, 2) ?></strong>
                            </td>
                            <td>
                                <?= htmlspecialchars($item['remarks'] ?? '') ?>
                            </td>
                        </tr>
                    <?php endwhile; ?>
                </tbody>
                <tfoot>
                    <tr class="table-info">
                        <th colspan="3" class="text-end">Total Value:</th>
                        <th><?= number_format($total_value, 2) ?></th>
                        <th></th>
                    </tr>
                </tfoot>
            </table>
        </div>
    </div>
</div>
