<?php
$title = 'Issue Details';
$active = 'weapon_issue';
require_once __DIR__ . '/../layouts/header.php';
require_once __DIR__ . '/../layouts/sidebar.php';

$issue = isset($issue) ? $issue : [];
$type = isset($type) ? $type : 'weapon';
?>

<div class="container-fluid">
    <!-- Page Header -->
    <div class="page-header">
        <div class="header-content">
            <h1>
                <i class="fas fa-eye"></i>
                Issue Details
            </h1>
            <p>
                <?php if ($type === 'weapon'): ?>
                    Weapon Issue #<?php echo $issue['id']; ?>
                <?php else: ?>
                    Ammunition Issue #<?php echo $issue['id']; ?>
                <?php endif; ?>
            </p>
        </div>
        <div class="header-actions">
            <a href="<?php echo BASE_URL; ?>/weapon_issue/history" class="btn btn-secondary">
                <i class="fas fa-arrow-left"></i> Back to History
            </a>
            <?php if (($type === 'weapon' && $issue['status'] == 'Issued') || ($type === 'ammunition' && ($issue['status'] ?? 'Issued') == 'Issued')): ?>
            <a href="<?php echo BASE_URL; ?>/weapon_issue/return/<?php echo $issue['id']; ?>?type=<?php echo $type; ?>" 
               class="btn btn-success">
                <i class="fas fa-undo-alt"></i> Process Return
            </a>
            <?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 ($type === 'weapon') {
        if ($issue['status'] == 'Issued') {
            $statusClass = 'status-warning';
            $statusMessage = 'This weapon is currently issued.';
        } elseif ($issue['status'] == 'Returned') {
            $statusClass = 'status-success';
            $statusMessage = 'This weapon has been returned.';
        }
    } else {
        if (($issue['status'] ?? 'Issued') == 'Issued') {
            $statusClass = 'status-warning';
            $statusMessage = 'This ammunition is currently issued.';
        } elseif (($issue['status'] ?? '') == 'Returned') {
            $statusClass = 'status-success';
            $statusMessage = 'This ammunition has been returned.';
        }
    }
    
    if ($type === 'weapon' && $issue['status'] == 'Issued' && $issue['expected_return_date'] && strtotime($issue['expected_return_date']) < time()):
        $statusClass = 'status-danger';
        $statusMessage = 'This weapon is OVERDUE for return.';
    endif;
    ?>
    
    <div class="status-banner <?php echo $statusClass; ?>">
        <div class="status-icon">
            <i class="fas <?php 
                if ($statusClass == 'status-warning') echo 'fa-clock';
                elseif ($statusClass == 'status-success') echo 'fa-check-circle';
                elseif ($statusClass == 'status-danger') echo 'fa-exclamation-circle';
            ?>"></i>
        </div>
        <div class="status-content">
            <h3><?php echo $type === 'weapon' ? $issue['status'] : ($issue['status'] ?? 'Issued'); ?></h3>
            <p><?php echo $statusMessage; ?></p>
        </div>
    </div>

    <!-- Details Grid -->
    <div class="details-grid">
        <?php if ($type === 'weapon'): ?>
            <!-- Weapon Issue Details -->
            <div class="detail-card">
                <div class="card-header">
                    <h3><i class="fas fa-gun"></i> Weapon Information</h3>
                </div>
                <div class="card-body">
                    <div class="detail-row">
                        <div class="detail-label">Weapon ID:</div>
                        <div class="detail-value"><?php echo Security::escape($issue['weapon_id']); ?></div>
                    </div>
                    <div class="detail-row">
                        <div class="detail-label">Make/Model:</div>
                        <div class="detail-value"><?php echo Security::escape($issue['make_model']); ?></div>
                    </div>
                    <div class="detail-row">
                        <div class="detail-label">Serial Number:</div>
                        <div class="detail-value"><?php echo Security::escape($issue['serial_no']); ?></div>
                    </div>
                    <div class="detail-row">
                        <div class="detail-label">Type:</div>
                        <div class="detail-value"><?php echo Security::escape($issue['type_name'] ?? 'N/A'); ?></div>
                    </div>
                    <div class="detail-row">
                        <div class="detail-label">Calibre:</div>
                        <div class="detail-value"><?php echo Security::escape($issue['calibre_name'] ?? 'N/A'); ?></div>
                    </div>
                </div>
            </div>

            <div class="detail-card">
                <div class="card-header">
                    <h3><i class="fas fa-user-shield"></i> Officer Information</h3>
                </div>
                <div class="card-body">
                    <div class="detail-row">
                        <div class="detail-label">Name:</div>
                        <div class="detail-value"><?php echo Security::escape($issue['officer_name']); ?></div>
                    </div>
                    <div class="detail-row">
                        <div class="detail-label">Rank:</div>
                        <div class="detail-value"><?php echo Security::escape($issue['officer_rank']); ?></div>
                    </div>
                    <?php if (!empty($issue['officer_nis'])): ?>
                    <div class="detail-row">
                        <div class="detail-label">NIS Number:</div>
                        <div class="detail-value"><?php echo Security::escape($issue['officer_nis']); ?></div>
                    </div>
                    <?php endif; ?>
                    <div class="detail-row">
                        <div class="detail-label">Unit:</div>
                        <div class="detail-value"><?php echo Security::escape($issue['unit']); ?></div>
                    </div>
                </div>
            </div>

        <?php else: ?>
            <!-- Ammunition Issue Details -->
            <div class="detail-card">
                <div class="card-header">
                    <h3><i class="fas fa-bullseye"></i> Ammunition Information</h3>
                </div>
                <div class="card-body">
                    <div class="detail-row">
                        <div class="detail-label">Ammunition ID:</div>
                        <div class="detail-value"><?php echo Security::escape($issue['ammo_id']); ?></div>
                    </div>
                    <div class="detail-row">
                        <div class="detail-label">Type:</div>
                        <div class="detail-value"><?php echo Security::escape($issue['ammo_type'] ?? 'N/A'); ?></div>
                    </div>
                    <div class="detail-row">
                        <div class="detail-label">Calibre:</div>
                        <div class="detail-value"><?php echo Security::escape($issue['calibre'] ?? 'N/A'); ?></div>
                    </div>
                </div>
            </div>

            <div class="detail-card">
                <div class="card-header">
                    <h3><i class="fas fa-info-circle"></i> Issue Details</h3>
                </div>
                <div class="card-body">
                    <div class="detail-row">
                        <div class="detail-label">Units Issued:</div>
                        <div class="detail-value"><?php echo $issue['units_issued']; ?></div>
                    </div>
                    <div class="detail-row">
                        <div class="detail-label">Total Rounds:</div>
                        <div class="detail-value"><?php echo $issue['total_rounds']; ?></div>
                    </div>
                    <div class="detail-row">
                        <div class="detail-label">Issued To:</div>
                        <div class="detail-value"><?php echo Security::escape($issue['issued_to']); ?></div>
                    </div>
                </div>
            </div>
        <?php endif; ?>

        <!-- Issue Information (Common) -->
        <div class="detail-card">
            <div class="card-header">
                <h3><i class="fas fa-calendar-alt"></i> Issue Information</h3>
            </div>
            <div class="card-body">
                <div class="detail-row">
                    <div class="detail-label">Issue Date:</div>
                    <div class="detail-value"><?php echo date('d/m/Y', strtotime($issue['issue_date'])); ?></div>
                </div>
                <?php if (!empty($issue['requisition_number'])): ?>
                <div class="detail-row">
                    <div class="detail-label">Requisition:</div>
                    <div class="detail-value">
                        <a href="<?php echo BASE_URL; ?>/requisitions/show/<?php echo $issue['requisition_id']; ?>">
                            <?php echo Security::escape($issue['requisition_number']); ?>
                        </a>
                    </div>
                </div>
                <?php endif; ?>
                <div class="detail-row">
                    <div class="detail-label">Purpose:</div>
                    <div class="detail-value"><?php echo Security::escape($issue['purpose']); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Approved By:</div>
                    <div class="detail-value"><?php echo Security::escape($issue['approved_by']); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Issued By:</div>
                    <div class="detail-value"><?php echo Security::escape($issue['issued_by_name'] ?? 'System'); ?></div>
                </div>
            </div>
        </div>

        <!-- Return Information -->
        <?php if (($type === 'weapon' && !empty($issue['actual_return_date'])) || ($type === 'ammunition' && !empty($issue['return_date']))): ?>
        <div class="detail-card">
            <div class="card-header">
                <h3><i class="fas fa-undo-alt"></i> Return Information</h3>
            </div>
            <div class="card-body">
                <?php if ($type === 'weapon'): ?>
                <div class="detail-row">
                    <div class="detail-label">Return Date:</div>
                    <div class="detail-value"><?php echo date('d/m/Y', strtotime($issue['actual_return_date'])); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Return Condition:</div>
                    <div class="detail-value"><?php echo Security::escape($issue['return_condition']); ?></div>
                </div>
                <?php else: ?>
                <div class="detail-row">
                    <div class="detail-label">Return Date:</div>
                    <div class="detail-value"><?php echo date('d/m/Y', strtotime($issue['return_date'])); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Rounds Returned:</div>
                    <div class="detail-value"><?php echo $issue['rounds_returned']; ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Rounds Used:</div>
                    <div class="detail-value"><?php echo $issue['rounds_used']; ?></div>
                </div>
                <?php endif; ?>
            </div>
        </div>
        <?php endif; ?>

        <!-- Remarks Card -->
        <?php if (!empty($issue['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 class="remarks-text"><?php echo nl2br(Security::escape($issue['remarks'])); ?></p>
            </div>
        </div>
        <?php endif; ?>
    </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-danger {
    border-left-color: var(--danger-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-danger .status-icon i {
    color: var(--danger-color);
}

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

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

@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;
    }
}

@media print {
    .header-actions,
    .sidebar,
    footer {
        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>

