<?php
require_once 'app/helpers/auth.php';
require_once 'app/helpers/rbac.php';
requireLogin();

$canImport = isAdmin() || can('data.import');
$canExport = isAdmin() || can('data.export');

$page_js = 'data_tools.js';
include 'views/layouts/header.php';
include 'views/layouts/sidebar.php';
?>
<main class="main-content">
<?php include 'views/layouts/topbar.php'; ?>
<div class="container-fluid p-4">
    <div class="d-flex align-items-center gap-3 mb-4">
        <div>
            <span class="section-chip mb-2"><i class="bi bi-arrow-left-right"></i> Tools</span>
            <h2 class="mb-1">Data Import / Export</h2>
            <p class="text-muted mb-0">Import records from CSV or export data for offline analysis.</p>
        </div>
    </div>

    <div class="row g-4">
        <div class="col-lg-6">
            <div class="card shadow-sm border-0 h-100">
                <div class="card-header bg-white"><h5 class="mb-0 fw-semibold"><i class="bi bi-download me-2"></i>Export Data</h5></div>
                <div class="card-body">
                    <p class="text-muted small">Download data as CSV files for offline analysis in Excel or Google Sheets.</p>
                    <p class="text-muted small">Download data in CSV, Excel, or PDF format.</p>
                    <div class="list-group">
                        <a href="api/data_export.php?type=vehicles&format=csv" class="list-group-item list-group-item-action d-flex align-items-center gap-3">
                            <i class="bi bi-truck" style="font-size:20px;color:#1d4ed8;"></i>
                            <div><div class="fw-semibold">Vehicles</div><small class="text-muted">Fleet register</small></div>
                            <span class="ms-auto"><span class="badge bg-secondary">CSV</span></span>
                        </a>
                        <a href="api/data_export.php?type=vehicles&format=xlsx" class="list-group-item list-group-item-action d-flex align-items-center gap-3">
                            <i class="bi bi-truck" style="font-size:20px;color:#1d4ed8;"></i>
                            <div><div class="fw-semibold">Vehicles (Excel)</div><small class="text-muted">Fleet register</small></div>
                            <span class="ms-auto"><span class="badge bg-success">XLSX</span></span>
                        </a>
                        <a href="api/data_export.php?type=vehicles&format=pdf" class="list-group-item list-group-item-action d-flex align-items-center gap-3">
                            <i class="bi bi-truck" style="font-size:20px;color:#1d4ed8;"></i>
                            <div><div class="fw-semibold">Vehicles (PDF)</div><small class="text-muted">Fleet register</small></div>
                            <span class="ms-auto"><span class="badge bg-danger">PDF</span></span>
                        </a>
                        <a href="api/data_export.php?type=drivers&format=csv" class="list-group-item list-group-item-action d-flex align-items-center gap-3">
                            <i class="bi bi-person" style="font-size:20px;color:#16a34a;"></i>
                            <div><div class="fw-semibold">Drivers</div><small class="text-muted">Driver records and licenses</small></div>
                            <span class="ms-auto"><span class="badge bg-secondary">CSV</span></span>
                        </a>
                        <a href="api/data_export.php?type=drivers&format=xlsx" class="list-group-item list-group-item-action d-flex align-items-center gap-3">
                            <i class="bi bi-person" style="font-size:20px;color:#16a34a;"></i>
                            <div><div class="fw-semibold">Drivers (Excel)</div><small class="text-muted">Driver records and licenses</small></div>
                            <span class="ms-auto"><span class="badge bg-success">XLSX</span></span>
                        </a>
                        <a href="api/data_export.php?type=drivers&format=pdf" class="list-group-item list-group-item-action d-flex align-items-center gap-3">
                            <i class="bi bi-person" style="font-size:20px;color:#16a34a;"></i>
                            <div><div class="fw-semibold">Drivers (PDF)</div><small class="text-muted">Driver records and licenses</small></div>
                            <span class="ms-auto"><span class="badge bg-danger">PDF</span></span>
                        </a>
                        <a href="api/data_export.php?type=trips&format=csv" class="list-group-item list-group-item-action d-flex align-items-center gap-3">
                            <i class="bi bi-signpost-2" style="font-size:20px;color:#d97706;"></i>
                            <div><div class="fw-semibold">Trips</div><small class="text-muted">Journey history</small></div>
                            <span class="ms-auto"><span class="badge bg-secondary">CSV</span></span>
                        </a>
                        <a href="api/data_export.php?type=trips&format=xlsx" class="list-group-item list-group-item-action d-flex align-items-center gap-3">
                            <i class="bi bi-signpost-2" style="font-size:20px;color:#d97706;"></i>
                            <div><div class="fw-semibold">Trips (Excel)</div><small class="text-muted">Journey history</small></div>
                            <span class="ms-auto"><span class="badge bg-success">XLSX</span></span>
                        </a>
                        <a href="api/data_export.php?type=insurance&format=csv" class="list-group-item list-group-item-action d-flex align-items-center gap-3">
                            <i class="bi bi-shield-check" style="font-size:20px;color:#7c3aed;"></i>
                            <div><div class="fw-semibold">Insurance Policies</div><small class="text-muted">Policy register</small></div>
                            <span class="ms-auto"><span class="badge bg-secondary">CSV</span></span>
                        </a>
                        <a href="api/data_export.php?type=insurance&format=xlsx" class="list-group-item list-group-item-action d-flex align-items-center gap-3">
                            <i class="bi bi-shield-check" style="font-size:20px;color:#7c3aed;"></i>
                            <div><div class="fw-semibold">Insurance Policies (Excel)</div><small class="text-muted">Policy register</small></div>
                            <span class="ms-auto"><span class="badge bg-success">XLSX</span></span>
                        </a>
                        <a href="api/data_export.php?type=incidents&format=csv" class="list-group-item list-group-item-action d-flex align-items-center gap-3">
                            <i class="bi bi-exclamation-triangle" style="font-size:20px;color:#dc2626;"></i>
                            <div><div class="fw-semibold">Incidents</div><small class="text-muted">Accident reports</small></div>
                            <span class="ms-auto"><span class="badge bg-secondary">CSV</span></span>
                        </a>
                        <a href="api/data_export.php?type=incidents&format=xlsx" class="list-group-item list-group-item-action d-flex align-items-center gap-3">
                            <i class="bi bi-exclamation-triangle" style="font-size:20px;color:#dc2626;"></i>
                            <div><div class="fw-semibold">Incidents (Excel)</div><small class="text-muted">Accident reports</small></div>
                            <span class="ms-auto"><span class="badge bg-success">XLSX</span></span>
                        </a>
                        <a href="api/data_export.php?type=parts&format=csv" class="list-group-item list-group-item-action d-flex align-items-center gap-3">
                            <i class="bi bi-box-seam" style="font-size:20px;color:#0891b2;"></i>
                            <div><div class="fw-semibold">Parts Inventory</div><small class="text-muted">Spare parts stock</small></div>
                            <span class="ms-auto"><span class="badge bg-secondary">CSV</span></span>
                        </a>
                        <a href="api/data_export.php?type=parts&format=xlsx" class="list-group-item list-group-item-action d-flex align-items-center gap-3">
                            <i class="bi bi-box-seam" style="font-size:20px;color:#0891b2;"></i>
                            <div><div class="fw-semibold">Parts Inventory (Excel)</div><small class="text-muted">Spare parts stock</small></div>
                            <span class="ms-auto"><span class="badge bg-success">XLSX</span></span>
                        </a>
                        <a href="api/data_export.php?type=tyres&format=csv" class="list-group-item list-group-item-action d-flex align-items-center gap-3">
                            <i class="bi bi-circle" style="font-size:20px;color:#be185d;"></i>
                            <div><div class="fw-semibold">Tyres</div><small class="text-muted">Tyre inventory</small></div>
                            <span class="ms-auto"><span class="badge bg-secondary">CSV</span></span>
                        </a>
                        <a href="api/data_export.php?type=tyres&format=xlsx" class="list-group-item list-group-item-action d-flex align-items-center gap-3">
                            <i class="bi bi-circle" style="font-size:20px;color:#be185d;"></i>
                            <div><div class="fw-semibold">Tyres (Excel)</div><small class="text-muted">Tyre inventory</small></div>
                            <span class="ms-auto"><span class="badge bg-success">XLSX</span></span>
                        </a>
                        <a href="api/data_export.php?type=expenses&format=csv" class="list-group-item list-group-item-action d-flex align-items-center gap-3">
                            <i class="bi bi-cash-stack" style="font-size:20px;color:#65a30d;"></i>
                            <div><div class="fw-semibold">Vehicle Expenses</div><small class="text-muted">Expense records</small></div>
                            <span class="ms-auto"><span class="badge bg-secondary">CSV</span></span>
                        </a>
                        <a href="api/data_export.php?type=expenses&format=xlsx" class="list-group-item list-group-item-action d-flex align-items-center gap-3">
                            <i class="bi bi-cash-stack" style="font-size:20px;color:#65a30d;"></i>
                            <div><div class="fw-semibold">Vehicle Expenses (Excel)</div><small class="text-muted">Expense records</small></div>
                            <span class="ms-auto"><span class="badge bg-success">XLSX</span></span>
                        </a>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-lg-6">
            <div class="card shadow-sm border-0 h-100">
                <div class="card-header bg-white"><h5 class="mb-0 fw-semibold"><i class="bi bi-upload me-2"></i>Import Data</h5></div>
                <div class="card-body">
                    <?php if (!$canImport): ?>
                        <div class="alert alert-warning">You don't have permission to import data.</div>
                    <?php else: ?>
                    <p class="text-muted small">Upload a CSV file to import records. The first row must contain column headers.</p>

                    <div class="mb-3">
                        <label class="form-label fw-semibold">Import Type</label>
                        <select id="importType" class="form-select">
                            <option value="">Select type...</option>
                            <option value="vehicles">Vehicles</option>
                            <option value="drivers">Drivers</option>
                            <option value="trips">Trips</option>
                            <option value="parts">Parts</option>
                            <option value="tyres">Tyres</option>
                            <option value="expenses">Expenses</option>
                        </select>
                    </div>

                    <div class="mb-3">
                        <label class="form-label fw-semibold">CSV File</label>
                        <input type="file" id="importFile" class="form-control" accept=".csv">
                    </div>

                    <div id="importPreview" class="d-none mb-3">
                        <h6 class="fw-semibold">Preview</h6>
                        <div class="table-responsive"><table class="table table-sm table-bordered"><thead id="previewHead"></thead><tbody id="previewBody"></tbody></table></div>
                        <p class="small text-muted" id="previewCount"></p>
                    </div>

                    <div class="d-flex gap-2">
                        <button class="btn btn-outline-primary" id="btnPreviewImport"><i class="bi bi-eye me-2"></i>Preview</button>
                        <button class="btn btn-primary" id="btnRunImport" disabled><i class="bi bi-check-lg me-2"></i>Import</button>
                    </div>

                    <div id="importResult" class="mt-3 d-none"></div>

                    <hr class="my-4">
                    <h6 class="fw-semibold">Expected CSV Columns</h6>
                    <div id="columnGuide" class="small text-muted">
                        <p><strong>Vehicles:</strong> plate_number, vehicle_make, vehicle_model, vehicle_type, year, color, engine_number, chasis_number</p>
                        <p><strong>Drivers:</strong> name, phone, email, license_number, license_expiry, date_of_birth</p>
                        <p><strong>Trips:</strong> vehicle_id, driver_id, start_date, end_date, start_odometer, end_odometer, start_location, end_location, purpose, status</p>
                        <p><strong>Parts:</strong> name, part_number, category_id, quantity_in_stock, reorder_level, unit_cost, supplier_name, bin_location</p>
                        <p><strong>Tyres:</strong> brand, size, serial_number, position, purchase_date, purchase_cost, status</p>
                        <p><strong>Expenses:</strong> vehicle_id, category_id, expense_date, amount, description, driver_id</p>
                    </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</div>
</main>
<script>window.CSRF_TOKEN = <?= json_encode(generateCsrfToken(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) ?>;</script>
<?php include 'views/layouts/footer.php'; ?>
