<?php
$title = 'Requisition Details';
$active = 'requisitions';
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>
                Requisition Details
            </h1>
            <p>Requisition #: <?php echo Security::escape($requisition['requisition_number']); ?></p>
        </div>
        <div class="header-actions">
            <a href="<?php echo BASE_URL; ?>/requisition" class="btn btn-secondary">
                <i class="fas fa-arrow-left"></i> Back to Requisitions
            </a>
            <?php if ($requisition['status'] == 'Pending' && Auth::can('requisition.edit')): ?>
            <a href="<?php echo BASE_URL; ?>/requisition/edit/<?php echo $requisition['id']; ?>" class="btn btn-success">
                <i class="fas fa-edit"></i> Edit Requisition
            </a>
            <?php endif; ?>
            <?php if ($requisition['status'] == 'Pending' && Auth::can('requisition.approve')): ?>
            <button class="btn btn-primary" onclick="approveRequisition()">
                <i class="fas fa-check-circle"></i> Approve
            </button>
            <button class="btn btn-danger" onclick="rejectRequisition()">
                <i class="fas fa-times-circle"></i> Reject
            </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 ($requisition['status'] == 'Pending') {
        $statusClass = 'status-warning';
        $statusMessage = 'This requisition is pending approval.';
    } elseif ($requisition['status'] == 'Approved') {
        $statusClass = 'status-success';
        $statusMessage = 'This requisition has been approved and is awaiting issue.';
    } elseif ($requisition['status'] == 'Rejected') {
        $statusClass = 'status-danger';
        $statusMessage = 'This requisition has been rejected. Reason: ' . $requisition['rejection_reason'];
    } elseif ($requisition['status'] == 'Issued') {
        $statusClass = 'status-info';
        $statusMessage = 'Items have been issued.';
    } elseif ($requisition['status'] == 'Partially Issued') {
        $statusClass = 'status-warning';
        $statusMessage = 'Some items have been issued.';
    } elseif ($requisition['status'] == 'Completed') {
        $statusClass = 'status-success';
        $statusMessage = 'All items have been returned and requisition is complete.';
    }
    ?>
    
    <div class="status-banner <?php echo $statusClass; ?>">
        <div class="status-icon">
            <i class="fas <?php 
                if ($requisition['status'] == 'Pending') echo 'fa-clock';
                elseif ($requisition['status'] == 'Approved') echo 'fa-check-circle';
                elseif ($requisition['status'] == 'Rejected') echo 'fa-times-circle';
                elseif ($requisition['status'] == 'Issued') echo 'fa-hand-holding';
                elseif ($requisition['status'] == 'Partially Issued') echo 'fa-adjust';
                elseif ($requisition['status'] == 'Completed') echo 'fa-check-double';
            ?>"></i>
        </div>
        <div class="status-content">
            <h3><?php echo Security::escape($requisition['status']); ?></h3>
            <p><?php echo $statusMessage; ?></p>
        </div>
    </div>

    <!-- Requisition Details Grid -->
    <div class="details-grid">
        <!-- Requisition Information -->
        <div class="detail-card">
            <div class="card-header">
                <h3><i class="fas fa-file-signature"></i> Requisition Information</h3>
            </div>
            <div class="card-body">
                <div class="detail-row">
                    <div class="detail-label">Requisition #:</div>
                    <div class="detail-value"><?php echo Security::escape($requisition['requisition_number']); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Date:</div>
                    <div class="detail-value"><?php echo date('d/m/Y', strtotime($requisition['requisition_date'])); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Type:</div>
                    <div class="detail-value">
                        <span class="badge badge-info"><?php echo Security::escape($requisition['requisition_type']); ?></span>
                    </div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Priority:</div>
                    <div class="detail-value">
                        <?php 
                        $priorityClass = '';
                        if ($requisition['priority_level'] == 'Urgent') $priorityClass = 'priority-urgent';
                        elseif ($requisition['priority_level'] == 'High') $priorityClass = 'priority-high';
                        elseif ($requisition['priority_level'] == 'Medium') $priorityClass = 'priority-medium';
                        elseif ($requisition['priority_level'] == 'Low') $priorityClass = 'priority-low';
                        ?>
                        <span class="priority-badge <?php echo $priorityClass; ?>">
                            <?php echo Security::escape($requisition['priority_level']); ?>
                        </span>
                    </div>
                </div>
                <?php if ($requisition['expected_return_date']): ?>
                <div class="detail-row">
                    <div class="detail-label">Expected Return:</div>
                    <div class="detail-value"><?php echo date('d/m/Y', strtotime($requisition['expected_return_date'])); ?></div>
                </div>
                <?php endif; ?>
            </div>
        </div>

        <!-- Requester Information -->
        <div class="detail-card">
            <div class="card-header">
                <h3><i class="fas fa-user-shield"></i> Requester 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($requisition['requesting_officer_name']); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Rank:</div>
                    <div class="detail-value"><?php echo Security::escape($requisition['requesting_rank']); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">NIS Number:</div>
                    <div class="detail-value"><?php echo Security::escape($requisition['requesting_nis']); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Phone:</div>
                    <div class="detail-value">
                        <a href="tel:<?php echo Security::escape($requisition['requesting_phone']); ?>">
                            <?php echo Security::escape($requisition['requesting_phone']); ?>
                        </a>
                    </div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Command:</div>
                    <div class="detail-value"><?php echo Security::escape($requisition['command_name']); ?></div>
                </div>
            </div>
        </div>

        <!-- Approval Information (if approved/rejected) -->
        <?php if ($requisition['status'] != 'Pending'): ?>
        <div class="detail-card">
            <div class="card-header">
                <h3><i class="fas fa-clipboard-check"></i> Approval Information</h3>
            </div>
            <div class="card-body">
                <?php if ($requisition['approved_by']): ?>
                <div class="detail-row">
                    <div class="detail-label">Approved By:</div>
                    <div class="detail-value"><?php echo Security::escape($requisition['approved_by_name'] ?? 'N/A'); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Approval Date:</div>
                    <div class="detail-value"><?php echo $requisition['approval_date'] ? date('d/m/Y H:i', strtotime($requisition['approval_date'])) : 'N/A'; ?></div>
                </div>
                <?php if ($requisition['approval_remarks']): ?>
                <div class="detail-row">
                    <div class="detail-label">Remarks:</div>
                    <div class="detail-value"><?php echo Security::escape($requisition['approval_remarks']); ?></div>
                </div>
                <?php endif; ?>
                <?php endif; ?>
                
                <?php if ($requisition['rejection_reason']): ?>
                <div class="detail-row">
                    <div class="detail-label">Rejection Reason:</div>
                    <div class="detail-value text-danger"><?php echo Security::escape($requisition['rejection_reason']); ?></div>
                </div>
                <?php endif; ?>
                
                <?php if ($requisition['issued_by']): ?>
                <div class="detail-row">
                    <div class="detail-label">Issued By:</div>
                    <div class="detail-value"><?php echo Security::escape($requisition['issued_by_name'] ?? 'N/A'); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Issue Date:</div>
                    <div class="detail-value"><?php echo $requisition['issue_date'] ? date('d/m/Y H:i', strtotime($requisition['issue_date'])) : 'N/A'; ?></div>
                </div>
                <?php endif; ?>
            </div>
        </div>
        <?php endif; ?>

        <!-- Items Card -->
        <div class="detail-card full-width">
            <div class="card-header">
                <h3><i class="fas fa-clipboard-list"></i> Requisition Items</h3>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="items-table">
                        <thead>
                            <tr>
                                <th>S/N</th>
                                <th>Item Type</th>
                                <th>Details</th>
                                <th>Calibre</th>
                                <th>Quantity</th>
                                <th>Purpose</th>
                                <th>Remarks</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($items as $index => $item): ?>
                            <tr>
                                <td><?php echo $index + 1; ?></td>
                                <td>
                                    <span class="badge badge-info"><?php echo Security::escape($item['item_type']); ?></span>
                                </td>
                                <td>
                                    <?php if ($item['item_type'] == 'Weapon'): ?>
                                        <?php echo Security::escape($item['weapon_type_name'] ?? $item['weapon_type_other']); ?>
                                    <?php else: ?>
                                        <?php echo Security::escape($item['ammo_type_name'] ?? $item['ammo_type_other']); ?>
                                    <?php endif; ?>
                                </td>
                                <td><?php echo Security::escape($item['calibre_name'] ?? $item['calibre_other'] ?? 'N/A'); ?></td>
                                <td class="text-right"><?php echo number_format($item['quantity']); ?></td>
                                <td><?php echo Security::escape($item['purpose'] ?? $item['purpose_other'] ?? 'N/A'); ?></td>
                                <td><?php echo Security::escape($item['remarks'] ?? '-'); ?></td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                        <tfoot>
                            <tr>
                                <th colspan="4" class="text-right">Total Items:</th>
                                <th class="text-right"><?php echo count($items); ?></th>
                                <th colspan="2"></th>
                            </tr>
                        </tfoot>
                    </table>
                </div>
            </div>
        </div>

        <!-- Justification Card -->
        <div class="detail-card full-width">
            <div class="card-header">
                <h3><i class="fas fa-pen"></i> Justification</h3>
            </div>
            <div class="card-body">
                <p class="justification-text"><?php echo nl2br(Security::escape($requisition['justification'])); ?></p>
            </div>
        </div>

        <!-- Remarks Card -->
        <?php if (!empty($requisition['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($requisition['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($requisition['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($requisition['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($requisition['updated_at'])); ?></span>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Action Buttons for Issuing (if approved) -->
    <?php if ($requisition['status'] == 'Approved' && Auth::can('requisition.issue')): ?>
    <div class="action-buttons-group">
        <a href="<?php echo BASE_URL; ?>/issue/create?requisition_id=<?php echo $requisition['id']; ?>" class="btn btn-primary">
            <i class="fas fa-hand-holding"></i> Process Issue
        </a>
    </div>
    <?php endif; ?>
</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-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-content h3 {
    margin: 0 0 5px 0;
    color: var(--text-primary);
}

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

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

.priority-urgent {
    background: #ffebee;
    color: #c62828;
}

.priority-high {
    background: #fff3e0;
    color: #ef6c00;
}

.priority-medium {
    background: #e8f5e9;
    color: #2e7d32;
}

.priority-low {
    background: #e3f2fd;
    color: #1565c0;
}

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

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

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

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

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

/* Action Buttons */
.action-buttons-group {
    display: flex;
    gap: 15px;
    margin-top: 20px;
    padding-top: 20px;
    border-top: 1px solid var(--border-color);
}

@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.85rem;
    }
    
    .metadata-grid {
        grid-template-columns: 1fr;
    }
    
    .action-buttons-group {
        flex-direction: column;
    }
    
    .action-buttons-group .btn {
        width: 100%;
    }
}

@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 approveRequisition() {
    if (confirm('Are you sure you want to approve this requisition?')) {
        const form = document.createElement('form');
        form.method = 'POST';
        form.action = '<?php echo BASE_URL; ?>/requisitions/approve/<?php echo $requisition['id']; ?>';
        
        const csrf = document.createElement('input');
        csrf.type = 'hidden';
        csrf.name = 'csrf_token';
        csrf.value = '<?php echo Security::csrfToken(); ?>';
        form.appendChild(csrf);
        
        document.body.appendChild(form);
        form.submit();
    }
}

function rejectRequisition() {
    const reason = prompt('Please enter reason for rejection:');
    if (reason) {
        const form = document.createElement('form');
        form.method = 'POST';
        form.action = '<?php echo BASE_URL; ?>/requisitions/reject/<?php echo $requisition['id']; ?>';
        
        const csrf = document.createElement('input');
        csrf.type = 'hidden';
        csrf.name = 'csrf_token';
        csrf.value = '<?php echo Security::csrfToken(); ?>';
        form.appendChild(csrf);
        
        const reasonInput = document.createElement('input');
        reasonInput.type = 'hidden';
        reasonInput.name = 'rejection_reason';
        reasonInput.value = reason;
        form.appendChild(reasonInput);
        
        document.body.appendChild(form);
        form.submit();
    }
}
</script>
