<?php
require_once 'config/database.php';

// Create directory
$storeDir = __DIR__ . '/uploads/store';
if (!is_dir($storeDir)) { @mkdir($storeDir, 0777, true); }

// List files
$files = [];
foreach (is_dir($storeDir) ? scandir($storeDir) : [] as $f) {
    if ($f==='.'||$f==='..') continue;
    $path = $storeDir . '/' . $f;
    if (is_file($path)) {
        $files[] = [
            'name' => $f,
            'size' => filesize($path),
            'mtime'=> filemtime($path),
            'url'  => 'uploads/store/' . rawurlencode($f)
        ];
    }
}
usort($files, function($a,$b){return $b['mtime'] <=> $a['mtime'];});
?>
<!DOCTYPE html>
<html lang="bn">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>স্টোর রুম - বিদ্যা নিকেতন উচ্চ বিদ্যালয়</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
  <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
  <link href="https://fonts.googleapis.com/css2?family=Hind+Siliguri:wght@300;400;500;600;700&display=swap" rel="stylesheet">
  <style>
    body { 
      font-family: 'Hind Siliguri', sans-serif; 
      background: #f5f7fa; 
    }
    .navbar {
      background: linear-gradient(135deg, #006747 0%, #004d35 100%);
      box-shadow: 0 4px 20px rgba(0, 103, 71, 0.3);
      border-bottom: 3px solid #F42A41;
    }
    .navbar-brand {
      color: #fff !important;
      font-weight: 800;
    }
    .section-title { 
      font-weight: 700; 
      color: #006747;
      border-bottom: 3px solid #F42A41;
      padding-bottom: 10px;
      margin-bottom: 30px;
    }
    .card { 
      border: 0; 
      border-radius: 14px; 
      box-shadow: 0 10px 25px rgba(0,103,71,0.1); 
      border-top: 4px solid #006747;
    }
  </style>
</head>
<body>
  <nav class="navbar navbar-expand-lg navbar-dark">
    <div class="container">
      <a class="navbar-brand" href="index.php">
        <i class="fas fa-graduation-cap"></i> বিদ্যা নিকেতন উচ্চ বিদ্যালয়
      </a>
      <div class="navbar-nav ms-auto">
        <a class="nav-link text-white" href="index.php">
          <i class="fas fa-home"></i> হোম
        </a>
      </div>
    </div>
  </nav>

  <div class="container mt-5">
    <h2 class="section-title">
      <i class="fas fa-folder"></i> স্টোর রুম
    </h2>
    <div class="card">
      <div class="card-body">
        <?php if (empty($files)): ?>
          <div class="text-center text-muted py-5">
            <i class="fas fa-box-open fa-3x mb-3"></i>
            <p>স্টোর রুমে কোনো ফাইল নেই</p>
          </div>
        <?php else: ?>
        <div class="table-responsive">
          <table class="table table-striped table-hover align-middle">
            <thead class="table-light">
              <tr>
                <th><i class="fas fa-file-alt"></i> ফাইল</th>
                <th><i class="fas fa-weight-hanging"></i> সাইজ</th>
                <th><i class="fas fa-calendar-alt"></i> আপলোডের তারিখ</th>
                <th class="text-end"><i class="fas fa-cogs"></i> অ্যাকশন</th>
              </tr>
            </thead>
            <tbody>
              <?php foreach ($files as $f): ?>
              <tr>
                <td>
                  <a href="<?php echo $f['url']; ?>" target="_blank" class="text-decoration-none">
                    <i class="far fa-file-alt me-2"></i>
                    <?php echo htmlspecialchars($f['name']); ?>
                  </a>
                </td>
                <td><?php echo number_format($f['size']/1024,1); ?> KB</td>
                <td><?php echo date('d-m-Y H:i', $f['mtime']); ?></td>
                <td class="text-end">
                  <a class="btn btn-sm btn-outline-primary" href="<?php echo $f['url']; ?>" target="_blank" title="ডাউনলোড">
                    <i class="fas fa-download"></i>
                  </a>
                </td>
              </tr>
              <?php endforeach; ?>
            </tbody>
          </table>
        </div>
        <?php endif; ?>
      </div>
    </div>
  </div>

  <footer class="text-center py-4 mt-5">
    <p class="text-muted">&copy; <?php echo date('Y'); ?> বিদ্যা নিকেতন উচ্চ বিদ্যালয়। সর্বস্বত্ব সংরক্ষিত।</p>
  </footer>

  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
