<div class="content-wrapper">
<section class="content pt-3">
<div class="container-fluid pb-5">

<div class="d-flex flex-wrap justify-content-between align-items-start gap-3 mb-4">
    <div>
        <h3 class="mb-1 fw-bold"><i class="bi bi-sliders me-2 text-primary"></i><?= __('shipping.control.title','Shipping Control Center') ?></h3>
        <p class="text-muted mb-0 small"><?= __('shipping.control.subtitle','Manage shipping providers and automation') ?></p>
    </div>
</div>

<!-- CRON Status -->
<div class="card border-0 shadow-sm mb-4">
    <div class="card-header bg-white border-bottom py-3">
        <h6 class="mb-0 fw-semibold"><i class="bi bi-clock me-2 text-muted"></i><?= __('shipping.control.cron_title','Automation Status') ?></h6>
    </div>
    <div class="card-body d-flex justify-content-between align-items-center flex-wrap gap-3">
        <div class="d-flex align-items-center gap-3">
            <div class="rounded-circle d-flex align-items-center justify-content-center"
                 style="width:48px;height:48px;background:<?= $cronEnabled?'rgba(25,135,84,.1)':'rgba(220,53,69,.1)' ?>">
                <i class="bi <?= $cronEnabled?'bi-check-circle-fill text-success':'bi-x-circle-fill text-danger' ?> fs-4"></i>
            </div>
            <div>
                <div class="fw-semibold"><?= __('shipping.control.cron_status','CRON Status') ?></div>
                <span class="badge <?= $cronEnabled?'bg-success':'bg-danger' ?>">
                    <?= $cronEnabled?__('shipping.control.cron_enabled','Enabled'):__('shipping.control.cron_disabled','Disabled') ?>
                </span>
            </div>
        </div>
        <button class="btn btn-primary" onclick="runShippingNow()">
            <i class="bi bi-play-circle me-1"></i><?= __('shipping.control.run_now','Run Now') ?>
        </button>
    </div>
</div>

<!-- Providers -->
<div class="row g-3">
<?php foreach($integrations as $provider => $integration): ?>
<?php
$isActive = (int)$integration['is_active'] === 1;
$pvColors = ['ecotrack'=>'primary','zr_express'=>'warning','yalidine'=>'success','maystro'=>'info','dhd'=>'secondary'];
$color = $pvColors[strtolower($provider)] ?? 'secondary';
?>
<div class="col-md-6 col-xl-4">
    <div class="card border-0 shadow-sm h-100" style="border-top:3px solid var(--bs-<?= $color ?>) !important">
        <div class="card-body">
            <div class="d-flex justify-content-between align-items-start mb-3">
                <div>
                    <h6 class="fw-bold mb-1"><?= htmlspecialchars($integration['display_name']) ?></h6>
                    <span class="badge bg-<?= $color ?>-subtle text-<?= $color ?> border border-<?= $color ?>-subtle"><?= strtoupper($provider) ?></span>
                </div>
                <span class="badge <?= $isActive?'bg-success':'bg-secondary' ?>">
                    <?= $isActive?__('actions.active','Active'):__('actions.inactive','Inactive') ?>
                </span>
            </div>
            <div class="d-flex justify-content-between align-items-center border-top pt-3">
                <span class="text-muted small">
                    <i class="bi bi-geo-alt me-1"></i><?= __('shipping.control.country','Country') ?>: <?= htmlspecialchars($integration['country_code']??'—') ?>
                </span>
                <button class="btn btn-sm btn-outline-<?= $isActive?'warning':'success' ?>" onclick="toggleProvider('<?= $provider ?>')">
                    <i class="bi bi-power me-1"></i><?= __('actions.toggle','Toggle') ?>
                </button>
            </div>
        </div>
    </div>
</div>
<?php endforeach; ?>
</div>

</div></section></div>

<script>
function runShippingNow(){
    fetch('/admin/shipments/control/run', {
        method: 'POST',
        headers: {'Content-Type':'application/x-www-form-urlencoded'},
        body: '_csrf=' + encodeURIComponent(document.querySelector('meta[name=csrf-token]')?.content || '')
    }).then(r=>r.json()).then(res=>{
        if(res.success) alert('✔ '+(res.executed_at||'Done'));
        else alert(res.error||'Error');
    });
}
function toggleProvider(p){
    if(!confirm('<?= __('shipping.control.confirm_toggle','Are you sure?') ?>')) return;
    fetch('/admin/shipments/control/toggle',{method:'POST',headers:{'Content-Type':'application/x-www-form-urlencoded'},body:'provider='+encodeURIComponent(p)})
    .then(r=>r.json()).then(res=>{ if(res.success) location.reload(); });
}
</script>