<?php
session_start();

// التحقق من تسجيل الدخول
if (!isset($_SESSION['user_id'])) {
    header("Location: index.php");
    exit();
}

require_once 'db_connect.php';

// إضافة وحدة جديدة
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['add_unit'])) {
    $name = $_POST['name'];

    $stmt = $conn->prepare("INSERT INTO units (name) VALUES (:name)");
    $stmt->execute([':name' => $name]);
    $success_message = "تم إضافة الوحدة بنجاح!";
}

// تعديل وحدة
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['edit_unit'])) {
    $id = $_POST['id'];
    $name = $_POST['name'];

    $stmt = $conn->prepare("UPDATE units SET name = :name WHERE id = :id");
    $stmt->execute([':id' => $id, ':name' => $name]);
    $success_message = "تم تعديل الوحدة بنجاح!";
}

// حذف وحدة
if (isset($_GET['delete_id'])) {
    $id = $_GET['delete_id'];
    $stmt = $conn->prepare("DELETE FROM units WHERE id = :id");
    $stmt->execute([':id' => $id]);
    $success_message = "تم حذف الوحدة بنجاح!";
}
?>

<!DOCTYPE html>
<html lang="ar">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Astra - الوحدات</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        body {
            margin: 0;
            padding: 0;
            min-height: 100vh;
            direction: rtl;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #3498DB, #F1C40F, #2ECC71);
            overflow-x: hidden;
            animation: gradientShift 10s ease infinite;
            background-size: 400% 400%;
        }

        @keyframes gradientShift {
            0% { background-position: 0% 50%; }
            50% { background-position: 100% 50%; }
            100% { background-position: 0% 50%; }
        }

        .container-fluid {
            padding: 40px;
            height: 100vh;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
        }

        .card {
            background: rgba(255, 255, 255, 0.95);
            border-radius: 20px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
            padding: 20px;
            margin-bottom: 20px;
            flex-grow: 1;
            animation: fadeIn 1s ease-in-out;
        }

        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(-20px); }
            to { opacity: 1; transform: translateY(0); }
        }

        h2 {
            color: #3498DB;
            font-weight: bold;
            text-align: center;
            margin-bottom: 30px;
            text-shadow: 0 0 5px rgba(52, 152, 219, 0.5);
        }

        .form-label {
            color: #F1C40F;
            font-weight: bold;
            margin-bottom: 10px;
            display: flex;
            align-items: center;
        }

        .form-label i {
            margin-left: 8px;
            color: #2ECC71;
        }

        .form-control {
            border-radius: 15px;
            border: 2px solid #3498DB;
            padding: 12px 20px;
            background: rgba(255, 255, 255, 0.8);
            transition: all 0.3s ease;
            box-shadow: inset 0 2px 5px rgba(0, 0, 0, 0.1);
            width: 100%;
        }

        .form-control:focus {
            border-color: #2ECC71;
            box-shadow: 0 0 10px rgba(46, 204, 113, 0.5), inset 0 2px 5px rgba(0, 0, 0, 0.1);
            background: #fff;
        }

        .btn {
            border-radius: 15px;
            padding: 12px 30px;
            font-weight: bold;
            transition: all 0.4s ease;
            position: relative;
            overflow: hidden;
            width: 100%;
        }

        .btn-add {
            background: linear-gradient(45deg, #2ECC71, #27AE60);
            border: none;
            color: #fff;
        }

        .btn-add:hover {
            background: linear-gradient(45deg, #27AE60, #219653);
            transform: translateY(-3px);
            box-shadow: 0 5px 15px rgba(46, 204, 113, 0.5);
        }

        .btn-cancel {
            background: linear-gradient(45deg, #E74C3C, #C0392B);
            border: none;
            color: #fff;
        }

        .btn-cancel:hover {
            background: linear-gradient(45deg, #C0392B, #A93226);
            transform: translateY(-3px);
            box-shadow: 0 5px 15px rgba(231, 76, 60, 0.5);
        }

        .btn-save {
            background: linear-gradient(45deg, #3498DB, #2980B9);
            border: none;
            color: #fff;
        }

        .btn-save:hover {
            background: linear-gradient(45deg, #2980B9, #2471A3);
            transform: translateY(-3px);
            box-shadow: 0 5px 15px rgba(52, 152, 219, 0.5);
        }

        .btn-edit {
            background: #F1C40F;
            border: none;
            color: #fff;
        }

        .btn-edit:hover {
            background: #D4AC0D;
            transform: translateY(-2px);
        }

        .btn-delete {
            background: #E74C3C;
            border: none;
            color: #fff;
        }

        .btn-delete:hover {
            background: #C0392B;
            transform: translateY(-2px);
        }

        .btn i {
            margin-left: 8px;
        }

        .success-message {
            color: #2ECC71;
            font-weight: bold;
            text-align: center;
            margin-bottom: 20px;
            animation: fadeIn 0.5s ease-in-out;
        }

        .table {
            background: #fff;
            border-radius: 10px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
            width: 100%;
        }

        .table thead {
            background: #3498DB;
            color: #fff;
        }

        .table tbody tr:hover {
            background: rgba(52, 152, 219, 0.1);
        }

        .modal-content {
            border-radius: 15px;
            background: rgba(255, 255, 255, 0.95);
        }

        .modal-header {
            background: #3498DB;
            color: #fff;
            border-radius: 15px 15px 0 0;
        }
    </style>
</head>
<body>
    <div class="container-fluid">
        <div class="card">
            <h2><i class="fas fa-ruler me-2"></i> إضافة وحدة جديدة</h2>
            <?php if (isset($success_message)): ?>
                <div class="success-message"><?php echo $success_message; ?></div>
            <?php endif; ?>
            <form method="POST">
                <div class="row g-4">
                    <div class="col-md-6 mb-3">
                        <label class="form-label"><i class="fas fa-ruler-horizontal"></i> اسم الوحدة</label>
                        <input type="text" class="form-control" name="name" placeholder="أدخل اسم الوحدة" required>
                    </div>
                    <div class="col-md-6 mb-3">
                        <button type="submit" class="btn btn-add" name="add_unit"><i class="fas fa-plus"></i> إضافة الوحدة</button>
                    </div>
                    <div class="col-md-6 mb-3">
                        <button type="button" class="btn btn-cancel" onclick="window.location.href='dashboard.php'"><i class="fas fa-times"></i> إلغاء</button>
                    </div>
                </div>
            </form>
        </div>

        <div class="card">
            <h2><i class="fas fa-table me-2"></i> قائمة الوحدات</h2>
            <div class="table-responsive" style="max-height: 50vh; overflow-y: auto;">
                <table class="table table-striped">
                    <thead>
                        <tr>
                            <th>اسم الوحدة</th>
                            <th>إجراءات</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $units = $conn->query("SELECT * FROM units")->fetchAll(PDO::FETCH_ASSOC);
                        foreach ($units as $unit):
                        ?>
                            <tr>
                                <td><?php echo htmlspecialchars($unit['name']); ?></td>
                                <td>
                                    <button class="btn btn-edit btn-sm me-1" onclick="editUnit(<?php echo $unit['id']; ?>, '<?php echo htmlspecialchars($unit['name']); ?>')"><i class="fas fa-edit"></i></button>
                                    <button class="btn btn-delete btn-sm" onclick="deleteUnit(<?php echo $unit['id']; ?>)"><i class="fas fa-trash"></i></button>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <!-- Modal لتعديل الوحدة -->
    <div class="modal fade" id="editUnitModal" tabindex="-1" aria-labelledby="editUnitModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="editUnitModalLabel"><i class="fas fa-edit me-2"></i> تعديل الوحدة</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <form method="POST" id="editUnitForm">
                        <input type="hidden" name="id" id="edit_id">
                        <div class="row g-4">
                            <div class="col-md-6 mb-3">
                                <label class="form-label"><i class="fas fa-ruler-horizontal"></i> اسم الوحدة</label>
                                <input type="text" class="form-control" name="name" id="edit_name" required>
                            </div>
                            <div class="col-md-6 mb-3">
                                <button type="submit" class="btn btn-save" name="edit_unit"><i class="fas fa-save"></i> حفظ</button>
                            </div>
                            <div class="col-md-6 mb-3">
                                <button type="button" class="btn btn-cancel" data-bs-dismiss="modal"><i class="fas fa-times"></i> إلغاء</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        // تعديل وحدة
        function editUnit(id, name) {
            document.getElementById('edit_id').value = id;
            document.getElementById('edit_name').value = name;

            const modal = new bootstrap.Modal(document.getElementById('editUnitModal'));
            modal.show();
        }

        // حذف وحدة
        function deleteUnit(id) {
            if (confirm('هل أنت متأكد من حذف هذه الوحدة؟')) {
                window.location.href = 'units.php?delete_id=' + id;
            }
        }
    </script>
</body>
</html>