

<?php $__env->startSection('title', 'Sync Center'); ?>
<?php $__env->startSection('page-title', 'Sync Center'); ?>
<?php $__env->startSection('page-subtitle', 'Move data between offline and online installations without Redis or VPS services'); ?>

<?php $__env->startSection('content'); ?>
<?php
    $syncSettings = $tenant->settings ?? [];
    $autoSyncEnabled = (bool) ($syncSettings['sync_auto_enabled'] ?? false);
    $autoSyncInterval = (int) ($syncSettings['sync_auto_interval'] ?? 15);
?>

<?php if(! ($syncTablesReady ?? true)): ?>
    <div class="alert alert-warning border-0 shadow-sm">
        <div class="fw-semibold mb-1">Sync Center database tables are missing.</div>
        <div>
            Import <code>database/patches/2026_05_14_import_sync_tables.sql</code> into this MySQL database from phpMyAdmin, then reload this page.
            Missing table<?php echo e($missingSyncTables->count() === 1 ? '' : 's'); ?>:
            <strong><?php echo e($missingSyncTables->implode(', ')); ?></strong>.
        </div>
    </div>
<?php endif; ?>

<div class="row g-4">
    <div class="col-lg-5">
        <div class="card border-0 shadow-sm">
            <div class="card-body">
                <h2 class="h5 mb-3">Offline to online sync</h2>
                <p class="text-muted">Use this for a branch or office running offline on XAMPP. Export a package and upload it to the online copy, or configure a remote URL and token to push directly.</p>
                <form method="POST" action="<?php echo e(route('tenant.sync.export')); ?>" class="mb-3">
                    <?php echo csrf_field(); ?>
                    <button class="btn btn-primary w-100" <?php if(! ($syncTablesReady ?? true)): echo 'disabled'; endif; ?>><i class="bi bi-download me-1"></i> Download sync package</button>
                </form>
                <form method="POST" action="<?php echo e(route('tenant.sync.import')); ?>" enctype="multipart/form-data" class="vstack gap-2">
                    <?php echo csrf_field(); ?>
                    <label class="form-label">Upload sync package</label>
                    <input name="package" type="file" accept=".json,.txt" class="form-control" required <?php if(! ($syncTablesReady ?? true)): echo 'disabled'; endif; ?>>
                    <button class="btn btn-outline-primary" <?php if(! ($syncTablesReady ?? true)): echo 'disabled'; endif; ?>><i class="bi bi-upload me-1"></i> Import package</button>
                </form>
            </div>
        </div>

        <div class="card border-0 shadow-sm mt-4">
            <div class="card-body">
                <h2 class="h5 mb-3">Remote online server</h2>
                <form method="POST" action="<?php echo e(route('tenant.sync.remote')); ?>" class="vstack gap-3">
                    <?php echo csrf_field(); ?>
                    <div>
                        <label class="form-label">Online URL</label>
                        <input name="sync_remote_url" value="<?php echo e($tenant->settings['sync_remote_url'] ?? ''); ?>" class="form-control" placeholder="https://yourdomain.com">
                    </div>
                    <div>
                        <label class="form-label">Online sync token</label>
                        <input name="sync_remote_token" value="<?php echo e($tenant->settings['sync_remote_token'] ?? ''); ?>" class="form-control" placeholder="Paste token from online copy">
                    </div>
                    <button class="btn btn-outline-secondary">Save remote settings</button>
                </form>
                <form method="POST" action="<?php echo e(route('tenant.sync.run')); ?>" class="mt-3">
                    <?php echo csrf_field(); ?>
                    <button class="btn btn-success w-100 btn-lg" <?php if(! ($syncTablesReady ?? true)): echo 'disabled'; endif; ?>><i class="bi bi-arrow-repeat me-1"></i> Sync now</button>
                </form>
                <div class="small text-muted mt-2">
                    Sync now pushes this copy to the online server first, then pulls the online server's latest package back into this copy.
                </div>

                <hr class="my-4">

                <form method="POST" action="<?php echo e(route('tenant.sync.auto')); ?>" class="vstack gap-3">
                    <?php echo csrf_field(); ?>
                    <div class="form-check form-switch">
                        <input class="form-check-input" type="checkbox" role="switch" id="sync_auto_enabled" name="sync_auto_enabled" value="1" <?php if($autoSyncEnabled): echo 'checked'; endif; ?> <?php if(! ($syncTablesReady ?? true)): echo 'disabled'; endif; ?>>
                        <label class="form-check-label fw-semibold" for="sync_auto_enabled">Automatic sync</label>
                        <div class="form-text">Runs push and pull quietly while the tenant portal is open.</div>
                    </div>
                    <div>
                        <label class="form-label">Sync every</label>
                        <select name="sync_auto_interval" class="form-select" <?php if(! ($syncTablesReady ?? true)): echo 'disabled'; endif; ?>>
                            <?php $__currentLoopData = [5, 10, 15, 30, 60]; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $minutes): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($minutes); ?>" <?php if($autoSyncInterval === $minutes): echo 'selected'; endif; ?>><?php echo e($minutes); ?> minutes</option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                    </div>
                    <button class="btn btn-outline-success" <?php if(! ($syncTablesReady ?? true)): echo 'disabled'; endif; ?>><i class="bi bi-clock-history me-1"></i> Save automatic sync</button>
                </form>
            </div>
        </div>
    </div>

    <div class="col-lg-7">
        <div class="card border-0 shadow-sm mb-4">
            <div class="card-body">
                <h2 class="h5 mb-3">Online receiver tokens</h2>
                <form method="POST" action="<?php echo e(route('tenant.sync.clients.store')); ?>" class="row g-2 mb-3">
                    <?php echo csrf_field(); ?>
                    <div class="col-md-8"><input name="name" class="form-control" placeholder="e.g. Head office offline laptop" required <?php if(! ($syncTablesReady ?? true)): echo 'disabled'; endif; ?>></div>
                    <div class="col-md-4 d-grid"><button class="btn btn-primary" <?php if(! ($syncTablesReady ?? true)): echo 'disabled'; endif; ?>>Create token</button></div>
                </form>
                <div class="table-responsive">
                    <table class="table mb-0">
                        <thead><tr><th>Client</th><th>Status</th><th>Last sync</th></tr></thead>
                        <tbody>
                            <?php $__empty_1 = true; $__currentLoopData = $clients; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $client): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                                <tr><td><?php echo e($client->name); ?></td><td><span class="badge <?php echo e($client->is_active ? 'text-bg-success' : 'text-bg-secondary'); ?>"><?php echo e($client->is_active ? 'Active' : 'Inactive'); ?></span></td><td><?php echo e($client->last_synced_at?->diffForHumans() ?? '-'); ?></td></tr>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                                <tr><td colspan="3" class="text-center text-muted py-4">No sync clients yet.</td></tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <div class="card border-0 shadow-sm">
            <div class="card-header bg-white border-0"><h2 class="h5 mb-0">Sync history</h2></div>
            <div class="table-responsive">
                <table class="table align-middle mb-0">
                    <thead><tr><th>Date</th><th>Direction</th><th>Status</th><th class="text-end">Records</th></tr></thead>
                    <tbody>
                        <?php $__empty_1 = true; $__currentLoopData = $packages; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $package): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                            <tr>
                                <td><?php echo e(app_datetime($package->created_at)); ?><div class="small text-muted"><?php echo e($package->client?->name ?: $package->user?->name); ?></div></td>
                                <td><?php echo e(ucfirst($package->direction)); ?></td>
                                <td><span class="badge <?php echo e(str_contains($package->status, 'error') ? 'text-bg-warning' : 'text-bg-success'); ?>"><?php echo e(ucfirst(str_replace('_', ' ', $package->status))); ?></span></td>
                                <td class="text-end"><?php echo e(number_format($package->records_count)); ?></td>
                            </tr>
                            <?php if($package->errors): ?>
                                <tr><td colspan="4"><pre class="small bg-light border rounded p-2 mb-0"><?php echo e($package->errors); ?></pre></td></tr>
                            <?php endif; ?>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                            <tr><td colspan="4" class="text-center text-muted py-4">No sync activity yet.</td></tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
            <div class="p-3"><?php echo e($packages->links()); ?></div>
        </div>
    </div>
</div>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\Users\Polite hp\Documents\Codex\2026-05-08\build-a-complete-production-ready-saas\microfinance-saas\resources\views\tenant\sync\index.blade.php ENDPATH**/ ?>