<?php
/** @var string $table @var array $cols @var array $names @var string $pk
 *  @var array $rows @var int $page @var int $totalPages @var int $total @var string $q */
?>
<div class="page-head">
  <div>
    <h1>表：<?php echo e($table); ?></h1>
    <div class="desc">共 <b><?php echo number_format($total); ?></b> 条记录，主键：<code><?php echo e($pk); ?></code></div>
  </div>
  <div class="head-actions">
    <a class="btn-soft" href="<?php echo site_url('admin/db'); ?>">← 所有表</a>
    <a class="btn-primary" href="<?php echo site_url('admin/db/create/' . $table); ?>"><?php echo admin_icon('plus', 16); ?> 新增记录</a>
  </div>
</div>

<div class="admin-card">
  <form class="db-search" method="get" action="<?php echo site_url('admin/db/browse/' . $table); ?>">
    <input type="text" name="q" value="<?php echo e($q); ?>" placeholder="跨字段搜索…" />
    <button class="btn-soft" type="submit">搜索</button>
    <?php if ($q !== ''): ?><a class="btn-link" href="<?php echo site_url('admin/db/browse/' . $table); ?>">清除</a><?php endif; ?>
  </form>

  <div class="tbl-wrap">
    <table class="tbl tbl-data">
      <thead>
        <tr>
          <?php foreach ($names as $c): ?><th class="mono"><?php echo e($c); ?></th><?php endforeach; ?>
          <th class="num">操作</th>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($rows as $r): ?>
          <tr>
            <?php foreach ($names as $c): ?>
              <td class="cell" title="<?php echo e((string)($r[$c] ?? '')); ?>"><?php echo e(mb_substr((string)($r[$c] ?? ''), 0, 120)); ?></td>
            <?php endforeach; ?>
            <td class="num nowrap">
              <a class="link-ok" href="<?php echo site_url('admin/db/edit/' . $table . '/' . rawurlencode($r[$pk])); ?>">编辑</a>
              <a class="link-del" href="<?php echo site_url('admin/db/delete/' . $table . '/' . rawurlencode($r[$pk])); ?>"
                 data-confirm="确认删除该记录？此操作不可撤销。">删除</a>
            </td>
          </tr>
        <?php endforeach; ?>
        <?php if (empty($rows)): ?><tr><td class="muted" colspan="<?php echo count($names) + 1; ?>">无记录</td></tr><?php endif; ?>
      </tbody>
    </table>
  </div>

  <?php if ($totalPages > 1): ?>
    <div class="pager">
      <?php
        $base = 'admin/db/browse/' . $table . '?q=' . urlencode($q) . '&page=';
        if ($page > 1): ?><a class="btn-soft btn-sm" href="<?php echo site_url($base . ($page - 1)); ?>">← 上一页</a><?php endif;
        echo '<span class="pager-info">第 ' . $page . ' / ' . $totalPages . ' 页</span>';
        if ($page < $totalPages): ?><a class="btn-soft btn-sm" href="<?php echo site_url($base . ($page + 1)); ?>">下一页 →</a><?php endif;
      ?>
    </div>
  <?php endif; ?>
</div>
