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

// These variables come from the controller
$availableWeapons = isset($availableWeapons) ? $availableWeapons : [];
$availableAmmunition = isset($availableAmmunition) ? $availableAmmunition : [];
$requisitions = isset($requisitions) ? $requisitions : [];
$recentWeaponIssues = isset($recentWeaponIssues) ? $recentWeaponIssues : [];
$recentAmmoIssues = isset($recentAmmoIssues) ? $recentAmmoIssues : [];
$issuedWeapons = isset($issuedWeapons) ? $issuedWeapons : [];
$issuedAmmunition = isset($issuedAmmunition) ? $issuedAmmunition : [];
$statistics = isset($statistics) ? $statistics : [];

// Generate CSRF token manually
if (!isset($_SESSION['csrf_token'])) {
    $_SESSION['csrf_token'] = bin2hex(random_bytes(32));
}
$csrfToken = $_SESSION['csrf_token'];
?>

<div class="container-fluid">
    <!-- Page Header -->
    <div class="page-header">
        <div class="header-content">
            <h1>
                <i class="fas fa-hand-holding"></i>
                Weapons & Ammunition Issue
            </h1>
            <p>Issue firearms and ammunition to authorized personnel</p>
        </div>
        <div class="header-actions">
            <a href="<?php echo BASE_URL ?? ''; ?>/weapon_issue/history" class="btn btn-info">
                <i class="fas fa-history"></i> View Full History
            </a>
            <a href="<?php echo BASE_URL ?? ''; ?>/weapons" class="btn btn-secondary">
                <i class="fas fa-arrow-left"></i> Back to Weapons
            </a>
        </div>
    </div>

    <!-- Statistics Cards -->
    <div class="stats-grid">
        <div class="stat-card">
            <div class="stat-icon issued">
                <i class="fas fa-gun"></i>
            </div>
            <div class="stat-details">
                <h4>Issued Weapons</h4>
                <p class="stat-number"><?php echo number_format($statistics['total_issued_weapons'] ?? 0); ?></p>
            </div>
        </div>
        <div class="stat-card">
            <div class="stat-icon available">
                <i class="fas fa-check-circle"></i>
            </div>
            <div class="stat-details">
                <h4>Available Weapons</h4>
                <p class="stat-number"><?php echo number_format($statistics['available_weapons'] ?? 0); ?></p>
            </div>
        </div>
        <div class="stat-card">
            <div class="stat-icon pending">
                <i class="fas fa-clock"></i>
            </div>
            <div class="stat-details">
                <h4>Pending Returns</h4>
                <p class="stat-number"><?php echo number_format($statistics['pending_returns'] ?? 0); ?></p>
            </div>
        </div>
        <div class="stat-card">
            <div class="stat-icon ammo">
                <i class="fas fa-bullseye"></i>
            </div>
            <div class="stat-details">
                <h4>Ammo Types</h4>
                <p class="stat-number"><?php echo number_format($statistics['available_ammo'] ?? 0); ?></p>
            </div>
        </div>
    </div>

    <!-- Tab Navigation -->
    <div class="tab-navigation">
        <button class="tab-btn active" onclick="showTab('issue', this)">
            <i class="fas fa-plus-circle"></i> Issue Items
        </button>
        <button class="tab-btn" onclick="showTab('returns', this)">
            <i class="fas fa-undo-alt"></i> Process Returns
        </button>
        <button class="tab-btn" onclick="showTab('recent', this)">
            <i class="fas fa-history"></i> Recent Issues
        </button>
    </div>

    <!-- Issue Tab -->
    <div id="tab-issue" class="tab-content active">
        <!-- Issue Type Selector -->
        <div class="issue-type-selector">
            <button class="type-btn active" onclick="showIssueType('weapon', this)">
                <i class="fas fa-gun"></i> Issue Weapon
            </button>
            <button class="type-btn" onclick="showIssueType('ammunition', this)">
                <i class="fas fa-bullseye"></i> Issue Ammunition
            </button>
        </div>

        <!-- Weapon Issue Form -->
        <div id="weapon-issue-form" class="issue-form active">
            <div class="form-section">
                <div class="section-title">
                    <h3><i class="fas fa-gun"></i> Issue Weapon</h3>
                </div>

                <form method="POST" action="<?php echo BASE_URL ?? ''; ?>/weapon_issue/store" id="weaponIssueForm">
                    <!-- Manual CSRF field - THIS IS LINE 111 -->
                    <input type="hidden" name="csrf_token" value="<?php echo $csrfToken; ?>">
                    <input type="hidden" name="issue_type" value="weapon">

                    <div class="form-row">
                        <div class="form-group">
                            <label for="requisition_id">Requisition ID (Optional)</label>
                            <select name="requisition_id" id="requisition_id" class="form-control" onchange="loadRequisitionDetails(this.value, 'weapon')">
                                <option value="">None (Direct Issue)</option>
                                <?php foreach ($requisitions as $req): ?>
                                    <?php if (($req['total_weapons'] ?? 0) > 0): ?>
                                    <option value="<?php echo $req['id'] ?? ''; ?>" 
                                            data-officer="<?php echo htmlspecialchars($req['requesting_officer_name'] ?? ''); ?>"
                                            data-rank="<?php echo htmlspecialchars($req['requesting_rank'] ?? ''); ?>"
                                            data-unit="<?php echo htmlspecialchars($req['requesting_command_name'] ?? ''); ?>"
                                            data-purpose="<?php echo htmlspecialchars($req['justification'] ?? ''); ?>">
                                        <?php echo htmlspecialchars(($req['requisition_number'] ?? '') . ' - ' . ($req['requesting_officer_name'] ?? '') . ' (' . ($req['total_weapons'] ?? 0) . ' weapons)'); ?>
                                    </option>
                                    <?php endif; ?>
                                <?php endforeach; ?>
                            </select>
                        </div>

                        <div class="form-group">
                            <label for="issue_date" class="required">Issue Date</label>
                            <input type="date" id="issue_date" name="issue_date" class="form-control" 
                                   value="<?php echo date('Y-m-d'); ?>" required>
                        </div>
                    </div>

                    <div class="form-row">
                        <div class="form-group">
                            <label for="weapon_id" class="required">Select Weapon</label>
                            <select name="weapon_id" id="weapon_id" class="form-control" required onchange="updateWeaponDetails(this)">
                                <option value="">-- Select Weapon --</option>
                                <?php foreach ($availableWeapons as $weapon): ?>
                                <option value="<?php echo $weapon['id'] ?? ''; ?>"
                                        data-type="<?php echo htmlspecialchars($weapon['type_name'] ?? $weapon['weapon_type_other'] ?? 'Other'); ?>"
                                        data-model="<?php echo htmlspecialchars($weapon['make_model'] ?? ''); ?>"
                                        data-serial="<?php echo htmlspecialchars($weapon['serial_no'] ?? ''); ?>"
                                        data-calibre="<?php echo htmlspecialchars($weapon['calibre_name'] ?? $weapon['calibre_other'] ?? 'N/A'); ?>">
                                    <?php echo htmlspecialchars(($weapon['weapon_id'] ?? '') . ' - ' . ($weapon['make_model'] ?? '') . ' (SN: ' . ($weapon['serial_no'] ?? '') . ')'); ?>
                                </option>
                                <?php endforeach; ?>
                            </select>
                        </div>

                        <div class="form-group">
                            <label>Weapon Details</label>
                            <div class="weapon-details-display" id="weaponDetails">
                                <div class="detail-item"><span class="label">Type:</span> <span id="weaponTypeDisplay">-</span></div>
                                <div class="detail-item"><span class="label">Model:</span> <span id="weaponModelDisplay">-</span></div>
                                <div class="detail-item"><span class="label">Serial:</span> <span id="weaponSerialDisplay">-</span></div>
                                <div class="detail-item"><span class="label">Calibre:</span> <span id="weaponCalibreDisplay">-</span></div>
                            </div>
                        </div>
                    </div>

                    <div class="form-row">
                        <div class="form-group">
                            <label for="officer_name" class="required">Officer Name</label>
                            <input type="text" name="officer_name" id="officer_name" class="form-control" required>
                        </div>

                        <div class="form-group">
                            <label for="officer_rank" class="required">Rank</label>
                            <input type="text" name="officer_rank" id="officer_rank" class="form-control" required>
                        </div>
                    </div>

                    <div class="form-row">
                        <div class="form-group">
                            <label for="officer_nis">NIS Number</label>
                            <input type="text" name="officer_nis" id="officer_nis" class="form-control">
                        </div>

                        <div class="form-group">
                            <label for="unit" class="required">Unit/Department</label>
                            <input type="text" name="unit" id="unit" class="form-control" required>
                        </div>
                    </div>

                    <div class="form-row">
                        <div class="form-group">
                            <label for="purpose" class="required">Purpose</label>
                            <select name="purpose" id="purpose" class="form-control" required onchange="togglePurposeOther(this)">
                                <option value="">Select Purpose</option>
                                <option value="Special Operation">Special Operation</option>
                                <option value="Training Exercise">Training Exercise</option>
                                <option value="Patrol Duty">Patrol Duty</option>
                                <option value="Security Detail">Security Detail</option>
                                <option value="Emergency Response">Emergency Response</option>
                                <option value="Escort Duty">Escort Duty</option>
                                <option value="Border Patrol">Border Patrol</option>
                                <option value="Other">Other</option>
                            </select>
                        </div>

                        <div class="form-group" id="purposeOtherGroup" style="display: none;">
                            <label for="purpose_other">Specify Purpose</label>
                            <input type="text" name="purpose_other" id="purpose_other" class="form-control">
                        </div>

                        <div class="form-group">
                            <label for="approved_by" class="required">Approved By</label>
                            <input type="text" name="approved_by" id="approved_by" class="form-control" required>
                        </div>
                    </div>

                    <div class="form-row">
                        <div class="form-group">
                            <label for="expected_return_date">Expected Return Date</label>
                            <input type="date" name="expected_return_date" id="expected_return_date" class="form-control">
                        </div>

                        <div class="form-group">
                            <label for="remarks">Remarks</label>
                            <input type="text" name="remarks" id="remarks" class="form-control">
                        </div>
                    </div>

                    <div class="form-actions">
                        <button type="submit" class="btn btn-success">
                            <i class="fas fa-check-circle"></i> Issue Weapon
                        </button>
                        <button type="button" class="btn btn-secondary" onclick="resetWeaponForm()">
                            <i class="fas fa-undo"></i> Reset
                        </button>
                    </div>
                </form>
            </div>
        </div>

        <!-- Ammunition Issue Form -->
        <div id="ammunition-issue-form" class="issue-form">
            <div class="form-section">
                <div class="section-title">
                    <h3><i class="fas fa-bullseye"></i> Issue Ammunition</h3>
                </div>

                <form method="POST" action="<?php echo BASE_URL ?? ''; ?>/weapon_issue/store" id="ammoIssueForm">
                    <!-- Manual CSRF field -->
                    <input type="hidden" name="csrf_token" value="<?php echo $csrfToken; ?>">
                    <input type="hidden" name="issue_type" value="ammunition">

                    <div class="form-row">
                        <div class="form-group">
                            <label for="ammo_requisition_id">Requisition ID (Optional)</label>
                            <select name="requisition_id" id="ammo_requisition_id" class="form-control" onchange="loadRequisitionDetails(this.value, 'ammo')">
                                <option value="">None (Direct Issue)</option>
                                <?php foreach ($requisitions as $req): ?>
                                    <?php if (($req['total_ammunition'] ?? 0) > 0): ?>
                                    <option value="<?php echo $req['id'] ?? ''; ?>" 
                                            data-officer="<?php echo htmlspecialchars($req['requesting_officer_name'] ?? ''); ?>">
                                        <?php echo htmlspecialchars(($req['requisition_number'] ?? '') . ' - ' . ($req['requesting_officer_name'] ?? '') . ' (' . ($req['total_ammunition'] ?? 0) . ' rounds)'); ?>
                                    </option>
                                    <?php endif; ?>
                                <?php endforeach; ?>
                            </select>
                        </div>

                        <div class="form-group">
                            <label for="ammo_issue_date" class="required">Issue Date</label>
                            <input type="date" id="ammo_issue_date" name="issue_date" class="form-control" 
                                   value="<?php echo date('Y-m-d'); ?>" required>
                        </div>
                    </div>

                    <div class="form-row">
                        <div class="form-group">
                            <label for="ammo_id" class="required">Select Ammunition</label>
                            <select name="ammo_id" id="ammo_id" class="form-control" required onchange="updateAmmoDetails(this)">
                                <option value="">-- Select Ammunition --</option>
                                <?php foreach ($availableAmmunition as $ammo): ?>
                                <option value="<?php echo $ammo['id'] ?? ''; ?>"
                                        data-type="<?php echo htmlspecialchars($ammo['ammo_type'] ?? 'Ammunition'); ?>"
                                        data-calibre="<?php echo htmlspecialchars($ammo['calibre'] ?? 'N/A'); ?>"
                                        data-balance="<?php echo $ammo['balance'] ?? 0; ?>">
                                    <?php echo htmlspecialchars(($ammo['ammo_id'] ?? '') . ' - ' . ($ammo['ammo_type'] ?? '') . ' (' . ($ammo['calibre'] ?? '') . ') - Balance: ' . ($ammo['balance'] ?? 0)); ?>
                                </option>
                                <?php endforeach; ?>
                            </select>
                        </div>

                        <div class="form-group">
                            <label>Ammunition Details</label>
                            <div class="weapon-details-display" id="ammoDetails">
                                <div class="detail-item"><span class="label">Type:</span> <span id="ammoTypeDisplay">-</span></div>
                                <div class="detail-item"><span class="label">Calibre:</span> <span id="ammoCalibreDisplay">-</span></div>
                                <div class="detail-item"><span class="label">Balance:</span> <span id="ammoBalanceDisplay">-</span></div>
                            </div>
                        </div>
                    </div>

                    <div class="form-row">
                        <div class="form-group">
                            <label for="units_issued" class="required">Units to Issue</label>
                            <input type="number" name="units_issued" id="units_issued" class="form-control" 
                                   min="1" value="1" required oninput="calculateTotalRounds()">
                        </div>

                        <div class="form-group">
                            <label for="total_rounds">Total Rounds</label>
                            <input type="number" name="total_rounds" id="total_rounds" class="form-control" 
                                   value="30" readonly>
                            <small class="form-hint">Calculated automatically (30 rounds per unit)</small>
                        </div>
                    </div>

                    <div class="form-row">
                        <div class="form-group">
                            <label for="issued_to" class="required">Issued To (Officer Name)</label>
                            <input type="text" name="issued_to" id="issued_to" class="form-control" required>
                        </div>

                        <div class="form-group">
                            <label for="ammo_purpose" class="required">Purpose</label>
                            <select name="purpose" id="ammo_purpose" class="form-control" required onchange="toggleAmmoPurposeOther(this)">
                                <option value="">Select Purpose</option>
                                <option value="Special Operation">Special Operation</option>
                                <option value="Training Exercise">Training Exercise</option>
                                <option value="Patrol Duty">Patrol Duty</option>
                                <option value="Security Detail">Security Detail</option>
                                <option value="Emergency Response">Emergency Response</option>
                                <option value="Escort Duty">Escort Duty</option>
                                <option value="Other">Other</option>
                            </select>
                        </div>
                    </div>

                    <div class="form-group" id="ammoPurposeOtherGroup" style="display: none;">
                        <label for="ammo_purpose_other">Specify Purpose</label>
                        <input type="text" name="purpose_other" id="ammo_purpose_other" class="form-control">
                    </div>

                    <div class="form-row">
                        <div class="form-group">
                            <label for="ammo_approved_by" class="required">Approved By</label>
                            <input type="text" name="approved_by" id="ammo_approved_by" class="form-control" required>
                        </div>

                        <div class="form-group">
                            <label for="ammo_remarks">Remarks</label>
                            <input type="text" name="remarks" id="ammo_remarks" class="form-control">
                        </div>
                    </div>

                    <div class="form-actions">
                        <button type="submit" class="btn btn-success">
                            <i class="fas fa-check-circle"></i> Issue Ammunition
                        </button>
                        <button type="button" class="btn btn-secondary" onclick="resetAmmoForm()">
                            <i class="fas fa-undo"></i> Reset
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <!-- Returns Tab -->
    <div id="tab-returns" class="tab-content">
        <div class="returns-section">
            <div class="section-title">
                <h3><i class="fas fa-undo-alt"></i> Process Returns</h3>
                <div class="card-actions">
                    <input type="text" id="searchReturns" class="search-input" placeholder="Search issued items...">
                </div>
            </div>

            <!-- Issued Weapons Table -->
            <div class="subsection">
                <h4><i class="fas fa-gun"></i> Issued Weapons</h4>
                <div class="table-responsive">
                    <?php if (empty($issuedWeapons)): ?>
                        <div class="empty-state">
                            <i class="fas fa-gun"></i>
                            <p>No weapons currently issued</p>
                        </div>
                    <?php else: ?>
                    <table class="asset-table" id="issuedWeaponsTable">
                        <thead>
                            <tr>
                                <th>Weapon ID</th>
                                <th>Make/Model</th>
                                <th>Serial No.</th>
                                <th>Issued To</th>
                                <th>Issue Date</th>
                                <th>Expected Return</th>
                                <th>Days Out</th>
                                <th>Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($issuedWeapons as $weapon): 
                                $issueDate = isset($weapon['issue_date']) ? strtotime($weapon['issue_date']) : time();
                                $daysOut = floor((time() - $issueDate) / (60 * 60 * 24));
                                $expectedReturn = isset($weapon['expected_return_date']) ? strtotime($weapon['expected_return_date']) : 0;
                                $overdueClass = ($expectedReturn && $expectedReturn < time()) ? 'overdue' : '';
                            ?>
                            <tr class="<?php echo $overdueClass; ?>">
                                <td><?php echo htmlspecialchars($weapon['weapon_id'] ?? ''); ?></td>
                                <td><?php echo htmlspecialchars($weapon['make_model'] ?? ''); ?></td>
                                <td><?php echo htmlspecialchars($weapon['serial_no'] ?? ''); ?></td>
                                <td><?php echo htmlspecialchars($weapon['officer_name'] ?? ''); ?></td>
                                <td><?php echo !empty($weapon['issue_date']) ? date('d/m/Y', strtotime($weapon['issue_date'])) : '-'; ?></td>
                                <td>
                                    <?php echo !empty($weapon['expected_return_date']) ? date('d/m/Y', strtotime($weapon['expected_return_date'])) : 'Not set'; ?>
                                </td>
                                <td>
                                    <span class="days-badge <?php echo $daysOut > 30 ? 'badge-danger' : ($daysOut > 14 ? 'badge-warning' : 'badge-success'); ?>">
                                        <?php echo $daysOut; ?> days
                                    </span>
                                </td>
                                <td>
                                    <a href="<?php echo BASE_URL ?? ''; ?>/weapon_issue/return/<?php echo $weapon['id'] ?? ''; ?>?type=weapon" 
                                       class="btn btn-sm btn-primary">
                                        <i class="fas fa-undo-alt"></i> Return
                                    </a>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                    <?php endif; ?>
                </div>
            </div>

            <!-- Issued Ammunition Table -->
            <div class="subsection">
                <h4><i class="fas fa-bullseye"></i> Issued Ammunition</h4>
                <div class="table-responsive">
                    <?php if (empty($issuedAmmunition)): ?>
                        <div class="empty-state">
                            <i class="fas fa-bullseye"></i>
                            <p>No ammunition currently issued</p>
                        </div>
                    <?php else: ?>
                    <table class="asset-table" id="issuedAmmoTable">
                        <thead>
                            <tr>
                                <th>Ammunition</th>
                                <th>Type/Calibre</th>
                                <th>Units Issued</th>
                                <th>Issued To</th>
                                <th>Issue Date</th>
                                <th>Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($issuedAmmunition as $ammo): ?>
                            <tr>
                                <td><?php echo htmlspecialchars($ammo['ammo_id'] ?? ''); ?></td>
                                <td><?php echo htmlspecialchars(($ammo['ammo_type'] ?? '') . ' (' . ($ammo['calibre'] ?? '') . ')'); ?></td>
                                <td><?php echo $ammo['units_issued'] ?? 0; ?></td>
                                <td><?php echo htmlspecialchars($ammo['issued_to'] ?? ''); ?></td>
                                <td><?php echo !empty($ammo['issue_date']) ? date('d/m/Y', strtotime($ammo['issue_date'])) : '-'; ?></td>
                                <td>
                                    <a href="<?php echo BASE_URL ?? ''; ?>/weapon_issue/return/<?php echo $ammo['id'] ?? ''; ?>?type=ammunition" 
                                       class="btn btn-sm btn-primary">
                                        <i class="fas fa-undo-alt"></i> Return
                                    </a>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>

    <!-- Recent Issues Tab -->
    <div id="tab-recent" class="tab-content">
        <div class="recent-section">
            <!-- Recent Weapon Issues -->
            <div class="subsection">
                <div class="section-title">
                    <h3><i class="fas fa-gun"></i> Recent Weapon Issues</h3>
                    <a href="<?php echo BASE_URL ?? ''; ?>/weapon_issue/history?type=weapons" class="view-all">View All</a>
                </div>

                <div class="table-responsive">
                    <?php if (empty($recentWeaponIssues)): ?>
                        <div class="empty-state">
                            <i class="fas fa-gun"></i>
                            <p>No weapon issues recorded</p>
                        </div>
                    <?php else: ?>
                    <table class="asset-table">
                        <thead>
                            <tr>
                                <th>Date</th>
                                <th>Weapon ID</th>
                                <th>Make/Model</th>
                                <th>Officer</th>
                                <th>Purpose</th>
                                <th>Status</th>
                                <th>Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($recentWeaponIssues as $issue): ?>
                            <tr>
                                <td><?php echo !empty($issue['issue_date']) ? date('d/m/Y', strtotime($issue['issue_date'])) : '-'; ?></td>
                                <td><?php echo htmlspecialchars($issue['weapon_id'] ?? ''); ?></td>
                                <td><?php echo htmlspecialchars($issue['make_model'] ?? ''); ?></td>
                                <td><?php echo htmlspecialchars($issue['officer_name'] ?? ''); ?></td>
                                <td><?php echo htmlspecialchars($issue['purpose'] ?? ''); ?></td>
                                <td>
                                    <?php 
                                    $statusClass = '';
                                    if (($issue['status'] ?? '') == 'Returned') $statusClass = 'status-active';
                                    elseif (($issue['status'] ?? '') == 'Issued') $statusClass = 'status-warning';
                                    ?>
                                    <span class="status-badge <?php echo $statusClass; ?>">
                                        <?php echo htmlspecialchars($issue['status'] ?? 'Issued'); ?>
                                    </span>
                                </td>
                                <td>
                                    <a href="<?php echo BASE_URL ?? ''; ?>/weapon_issue/show/<?php echo $issue['id'] ?? ''; ?>?type=weapon" 
                                       class="btn-icon" title="View Details">
                                        <i class="fas fa-eye"></i>
                                    </a>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                    <?php endif; ?>
                </div>
            </div>

            <!-- Recent Ammunition Issues -->
            <div class="subsection">
                <div class="section-title">
                    <h3><i class="fas fa-bullseye"></i> Recent Ammunition Issues</h3>
                    <a href="<?php echo BASE_URL ?? ''; ?>/weapon_issue/history?type=ammunition" class="view-all">View All</a>
                </div>

                <div class="table-responsive">
                    <?php if (empty($recentAmmoIssues)): ?>
                        <div class="empty-state">
                            <i class="fas fa-bullseye"></i>
                            <p>No ammunition issues recorded</p>
                        </div>
                    <?php else: ?>
                    <table class="asset-table">
                        <thead>
                            <tr>
                                <th>Date</th>
                                <th>Ammunition</th>
                                <th>Units</th>
                                <th>Rounds</th>
                                <th>Issued To</th>
                                <th>Status</th>
                                <th>Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($recentAmmoIssues as $issue): ?>
                            <tr>
                                <td><?php echo !empty($issue['issue_date']) ? date('d/m/Y', strtotime($issue['issue_date'])) : '-'; ?></td>
                                <td><?php echo htmlspecialchars($issue['ammo_id'] ?? ''); ?></td>
                                <td><?php echo $issue['units_issued'] ?? 0; ?></td>
                                <td><?php echo $issue['total_rounds'] ?? 0; ?></td>
                                <td><?php echo htmlspecialchars($issue['issued_to'] ?? ''); ?></td>
                                <td>
                                    <span class="status-badge status-<?php echo strtolower($issue['status'] ?? 'issued'); ?>">
                                        <?php echo htmlspecialchars($issue['status'] ?? 'Issued'); ?>
                                    </span>
                                </td>
                                <td>
                                    <a href="<?php echo BASE_URL ?? ''; ?>/weapon_issue/show/<?php echo $issue['id'] ?? ''; ?>?type=ammunition" 
                                       class="btn-icon" title="View Details">
                                        <i class="fas fa-eye"></i>
                                    </a>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</div>

<style>
/* Stats Grid */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 20px;
    margin-bottom: 25px;
}

.stat-card {
    background: white;
    border-radius: 10px;
    padding: 20px;
    display: flex;
    align-items: center;
    gap: 15px;
    box-shadow: 0 2px 10px rgba(0,0,0,0.05);
}

.stat-icon {
    width: 50px;
    height: 50px;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.5rem;
}

.stat-icon.issued {
    background: #fff3e0;
    color: #f57c00;
}

.stat-icon.available {
    background: #e8f5e9;
    color: #388e3c;
}

.stat-icon.pending {
    background: #ffebee;
    color: #c62828;
}

.stat-icon.ammo {
    background: #e0f2f1;
    color: #00796b;
}

.stat-details h4 {
    margin: 0 0 5px 0;
    font-size: 0.9rem;
    color: #7f8c8d;
}

.stat-number {
    font-size: 1.8rem;
    font-weight: bold;
    margin: 0;
    color: #2c3e50;
    line-height: 1.2;
}

/* Tab Navigation */
.tab-navigation {
    display: flex;
    gap: 10px;
    margin-bottom: 25px;
    background: white;
    padding: 15px;
    border-radius: 10px;
    box-shadow: 0 2px 10px rgba(0,0,0,0.05);
}

.tab-btn {
    padding: 10px 20px;
    border: none;
    border-radius: 6px;
    background: #f8f9fa;
    color: #2c3e50;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s;
    display: flex;
    align-items: center;
    gap: 8px;
}

.tab-btn:hover {
    background: #e9ecef;
}

.tab-btn.active {
    background: #27ae60;
    color: white;
}

.tab-content {
    display: none;
}

.tab-content.active {
    display: block;
}

/* Issue Type Selector */
.issue-type-selector {
    display: flex;
    gap: 10px;
    margin-bottom: 20px;
    background: #f8f9fa;
    padding: 10px;
    border-radius: 8px;
}

.type-btn {
    flex: 1;
    padding: 12px;
    border: none;
    border-radius: 6px;
    background: white;
    color: #2c3e50;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
}

.type-btn:hover {
    background: #e9ecef;
}

.type-btn.active {
    background: #2c3e50;
    color: white;
}

/* Issue Forms */
.issue-form {
    display: none;
}

.issue-form.active {
    display: block;
}

/* Form Sections */
.form-section {
    background: white;
    border-radius: 10px;
    padding: 25px;
    margin-bottom: 25px;
    box-shadow: 0 2px 10px rgba(0,0,0,0.05);
}

.section-title {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    padding-bottom: 15px;
    border-bottom: 1px solid #e2e8f0;
}

.section-title h3 {
    margin: 0;
    font-size: 1.2rem;
    color: #2c3e50;
    display: flex;
    align-items: center;
    gap: 8px;
}

.view-all {
    color: #27ae60;
    text-decoration: none;
    font-weight: 500;
    font-size: 0.9rem;
}

.view-all:hover {
    text-decoration: underline;
}

.form-row {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 20px;
    margin-bottom: 20px;
}

.form-group {
    display: flex;
    flex-direction: column;
}

.form-group label {
    font-weight: 600;
    color: #2c3e50;
    margin-bottom: 8px;
}

.required::after {
    content: " *";
    color: #e53e3e;
}

.form-control {
    padding: 10px 12px;
    border: 1px solid #e2e8f0;
    border-radius: 6px;
    font-size: 0.95rem;
    transition: all 0.3s;
}

.form-control:focus {
    outline: none;
    border-color: #27ae60;
    box-shadow: 0 0 0 3px rgba(39, 174, 96, 0.2);
}

.form-hint {
    font-size: 0.8rem;
    color: #95a5a6;
    margin-top: 5px;
}

/* Weapon Details Display */
.weapon-details-display {
    background: #f8f9fa;
    padding: 12px;
    border-radius: 6px;
    border: 1px solid #e2e8f0;
}

.detail-item {
    display: flex;
    margin-bottom: 5px;
}

.detail-item .label {
    font-weight: 600;
    width: 60px;
    color: #7f8c8d;
}

.detail-item span:last-child {
    color: #2c3e50;
}

/* Form Actions */
.form-actions {
    display: flex;
    gap: 15px;
    margin-top: 25px;
    padding-top: 20px;
    border-top: 1px solid #e2e8f0;
}

.btn {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 10px 20px;
    border: none;
    border-radius: 6px;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s;
    text-decoration: none;
}

.btn-success {
    background: #27ae60;
    color: white;
}

.btn-success:hover {
    background: #219a52;
    transform: translateY(-2px);
    box-shadow: 0 4px 8px rgba(39, 174, 96, 0.3);
}

.btn-secondary {
    background: #7f8c8d;
    color: white;
}

.btn-secondary:hover {
    background: #6c757d;
}

.btn-primary {
    background: #2c3e50;
    color: white;
}

.btn-primary:hover {
    background: #1a252f;
}

.btn-sm {
    padding: 5px 10px;
    font-size: 0.85rem;
}

/* Subsections */
.subsection {
    background: white;
    border-radius: 10px;
    padding: 20px;
    margin-bottom: 25px;
    box-shadow: 0 2px 10px rgba(0,0,0,0.05);
}

.subsection h4 {
    margin: 0 0 15px 0;
    color: #2c3e50;
    display: flex;
    align-items: center;
    gap: 8px;
}

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

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

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

/* Days Badge */
.days-badge {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 12px;
    font-size: 0.8rem;
    font-weight: 600;
}

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

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

.badge-danger {
    background: #f8d7da;
    color: #721c24;
}

.overdue {
    background: #fff5f5;
}

/* Empty State */
.empty-state {
    text-align: center;
    padding: 40px;
    color: #7f8c8d;
}

.empty-state i {
    font-size: 3rem;
    margin-bottom: 15px;
    opacity: 0.5;
}

.empty-state p {
    font-size: 1rem;
    margin: 0;
}

/* Search Input */
.search-input {
    padding: 8px 12px;
    border: 1px solid #e2e8f0;
    border-radius: 6px;
    width: 250px;
    font-size: 0.9rem;
}

/* Responsive */
@media (max-width: 768px) {
    .stats-grid {
        grid-template-columns: 1fr;
    }
    
    .tab-navigation {
        flex-direction: column;
    }
    
    .form-row {
        grid-template-columns: 1fr;
    }
    
    .form-actions {
        flex-direction: column;
    }
    
    .form-actions .btn {
        width: 100%;
    }
    
    .search-input {
        width: 100%;
    }
}
</style>

<script>
// Tab switching - Fixed to accept element parameter
function showTab(tabName, element) {
    // Hide all tabs
    document.querySelectorAll('.tab-content').forEach(tab => {
        tab.classList.remove('active');
    });
    
    // Remove active class from all tab buttons
    document.querySelectorAll('.tab-btn').forEach(btn => {
        btn.classList.remove('active');
    });
    
    // Show selected tab
    document.getElementById('tab-' + tabName).classList.add('active');
    
    // Add active class to clicked button
    if (element) {
        element.classList.add('active');
    }
}

// Issue type switching - Fixed to accept element parameter
function showIssueType(type, element) {
    document.querySelectorAll('.issue-form').forEach(form => {
        form.classList.remove('active');
    });
    
    document.querySelectorAll('.type-btn').forEach(btn => {
        btn.classList.remove('active');
    });
    
    document.getElementById(type + '-issue-form').classList.add('active');
    
    if (element) {
        element.classList.add('active');
    }
}

// Purpose other field toggle
function togglePurposeOther(select) {
    const otherGroup = document.getElementById('purposeOtherGroup');
    const otherInput = document.getElementById('purpose_other');
    
    if (select.value === 'Other') {
        otherGroup.style.display = 'block';
        if (otherInput) otherInput.required = true;
    } else {
        otherGroup.style.display = 'none';
        if (otherInput) {
            otherInput.required = false;
            otherInput.value = '';
        }
    }
}

function toggleAmmoPurposeOther(select) {
    const otherGroup = document.getElementById('ammoPurposeOtherGroup');
    const otherInput = document.getElementById('ammo_purpose_other');
    
    if (select.value === 'Other') {
        otherGroup.style.display = 'block';
        if (otherInput) otherInput.required = true;
    } else {
        otherGroup.style.display = 'none';
        if (otherInput) {
            otherInput.required = false;
            otherInput.value = '';
        }
    }
}

// Load requisition details
function loadRequisitionDetails(requisitionId, type) {
    if (!requisitionId) return;
    
    const select = document.getElementById(type === 'weapon' ? 'requisition_id' : 'ammo_requisition_id');
    const option = select.options[select.selectedIndex];
    
    if (type === 'weapon') {
        const officerField = document.getElementById('officer_name');
        const rankField = document.getElementById('officer_rank');
        const unitField = document.getElementById('unit');
        
        if (officerField) officerField.value = option.getAttribute('data-officer') || '';
        if (rankField) rankField.value = option.getAttribute('data-rank') || '';
        if (unitField) unitField.value = option.getAttribute('data-unit') || '';
        
        const purpose = option.getAttribute('data-purpose') || '';
        const purposeSelect = document.getElementById('purpose');
        
        if (purposeSelect && purpose) {
            // Try to match purpose with existing options
            let matched = false;
            for (let i = 0; i < purposeSelect.options.length; i++) {
                if (purposeSelect.options[i].value === purpose) {
                    purposeSelect.selectedIndex = i;
                    matched = true;
                    break;
                }
            }
            
            if (!matched) {
                purposeSelect.value = 'Other';
                const purposeOther = document.getElementById('purpose_other');
                if (purposeOther) {
                    purposeOther.value = purpose;
                    document.getElementById('purposeOtherGroup').style.display = 'block';
                }
            }
        }
    } else {
        const issuedTo = document.getElementById('issued_to');
        if (issuedTo) issuedTo.value = option.getAttribute('data-officer') || '';
    }
}

// Update weapon details
function updateWeaponDetails(select) {
    const option = select.options[select.selectedIndex];
    
    document.getElementById('weaponTypeDisplay').textContent = option.getAttribute('data-type') || '-';
    document.getElementById('weaponModelDisplay').textContent = option.getAttribute('data-model') || '-';
    document.getElementById('weaponSerialDisplay').textContent = option.getAttribute('data-serial') || '-';
    document.getElementById('weaponCalibreDisplay').textContent = option.getAttribute('data-calibre') || '-';
}

// Update ammunition details
function updateAmmoDetails(select) {
    const option = select.options[select.selectedIndex];
    
    document.getElementById('ammoTypeDisplay').textContent = option.getAttribute('data-type') || '-';
    document.getElementById('ammoCalibreDisplay').textContent = option.getAttribute('data-calibre') || '-';
    document.getElementById('ammoBalanceDisplay').textContent = option.getAttribute('data-balance') || '-';
    
    // Update max for units issued
    const unitsInput = document.getElementById('units_issued');
    if (unitsInput) {
        const balance = parseInt(option.getAttribute('data-balance')) || 0;
        unitsInput.max = balance;
        
        if (parseInt(unitsInput.value) > balance) {
            unitsInput.value = balance;
            calculateTotalRounds();
        }
    }
}

// Calculate total rounds
function calculateTotalRounds() {
    const units = parseInt(document.getElementById('units_issued').value) || 0;
    const total = units * 30; // 30 rounds per unit as standard
    const totalField = document.getElementById('total_rounds');
    if (totalField) totalField.value = total;
}

// Reset forms
function resetWeaponForm() {
    if (confirm('Reset weapon issue form? All entered data will be lost.')) {
        const form = document.getElementById('weaponIssueForm');
        if (form) form.reset();
        
        // Reset weapon details display
        document.getElementById('weaponTypeDisplay').textContent = '-';
        document.getElementById('weaponModelDisplay').textContent = '-';
        document.getElementById('weaponSerialDisplay').textContent = '-';
        document.getElementById('weaponCalibreDisplay').textContent = '-';
        
        // Hide other field
        document.getElementById('purposeOtherGroup').style.display = 'none';
        
        // Set default date
        const today = new Date().toISOString().split('T')[0];
        const issueDate = document.getElementById('issue_date');
        if (issueDate) issueDate.value = today;
        
        // Set default return date (30 days from now)
        const date = new Date();
        date.setDate(date.getDate() + 30);
        const returnDate = document.getElementById('expected_return_date');
        if (returnDate) returnDate.value = date.toISOString().split('T')[0];
    }
}

function resetAmmoForm() {
    if (confirm('Reset ammunition issue form? All entered data will be lost.')) {
        const form = document.getElementById('ammoIssueForm');
        if (form) form.reset();
        
        // Reset ammo details display
        document.getElementById('ammoTypeDisplay').textContent = '-';
        document.getElementById('ammoCalibreDisplay').textContent = '-';
        document.getElementById('ammoBalanceDisplay').textContent = '-';
        
        // Hide other field
        document.getElementById('ammoPurposeOtherGroup').style.display = 'none';
        
        // Reset total rounds
        const totalField = document.getElementById('total_rounds');
        if (totalField) totalField.value = '30';
        
        // Set default date
        const today = new Date().toISOString().split('T')[0];
        const ammoIssueDate = document.getElementById('ammo_issue_date');
        if (ammoIssueDate) ammoIssueDate.value = today;
    }
}

// Initialize on page load
document.addEventListener('DOMContentLoaded', function() {
    // Set default dates if not set
    const today = new Date().toISOString().split('T')[0];
    
    const issueDate = document.getElementById('issue_date');
    if (issueDate && !issueDate.value) {
        issueDate.value = today;
    }
    
    const ammoIssueDate = document.getElementById('ammo_issue_date');
    if (ammoIssueDate && !ammoIssueDate.value) {
        ammoIssueDate.value = today;
    }
    
    // Set default expected return date (30 days from now) if not set
    const returnDate = document.getElementById('expected_return_date');
    if (returnDate && !returnDate.value) {
        const date = new Date();
        date.setDate(date.getDate() + 30);
        returnDate.value = date.toISOString().split('T')[0];
    }
    
    // Initialize search functionality for returns
    const searchReturns = document.getElementById('searchReturns');
    if (searchReturns) {
        searchReturns.addEventListener('input', function(e) {
            const searchTerm = e.target.value.toLowerCase();
            
            // Search weapons table
            const weaponRows = document.querySelectorAll('#issuedWeaponsTable tbody tr');
            weaponRows.forEach(row => {
                const text = row.textContent.toLowerCase();
                row.style.display = text.includes(searchTerm) ? '' : 'none';
            });
            
            // Search ammo table
            const ammoRows = document.querySelectorAll('#issuedAmmoTable tbody tr');
            ammoRows.forEach(row => {
                const text = row.textContent.toLowerCase();
                row.style.display = text.includes(searchTerm) ? '' : 'none';
            });
        });
    }
});

// Form validation
document.getElementById('weaponIssueForm')?.addEventListener('submit', function(e) {
    const weaponId = document.getElementById('weapon_id').value;
    const officerName = document.getElementById('officer_name').value;
    const approvedBy = document.getElementById('approved_by').value;
    
    if (!weaponId) {
        e.preventDefault();
        alert('Please select a weapon');
        return false;
    }
    
    if (!officerName) {
        e.preventDefault();
        alert('Please enter officer name');
        return false;
    }
    
    if (!approvedBy) {
        e.preventDefault();
        alert('Please enter approving officer');
        return false;
    }
    
    // Show loading state
    const submitBtn = this.querySelector('button[type="submit"]');
    if (submitBtn) {
        submitBtn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> Processing...';
        submitBtn.disabled = true;
    }
});

document.getElementById('ammoIssueForm')?.addEventListener('submit', function(e) {
    const ammoId = document.getElementById('ammo_id').value;
    const units = parseInt(document.getElementById('units_issued').value) || 0;
    const issuedTo = document.getElementById('issued_to').value;
    const approvedBy = document.getElementById('ammo_approved_by').value;
    
    if (!ammoId) {
        e.preventDefault();
        alert('Please select ammunition');
        return false;
    }
    
    if (!issuedTo) {
        e.preventDefault();
        alert('Please enter who the ammunition is issued to');
        return false;
    }
    
    if (!approvedBy) {
        e.preventDefault();
        alert('Please enter approving officer');
        return false;
    }
    
    // Get selected option to check balance
    const select = document.getElementById('ammo_id');
    const selectedOption = select.options[select.selectedIndex];
    const balance = parseInt(selectedOption?.getAttribute('data-balance') || 0);
    
    if (units > balance) {
        e.preventDefault();
        alert(`Cannot issue more than available balance. Available: ${balance} units`);
        return false;
    }
    
    // Show loading state
    const submitBtn = this.querySelector('button[type="submit"]');
    if (submitBtn) {
        submitBtn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> Processing...';
        submitBtn.disabled = true;
    }
});
</script>
