<?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';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $name = trim($_POST['name']);
    $description = trim($_POST['description']);
    $sort_order = (int)($_POST['sort_order'] ?? 0);
    $image_url = null;

    if (!empty($name) && !empty($description)) {
        try {
            // Handle image upload
            if (isset($_FILES['image']) && $_FILES['image']['error'] === UPLOAD_ERR_OK) {
                $uploadDir = '../public/assets/img/services/';
                if (!is_dir($uploadDir)) {
                    mkdir($uploadDir, 0777, true);
                }
                $filename = uniqid('service-') . '-' . basename($_FILES['image']['name']);
                $uploadFile = $uploadDir . $filename;

                if (move_uploaded_file($_FILES['image']['tmp_name'], $uploadFile)) {
                    $image_url = 'assets/img/services/' . $filename;
                }
            }

            $stmt = $pdo->prepare("INSERT INTO services (name, description, image_url, sort_order) VALUES (?, ?, ?, ?)");
            $stmt->execute([$name, $description, $image_url, $sort_order]);
            header('Location: services.php?success=add'); exit;
        } catch (\PDOException $e) {
            $error = $e->getMessage();
        }
    } else {
        $error = $isRTL ? 'يرجى تعبئة جميع الحقول المطلوبة.' : 'Please fill in all required fields.';
    }
}
$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['add_service_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['add_service_title']; ?></h1>
            <a href="services.php" class="btn btn-outline-secondary px-4 py-2"><?php echo $t['service_back_btn']; ?></a>
        </div>

        <?php if (isset($error)): ?>
            <div class="alert alert-danger fw-bold mb-4">⚠️ <?php echo htmlspecialchars($error); ?></div>
        <?php endif; ?>

        <div class="table-card p-4 p-md-5 mx-auto" style="max-width:800px;">
            <form method="POST" enctype="multipart/form-data">
                <div class="mb-3">
                    <label for="name" class="form-label"><?php echo $t['service_name']; ?> *</label>
                    <input type="text" id="name" name="name" class="form-control form-control-lg" required>
                </div>
                
                <div class="mb-3">
                    <label for="description" class="form-label"><?php echo $t['service_description']; ?> *</label>
                    <textarea id="description" name="description" class="form-control" rows="5" required></textarea>
                </div>

                <div class="mb-3" style="max-width: 200px;">
                    <label for="sort_order" class="form-label"><?php echo $isRTL ? 'ترتيب العرض' : 'Sort Order'; ?></label>
                    <input type="number" class="form-control" id="sort_order" name="sort_order" value="0">
                </div>

                <div class="mb-4">
                    <label for="image" class="form-label"><?php echo $t['service_image']; ?></label>
                    <input type="file" class="form-control" id="image" name="image" accept="image/*">
                    <small class="form-text text-muted"><?php echo $t['service_image_instruct']; ?></small>
                </div>
                
                <div class="d-flex gap-3 mt-4">
                    <button type="submit" class="btn btn-gold flex-grow-1 py-2 fw-bold fs-6">💾 <?php echo $t['service_save_btn']; ?></button>
                    <a href="services.php" class="btn btn-outline-secondary py-2 px-4"><?php echo $t['service_cancel_btn']; ?></a>
                </div>
            </form>
        </div>
    </div>

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