<?php
$isOfficial = !empty($store['verified_at']);
$st = $store['stats'] ?? [];
?>
<div class="mb-3">
  <a href="<?= url('/admin/sellers') ?>" class="btn btn-outline-secondary btn-sm py-1 px-3">
    <i class="bi bi-arrow-left me-1"></i>กลับรายการร้านค้า
  </a>
</div>

<!-- ── Detail Summary Header ───────────────────────────── -->
<div class="admin-detail-summary-header">
  <div class="d-flex align-items-center gap-3">
    <div class="rounded-circle border overflow-hidden bg-light d-flex align-items-center justify-content-center flex-shrink-0" style="width: 56px; height: 56px;">
      <?php if ($store['logo']): ?>
        <img src="<?= store_image_url($store['logo'], 'logo') ?>" class="w-100 h-100 object-fit-cover" alt="">
      <?php else: ?>
        <i class="bi bi-shop text-primary fs-4"></i>
      <?php endif; ?>
    </div>
    <div>
      <div class="d-flex align-items-center gap-2 flex-wrap">
        <h5 class="fw-bold mb-0 text-dark"><?= e($store['store_name']) ?></h5>
        <?php if ($isOfficial): ?>
          <span class="status-pill status-official"><i class="bi bi-patch-check-fill me-1"></i>Official Store</span>
        <?php endif; ?>
        <?php if ($store['status'] === 'active'): ?>
          <span class="status-pill status-active">เปิดดำเนินการปกติ</span>
        <?php elseif ($store['status'] === 'suspended'): ?>
          <span class="status-pill status-suspended">ระงับการให้บริการ</span>
        <?php else: ?>
          <span class="status-pill status-secondary"><?= e($store['status']) ?></span>
        <?php endif; ?>
      </div>
      <div class="text-muted small mt-1">
        <span class="font-monospace">/shop/<?= e($store['store_slug']) ?></span> &bull;
        เจ้าของ: <strong><?= e($store['first_name'] . ' ' . $store['last_name']) ?></strong> (@<?= e($store['owner_username']) ?>)
      </div>
    </div>
  </div>

  <!-- Actions Bar -->
  <div class="d-flex align-items-center gap-2 flex-wrap">
    <a href="<?= url('/shop/' . e($store['store_slug'])) ?>" target="_blank" class="btn btn-outline-secondary btn-sm py-1 px-3">
      <i class="bi bi-box-arrow-up-right me-1"></i>ดูหน้าร้านค้า
    </a>

    <?php if (AdminMiddleware::can('seller.approve')): ?>
    <button type="button" class="btn <?= $isOfficial ? 'btn-outline-secondary' : 'btn-outline-primary' ?> btn-sm py-1 px-3"
            data-bs-toggle="modal" data-bs-target="#officialStatusModal">
      <i class="bi bi-patch-check me-1"></i><?= $isOfficial ? 'ยกเลิก Official' : 'ตั้งเป็น Official' ?>
    </button>
    <?php endif; ?>

    <?php if (AdminMiddleware::can('seller.suspend')): ?>
      <?php if ($store['status'] === 'active'): ?>
      <button type="button" class="btn btn-outline-danger btn-sm py-1 px-3" data-bs-toggle="modal" data-bs-target="#suspendStoreModal">
        <i class="bi bi-slash-circle me-1"></i>ระงับร้านค้า
      </button>
      <?php else: ?>
      <form action="<?= url('/admin/sellers/' . (int)$store['id'] . '/reactivate') ?>" method="POST" class="d-inline">
        <?= Security::csrfField() ?>
        <button type="submit" class="btn btn-success btn-sm py-1 px-3" onclick="return confirm('ยืนยันเปิดร้านค้านี้อีกครั้ง?')">
          <i class="bi bi-check-circle me-1"></i>เปิดร้านค้าอีกครั้ง
        </button>
      </form>
      <?php endif; ?>
    <?php endif; ?>
  </div>
</div>

<div class="row g-4">
  <!-- Left Store & Owner Profile -->
  <div class="col-lg-5">
    <!-- Store Info Card -->
    <div class="admin-detail-card mb-4">
      <div class="admin-detail-card-header">
        <span><i class="bi bi-shop text-primary me-2"></i>ข้อมูลร้านค้าและเจ้าของ</span>
      </div>
      <div class="admin-detail-card-body">
        <dl class="row mb-0 small">
          <dt class="col-5 text-muted mb-2">Store ID</dt>
          <dd class="col-7 font-monospace mb-2">#<?= (int)$store['id'] ?></dd>

          <dt class="col-5 text-muted mb-2">ชื่อร้านค้า</dt>
          <dd class="col-7 fw-semibold mb-2"><?= e($store['store_name']) ?></dd>

          <dt class="col-5 text-muted mb-2">เจ้าของร้าน</dt>
          <dd class="col-7 mb-2">
            <a href="<?= url('/admin/users/' . (int)$store['user_id']) ?>" class="text-primary fw-medium">
              <?= e($store['first_name'] . ' ' . $store['last_name']) ?>
            </a>
          </dd>

          <dt class="col-5 text-muted mb-2">อีเมลติดต่อ</dt>
          <dd class="col-7 mb-2"><?= e($store['owner_email']) ?></dd>

          <dt class="col-5 text-muted mb-2">เบอร์โทรศัพท์</dt>
          <dd class="col-7 mb-2"><?= e($store['phone'] ?? $store['owner_phone'] ?? '—') ?></dd>

          <dt class="col-5 text-muted mb-2">ค่าคอมมิชชัน</dt>
          <dd class="col-7 mb-2 font-monospace fw-semibold text-primary">
            <?= number_format((float)($store['commission_rate'] ?? 5), 2) ?>%
          </dd>

          <dt class="col-5 text-muted mb-2">วันที่เปิดร้าน</dt>
          <dd class="col-7 mb-2"><?= date('d M Y H:i', strtotime($store['created_at'])) ?></dd>

          <dt class="col-5 text-muted mb-0">ที่อยู่ร้านค้า</dt>
          <dd class="col-7 mb-0 text-muted"><?= nl2br(e($store['address'] ?? '—')) ?></dd>
        </dl>
      </div>
    </div>
  </div>

  <!-- Right Store Performance & Products/Orders Hub -->
  <div class="col-lg-7">
    <!-- Store Operational Stats -->
    <div class="admin-detail-card mb-4">
      <div class="admin-detail-card-header">
        <span><i class="bi bi-graph-up-arrow text-primary me-2"></i>สถิติผลการดำเนินงานของร้านค้า</span>
      </div>
      <div class="admin-detail-card-body p-3">
        <div class="row g-3">
          <div class="col-6 col-md-4">
            <div class="p-3 bg-light rounded-3 text-center">
              <span class="text-muted small d-block mb-1">สินค้าทั้งหมด</span>
              <span class="fs-4 fw-bold text-dark"><?= number_format((int)($st['total_products'] ?? 0)) ?></span>
              <span class="text-muted d-block small" style="font-size: 0.7rem;">Active <?= (int)($st['active_products'] ?? 0) ?></span>
            </div>
          </div>
          <div class="col-6 col-md-4">
            <div class="p-3 bg-light rounded-3 text-center">
              <span class="text-muted small d-block mb-1">ออเดอร์ทั้งหมด</span>
              <span class="fs-4 fw-bold text-dark"><?= number_format((int)($st['total_orders'] ?? 0)) ?></span>
              <span class="text-muted d-block small" style="font-size: 0.7rem;">รอแพ็ค <?= (int)($st['preparing_orders'] ?? 0) ?></span>
            </div>
          </div>
          <div class="col-12 col-md-4">
            <div class="p-3 bg-light rounded-3 text-center">
              <span class="text-muted small d-block mb-1">ยอดขายสะสม (GMV)</span>
              <span class="fs-4 fw-bold text-success">฿<?= number_format((float)($st['total_sales'] ?? 0), 2) ?></span>
              <span class="text-muted d-block small" style="font-size: 0.7rem;">ยอดขายสำเร็จ</span>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- Quick Navigation Hub for this Store -->
    <div class="admin-detail-card">
      <div class="admin-detail-card-header">
        <span><i class="bi bi-grid text-primary me-2"></i>ข้อมูลที่เกี่ยวข้องกับร้านค้านี้</span>
      </div>
      <div class="admin-detail-card-body p-3">
        <div class="row g-2">
          <div class="col-md-6">
            <a href="<?= url('/admin/products?store_id=' . (int)$store['id']) ?>" class="btn btn-outline-secondary btn-sm w-100 text-start py-2 d-flex justify-content-between align-items-center">
              <span><i class="bi bi-box-seam me-2 text-primary"></i>สินค้าของร้านนี้</span>
              <span class="badge bg-light text-muted border"><?= (int)($st['total_products'] ?? 0) ?></span>
            </a>
          </div>
          <div class="col-md-6">
            <a href="<?= url('/admin/orders?store_id=' . (int)$store['id']) ?>" class="btn btn-outline-secondary btn-sm w-100 text-start py-2 d-flex justify-content-between align-items-center">
              <span><i class="bi bi-receipt me-2 text-primary"></i>ออเดอร์ของร้านนี้</span>
              <span class="badge bg-light text-muted border"><?= (int)($st['total_orders'] ?? 0) ?></span>
            </a>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

<!-- ── Suspend Store Modal ─────────────────────────────── -->
<?php if (AdminMiddleware::can('seller.suspend')): ?>
<div class="modal fade" id="suspendStoreModal" tabindex="-1" aria-labelledby="suspendStoreModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered">
    <div class="modal-content">
      <form action="<?= url('/admin/sellers/' . (int)$store['id'] . '/suspend') ?>" method="POST">
        <?= Security::csrfField() ?>
        <div class="modal-header bg-danger text-white py-3">
          <h6 class="modal-title fw-bold" id="suspendStoreModalLabel">
            <i class="bi bi-exclamation-triangle-fill me-2"></i>ยืนยันการระงับร้านค้า
          </h6>
          <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
        </div>
        <div class="modal-body p-4 small">
          <p class="mb-3 text-dark">
            คุณกำลังจะระงับร้านค้า <strong><?= e($store['store_name']) ?></strong>
          </p>
          <div class="alert alert-warning border-warning small mb-0">
            <strong>ผลกระทบของการระงับ:</strong>
            <ul class="mb-0 ps-3 mt-1 text-dark">
              <li>หน้าร้านค้าและสินค้าทั้งหมดของร้านนี้จะไม่สามารถเข้าชมหรือสั่งซื้อได้</li>
              <li>ผู้ขายจะยังสามารถเข้า Seller Center เพื่อจัดการออเดอร์ค้างส่งได้</li>
            </ul>
          </div>
        </div>
        <div class="modal-footer py-2 bg-light">
          <button type="button" class="btn btn-sm btn-secondary" data-bs-dismiss="modal">ยกเลิก</button>
          <button type="submit" class="btn btn-sm btn-danger px-3">
            <i class="bi bi-slash-circle me-1"></i>ยืนยันระงับร้านค้า
          </button>
        </div>
      </form>
    </div>
  </div>
</div>
<?php endif; ?>

<!-- ── Official Status Modal ───────────────────────────── -->
<?php if (AdminMiddleware::can('seller.approve')): ?>
<div class="modal fade" id="officialStatusModal" tabindex="-1" aria-labelledby="officialStatusModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered">
    <div class="modal-content">
      <form action="<?= url('/admin/sellers/' . (int)$store['id'] . '/set-official') ?>" method="POST">
        <?= Security::csrfField() ?>
        <input type="hidden" name="official" value="<?= $isOfficial ? '0' : '1' ?>">
        <div class="modal-header <?= $isOfficial ? 'bg-secondary' : 'bg-primary' ?> text-white py-3">
          <h6 class="modal-title fw-bold" id="officialStatusModalLabel">
            <i class="bi bi-patch-check-fill me-2"></i><?= $isOfficial ? 'ยกเลิกสถานะ Official Store' : 'กำหนดเป็น Official Store' ?>
          </h6>
          <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
        </div>
        <div class="modal-body p-4 small">
          <p class="mb-2 text-dark">
            ร้านค้า: <strong><?= e($store['store_name']) ?></strong>
          </p>
          <p class="text-muted mb-0">
            <?= $isOfficial
              ? 'การยกเลิกสถานะ Official จะนำป้ายสัญลักษณ์ Official ออกจากหน้าร้านค้าและรายการสินค้า'
              : 'สถานะ Official จะแสดงเครื่องหมายยืนยันความน่าเชื่อถือบนหน้าร้านค้าและสินค้า โดยไม่มีผลกระทบต่อสิทธิ์ Seller ปกติ'
            ?>
          </p>
        </div>
        <div class="modal-footer py-2 bg-light">
          <button type="button" class="btn btn-sm btn-secondary" data-bs-dismiss="modal">ยกเลิก</button>
          <button type="submit" class="btn btn-sm <?= $isOfficial ? 'btn-secondary' : 'btn-primary' ?> px-3">
            ยืนยันการเปลี่ยนแปลง
          </button>
        </div>
      </form>
    </div>
  </div>
</div>
<?php endif; ?>

