<?= $this->extend('layouts/main') ?>
<?= $this->section('content') ?>
<div class="page-header"><div><h1><i class="bi bi-buildings me-2 text-primary"></i>Facilities</h1></div><a href="<?= base_url('facilities/create') ?>" class="btn btn-fm-primary btn-sm"><i class="bi bi-plus-lg me-1"></i>Add Facility</a></div>
<div class="row g-3">
<?php foreach($facilities as $f): ?>
<div class="col-md-6 col-xl-4">
<div class="facility-card">
  <div class="facility-header">
    <div class="d-flex justify-content-between align-items-start">
      <div><h5 class="mb-1 fw-bold"><?= esc($f['name']) ?></h5><div class="small opacity-75"><?= esc($f['code']) ?> · <?= esc($f['city']) ?>, <?= esc($f['country']) ?></div></div>
      <span class="fm-badge badge-status-<?= esc($f['status']) ?>"><?= ucfirst(str_replace('_',' ',$f['status'])) ?></span>
    </div>
  </div>
  <div class="p-3">
    <div class="row g-2 mb-3">
      <div class="col-4 text-center"><div class="fw-bold fs-5 text-primary"><?= $f['asset_count'] ?></div><div class="x-small text-muted">Assets</div></div>
      <div class="col-4 text-center"><div class="fw-bold fs-5 <?= $f['open_wo']>0?'text-warning':'text-success' ?>"><?= $f['open_wo'] ?></div><div class="x-small text-muted">Open WO</div></div>
      <div class="col-4 text-center"><div class="fw-bold fs-5"><?= (int)round($f['avg_health']??0) ?>%</div><div class="x-small text-muted">Health</div></div>
    </div>
    <div class="health-bar mb-3"><?php $h=(int)round($f['avg_health']??0); ?><div class="health-bar-fill <?= $h>=80?'good':($h>=50?'warn':'bad') ?>" style="width:<?= $h ?>%"></div></div>
    <div class="small text-muted mb-3"><i class="bi bi-person me-1"></i>Manager: <?= esc($f['manager_name'] ?? 'Not assigned') ?></div>
    <div class="d-flex gap-2">
      <a href="<?= base_url('facilities/view/'.$f['id']) ?>" class="btn btn-fm-primary btn-sm flex-grow-1">View Details</a>
      <a href="<?= base_url('facilities/edit/'.$f['id']) ?>" class="btn btn-fm-outline btn-sm">Edit</a>
    </div>
  </div>
</div></div>
<?php endforeach; ?>
<?php if(empty($facilities)): ?><div class="col-12"><div class="text-center py-5 text-muted">No facilities found. <a href="<?= base_url('facilities/create') ?>">Add one?</a></div></div><?php endif; ?>
</div>
<?= $this->endSection() ?>
