<?php /* branches/index.php */ ?>

<div class="d-flex justify-content-between align-items-center mb-3">
  <div>
    <h5 class="fw-bold mb-0">
      <i class="bi bi-building me-2 text-primary"></i>Branch Management
    </h5>
    <small class="text-muted ms-4"><?= count($branches) ?> branch<?= count($branches)!=1?'es':'' ?></small>
  </div>
  <a href="<?= APP_URL ?>/branches/create" class="btn btn-primary btn-sm fw-semibold">
    <i class="bi bi-plus-circle me-1"></i>Add Branch
  </a>
</div>

<?= flashMessages() ?>

<div class="row g-3">
  <?php foreach($branches as $b):
    $isMain = (bool)$b['is_main'];
    $isActive = $b['status'] === 'active';
  ?>
  <div class="col-md-6 col-lg-4">
    <div class="card h-100 shadow-sm <?= $isMain?'border-primary border-2':'' ?>
                 <?= !$isActive?'opacity-60':'' ?>">

      <!-- Card header -->
      <div class="card-header d-flex justify-content-between align-items-center
                  <?= $isMain?'bg-primary text-white':'bg-transparent' ?>">
        <div class="d-flex align-items-center gap-2">
          <i class="bi bi-building<?= $isMain?'-fill':'' ?> fs-5"></i>
          <span class="fw-bold"><?= e($b['name']) ?></span>
        </div>
        <div class="d-flex gap-1 align-items-center">
          <?php if($isMain):?>
          <span class="badge bg-light text-primary border" style="font-size:.65rem">
            ★ Main
          </span>
          <?php endif;?>
          <span class="badge <?= $isActive?'bg-success':'bg-secondary' ?>"
                style="font-size:.65rem">
            <?= ucfirst($b['status']) ?>
          </span>
        </div>
      </div>

      <div class="card-body py-3">
        <div class="row g-2 mb-3">
          <div class="col-6 text-center">
            <div class="fw-bold fs-4 text-primary"><?= $b['user_count'] ?></div>
            <small class="text-muted">Users Assigned</small>
          </div>
          <div class="col-6 text-center">
            <div class="fw-bold fs-4 <?= $isActive?'text-success':'text-secondary' ?>">
              <?= $isActive?'●':'○' ?>
            </div>
            <small class="text-muted"><?= $isActive?'Active':'Inactive' ?></small>
          </div>
        </div>

        <?php if($b['address'] || $b['phone'] || $b['email']):?>
        <div class="border-top pt-2">
          <?php if($b['address']):?>
          <div class="small text-muted mb-1">
            <i class="bi bi-geo-alt me-1"></i><?= e($b['address']) ?>
          </div>
          <?php endif;?>
          <?php if($b['phone']):?>
          <div class="small text-muted mb-1">
            <i class="bi bi-telephone me-1"></i><?= e($b['phone']) ?>
          </div>
          <?php endif;?>
          <?php if($b['email']):?>
          <div class="small text-muted">
            <i class="bi bi-envelope me-1"></i><?= e($b['email']) ?>
          </div>
          <?php endif;?>
        </div>
        <?php endif;?>
      </div>

      <div class="card-footer bg-transparent d-flex justify-content-between align-items-center py-2">
        <a href="<?= APP_URL ?>/branches/edit/<?= $b['id'] ?>"
           class="btn btn-sm btn-outline-primary">
          <i class="bi bi-pencil me-1"></i>Edit & Assign Users
        </a>
        <?php if(!$isMain && $b['user_count'] == 0):?>
        <form method="POST" action="<?= APP_URL ?>/branches/delete/<?= $b['id'] ?>"
              onsubmit="return confirm('Delete branch \'<?= e($b['name']) ?>\'?')">
          <?= Auth::csrfField() ?>
          <button class="btn btn-sm btn-outline-danger">
            <i class="bi bi-trash3"></i>
          </button>
        </form>
        <?php elseif($isMain):?>
        <span class="text-muted small"><i class="bi bi-lock me-1"></i>Protected</span>
        <?php else:?>
        <span class="text-muted small">
          <i class="bi bi-people me-1"></i><?= $b['user_count'] ?> user<?= $b['user_count']!=1?'s':'' ?>
        </span>
        <?php endif;?>
      </div>
    </div>
  </div>
  <?php endforeach;?>

  <!-- Add branch card -->
  <div class="col-md-6 col-lg-4">
    <a href="<?= APP_URL ?>/branches/create"
       class="card h-100 text-decoration-none border-dashed"
       style="border:2px dashed #cbd5e1;min-height:180px">
      <div class="card-body d-flex flex-column align-items-center justify-content-center text-muted">
        <i class="bi bi-plus-circle fs-2 mb-2 text-primary"></i>
        <span class="fw-semibold">Add New Branch</span>
      </div>
    </a>
  </div>
</div>