<?php /** @var string $driver @var array $list @var int $pending @var array $history */ ?>
<div class="page-head">
  <div>
    <h1>数据库升级</h1>
    <div class="desc">将升级包（<code>*.sql</code>）放入 <code>install/upgrades/</code> 目录后，系统自动提示可升级；每次升级均留痕记录。</div>
  </div>
  <a class="btn-soft" href="<?php echo site_url('admin/db'); ?>"><?php echo admin_icon('database', 16); ?> 数据库管理</a>
</div>

<?php if (($driver ?? '') === 'file'): ?>
  <div class="admin-card">
    <div class="alert alert-warn">当前为文件存储模式，不支持 SQL 升级。请切换到 MySQL 后使用本功能。</div>
  </div>
<?php else: ?>

  <?php if ($pending > 0): ?>
    <div class="alert alert-upgrade">
      <div class="alert-upgrade-main">
        <span class="alert-upgrade-ic"><?php echo admin_icon('upload', 22); ?></span>
        <div>
          <b>发现 <?php echo (int)$pending; ?> 个可升级数据库脚本</b>
          <div class="muted">执行后将在「升级记录」中留痕，可随时追溯。</div>
        </div>
      </div>
      <a class="btn-primary" href="<?php echo site_url('admin/upgrade/applyall'); ?>"
         data-confirm="确认执行全部待升级脚本？建议升级前先备份数据库。">⬆️ 立即全部升级</a>
    </div>
  <?php else: ?>
    <div class="alert alert-ok">✓ 数据库已是最新，没有待升级的脚本。</div>
  <?php endif; ?>

  <div class="admin-card">
    <h3 class="card-title">升级包（install/upgrades/）</h3>
    <?php if (empty($list)): ?>
      <div class="muted" style="padding:12px 0">暂无升级包。将 <code>*.sql</code> 文件放入 <code>install/upgrades/</code> 目录即可在此处升级。</div>
    <?php else: ?>
      <div class="tbl-wrap">
        <table class="tbl">
          <thead>
            <tr><th>文件名</th><th class="num">大小</th><th>修改时间</th><th>状态</th><th class="num">操作</th></tr>
          </thead>
          <tbody>
            <?php foreach ($list as $it): ?>
              <tr>
                <td class="mono"><?php echo e($it['name']); ?></td>
                <td class="num"><?php echo number_format($it['size']); ?> B</td>
                <td class="muted"><?php echo date('Y-m-d H:i', $it['mtime']); ?></td>
                <td>
                  <?php if ($it['state'] === 'done'): ?><span class="tag-mini tag-green">已应用</span>
                  <?php elseif ($it['state'] === 'changed'): ?><span class="tag-mini tag-amber">内容已变更</span>
                  <?php else: ?><span class="tag-mini tag-blue">待升级</span><?php endif; ?>
                </td>
                <td class="num">
                  <?php if ($it['state'] !== 'done'): ?>
                    <a class="link-ok" href="<?php echo site_url('admin/upgrade/apply/' . rawurlencode($it['name'])); ?>"
                       data-confirm="确认执行该升级包？建议先备份数据库。">升级</a>
                  <?php else: ?><span class="muted">—</span><?php endif; ?>
                </td>
              </tr>
            <?php endforeach; ?>
          </tbody>
        </table>
      </div>
    <?php endif; ?>
  </div>

  <div class="admin-card">
    <h3 class="card-title">升级记录（db_upgrades）</h3>
    <?php if (empty($history)): ?>
      <div class="muted" style="padding:12px 0">暂无升级记录。</div>
    <?php else: ?>
      <div class="tbl-wrap">
        <table class="tbl">
          <thead>
            <tr><th>文件名</th><th>操作人</th><th>执行时间</th><th>文件指纹(MD5)</th></tr>
          </thead>
          <tbody>
            <?php foreach ($history as $h): ?>
              <tr>
                <td class="mono"><?php echo e($h['file']); ?></td>
                <td><?php echo e($h['applied_by'] ?? ''); ?></td>
                <td class="muted"><?php echo e($h['applied_at'] ?? ''); ?></td>
                <td class="mono muted" title="<?php echo e($h['hash']); ?>"><?php echo e(substr($h['hash'], 0, 12)); ?>…</td>
              </tr>
            <?php endforeach; ?>
          </tbody>
        </table>
      </div>
    <?php endif; ?>
  </div>

  <div class="admin-card">
    <h3 class="card-title">基础初始化工具</h3>
    <p class="muted" style="line-height:1.8">
      重新执行基础表结构与种子数据补齐（<code>install/schema.sql</code> / <code>install/seed.php</code>）。
      该操作会补齐缺失的新模块表 / 列，并保留已有订单与管理员账号，但不会覆盖客户已编辑的内容数据。
    </p>
    <form method="post" action="<?php echo site_url('admin/upgrade/init'); ?>">
      <?php echo csrf_field(); ?>
      <button class="btn-soft" type="submit"
              data-confirm="确认执行基础数据升级？">⬆️ 执行基础数据升级</button>
    </form>
  </div>

<?php endif; ?>
