<?php
$title = 'Asset Reports';
$active = 'reports';
$init_charts = true;
require_once __DIR__ . '/../layouts/header.php';
require_once __DIR__ . '/../layouts/sidebar.php';

$type = $_GET['type'] ?? 'summary';
$startDate = $_GET['start_date'] ?? date('Y-m-01');
$endDate = $_GET['end_date'] ?? date('Y-m-d');

// Ensure $data is passed from controller and is an array
$data = isset($data) && is_array($data) ? $data : [];

// For summary type, ensure data has all required keys
if ($type == 'summary') {
    $data = array_merge([
        'land' => [],
        'buildings' => [],
        'rented' => [],
        'movable' => [],
        'ict' => []
    ], $data);
}

// Check if we have data to display
$hasData = !empty($data);

// For summary type, check if any category has data
if ($type == 'summary') {
    $hasData = !empty($data['land']) || !empty($data['buildings']) || !empty($data['rented']) || 
               !empty($data['movable']) || !empty($data['ict']);
} elseif ($type == 'projects') {
    $hasData = !empty($data);
}
?>

<div class="container-fluid">
    <!-- Page Header -->
    <div class="page-header">
        <div class="header-content">
            <h1>
                <i class="fas fa-boxes"></i>
                Asset Reports
            </h1>
            <p>Generate comprehensive asset reports</p>
        </div>
        <div class="header-actions">
            <button class="btn btn-success" onclick="saveReport()" <?php echo !$hasData ? 'disabled' : ''; ?>>
                <i class="fas fa-bookmark"></i> Save Report
            </button>
            <div class="btn-group">
                <button class="btn btn-outline" onclick="exportReport('pdf')" <?php echo !$hasData ? 'disabled' : ''; ?>>
                    <i class="fas fa-file-pdf"></i> PDF
                </button>
                <button class="btn btn-outline" onclick="exportReport('csv')" <?php echo !$hasData ? 'disabled' : ''; ?>>
                    <i class="fas fa-file-csv"></i> CSV
                </button>
                <button class="btn btn-outline" onclick="exportReport('excel')" <?php echo !$hasData ? 'disabled' : ''; ?>>
                    <i class="fas fa-file-excel"></i> Excel
                </button>
            </div>
            <a href="<?php echo BASE_URL; ?>/reports" class="btn btn-secondary">
                <i class="fas fa-arrow-left"></i> Back
            </a>
        </div>
    </div>

    <!-- Report Controls -->
    <div class="report-controls">
        <form method="GET" action="<?php echo BASE_URL; ?>/reports/assets" id="reportForm">
            <div class="controls-grid">
                <div class="control-group">
                    <label>Report Type</label>
                    <select name="type" id="reportType" onchange="this.form.submit()">
                        <option value="summary" <?php echo $type == 'summary' ? 'selected' : ''; ?>>Summary Report</option>
                        <option value="land" <?php echo $type == 'land' ? 'selected' : ''; ?>>Land Assets</option>
                        <option value="buildings" <?php echo $type == 'buildings' ? 'selected' : ''; ?>>Building Assets</option>
                        <option value="rented" <?php echo $type == 'rented' ? 'selected' : ''; ?>>Rented Properties</option>
                        <option value="projects" <?php echo $type == 'projects' ? 'selected' : ''; ?>>Ongoing Projects</option>
                        <option value="movable" <?php echo $type == 'movable' ? 'selected' : ''; ?>>Movable Assets</option>
                        <option value="ict" <?php echo $type == 'ict' ? 'selected' : ''; ?>>ICT Assets</option>
                    </select>
                </div>
                <div class="control-group">
                    <label>Start Date</label>
                    <input type="date" name="start_date" value="<?php echo $startDate; ?>" onchange="this.form.submit()">
                </div>
                <div class="control-group">
                    <label>End Date</label>
                    <input type="date" name="end_date" value="<?php echo $endDate; ?>" onchange="this.form.submit()">
                </div>
            </div>
        </form>
    </div>

    <!-- Report Content -->
    <div class="report-content" id="reportContent">
        <?php if (!$hasData): ?>
            <div class="empty-state">
                <i class="fas fa-boxes"></i>
                <p>No asset data found for the selected period.</p>
            </div>
        <?php elseif ($type == 'summary'): ?>
            <!-- Summary Report -->
            <div class="report-section">
                <h2>Asset Summary Report</h2>
                <p class="report-period">Period: <?php echo date('d/m/Y', strtotime($startDate)); ?> - <?php echo date('d/m/Y', strtotime($endDate)); ?></p>
                
                <!-- Summary Cards -->
                <div class="summary-cards">
                    <?php
                    $landCount = count($data['land'] ?? []);
                    $buildingCount = count($data['buildings'] ?? []);
                    $rentedCount = count($data['rented'] ?? []);
                    $movableCount = count($data['movable'] ?? []);
                    $ictCount = count($data['ict'] ?? []);
                    $totalAssets = $landCount + $buildingCount + $rentedCount + $movableCount + $ictCount;
                    ?>
                    
                    <div class="summary-card" style="background: linear-gradient(135deg, #0D47A1 0%, #1976D2 100%);">
                        <div class="card-title">Total Assets</div>
                        <div class="card-value"><?php echo number_format($totalAssets); ?></div>
                    </div>
                    <div class="summary-card" style="background: linear-gradient(135deg, #2E7D32 0%, #4CAF50 100%);">
                        <div class="card-title">Land Assets</div>
                        <div class="card-value"><?php echo number_format($landCount); ?></div>
                    </div>
                    <div class="summary-card" style="background: linear-gradient(135deg, #B71C1C 0%, #D32F2F 100%);">
                        <div class="card-title">Buildings</div>
                        <div class="card-value"><?php echo number_format($buildingCount); ?></div>
                    </div>
                    <div class="summary-card" style="background: linear-gradient(135deg, #B26A00 0%, #FF8F00 100%);">
                        <div class="card-title">Rented</div>
                        <div class="card-value"><?php echo number_format($rentedCount); ?></div>
                    </div>
                    <div class="summary-card" style="background: linear-gradient(135deg, #6A1B9A 0%, #8E24AA 100%);">
                        <div class="card-title">Movable</div>
                        <div class="card-value"><?php echo number_format($movableCount); ?></div>
                    </div>
                    <div class="summary-card" style="background: linear-gradient(135deg, #00796B 0%, #009688 100%);">
                        <div class="card-title">ICT</div>
                        <div class="card-value"><?php echo number_format($ictCount); ?></div>
                    </div>
                </div>

                <?php if (!empty($data['land'])): ?>
                <h3>Land Assets</h3>
                <div class="table-responsive">
                    <table class="report-table">
                        <thead>
                            <tr>
                                <th>Asset Code</th>
                                <th>Title Holder</th>
                                <th>Location</th>
                                <th>Size</th>
                                <th>Status</th>
                                <th>Date Acquired</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($data['land'] as $asset): ?>
                            <?php 
                            $status = $asset['status'] ?? 'Unknown';
                            $statusClass = $status == 'Active' ? 'badge-success' : 'badge-warning';
                            ?>
                            <tr>
                                <td><span class="asset-code"><?php echo isset($asset['asset_code']) ? htmlspecialchars($asset['asset_code']) : '-'; ?></span></td>
                                <td><?php echo isset($asset['title_holder']) ? htmlspecialchars($asset['title_holder']) : '-'; ?></td>
                                <td><?php echo (isset($asset['state_name']) ? htmlspecialchars($asset['state_name']) : '') . ', ' . (isset($asset['lga_name']) ? htmlspecialchars($asset['lga_name']) : ''); ?></td>
                                <td><?php echo isset($asset['size']) ? number_format($asset['size'], 2) . ' ' . ($asset['size_unit'] ?? '') : '-'; ?></td>
                                <td>
                                    <span class="badge <?php echo $statusClass; ?>">
                                        <?php echo htmlspecialchars($status); ?>
                                    </span>
                                </td>
                                <td><?php echo isset($asset['date_acquired']) ? date('d/m/Y', strtotime($asset['date_acquired'])) : '-'; ?></td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
                <?php endif; ?>

                <?php if (!empty($data['buildings'])): ?>
                <h3>Building Assets</h3>
                <div class="table-responsive">
                    <table class="report-table">
                        <thead>
                            <tr>
                                <th>Asset Code</th>
                                <th>Building Name</th>
                                <th>Location</th>
                                <th>Floors</th>
                                <th>Condition</th>
                                <th>Contract Sum</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($data['buildings'] as $asset): ?>
                            <?php 
                            $condition = $asset['condition_status'] ?? 'Unknown';
                            $conditionClass = $condition == 'Good' ? 'badge-success' : 'badge-warning';
                            ?>
                            <tr>
                                <td><span class="asset-code"><?php echo isset($asset['asset_code']) ? htmlspecialchars($asset['asset_code']) : '-'; ?></span></td>
                                <td><?php echo isset($asset['building_name']) ? htmlspecialchars($asset['building_name']) : '-'; ?></td>
                                <td><?php echo (isset($asset['state_name']) ? htmlspecialchars($asset['state_name']) : '') . ', ' . (isset($asset['lga_name']) ? htmlspecialchars($asset['lga_name']) : ''); ?></td>
                                <td class="text-center"><?php echo $asset['floor_count'] ?? '-'; ?></td>
                                <td>
                                    <span class="badge <?php echo $conditionClass; ?>">
                                        <?php echo htmlspecialchars($condition); ?>
                                    </span>
                                </td>
                                <td class="text-right"><?php echo isset($asset['contract_sum']) ? '₦' . number_format($asset['contract_sum'], 2) : '-'; ?></td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
                <?php endif; ?>

                <?php if (!empty($data['rented'])): ?>
                <h3>Rented Properties</h3>
                <div class="table-responsive">
                    <table class="report-table">
                        <thead>
                            <tr>
                                <th>Asset Code</th>
                                <th>Property Name</th>
                                <th>Location</th>
                                <th>Monthly Rent</th>
                                <th>Landlord</th>
                                <th>Lease Expiry</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($data['rented'] as $asset): ?>
                            <?php 
                            $status = $asset['status'] ?? 'Unknown';
                            $statusClass = $status == 'Active' ? 'badge-success' : 'badge-warning';
                            ?>
                            <tr>
                                <td><span class="asset-code"><?php echo isset($asset['asset_code']) ? htmlspecialchars($asset['asset_code']) : '-'; ?></span></td>
                                <td><?php echo isset($asset['property_name']) ? htmlspecialchars($asset['property_name']) : '-'; ?></td>
                                <td><?php echo (isset($asset['state_name']) ? htmlspecialchars($asset['state_name']) : '') . ', ' . (isset($asset['lga_name']) ? htmlspecialchars($asset['lga_name']) : ''); ?></td>
                                <td class="text-right"><?php echo isset($asset['monthly_rent']) ? '₦' . number_format($asset['monthly_rent'], 2) : '-'; ?></td>
                                <td><?php echo isset($asset['landlord_name']) ? htmlspecialchars($asset['landlord_name']) : '-'; ?></td>
                                <td><?php echo isset($asset['lease_expiry']) ? date('d/m/Y', strtotime($asset['lease_expiry'])) : '-'; ?></td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
                <?php endif; ?>

                <?php if (!empty($data['movable'])): ?>
                <h3>Movable Assets</h3>
                <div class="table-responsive">
                    <table class="report-table">
                        <thead>
                            <tr>
                                <th>Asset Code</th>
                                <th>Asset Name</th>
                                <th>Category</th>
                                <th>Condition</th>
                                <th>Location</th>
                                <th>Value</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($data['movable'] as $asset): ?>
                            <?php 
                            $condition = $asset['condition_status'] ?? 'Unknown';
                            $conditionClass = $condition == 'Good' ? 'badge-success' : 'badge-warning';
                            ?>
                            <tr>
                                <td><span class="asset-code"><?php echo isset($asset['asset_code']) ? htmlspecialchars($asset['asset_code']) : '-'; ?></span></td>
                                <td><?php echo isset($asset['asset_name']) ? htmlspecialchars($asset['asset_name']) : '-'; ?></td>
                                <td><?php echo $asset['category'] ?? '-'; ?></td>
                                <td>
                                    <span class="badge <?php echo $conditionClass; ?>">
                                        <?php echo htmlspecialchars($condition); ?>
                                    </span>
                                </td>
                                <td><?php echo isset($asset['location']) ? htmlspecialchars($asset['location']) : '-'; ?></td>
                                <td class="text-right"><?php echo isset($asset['current_value']) ? '₦' . number_format($asset['current_value'], 2) : '-'; ?></td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
                <?php endif; ?>

                <?php if (!empty($data['ict'])): ?>
                <h3>ICT Assets</h3>
                <div class="table-responsive">
                    <table class="report-table">
                        <thead>
                            <tr>
                                <th>Asset Code</th>
                                <th>Equipment Name</th>
                                <th>Type</th>
                                <th>Specifications</th>
                                <th>Condition</th>
                                <th>Assigned To</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($data['ict'] as $asset): ?>
                            <?php 
                            $condition = $asset['condition_status'] ?? 'Unknown';
                            $conditionClass = $condition == 'Good' ? 'badge-success' : 'badge-warning';
                            ?>
                            <tr>
                                <td><span class="asset-code"><?php echo isset($asset['asset_code']) ? htmlspecialchars($asset['asset_code']) : '-'; ?></span></td>
                                <td><?php echo isset($asset['equipment_name']) ? htmlspecialchars($asset['equipment_name']) : '-'; ?></td>
                                <td><?php echo $asset['equipment_type'] ?? '-'; ?></td>
                                <td><?php echo isset($asset['specifications']) ? htmlspecialchars($asset['specifications']) : '-'; ?></td>
                                <td>
                                    <span class="badge <?php echo $conditionClass; ?>">
                                        <?php echo htmlspecialchars($condition); ?>
                                    </span>
                                </td>
                                <td><?php echo isset($asset['assigned_to_name']) ? htmlspecialchars($asset['assigned_to_name']) : '-'; ?></td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
                <?php endif; ?>
            </div>

        <?php elseif ($type == 'land'): ?>
            <!-- Detailed Land Assets Report -->
            <div class="report-section">
                <h2>Land Assets Detailed Report</h2>
                <p class="report-period">Period: <?php echo date('d/m/Y', strtotime($startDate)); ?> - <?php echo date('d/m/Y', strtotime($endDate)); ?></p>
                
                <?php if (!empty($data)): ?>
                <div class="table-responsive">
                    <table class="report-table">
                        <thead>
                            <tr>
                                <th>Asset Code</th>
                                <th>Ownership</th>
                                <th>Title Holder</th>
                                <th>Address</th>
                                <th>State/LGA</th>
                                <th>Size</th>
                                <th>Survey Plan</th>
                                <th>C of O</th>
                                <th>Purpose</th>
                                <th>Date Acquired</th>
                                <th>Status</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($data as $asset): ?>
                            <?php 
                            $status = $asset['status'] ?? 'Unknown';
                            $statusClass = $status == 'Active' ? 'badge-success' : 'badge-warning';
                            ?>
                            <tr>
                                <td><span class="asset-code"><?php echo isset($asset['asset_code']) ? htmlspecialchars($asset['asset_code']) : '-'; ?></span></td>
                                <td><?php echo $asset['ownership_type'] ?? '-'; ?></td>
                                <td><?php echo isset($asset['title_holder']) ? htmlspecialchars($asset['title_holder']) : '-'; ?></td>
                                <td><?php echo isset($asset['address']) ? htmlspecialchars($asset['address']) : '-'; ?></td>
                                <td><?php echo (isset($asset['state_name']) ? htmlspecialchars($asset['state_name']) : '') . '/' . (isset($asset['lga_name']) ? htmlspecialchars($asset['lga_name']) : ''); ?></td>
                                <td><?php echo isset($asset['size']) ? number_format($asset['size'], 2) . ' ' . ($asset['size_unit'] ?? '') : '-'; ?></td>
                                <td><?php echo $asset['survey_plan_no'] ?? '-'; ?></td>
                                <td><?php echo $asset['certificate_of_occupancy_no'] ?? '-'; ?></td>
                                <td><?php echo $asset['purpose_use'] ?? '-'; ?></td>
                                <td><?php echo isset($asset['date_acquired']) ? date('d/m/Y', strtotime($asset['date_acquired'])) : '-'; ?></td>
                                <td>
                                    <span class="badge <?php echo $statusClass; ?>">
                                        <?php echo htmlspecialchars($status); ?>
                                    </span>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
                <?php else: ?>
                <div class="empty-state">
                    <i class="fas fa-map-marked-alt"></i>
                    <p>No land assets found for the selected period.</p>
                </div>
                <?php endif; ?>
            </div>

        <?php elseif ($type == 'buildings'): ?>
            <!-- Detailed Building Assets Report -->
            <div class="report-section">
                <h2>Building Assets Detailed Report</h2>
                <p class="report-period">Period: <?php echo date('d/m/Y', strtotime($startDate)); ?> - <?php echo date('d/m/Y', strtotime($endDate)); ?></p>
                
                <?php if (!empty($data)): ?>
                <div class="table-responsive">
                    <table class="report-table">
                        <thead>
                            <tr>
                                <th>Asset Code</th>
                                <th>Building Name</th>
                                <th>Type</th>
                                <th>Address</th>
                                <th>Location</th>
                                <th>Floors</th>
                                <th>Area</th>
                                <th>Contractor</th>
                                <th>Contract Sum</th>
                                <th>Completion Date</th>
                                <th>Condition</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($data as $asset): ?>
                            <?php 
                            $condition = $asset['condition_status'] ?? 'Unknown';
                            $conditionClass = $condition == 'Good' ? 'badge-success' : 'badge-warning';
                            ?>
                            <tr>
                                <td><span class="asset-code"><?php echo isset($asset['asset_code']) ? htmlspecialchars($asset['asset_code']) : '-'; ?></span></td>
                                <td><?php echo isset($asset['building_name']) ? htmlspecialchars($asset['building_name']) : '-'; ?></td>
                                <td><?php echo $asset['building_type'] ?? '-'; ?></td>
                                <td><?php echo isset($asset['address']) ? htmlspecialchars($asset['address']) : '-'; ?></td>
                                <td><?php echo (isset($asset['state_name']) ? htmlspecialchars($asset['state_name']) : '') . '/' . (isset($asset['lga_name']) ? htmlspecialchars($asset['lga_name']) : ''); ?></td>
                                <td class="text-center"><?php echo $asset['floor_count'] ?? '-'; ?></td>
                                <td><?php echo isset($asset['total_area']) ? number_format($asset['total_area'], 2) . ' m²' : '-'; ?></td>
                                <td><?php echo $asset['construction_contractor'] ?? '-'; ?></td>
                                <td class="text-right"><?php echo isset($asset['contract_sum']) ? '₦' . number_format($asset['contract_sum'], 2) : '-'; ?></td>
                                <td><?php echo isset($asset['completion_date']) ? date('d/m/Y', strtotime($asset['completion_date'])) : '-'; ?></td>
                                <td>
                                    <span class="badge <?php echo $conditionClass; ?>">
                                        <?php echo htmlspecialchars($condition); ?>
                                    </span>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
                <?php else: ?>
                <div class="empty-state">
                    <i class="fas fa-building"></i>
                    <p>No building assets found for the selected period.</p>
                </div>
                <?php endif; ?>
            </div>

        <?php elseif ($type == 'rented'): ?>
            <!-- Detailed Rented Properties Report -->
            <div class="report-section">
                <h2>Rented Properties Detailed Report</h2>
                <p class="report-period">Period: <?php echo date('d/m/Y', strtotime($startDate)); ?> - <?php echo date('d/m/Y', strtotime($endDate)); ?></p>
                
                <?php if (!empty($data)): ?>
                <div class="table-responsive">
                    <table class="report-table">
                        <thead>
                            <tr>
                                <th>Asset Code</th>
                                <th>Property Name</th>
                                <th>Address</th>
                                <th>Location</th>
                                <th>Monthly Rent</th>
                                <th>Landlord</th>
                                <th>Landlord Phone</th>
                                <th>Lease Start</th>
                                <th>Lease Expiry</th>
                                <th>Status</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($data as $asset): ?>
                            <?php 
                            $status = $asset['status'] ?? 'Unknown';
                            $statusClass = $status == 'Active' ? 'badge-success' : 'badge-warning';
                            ?>
                            <tr>
                                <td><span class="asset-code"><?php echo isset($asset['asset_code']) ? htmlspecialchars($asset['asset_code']) : '-'; ?></span></td>
                                <td><?php echo isset($asset['property_name']) ? htmlspecialchars($asset['property_name']) : '-'; ?></td>
                                <td><?php echo isset($asset['address']) ? htmlspecialchars($asset['address']) : '-'; ?></td>
                                <td><?php echo (isset($asset['state_name']) ? htmlspecialchars($asset['state_name']) : '') . '/' . (isset($asset['lga_name']) ? htmlspecialchars($asset['lga_name']) : ''); ?></td>
                                <td class="text-right"><?php echo isset($asset['monthly_rent']) ? '₦' . number_format($asset['monthly_rent'], 2) : '-'; ?></td>
                                <td><?php echo isset($asset['landlord_name']) ? htmlspecialchars($asset['landlord_name']) : '-'; ?></td>
                                <td><?php echo $asset['landlord_phone'] ?? '-'; ?></td>
                                <td><?php echo isset($asset['lease_start']) ? date('d/m/Y', strtotime($asset['lease_start'])) : '-'; ?></td>
                                <td><?php echo isset($asset['lease_expiry']) ? date('d/m/Y', strtotime($asset['lease_expiry'])) : '-'; ?></td>
                                <td>
                                    <span class="badge <?php echo $statusClass; ?>">
                                        <?php echo htmlspecialchars($status); ?>
                                    </span>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
                <?php else: ?>
                <div class="empty-state">
                    <i class="fas fa-home"></i>
                    <p>No rented properties found for the selected period.</p>
                </div>
                <?php endif; ?>
            </div>

        <?php elseif ($type == 'movable'): ?>
            <!-- Detailed Movable Assets Report -->
            <div class="report-section">
                <h2>Movable Assets Detailed Report</h2>
                <p class="report-period">Period: <?php echo date('d/m/Y', strtotime($startDate)); ?> - <?php echo date('d/m/Y', strtotime($endDate)); ?></p>
                
                <?php if (!empty($data)): ?>
                <div class="table-responsive">
                    <table class="report-table">
                        <thead>
                            <tr>
                                <th>Asset Code</th>
                                <th>Asset Name</th>
                                <th>Category</th>
                                <th>Manufacturer</th>
                                <th>Model</th>
                                <th>Serial No.</th>
                                <th>Condition</th>
                                <th>Location</th>
                                <th>Assigned To</th>
                                <th>Value</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($data as $asset): ?>
                            <?php 
                            $condition = $asset['condition_status'] ?? 'Unknown';
                            $conditionClass = $condition == 'Good' ? 'badge-success' : 'badge-warning';
                            ?>
                            <tr>
                                <td><span class="asset-code"><?php echo isset($asset['asset_code']) ? htmlspecialchars($asset['asset_code']) : '-'; ?></span></td>
                                <td><?php echo isset($asset['asset_name']) ? htmlspecialchars($asset['asset_name']) : '-'; ?></td>
                                <td><?php echo $asset['category'] ?? '-'; ?></td>
                                <td><?php echo $asset['manufacturer'] ?? '-'; ?></td>
                                <td><?php echo $asset['model'] ?? '-'; ?></td>
                                <td><code><?php echo $asset['serial_number'] ?? '-'; ?></code></td>
                                <td>
                                    <span class="badge <?php echo $conditionClass; ?>">
                                        <?php echo htmlspecialchars($condition); ?>
                                    </span>
                                </td>
                                <td><?php echo isset($asset['location']) ? htmlspecialchars($asset['location']) : '-'; ?></td>
                                <td><?php echo isset($asset['assigned_to_name']) ? htmlspecialchars($asset['assigned_to_name']) : '-'; ?></td>
                                <td class="text-right"><?php echo isset($asset['current_value']) ? '₦' . number_format($asset['current_value'], 2) : '-'; ?></td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
                <?php else: ?>
                <div class="empty-state">
                    <i class="fas fa-truck"></i>
                    <p>No movable assets found for the selected period.</p>
                </div>
                <?php endif; ?>
            </div>

        <?php elseif ($type == 'ict'): ?>
            <!-- Detailed ICT Assets Report -->
            <div class="report-section">
                <h2>ICT Assets Detailed Report</h2>
                <p class="report-period">Period: <?php echo date('d/m/Y', strtotime($startDate)); ?> - <?php echo date('d/m/Y', strtotime($endDate)); ?></p>
                
                <?php if (!empty($data)): ?>
                <div class="table-responsive">
                    <table class="report-table">
                        <thead>
                            <tr>
                                <th>Asset Code</th>
                                <th>Equipment Name</th>
                                <th>Type</th>
                                <th>Manufacturer</th>
                                <th>Model</th>
                                <th>Serial No.</th>
                                <th>Specifications</th>
                                <th>Condition</th>
                                <th>Assigned To</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($data as $asset): ?>
                            <?php 
                            $condition = $asset['condition_status'] ?? 'Unknown';
                            $conditionClass = $condition == 'Good' ? 'badge-success' : 'badge-warning';
                            ?>
                            <tr>
                                <td><span class="asset-code"><?php echo isset($asset['asset_code']) ? htmlspecialchars($asset['asset_code']) : '-'; ?></span></td>
                                <td><?php echo isset($asset['equipment_name']) ? htmlspecialchars($asset['equipment_name']) : '-'; ?></td>
                                <td><?php echo $asset['equipment_type'] ?? '-'; ?></td>
                                <td><?php echo $asset['manufacturer'] ?? '-'; ?></td>
                                <td><?php echo $asset['model'] ?? '-'; ?></td>
                                <td><code><?php echo $asset['serial_number'] ?? '-'; ?></code></td>
                                <td><?php echo isset($asset['specifications']) ? htmlspecialchars($asset['specifications']) : '-'; ?></td>
                                <td>
                                    <span class="badge <?php echo $conditionClass; ?>">
                                        <?php echo htmlspecialchars($condition); ?>
                                    </span>
                                </td>
                                <td><?php echo isset($asset['assigned_to_name']) ? htmlspecialchars($asset['assigned_to_name']) : '-'; ?></td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
                <?php else: ?>
                <div class="empty-state">
                    <i class="fas fa-microchip"></i>
                    <p>No ICT assets found for the selected period.</p>
                </div>
                <?php endif; ?>
            </div>

        <?php elseif ($type == 'projects'): ?>
            <!-- Ongoing Projects Report -->
            <div class="report-section">
                <h2>Ongoing Projects Report</h2>
                <p class="report-period">Period: <?php echo date('d/m/Y', strtotime($startDate)); ?> - <?php echo date('d/m/Y', strtotime($endDate)); ?></p>
                
                <?php if (!empty($data)): ?>
                <div class="table-responsive">
                    <table class="report-table">
                        <thead>
                            <tr>
                                <th>Project Name</th>
                                <th>Location</th>
                                <th>Contractor</th>
                                <th>Contract Sum</th>
                                <th>Start Date</th>
                                <th>End Date</th>
                                <th>% Complete</th>
                                <th>Status</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($data as $project): ?>
                            <?php 
                            $status = $project['status'] ?? 'Unknown';
                            $statusClass = $status == 'Active' ? 'badge-success' : 'badge-warning';
                            $completion = $project['completion_percentage'] ?? 0;
                            ?>
                            <tr>
                                <td><?php echo isset($project['project_name']) ? htmlspecialchars($project['project_name']) : '-'; ?></td>
                                <td><?php echo isset($project['location']) ? htmlspecialchars($project['location']) : '-'; ?></td>
                                <td><?php echo $project['contractor'] ?? '-'; ?></td>
                                <td class="text-right"><?php echo isset($project['contract_sum']) ? '₦' . number_format($project['contract_sum'], 2) : '-'; ?></td>
                                <td><?php echo isset($project['start_date']) ? date('d/m/Y', strtotime($project['start_date'])) : '-'; ?></td>
                                <td><?php echo isset($project['end_date']) ? date('d/m/Y', strtotime($project['end_date'])) : '-'; ?></td>
                                <td class="text-center">
                                    <div class="progress-container">
                                        <div class="progress-bar" style="width: <?php echo $completion; ?>%;"></div>
                                        <span><?php echo $completion; ?>%</span>
                                    </div>
                                </td>
                                <td>
                                    <span class="badge <?php echo $statusClass; ?>">
                                        <?php echo htmlspecialchars($status); ?>
                                    </span>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
                <?php else: ?>
                <div class="empty-state">
                    <i class="fas fa-project-diagram"></i>
                    <p>No ongoing projects found for the selected period.</p>
                </div>
                <?php endif; ?>
            </div>
        <?php endif; ?>
    </div>
</div>

<style>
:root {
    --primary: #27ae60;
    --primary-dark: #219a52;
    --danger: #e53e3e;
    --warning: #f39c12;
    --info: #3498db;
    --text-dark: #2c3e50;
    --text-light: #7f8c8d;
    --border: #e2e8f0;
    --bg-light: #f8f9fa;
    --shadow-sm: 0 2px 4px rgba(0,0,0,0.02);
    --shadow-md: 0 4px 6px rgba(0,0,0,0.05);
    --shadow-lg: 0 10px 15px rgba(0,0,0,0.05);
}

.report-controls {
    background: white;
    border-radius: 8px;
    padding: 20px 25px;
    margin-bottom: 25px;
    box-shadow: var(--shadow-md);
    border: 1px solid var(--border);
}

.controls-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 20px;
}

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

.control-group label {
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-light);
    margin-bottom: 5px;
    font-weight: 600;
}

.control-group select,
.control-group input {
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 0.9rem;
    transition: all 0.2s;
    background: white;
}

.control-group select:focus,
.control-group input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(39, 174, 96, 0.1);
}

.report-content {
    background: white;
    border-radius: 8px;
    padding: 30px;
    box-shadow: var(--shadow-md);
    border: 1px solid var(--border);
}

.report-section h2 {
    margin: 0 0 8px 0;
    color: var(--text-dark);
    font-size: 1.5rem;
    font-weight: 500;
    letter-spacing: -0.5px;
}

.report-period {
    color: var(--text-light);
    margin-bottom: 25px;
    font-size: 0.85rem;
    padding-bottom: 15px;
    border-bottom: 1px solid var(--border);
}

.report-section h3 {
    margin: 30px 0 15px 0;
    color: var(--text-dark);
    font-size: 1.2rem;
    font-weight: 500;
    border-bottom: 2px solid var(--border);
    padding-bottom: 8px;
}

/* Summary Cards */
.summary-cards {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    gap: 15px;
    margin-bottom: 30px;
}

.summary-card {
    color: white;
    padding: 20px;
    border-radius: 8px;
    text-align: center;
    box-shadow: var(--shadow-lg);
    transition: all 0.3s ease;
    position: relative;
    overflow: hidden;
}

.summary-card::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(45deg, rgba(255,255,255,0.1) 0%, rgba(255,255,255,0) 100%);
    pointer-events: none;
}

.summary-card:hover {
    transform: translateY(-3px);
    box-shadow: 0 15px 30px rgba(0,0,0,0.1);
}

.summary-card .card-title {
    font-size: 0.85rem;
    opacity: 0.9;
    margin-bottom: 8px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: white;
}

.summary-card .card-value {
    font-size: 2rem;
    font-weight: 600;
    line-height: 1.2;
    color: white;
}

/* Table Styles */
.table-responsive {
    overflow-x: auto;
    margin-top: 15px;
    border-radius: 6px;
    border: 1px solid var(--border);
}

.report-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.85rem;
}

.report-table th {
    background: var(--bg-light);
    padding: 12px 8px;
    text-align: left;
    font-weight: 600;
    color: var(--text-light);
    border-bottom: 2px solid var(--border);
    text-transform: uppercase;
    font-size: 0.7rem;
    letter-spacing: 0.5px;
}

.report-table td {
    padding: 10px 8px;
    border-bottom: 1px solid var(--border);
    vertical-align: middle;
}

.report-table tbody tr:hover {
    background: var(--bg-light);
}

/* Badges */
.badge {
    display: inline-block;
    padding: 4px 8px;
    border-radius: 4px;
    font-size: 0.7rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.3px;
}

.badge-success {
    background: #E8F5E9;
    color: #2E7D32;
}

.badge-warning {
    background: #FFF3E0;
    color: #B26A00;
}

.badge-info {
    background: #E3F2FD;
    color: #0D47A1;
}

/* Asset Code */
.asset-code {
    font-family: 'SF Mono', 'Fira Code', monospace;
    background: var(--bg-light);
    padding: 3px 6px;
    border-radius: 4px;
    font-size: 0.8rem;
    color: var(--info);
}

code {
    font-family: 'SF Mono', 'Fira Code', monospace;
    background: var(--bg-light);
    padding: 2px 4px;
    border-radius: 3px;
    font-size: 0.8rem;
}

/* Progress Bar */
.progress-container {
    width: 100px;
    height: 6px;
    background: var(--bg-light);
    border-radius: 3px;
    position: relative;
    display: inline-block;
}

.progress-bar {
    height: 100%;
    background: var(--primary);
    border-radius: 3px;
    transition: width 0.3s ease;
}

.progress-container span {
    margin-left: 8px;
    font-size: 0.75rem;
    color: var(--text-light);
}

/* Empty State */
.empty-state {
    text-align: center;
    padding: 60px 20px;
    color: var(--text-light);
    background: var(--bg-light);
    border-radius: 8px;
    margin: 20px 0;
}

.empty-state i {
    font-size: 3rem;
    margin-bottom: 15px;
    color: #ccc;
}

.empty-state p {
    font-size: 1rem;
    margin-bottom: 20px;
}

/* Text utilities */
.text-center {
    text-align: center;
}

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

.btn-group {
    display: flex;
    gap: 5px;
}

.btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.btn:hover:not(:disabled) {
    transform: translateY(-1px);
    box-shadow: 0 4px 8px rgba(0,0,0,0.05);
}

/* Responsive */
@media (max-width: 768px) {
    .report-content {
        padding: 20px;
    }
    
    .controls-grid {
        grid-template-columns: 1fr;
    }
    
    .summary-cards {
        grid-template-columns: repeat(2, 1fr);
    }
    
    .btn-group {
        flex-direction: column;
        width: 100%;
    }
    
    .btn-group .btn {
        width: 100%;
    }
}

@media (max-width: 480px) {
    .summary-cards {
        grid-template-columns: 1fr;
    }
}

/* Print styles */
@media print {
    .header-actions,
    .sidebar,
    footer,
    .report-controls,
    .btn-group {
        display: none !important;
    }
    
    .main-content {
        margin-left: 0 !important;
        width: 100% !important;
        padding: 20px !important;
    }
    
    .report-content {
        box-shadow: none !important;
        border: none !important;
        padding: 0 !important;
    }
    
    .report-table th {
        background: var(--bg-light) !important;
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
    }
}
</style>

<script>
function exportReport(format) {
    const type = document.getElementById('reportType').value;
    const startDate = document.querySelector('input[name="start_date"]').value;
    const endDate = document.querySelector('input[name="end_date"]').value;
    
    window.location.href = '<?php echo BASE_URL; ?>/reports/assets?type=' + type + '&start_date=' + startDate + '&end_date=' + endDate + '&format=' + format;
}

function saveReport() {
    const reportName = prompt('Enter a name for this report:');
    if (reportName) {
        const type = document.getElementById('reportType').value;
        const startDate = document.querySelector('input[name="start_date"]').value;
        const endDate = document.querySelector('input[name="end_date"]').value;
        
        const form = document.createElement('form');
        form.method = 'POST';
        form.action = '<?php echo BASE_URL; ?>/reports/save';
        
        const csrf = document.createElement('input');
        csrf.type = 'hidden';
        csrf.name = 'csrf_token';
        
        <?php if (class_exists('Security') && method_exists('Security', 'csrfToken')): ?>
        csrf.value = '<?php echo Security::csrfToken(); ?>';
        <?php else: ?>
        csrf.value = '<?php echo isset($_SESSION['csrf_token']) ? $_SESSION['csrf_token'] : ''; ?>';
        <?php endif; ?>
        
        form.appendChild(csrf);
        
        const nameInput = document.createElement('input');
        nameInput.type = 'hidden';
        nameInput.name = 'report_name';
        nameInput.value = reportName;
        form.appendChild(nameInput);
        
        const typeInput = document.createElement('input');
        typeInput.type = 'hidden';
        typeInput.name = 'report_type';
        typeInput.value = 'assets';
        form.appendChild(typeInput);
        
        const paramsInput = document.createElement('input');
        paramsInput.type = 'hidden';
        paramsInput.name = 'parameters';
        paramsInput.value = JSON.stringify({type, start_date: startDate, end_date: endDate});
        form.appendChild(paramsInput);
        
        document.body.appendChild(form);
        form.submit();
    }
}
</script>

<?php require_once __DIR__ . '/../layouts/footer.php'; ?>