<?php
$title = 'Return Details';
$active = 'returns';
require_once __DIR__ . '/../layouts/header.php';
require_once __DIR__ . '/../layouts/sidebar.php';
?>

<div class="container-fluid">
    <!-- Page Header -->
    <div class="page-header">
        <div class="header-content">
            <h1>
                <i class="fas fa-eye"></i>
                Return Details
            </h1>
            <p>Return #: <?php echo Security::escape($return['return_number']); ?></p>
        </div>
        <div class="header-actions">
            <a href="<?php echo BASE_URL; ?>/returns" class="btn btn-secondary">
                <i class="fas fa-arrow-left"></i> Back to Returns
            </a>
            <?php if ($return['status'] == 'Pending' && Auth::can('returns.edit')): ?>
            <a href="<?php echo BASE_URL; ?>/returns/edit/<?php echo $return['id']; ?>" class="btn btn-success">
                <i class="fas fa-edit"></i> Edit Return
            </a>
            <?php endif; ?>
            <?php if ($return['status'] == 'Pending' && Auth::can('returns.process')): ?>
            <button class="btn btn-primary" onclick="processReturn()">
                <i class="fas fa-check-double"></i> Process Return
            </button>
            <?php endif; ?>
            <button class="btn btn-outline" onclick="window.print()">
                <i class="fas fa-print"></i> Print
            </button>
        </div>
    </div>

    <!-- Status Banner -->
    <?php
    $statusClass = '';
    $statusMessage = '';
    
    if ($return['status'] == 'Pending') {
        $statusClass = 'status-warning';
        $statusMessage = 'This return is pending processing.';
    } elseif ($return['status'] == 'Processed') {
        $statusClass = 'status-info';
        $statusMessage = 'This return has been processed and is awaiting verification.';
    } elseif ($return['status'] == 'Verified') {
        $statusClass = 'status-success';
        $statusMessage = 'This return has been verified.';
    } elseif ($return['status'] == 'Completed') {
        $statusClass = 'status-success';
        $statusMessage = 'This return has been completed successfully.';
    }
    ?>
    
    <div class="status-banner <?php echo $statusClass; ?>">
        <div class="status-icon">
            <i class="fas <?php 
                if ($return['status'] == 'Pending') echo 'fa-clock';
                elseif ($return['status'] == 'Processed') echo 'fa-sync';
                elseif ($return['status'] == 'Verified') echo 'fa-check-circle';
                elseif ($return['status'] == 'Completed') echo 'fa-check-double';
            ?>"></i>
        </div>
        <div class="status-content">
            <h3><?php echo Security::escape($return['status']); ?></h3>
            <p><?php echo $statusMessage; ?></p>
        </div>
    </div>

    <!-- Return Details Grid -->
    <div class="details-grid">
        <!-- Return Information -->
        <div class="detail-card">
            <div class="card-header">
                <h3><i class="fas fa-file-signature"></i> Return Information</h3>
            </div>
            <div class="card-body">
                <div class="detail-row">
                    <div class="detail-label">Return #:</div>
                    <div class="detail-value"><?php echo Security::escape($return['return_number']); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Return Date:</div>
                    <div class="detail-value"><?php echo date('d/m/Y', strtotime($return['return_date'])); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Return Type:</div>
                    <div class="detail-value">
                        <span class="badge badge-info"><?php echo Security::escape($return['return_type']); ?></span>
                    </div>
                </div>
                <?php if ($return['requisition_id']): ?>
                <div class="detail-row">
                    <div class="detail-label">Requisition:</div>
                    <div class="detail-value">
                        <a href="<?php echo BASE_URL; ?>/requisitions/show/<?php echo $return['requisition_id']; ?>">
                            <?php echo Security::escape($return['requisition_number']); ?>
                        </a>
                    </div>
                </div>
                <?php endif; ?>
            </div>
        </div>

        <!-- Returning Officer Information -->
        <div class="detail-card">
            <div class="card-header">
                <h3><i class="fas fa-user-shield"></i> Returning Officer</h3>
            </div>
            <div class="card-body">
                <div class="detail-row">
                    <div class="detail-label">Name:</div>
                    <div class="detail-value"><?php echo Security::escape($return['returning_officer_name']); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Rank:</div>
                    <div class="detail-value"><?php echo Security::escape($return['returning_rank']); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">NIS Number:</div>
                    <div class="detail-value"><?php echo Security::escape($return['returning_nis']); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Unit:</div>
                    <div class="detail-value"><?php echo Security::escape($return['returning_unit']); ?></div>
                </div>
            </div>
        </div>

        <!-- Receiving Officer Information -->
        <?php if ($return['received_by']): ?>
        <div class="detail-card">
            <div class="card-header">
                <h3><i class="fas fa-user-check"></i> Received By</h3>
            </div>
            <div class="card-body">
                <div class="detail-row">
                    <div class="detail-label">Name:</div>
                    <div class="detail-value"><?php echo Security::escape($return['received_by_name']); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Date:</div>
                    <div class="detail-value"><?php echo date('d/m/Y H:i', strtotime($return['updated_at'])); ?></div>
                </div>
            </div>
        </div>
        <?php endif; ?>

        <!-- Weapons Returned -->
        <?php if (!empty($weapons)): ?>
        <div class="detail-card full-width">
            <div class="card-header">
                <h3><i class="fas fa-gun"></i> Weapons Returned</h3>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="items-table">
                        <thead>
                            <tr>
                                <th>Weapon ID</th>
                                <th>Type</th>
                                <th>Make/Model</th>
                                <th>Serial Number</th>
                                <th>Condition</th>
                                <th>Remarks</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($weapons as $weapon): ?>
                            <tr>
                                <td><?php echo Security::escape($weapon['weapon_id']); ?></td>
                                <td><?php echo Security::escape($weapon['weapon_type']); ?></td>
                                <td><?php echo Security::escape($weapon['make_model']); ?></td>
                                <td><?php echo Security::escape($weapon['serial_number']); ?></td>
                                <td>
                                    <?php 
                                    $conditionClass = '';
                                    if ($weapon['condition'] == 'Serviceable') $conditionClass = 'status-active';
                                    elseif ($weapon['condition'] == 'Unserviceable') $conditionClass = 'status-warning';
                                    elseif ($weapon['condition'] == 'Damaged') $conditionClass = 'status-rejected';
                                    ?>
                                    <span class="status-badge <?php echo $conditionClass; ?>">
                                        <?php echo Security::escape($weapon['condition']); ?>
                                    </span>
                                </td>
                                <td><?php echo Security::escape($weapon['remarks'] ?? '-'); ?></td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        <?php endif; ?>

        <!-- Ammunition Returned -->
        <?php if (!empty($ammunition)): ?>
        <div class="detail-card full-width">
            <div class="card-header">
                <h3><i class="fas fa-bullseye"></i> Ammunition Returned</h3>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="items-table">
                        <thead>
                            <tr>
                                <th>Ammunition ID</th>
                                <th>Type</th>
                                <th>Calibre</th>
                                <th>Batch Number</th>
                                <th>Rounds Returned</th>
                                <th>Rounds Used</th>
                                <th>Condition</th>
                                <th>Remarks</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($ammunition as $ammo): ?>
                            <tr>
                                <td><?php echo Security::escape($ammo['ammo_id']); ?></td>
                                <td><?php echo Security::escape($ammo['ammo_type']); ?></td>
                                <td><?php echo Security::escape($ammo['calibre']); ?></td>
                                <td><?php echo Security::escape($ammo['batch_number']); ?></td>
                                <td class="text-right"><?php echo number_format($ammo['rounds_returned']); ?></td>
                                <td class="text-right"><?php echo number_format($ammo['rounds_used']); ?></td>
                                <td>
                                    <?php 
                                    $conditionClass = '';
                                    if ($ammo['condition'] == 'Serviceable') $conditionClass = 'status-active';
                                    elseif ($ammo['condition'] == 'Unserviceable') $conditionClass = 'status-warning';
                                    elseif ($ammo['condition'] == 'Damaged') $conditionClass = 'status-rejected';
                                    ?>
                                    <span class="status-badge <?php echo $conditionClass; ?>">
                                        <?php echo Security::escape($ammo['condition']); ?>
                                    </span>
                                </td>
                                <td><?php echo Security::escape($ammo['remarks'] ?? '-'); ?></td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                        <tfoot>
                            <tr>
                                <th colspan="4" class="text-right">Totals:</th>
                                <th class="text-right"><?php echo array_sum(array_column($ammunition, 'rounds_returned')); ?></th>
                                <th class="text-right"><?php echo array_sum(array_column($ammunition, 'rounds_used')); ?></th>
                                <th colspan="2"></th>
                            </tr>
                        </tfoot>
                    </table>
                </div>
            </div>
        </div>
        <?php endif; ?>

        <!-- Remarks Card -->
        <?php if (!empty($return['remarks'])): ?>
        <div class="detail-card full-width">
            <div class="card-header">
                <h3><i class="fas fa-sticky-note"></i> Remarks</h3>
            </div>
            <div class="card-body">
                <p><?php echo nl2br(Security::escape($return['remarks'])); ?></p>
            </div>
        </div>
        <?php endif; ?>

        <!-- Metadata Card -->
        <div class="detail-card full-width">
            <div class="card-header">
                <h3><i class="fas fa-history"></i> Record Information</h3>
            </div>
            <div class="card-body">
                <div class="metadata-grid">
                    <div class="metadata-item">
                        <span class="metadata-label">Created By:</span>
                        <span class="metadata-value"><?php echo Security::escape($return['created_by_name'] ?? 'System'); ?></span>
                    </div>
                    <div class="metadata-item">
                        <span class="metadata-label">Created At:</span>
                        <span class="metadata-value"><?php echo date('d/m/Y H:i:s', strtotime($return['created_at'])); ?></span>
                    </div>
                    <div class="metadata-item">
                        <span class="metadata-label">Last Updated:</span>
                        <span class="metadata-value"><?php echo date('d/m/Y H:i:s', strtotime($return['updated_at'])); ?></span>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<style>
.details-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 20px;
    margin-bottom: 30px;
}

.detail-card {
    background: white;
    border-radius: 10px;
    box-shadow: 0 2px 10px rgba(0,0,0,0.05);
    overflow: hidden;
}

.detail-card.full-width {
    grid-column: 1 / -1;
}

.card-header {
    padding: 15px 20px;
    background: linear-gradient(135deg, var(--primary-color) 0%, var(--primary-light) 100%);
    color: white;
}

.card-header h3 {
    margin: 0;
    font-size: 1.1rem;
    display: flex;
    align-items: center;
    gap: 8px;
}

.card-body {
    padding: 20px;
}

.detail-row {
    display: flex;
    margin-bottom: 12px;
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 8px;
}

.detail-row:last-child {
    border-bottom: none;
    margin-bottom: 0;
    padding-bottom: 0;
}

.detail-label {
    flex: 0 0 120px;
    font-weight: 600;
    color: var(--text-secondary);
}

.detail-value {
    flex: 1;
    color: var(--text-primary);
    word-break: break-word;
}

/* Status Banner */
.status-banner {
    background: white;
    border-radius: 10px;
    padding: 20px 25px;
    margin-bottom: 25px;
    display: flex;
    align-items: center;
    gap: 20px;
    box-shadow: 0 2px 10px rgba(0,0,0,0.05);
    border-left: 6px solid;
}

.status-banner.status-success {
    border-left-color: var(--success-color);
}

.status-banner.status-warning {
    border-left-color: var(--warning-color);
}

.status-banner.status-info {
    border-left-color: var(--info-color);
}

.status-icon i {
    font-size: 2.5rem;
}

.status-success .status-icon i {
    color: var(--success-color);
}

.status-warning .status-icon i {
    color: var(--warning-color);
}

.status-info .status-icon i {
    color: var(--info-color);
}

.status-content h3 {
    margin: 0 0 5px 0;
    color: var(--text-primary);
}

.status-content p {
    margin: 0;
    color: var(--text-secondary);
}

/* Items Table */
.items-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.9rem;
}

.items-table th {
    background: var(--light-bg);
    padding: 12px 10px;
    text-align: left;
    font-weight: 600;
    color: var(--text-secondary);
    border-bottom: 2px solid var(--border-color);
}

.items-table td {
    padding: 10px;
    border-bottom: 1px solid var(--border-color);
}

.items-table tfoot th {
    padding: 15px 10px 5px;
    border-top: 2px solid var(--border-color);
}

.badge {
    display: inline-block;
    padding: 4px 8px;
    border-radius: 4px;
    font-size: 0.75rem;
    font-weight: 500;
}

.badge-info {
    background: #e0f2f1;
    color: #00695c;
}

.text-right {
    text-align: right;
}

/* Status Badges */
.status-badge {
    display: inline-block;
    padding: 4px 10px;
    border-radius: 20px;
    font-size: 0.75rem;
    font-weight: 600;
}

.status-active {
    background: #d4edda;
    color: #155724;
}

.status-warning {
    background: #fff3cd;
    color: #856404;
}

.status-rejected {
    background: #f8d7da;
    color: #721c24;
}

/* Metadata Grid */
.metadata-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 15px;
}

.metadata-item {
    padding: 10px;
    background: var(--light-bg);
    border-radius: 6px;
}

.metadata-label {
    display: block;
    font-size: 0.85rem;
    color: var(--text-secondary);
    margin-bottom: 5px;
}

.metadata-value {
    display: block;
    font-weight: 600;
    color: var(--text-primary);
}

@media (max-width: 768px) {
    .details-grid {
        grid-template-columns: 1fr;
    }
    
    .detail-row {
        flex-direction: column;
    }
    
    .detail-label {
        margin-bottom: 5px;
    }
    
    .status-banner {
        flex-direction: column;
        text-align: center;
    }
    
    .items-table {
        font-size: 0.8rem;
    }
    
    .metadata-grid {
        grid-template-columns: 1fr;
    }
}

@media print {
    .header-actions,
    .action-buttons-group,
    .sidebar,
    footer,
    .status-banner {
        display: none !important;
    }
    
    .main-content {
        margin-left: 0 !important;
        width: 100% !important;
        padding: 20px !important;
    }
    
    .detail-card {
        break-inside: avoid;
        box-shadow: none !important;
        border: 1px solid #ddd !important;
    }
    
    .card-header {
        background: #f8f9fa !important;
        color: #000 !important;
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
    }
}
</style>

<script>
function processReturn() {
    if (confirm('Are you sure you want to process this return? This will update inventory balances and cannot be undone.')) {
        window.location.href = '<?php echo BASE_URL; ?>/returns/process/<?php echo $return['id']; ?>';
    }
}
</script>

