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

$userName = $_SESSION['user_name'] ?? 'User';
$page_js = 'tracker_integrations.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 id="alertBox" class="alert d-none"></div>

    <div class="card shadow-sm border-0 mb-4">
        <div class="card-body p-4 p-lg-5">
            <div class="row g-4 align-items-center">
                <div class="col-lg-8">
                    <span class="badge bg-primary bg-opacity-10 text-primary mb-3 px-3 py-2">Tracker Integration</span>
                    <h2 class="mb-2">Tracking Software Integrations</h2>
                    <p class="mb-0 text-muted">Connect your fleet to GPS tracking platforms like Traccar, GPSWOX, Wialon, or any custom API.</p>
                </div>
                <div class="col-lg-4 text-lg-end">
                    <button type="button" class="btn btn-primary" id="btnAddIntegration"><i class="bi bi-plus-lg"></i> Add Integration</button>
                </div>
            </div>
        </div>
    </div>

    <div class="row g-4 mb-4">
        <div class="col-12 col-md-3">
            <div class="card kpi-card shadow-sm border-0 rounded-4 h-100">
                <div class="card-body py-3 px-3">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <div class="kpi-label">Total</div>
                            <div class="kpi-value" id="kpiTotal">0</div>
                        </div>
                        <div class="kpi-icon text-primary fs-3"><i class="bi bi-hdd-stack"></i></div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-12 col-md-3">
            <div class="card kpi-card shadow-sm border-0 rounded-4 h-100">
                <div class="card-body py-3 px-3">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <div class="kpi-label">Active</div>
                            <div class="kpi-value" id="kpiActive">0</div>
                        </div>
                        <div class="kpi-icon text-success fs-3"><i class="bi bi-check-circle"></i></div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-12 col-md-3">
            <div class="card kpi-card shadow-sm border-0 rounded-4 h-100">
                <div class="card-body py-3 px-3">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <div class="kpi-label">Inactive</div>
                            <div class="kpi-value" id="kpiInactive">0</div>
                        </div>
                        <div class="kpi-icon text-secondary fs-3"><i class="bi bi-x-circle"></i></div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-12 col-md-3">
            <div class="card kpi-card shadow-sm border-0 rounded-4 h-100">
                <div class="card-body py-3 px-3">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <div class="kpi-label">Error</div>
                            <div class="kpi-value" id="kpiError">0</div>
                        </div>
                        <div class="kpi-icon text-danger fs-3"><i class="bi bi-exclamation-triangle"></i></div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="card shadow-sm border-0">
        <div class="card-body">
            <div class="row mb-3">
                <div class="col-md-6">
                    <input type="text" id="searchBox" class="form-control" placeholder="Search by name, provider or URL...">
                </div>
                <div class="col-md-3">
                    <select id="statusFilter" class="form-select">
                        <option value="">All Status</option>
                        <option value="active">Active</option>
                        <option value="inactive">Inactive</option>
                        <option value="error">Error</option>
                    </select>
                </div>
            </div>
            <div class="table-responsive">
                <table class="table align-middle">
                    <thead class="table-light">
                        <tr>
                            <th>Name</th>
                            <th>Provider</th>
                            <th>Base URL</th>
                            <th>Devices</th>
                            <th>Status</th>
                            <th>Last Checked</th>
                            <th class="text-end">Actions</th>
                        </tr>
                    </thead>
                    <tbody id="integrationTableBody">
                        <tr><td colspan="7" class="text-center text-muted py-4">Loading...</td></tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<!-- Integration Modal -->
<div class="modal fade" id="integrationModal" tabindex="-1">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="integrationModalTitle">Add Integration</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <input type="hidden" id="integration_id" value="0">
                <div class="row g-3">
                    <div class="col-md-6">
                        <label class="form-label">Name <span class="text-danger">*</span></label>
                        <input type="text" id="integration_name" class="form-control" placeholder="e.g. Main Fleet Tracker">
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">Provider</label>
                        <select id="integration_provider" class="form-select">
                            <option value="generic">Generic REST API</option>
                            <option value="traccar">Traccar</option>
                            <option value="gpswox">GPSWOX</option>
                            <option value="wialon">Wialon</option>
                        </select>
                    </div>
                    <div class="col-md-8">
                        <label class="form-label">Base URL</label>
                        <input type="url" id="integration_base_url" class="form-control" placeholder="https://tracking.example.com">
                    </div>
                    <div class="col-md-4">
                        <label class="form-label">Auth Type</label>
                        <select id="integration_auth_type" class="form-select">
                            <option value="header">Header API Key</option>
                            <option value="bearer">Bearer Token</option>
                            <option value="basic">Basic Auth</option>
                            <option value="query">Query Parameter</option>
                        </select>
                    </div>
                    <div class="col-md-6">
                        <label class="form-label" id="labelApiKey">API Key / Token</label>
                        <input type="password" id="integration_api_key" class="form-control">
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">API Secret (if required)</label>
                        <input type="password" id="integration_api_secret" class="form-control">
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">Poll Interval (seconds)</label>
                        <input type="number" id="integration_poll_interval" class="form-control" value="300" min="30">
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">Status</label>
                        <select id="integration_status" class="form-select">
                            <option value="active">Active</option>
                            <option value="inactive">Inactive</option>
                        </select>
                    </div>
                    <div class="col-12">
                        <label class="form-label">Additional Config (JSON)</label>
                        <textarea id="integration_additional_config" class="form-control" rows="2" placeholder='{"endpoint_positions": "/api/positions", "endpoint_devices": "/api/devices"}'></textarea>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                <button type="button" class="btn btn-primary" id="btnSaveIntegration">Save</button>
            </div>
        </div>
    </div>
</div>

<!-- Device Mappings Modal -->
<div class="modal fade" id="deviceModal" tabindex="-1">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="deviceModalTitle">Device Mappings</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <input type="hidden" id="device_integration_id" value="0">
                <div id="deviceAlertBox" class="alert d-none"></div>
                <div class="d-flex gap-2 mb-3">
                    <button class="btn btn-outline-primary btn-sm" id="btnFetchDevices"><i class="bi bi-cloud-download"></i> Fetch from API</button>
                    <button class="btn btn-primary btn-sm" id="btnAddMapping"><i class="bi bi-plus-lg"></i> Add Manual</button>
                </div>
                <div class="table-responsive">
                    <table class="table align-middle">
                        <thead class="table-light">
                            <tr>
                                <th>Tracker Device</th>
                                <th>Device Name</th>
                                <th>Mapped Vehicle</th>
                                <th class="text-end">Actions</th>
                            </tr>
                        </thead>
                        <tbody id="deviceTableBody">
                            <tr><td colspan="4" class="text-center text-muted py-3">No device mappings</td></tr>
                        </tbody>
                    </table>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

<!-- Mapping Edit Modal -->
<div class="modal fade" id="mappingModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="mappingModalTitle">Add Device Mapping</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <input type="hidden" id="mapping_id" value="0">
                <input type="hidden" id="mapping_integration_id" value="0">
                <div class="row g-3">
                    <div class="col-12">
                        <label class="form-label">Tracker Device ID <span class="text-danger">*</span></label>
                        <input type="text" id="mapping_device_id" class="form-control" placeholder="Device ID from tracking platform">
                    </div>
                    <div class="col-12">
                        <label class="form-label">Device Name</label>
                        <input type="text" id="mapping_device_name" class="form-control" placeholder="e.g. Toyota Camry GPS">
                    </div>
                    <div class="col-12">
                        <label class="form-label">Mapped Vehicle</label>
                        <select id="mapping_vehicle_id" class="form-select">
                            <option value="">-- Not mapped --</option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                <button type="button" class="btn btn-primary" id="btnSaveMapping">Save Mapping</button>
            </div>
        </div>
    </div>
</div>

</main>

<?php include 'views/layouts/footer.php'; ?>
