<?php
$title = 'Quarterly Audit Details';
$active = 'audit';
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>
                Quarterly Audit Details
            </h1>
            <p>Audit #: <?php echo Security::escape($audit['audit_number']); ?> - <?php echo Security::escape($audit['quarter'] . ' ' . $audit['year']); ?></p>
        </div>
        <div class="header-actions">
            <a href="<?php echo BASE_URL; ?>/audit/quarterly" class="btn btn-secondary">
                <i class="fas fa-arrow-left"></i> Back to Audits
            </a>
            <?php if ($audit['status'] == 'Draft' && Auth::can('audit.edit')): ?>
            <a href="<?php echo BASE_URL; ?>/audit/quarterly/edit/<?php echo $audit['id']; ?>" class="btn btn-success">
                <i class="fas fa-edit"></i> Edit Audit
            </a>
            <?php endif; ?>
            <?php if ($audit['status'] == 'Submitted' && Auth::can('audit.approve')): ?>
            <button class="btn btn-primary" onclick="reviewAudit()">
                <i class="fas fa-eye"></i> Mark as Reviewed
            </button>
            <button class="btn btn-success" onclick="approveAudit()">
                <i class="fas fa-check-circle"></i> Approve Audit
            </button>
            <?php endif; ?>
            <?php if ($audit['status'] == 'Reviewed' && Auth::can('audit.approve')): ?>
            <button class="btn btn-success" onclick="approveAudit()">
                <i class="fas fa-check-circle"></i> Approve Audit
            </button>
            <?php endif; ?>
            <button class="btn btn-outline" onclick="window.print()">
                <i class="fas fa-print"></i> Print
            </button>
            <a href="<?php echo BASE_URL; ?>/reports/audit?audit_id=<?php echo $audit['id']; ?>&format=pdf" class="btn btn-outline">
                <i class="fas fa-file-pdf"></i> Export PDF
            </a>
        </div>
    </div>

    <!-- Status Banner -->
    <?php
    $statusClass = '';
    $statusMessage = '';
    $statusIcon = '';
    
    if ($audit['status'] == 'Draft') {
        $statusClass = 'status-secondary';
        $statusMessage = 'This audit is in draft state and has not been submitted.';
        $statusIcon = 'fa-pencil-alt';
    } elseif ($audit['status'] == 'Submitted') {
        $statusClass = 'status-warning';
        $statusMessage = 'This audit has been submitted and is pending review.';
        $statusIcon = 'fa-paper-plane';
    } elseif ($audit['status'] == 'Reviewed') {
        $statusClass = 'status-info';
        $statusMessage = 'This audit has been reviewed and is pending approval.';
        $statusIcon = 'fa-eye';
    } elseif ($audit['status'] == 'Approved') {
        $statusClass = 'status-success';
        $statusMessage = 'This audit has been approved and finalized.';
        $statusIcon = 'fa-check-circle';
    }
    ?>
    
    <div class="status-banner <?php echo $statusClass; ?>">
        <div class="status-icon">
            <i class="fas <?php echo $statusIcon; ?>"></i>
        </div>
        <div class="status-content">
            <h3><?php echo Security::escape($audit['status']); ?></h3>
            <p><?php echo $statusMessage; ?></p>
        </div>
    </div>

    <!-- Variance Alert -->
    <?php if (($audit['weapons_with_variance'] > 0 || $audit['ammunition_with_variance'] > 0) && $audit['status'] != 'Approved'): ?>
    <div class="status-banner status-danger">
        <div class="status-icon">
            <i class="fas fa-exclamation-triangle"></i>
        </div>
        <div class="status-content">
            <h3>Variances Detected</h3>
            <p>
                This audit has <?php echo $audit['weapons_with_variance']; ?> weapon variance(s) 
                and <?php echo $audit['ammunition_with_variance']; ?> ammunition variance(s).
                <?php if ($audit['total_missing_weapons'] > 0): ?>
                <strong><?php echo $audit['total_missing_weapons']; ?> weapon(s) reported missing.</strong>
                <?php endif; ?>
            </p>
        </div>
    </div>
    <?php endif; ?>

    <!-- Audit Details Grid -->
    <div class="details-grid">
        <!-- Audit Information -->
        <div class="detail-card">
            <div class="card-header">
                <h3><i class="fas fa-file-signature"></i> Audit Information</h3>
            </div>
            <div class="card-body">
                <div class="detail-row">
                    <div class="detail-label">Audit #:</div>
                    <div class="detail-value"><?php echo Security::escape($audit['audit_number']); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Audit Date:</div>
                    <div class="detail-value"><?php echo date('d/m/Y', strtotime($audit['audit_date'])); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Quarter:</div>
                    <div class="detail-value"><?php echo Security::escape($audit['quarter']); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Year:</div>
                    <div class="detail-value"><?php echo $audit['year']; ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Location:</div>
                    <div class="detail-value"><?php echo Security::escape($audit['audit_location']); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Command:</div>
                    <div class="detail-value"><?php echo Security::escape($audit['command_name'] ?? 'N/A'); ?></div>
                </div>
            </div>
        </div>

        <!-- Audit Officer Information -->
        <div class="detail-card">
            <div class="card-header">
                <h3><i class="fas fa-user-shield"></i> Audit 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($audit['audit_officer']); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Rank:</div>
                    <div class="detail-value"><?php echo Security::escape($audit['auditor_rank']); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">NIS Number:</div>
                    <div class="detail-value"><?php echo Security::escape($audit['auditor_nis']); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Unit:</div>
                    <div class="detail-value"><?php echo Security::escape($audit['unit']); ?></div>
                </div>
            </div>
        </div>

        <!-- Summary Statistics -->
        <div class="detail-card">
            <div class="card-header">
                <h3><i class="fas fa-chart-pie"></i> Summary Statistics</h3>
            </div>
            <div class="card-body">
                <div class="detail-row">
                    <div class="detail-label">Weapons Audited:</div>
                    <div class="detail-value"><?php echo $audit['total_weapons_audited']; ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Weapons with Variance:</div>
                    <div class="detail-value <?php echo $audit['weapons_with_variance'] > 0 ? 'text-danger' : ''; ?>">
                        <?php echo $audit['weapons_with_variance']; ?>
                    </div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Ammunition Audited:</div>
                    <div class="detail-value"><?php echo $audit['total_ammunition_audited']; ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Ammunition with Variance:</div>
                    <div class="detail-value <?php echo $audit['ammunition_with_variance'] > 0 ? 'text-danger' : ''; ?>">
                        <?php echo $audit['ammunition_with_variance']; ?>
                    </div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Missing Weapons:</div>
                    <div class="detail-value <?php echo $audit['total_missing_weapons'] > 0 ? 'text-danger' : ''; ?>">
                        <?php echo $audit['total_missing_weapons']; ?>
                    </div>
                </div>
            </div>
        </div>

        <!-- Approval Information -->
        <?php if ($audit['recommending_officer'] || $audit['approving_officer']): ?>
        <div class="detail-card">
            <div class="card-header">
                <h3><i class="fas fa-clipboard-check"></i> Approval Chain</h3>
            </div>
            <div class="card-body">
                <?php if ($audit['recommending_officer']): ?>
                <div class="detail-row">
                    <div class="detail-label">Recommending:</div>
                    <div class="detail-value"><?php echo Security::escape($audit['recommending_officer']); ?></div>
                </div>
                <?php endif; ?>
                <?php if ($audit['approving_officer']): ?>
                <div class="detail-row">
                    <div class="detail-label">Approving:</div>
                    <div class="detail-value"><?php echo Security::escape($audit['approving_officer']); ?></div>
                </div>
                <?php endif; ?>
            </div>
        </div>
        <?php endif; ?>

        <!-- Weapons Audit Results -->
        <?php if (!empty($weapons)): ?>
        <div class="detail-card full-width">
            <div class="card-header">
                <h3><i class="fas fa-gun"></i> Weapons Audit Results</h3>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="audit-results-table">
                        <thead>
                            <tr>
                                <th>Weapon ID</th>
                                <th>Type</th>
                                <th>Make/Model</th>
                                <th>Serial Number</th>
                                <th>System Status</th>
                                <th>Physical Status</th>
                                <th>Variance</th>
                                <th>Condition</th>
                                <th>Remarks</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($weapons as $weapon): ?>
                            <tr class="<?php echo $weapon['variance_value'] != 0 ? 'variance-row' : ''; ?>">
                                <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 echo Security::escape($weapon['system_status']); ?></td>
                                <td>
                                    <?php 
                                    $statusClass = '';
                                    if ($weapon['physical_status'] == 'Present') $statusClass = 'status-active';
                                    elseif ($weapon['physical_status'] == 'Missing') $statusClass = 'status-danger';
                                    elseif ($weapon['physical_status'] == 'Damaged') $statusClass = 'status-warning';
                                    elseif ($weapon['physical_status'] == 'Under Repair') $statusClass = 'status-info';
                                    ?>
                                    <span class="status-badge <?php echo $statusClass; ?>">
                                        <?php echo Security::escape($weapon['physical_status']); ?>
                                    </span>
                                </td>
                                <td class="<?php echo $weapon['variance_value'] < 0 ? 'text-danger' : ($weapon['variance_value'] > 0 ? 'text-warning' : ''); ?>">
                                    <?php echo $weapon['variance']; ?>
                                </td>
                                <td><?php echo Security::escape($weapon['condition'] ?? 'N/A'); ?></td>
                                <td><?php echo Security::escape($weapon['remarks'] ?? '-'); ?></td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        <?php endif; ?>

        <!-- Ammunition Audit Results -->
        <?php if (!empty($ammunition)): ?>
        <div class="detail-card full-width">
            <div class="card-header">
                <h3><i class="fas fa-bullseye"></i> Ammunition Audit Results</h3>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="audit-results-table">
                        <thead>
                            <tr>
                                <th>Ammunition ID</th>
                                <th>Type</th>
                                <th>Calibre</th>
                                <th>System Units</th>
                                <th>Physical Units</th>
                                <th>Variance</th>
                                <th>Condition</th>
                                <th>Remarks</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($ammunition as $ammo): ?>
                            <tr class="<?php echo $ammo['variance_value'] != 0 ? 'variance-row' : ''; ?>">
                                <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 class="text-right"><?php echo number_format($ammo['system_units']); ?></td>
                                <td class="text-right"><?php echo number_format($ammo['physical_units']); ?></td>
                                <td class="<?php echo $ammo['variance_value'] < 0 ? 'text-danger' : ($ammo['variance_value'] > 0 ? 'text-warning' : ''); ?> text-right">
                                    <?php echo $ammo['variance']; ?>
                                </td>
                                <td><?php echo Security::escape($ammo['condition'] ?? 'N/A'); ?></td>
                                <td><?php echo Security::escape($ammo['remarks'] ?? '-'); ?></td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                        <tfoot>
                            <tr>
                                <th colspan="3" class="text-right">Totals:</th>
                                <th class="text-right"><?php echo array_sum(array_column($ammunition, 'system_units')); ?></th>
                                <th class="text-right"><?php echo array_sum(array_column($ammunition, 'physical_units')); ?></th>
                                <th class="text-right"><?php echo array_sum(array_column($ammunition, 'variance_value')); ?></th>
                                <th colspan="2"></th>
                            </tr>
                        </tfoot>
                    </table>
                </div>
            </div>
        </div>
        <?php endif; ?>

        <!-- Missing Weapons -->
        <?php if (!empty($missing)): ?>
        <div class="detail-card full-width">
            <div class="card-header">
                <h3><i class="fas fa-exclamation-triangle"></i> Missing Weapons Report</h3>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="audit-results-table">
                        <thead>
                            <tr>
                                <th>Arm Type</th>
                                <th>Serial Number</th>
                                <th>Last Known Location</th>
                                <th>Date Missing</th>
                                <th>Reported By</th>
                                <th>Investigation Status</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($missing as $item): ?>
                            <tr>
                                <td><?php echo Security::escape($item['arm_type']); ?></td>
                                <td><?php echo Security::escape($item['serial_number']); ?></td>
                                <td><?php echo Security::escape($item['last_known_location'] ?? 'N/A'); ?></td>
                                <td><?php echo $item['date_missing'] ? date('d/m/Y', strtotime($item['date_missing'])) : 'N/A'; ?></td>
                                <td><?php echo Security::escape($item['reported_by'] ?? 'N/A'); ?></td>
                                <td>
                                    <?php 
                                    $investigationClass = '';
                                    if ($item['investigation_status'] == 'Reported') $investigationClass = 'status-warning';
                                    elseif ($item['investigation_status'] == 'Under Investigation') $investigationClass = 'status-info';
                                    elseif ($item['investigation_status'] == 'Board of Inquiry') $investigationClass = 'status-danger';
                                    elseif ($item['investigation_status'] == 'Closed') $investigationClass = 'status-success';
                                    ?>
                                    <span class="status-badge <?php echo $investigationClass; ?>">
                                        <?php echo Security::escape($item['investigation_status']); ?>
                                    </span>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        <?php endif; ?>

        <!-- Audit Conclusion -->
        <?php if (!empty($audit['audit_conclusion'])): ?>
        <div class="detail-card full-width">
            <div class="card-header">
                <h3><i class="fas fa-clipboard-check"></i> Audit Conclusion</h3>
            </div>
            <div class="card-body">
                <p class="conclusion-text"><?php echo nl2br(Security::escape($audit['audit_conclusion'])); ?></p>
            </div>
        </div>
        <?php endif; ?>

        <!-- Audit Remarks -->
        <?php if (!empty($audit['audit_remarks'])): ?>
        <div class="detail-card full-width">
            <div class="card-header">
                <h3><i class="fas fa-sticky-note"></i> Audit Remarks</h3>
            </div>
            <div class="card-body">
                <p><?php echo nl2br(Security::escape($audit['audit_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($audit['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($audit['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($audit['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-danger {
    border-left-color: var(--danger-color);
}

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

.status-banner.status-secondary {
    border-left-color: #95a5a6;
}

.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-info .status-icon i {
    color: var(--info-color);
}

.status-secondary .status-icon i {
    color: #95a5a6;
}

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

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

/* Audit Results Table */
.audit-results-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.9rem;
}

.audit-results-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);
}

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

.audit-results-table tfoot th {
    padding: 15px 10px 5px;
    border-top: 2px solid var(--border-color);
    background: var(--light-bg);
}

.variance-row {
    background-color: #fff3e0;
}

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

.text-danger {
    color: var(--danger-color);
    font-weight: 600;
}

.text-warning {
    color: var(--warning-color);
    font-weight: 600;
}

/* 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-danger {
    background: #f8d7da;
    color: #721c24;
}

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

.status-info {
    background: #d1ecf1;
    color: #0c5460;
}

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

/* 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);
}

.conclusion-text {
    line-height: 1.6;
    color: var(--text-primary);
    white-space: pre-wrap;
}

@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;
    }
    
    .audit-results-table {
        font-size: 0.8rem;
    }
    
    .metadata-grid {
        grid-template-columns: 1fr;
    }
}

@media print {
    .header-actions,
    .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 reviewAudit() {
    if (confirm('Mark this audit as reviewed?')) {
        window.location.href = '<?php echo BASE_URL; ?>/audit/quarterly/review/<?php echo $audit['id']; ?>';
    }
}

function approveAudit() {
    if (confirm('Are you sure you want to approve this audit? This action cannot be undone.')) {
        window.location.href = '<?php echo BASE_URL; ?>/audit/quarterly/approve/<?php echo $audit['id']; ?>';
    }
}
</script>

