<?php
session_start();
if (!isset($_SESSION['admin_logged_in'])) { header('Location: login.php'); exit; }
require_once '../src/config/db.php';
require_once '../src/includes/functions.php';
require_once 'includes/lang.php';

$services = get_all_services($pdo);
$unreadMessagesCount = $pdo->query("SELECT COUNT(*) FROM contact_messages WHERE is_read = 0")->fetchColumn();
?>
<!DOCTYPE html>
<html lang="<?php echo $t['lang']; ?>" dir="<?php echo $t['dir']; ?>">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $t['services_title']; ?> - VAULTIXAI</title>
    <link rel="stylesheet" href="<?php echo $bootstrapCSS; ?>" crossorigin="anonymous">
    <link rel="stylesheet" href="assets/css/style.css">
</head>
<body>
    <?php
    $currentPage = 'services';
    include 'includes/sidebar.php';
    ?>

    <div class="main-content">
        <div class="topbar">
            <h1>⚙️ <?php echo $t['services_title']; ?></h1>
            <a href="add_service.php" class="btn btn-add px-4 py-2">➕ <?php echo $t['services_add_btn']; ?></a>
        </div>

        <?php if (isset($_GET['success'])): ?>
            <div class="alert alert-success fw-bold mb-4">✅ <?php switch ($_GET['success']) {
                case 'add': echo $t['success_added']; break;
                case 'edit': echo $t['success_updated']; break;
                case 'delete': echo $t['success_deleted']; break;
            } ?></div>
        <?php endif; ?>

        <div class="table-card">
            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0">
                    <thead>
                        <tr>
                            <th style="width:60px;"><?php echo $t['users_table_id']; ?></th>
                            <th style="width:80px;"><?php echo $isRTL ? 'الترتيب' : 'Order'; ?></th>
                            <th style="width:110px;"><?php echo $isRTL ? 'الصورة' : 'Image'; ?></th>
                            <th style="width:240px;"><?php echo $t['service_name']; ?></th>
                            <th><?php echo $t['service_description']; ?></th>
                            <th style="width:200px;text-align:center;"><?php echo $t['users_table_actions']; ?></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (empty($services)): ?>
                            <tr><td colspan="6" class="text-center py-5 text-muted">😶 <?php echo $isRTL ? 'لا توجد خدمات.' : 'No services found.'; ?></td></tr>
                        <?php else: ?>
                            <?php foreach ($services as $service): ?>
                            <tr>
                                <td class="text-muted small fw-bold">#<?php echo $service['id']; ?></td>
                                <td class="text-center fw-bold"><?php echo $service['sort_order']; ?></td>
                                <td>
                                    <?php if (!empty($service['image_url'])): ?>
                                        <img src="../public/<?php echo htmlspecialchars($service['image_url']); ?>" alt="<?php echo htmlspecialchars($service['name']); ?>" style="width:60px;height:36px;object-fit:cover;border-radius:6px;">
                                    <?php else: ?>
                                        <span class="text-muted small">-</span>
                                    <?php endif; ?>
                                </td>
                                <td class="fw-bold text-dark"><?php echo htmlspecialchars($service['name']); ?></td>
                                <td class="text-muted small"><?php echo htmlspecialchars(mb_strimwidth($service['description'], 0, 110, '…')); ?></td>
                                <td style="text-align:center;">
                                    <a href="edit_service.php?id=<?php echo $service['id']; ?>" class="btn btn-edit btn-sm px-3 py-1 me-1">✏️ <?php echo $t['service_edit_btn']; ?></a>
                                    <a href="delete_service.php?id=<?php echo $service['id']; ?>"
                                       class="btn btn-delete btn-sm px-3 py-1" data-confirm="<?php echo htmlspecialchars($t['service_delete_confirm']); ?>">
                                       🗑️ <?php echo $t['service_delete_btn']; ?></a>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script>
    <script src="assets/js/main.js"></script>
</body>
</html>
