<?php
session_start();
if (!isset($_SESSION['admin_id'])) {
    header('Location: login.php');
    exit;
}
include '../db.php';

// Pagination config
$limit = 10;
$page = isset($_GET['page']) && is_numeric($_GET['page']) ? (int)$_GET['page'] : 1;
$offset = ($page - 1) * $limit;

// Count total companies
$total_stmt = $pdo->query("SELECT COUNT(*) FROM companies");
$total_companies = $total_stmt->fetchColumn();
$total_pages = ceil($total_companies / $limit);

// Fetch companies for current page
$stmt = $pdo->prepare("
    SELECT c.*, cat.name AS category_name
    FROM companies c
    LEFT JOIN categories cat ON c.category_id = cat.id
    ORDER BY c.id DESC
    LIMIT :limit OFFSET :offset
");
$stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
$stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
$stmt->execute();
$companies = $stmt->fetchAll();
?>

<?php include 'menu.php'; ?>
<!DOCTYPE html>
<html>
<head>
    <title>Edit Companies</title>
    <link rel="stylesheet" href="../style.css">
    <style>
        .container { max-width: 1000px; margin: 2rem auto; }
        table {
            border-collapse: collapse;
            width: 100%;
            margin-top: 1rem;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
            vertical-align: top;
        }
        th {
            background-color: #f2f2f2;
        }
        tr:nth-child(even){background-color: #fafafa;}
        tr:hover {background-color: #f1f1f1;}
        img { border-radius: 4px; }
        .pagination a {
            margin: 0 5px;
            text-decoration: none;
            padding: 5px 10px;
            border: 1px solid #ccc;
            border-radius: 4px;
        }
        .pagination a.active {
            background: #007BFF;
            color: white;
            border-color: #007BFF;
        }
        h2 { margin-bottom: 1rem; }
    </style>
</head>
<body>
<div class="container">
    <h2>Edit / Update Companies</h2>
    <table>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Website</th>
            <th>Region</th>
            <th>Category</th>
            <th>Image</th>
            <th>Description</th>
            <th>Action</th>
        </tr>
        <?php foreach($companies as $c): ?>
            <tr>
                <td><?php echo $c['id']; ?></td>
                <td><?php echo htmlspecialchars($c['name']); ?></td>
                <td><?php echo htmlspecialchars($c['website']); ?></td>
                <td><?php echo htmlspecialchars($c['region']); ?></td>
                <td><?php echo htmlspecialchars($c['category_name']); ?></td>
                <td>
                    <?php if($c['image']): ?>
                        <img src="../images/companies/<?php echo htmlspecialchars($c['image']); ?>" width="50">
                    <?php else: ?>
                        N/A
                    <?php endif; ?>
                </td>
                <td><?php echo htmlspecialchars(mb_strimwidth($c['description'], 0, 50, '...')); ?></td>
                <td>
                    <a href="edit_single.php?id=<?php echo $c['id']; ?>">Edit</a> /
                    <a href="delete_company.php?id=<?php echo $c['id']; ?>" onclick="return confirm('Are you sure?');">Delete</a>
                </td>
            </tr>
        <?php endforeach; ?>
    </table>

    <!-- Pagination links -->
    <div class="pagination" style="margin-top:15px;">
        <?php if($total_pages > 1): ?>
            <?php for($i = 1; $i <= $total_pages; $i++): ?>
                <a href="?page=<?php echo $i; ?>" class="<?php echo ($i == $page ? 'active' : ''); ?>"><?php echo $i; ?></a>
            <?php endfor; ?>
        <?php endif; ?>
    </div>

    <p><a href="dashboard.php">← Back to Dashboard</a></p>
</div>
</body>
</html>
