<?php
// Enable error reporting for debugging
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('log_errors', 1);

// Set proper encoding for Persian characters
header('Content-Type: text/html; charset=utf-8');
session_start();
require_once '../config/database.php';

// Check if admin is logged in
if (!isset($_SESSION['admin_id'])) {
    header('Location: login.php');
    exit;
}

// Handle actions
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $action = $_POST['action'] ?? '';
    
    if ($action === 'add_category') {
        try {
            $name = trim($_POST['name'] ?? '');
            $description = trim($_POST['description'] ?? '');
            $isFreeCoinsCategory = isset($_POST['is_free_coins_category']) ? 1 : 0;
            $priority = (int)($_POST['priority'] ?? 0);
            
            if (empty($name)) {
                $error = 'نام دسته‌بندی الزامی است';
            } else {
                // Check if category already exists
                $stmt = $pdo->prepare("SELECT id FROM categories WHERE name = ?");
                $stmt->execute([$name]);
                if ($stmt->fetch()) {
                    $error = 'این دسته‌بندی قبلاً وجود دارد';
                } else {
                    $stmt = $pdo->prepare("INSERT INTO categories (name, description, is_free_coins_category, priority) VALUES (?, ?, ?, ?)");
                    if ($stmt->execute([$name, $description, $isFreeCoinsCategory, $priority])) {
                        $success = "دسته‌بندی با موفقیت اضافه شد";
                    } else {
                        $error = "خطا در اضافه کردن دسته‌بندی";
                    }
                }
            }
        } catch (Exception $e) {
            error_log("Error adding category: " . $e->getMessage());
            $error = "خطا در پردازش درخواست: " . $e->getMessage();
        }
    } elseif ($action === 'edit_category') {
        try {
            $categoryId = (int)($_POST['category_id'] ?? 0);
            $name = trim($_POST['name'] ?? '');
            $description = trim($_POST['description'] ?? '');
            $isActive = isset($_POST['is_active']) ? 1 : 0;
            $isFreeCoinsCategory = isset($_POST['is_free_coins_category']) ? 1 : 0;
            $priority = (int)($_POST['priority'] ?? 0);
            
            if ($categoryId <= 0 || empty($name)) {
                $error = 'اطلاعات دسته‌بندی نامعتبر است';
            } else {
                // Check if category name already exists (excluding current category)
                $stmt = $pdo->prepare("SELECT id FROM categories WHERE name = ? AND id != ?");
                $stmt->execute([$name, $categoryId]);
                if ($stmt->fetch()) {
                    $error = 'این نام دسته‌بندی قبلاً وجود دارد';
                } else {
                    $stmt = $pdo->prepare("UPDATE categories SET name = ?, description = ?, is_active = ?, is_free_coins_category = ?, priority = ? WHERE id = ?");
                    if ($stmt->execute([$name, $description, $isActive, $isFreeCoinsCategory, $priority, $categoryId])) {
                        $success = "دسته‌بندی با موفقیت ویرایش شد";
                    } else {
                        $error = "خطا در ویرایش دسته‌بندی";
                    }
                }
            }
        } catch (Exception $e) {
            error_log("Error editing category: " . $e->getMessage());
            $error = "خطا در پردازش درخواست: " . $e->getMessage();
        }
    } elseif ($action === 'delete_category') {
        try {
            $categoryId = (int)($_POST['category_id'] ?? 0);
            
            if ($categoryId > 0) {
                // Check if category is used in products
                $stmt = $pdo->prepare("SELECT COUNT(*) FROM products WHERE category = (SELECT name FROM categories WHERE id = ?)");
                $stmt->execute([$categoryId]);
                $productCount = $stmt->fetchColumn();
                
                if ($productCount > 0) {
                    $error = "این دسته‌بندی در $productCount محصول استفاده شده و قابل حذف نیست";
                } else {
                    $stmt = $pdo->prepare("DELETE FROM categories WHERE id = ?");
                    if ($stmt->execute([$categoryId])) {
                        $success = "دسته‌بندی با موفقیت حذف شد";
                    } else {
                        $error = "خطا در حذف دسته‌بندی";
                    }
                }
            }
        } catch (Exception $e) {
            error_log("Error deleting category: " . $e->getMessage());
            $error = "خطا در پردازش درخواست: " . $e->getMessage();
        }
    }
}

// Check if categories table exists
try {
    $stmt = $pdo->query("SELECT COUNT(*) FROM categories");
    $totalCategories = $stmt->fetchColumn();
} catch (PDOException $e) {
    // If table doesn't exist, create it
    try {
        $pdo->exec("CREATE TABLE IF NOT EXISTS categories (
            id INT AUTO_INCREMENT PRIMARY KEY,
            name VARCHAR(255) NOT NULL UNIQUE,
            description TEXT,
            is_active TINYINT(1) DEFAULT 1,
            is_free_coins_category TINYINT(1) DEFAULT 0,
            priority INT DEFAULT 0,
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
        )");
        $totalCategories = 0;
    } catch (Exception $createError) {
        error_log("Error creating categories table: " . $createError->getMessage());
        $error = "خطا در ایجاد جدول دسته‌بندی‌ها";
        $totalCategories = 0;
    }
}

// Get categories with pagination
$page = max(1, (int)($_GET['page'] ?? 1));
$limit = 20;
$offset = ($page - 1) * $limit;
$totalPages = ceil($totalCategories / $limit);

// Use direct query instead of prepared statement for LIMIT/OFFSET
try {
    $query = "SELECT c.*, 
        (SELECT COUNT(*) FROM products WHERE category = c.name) as product_count 
        FROM categories c 
        ORDER BY c.created_at DESC 
        LIMIT " . (int)$limit . " OFFSET " . (int)$offset;

    $stmt = $pdo->query($query);
    $categories = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    error_log("Error loading categories: " . $e->getMessage());
    $categories = [];
    $error = "خطا در بارگذاری دسته‌بندی‌ها: " . $e->getMessage();
}
?>

<!DOCTYPE html>
<html lang="fa" dir="rtl">
<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.rtl.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Vazirmatn:wght@400;600;700&display=swap" rel="stylesheet">
    <link href="assets/css/admin.css" rel="stylesheet">
    <style>
        .category-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            border-radius: 16px;
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
            border: 1px solid rgba(255, 255, 255, 0.2);
            transition: all 0.3s ease;
        }
        
        .category-card:hover {
            transform: translateY(-4px);
            box-shadow: 0 12px 40px rgba(0, 0, 0, 0.15);
        }
        

        
        .table-container {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            border-radius: 16px;
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
            overflow: hidden;
        }
        
        .table th {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            font-weight: 600;
            padding: 16px;
        }
        
        .table td {
            padding: 16px;
            border-bottom: 1px solid rgba(0, 0, 0, 0.05);
            vertical-align: middle;
        }
        
        .table tbody tr:hover {
            background: rgba(102, 126, 234, 0.05);
        }
        
        .btn-action {
            border-radius: 8px;
            padding: 8px 16px;
            margin: 0 4px;
            transition: all 0.3s ease;
        }
        
        .btn-action:hover {
            transform: scale(1.05);
        }
        
        .badge-custom {
            border-radius: 20px;
            padding: 6px 12px;
            font-size: 12px;
            font-weight: 600;
        }
        
        .modal-content {
            border-radius: 16px;
            border: none;
            box-shadow: 0 20px 60px rgba(0, 0, 0, 0.2);
        }
        
        .modal-header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border-radius: 16px 16px 0 0;
            border: none;
        }
        
        .form-control, .form-select {
            border-radius: 8px;
            border: 1px solid rgba(0, 0, 0, 0.1);
            padding: 12px 16px;
            transition: all 0.3s ease;
        }
        
        .form-control:focus, .form-select:focus {
            border-color: #667eea;
            box-shadow: 0 0 0 0.2rem rgba(102, 126, 234, 0.25);
        }
        
        .pagination .page-link {
            border-radius: 8px;
            margin: 0 2px;
            border: none;
            color: #667eea;
            transition: all 0.3s ease;
        }
        
        .pagination .page-link:hover {
            background: #667eea;
            color: white;
            transform: translateY(-2px);
        }
        
        .pagination .page-item.active .page-link {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border: none;
        }
    </style>
</head>
<body class="admin-app">
    <?php include 'includes/menu.php'; ?>
    <?php include 'includes/header_bar.php'; ?>

    <main class="main">
        <div class="main-content">
                <?php
                $admin_page_title = 'مدیریت دسته‌بندی‌ها';
                $admin_page_icon = 'fas fa-tags';
                $admin_page_actions = '<span class="badge rounded-pill admin-badge-soft me-2">' . number_format($totalCategories) . ' دسته‌بندی</span>'
                    . '<button type="button" class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#addCategoryModal"><i class="fas fa-plus ms-1"></i>افزودن دسته‌بندی</button>';
                include 'includes/topbar.php';
                ?>

                    <!-- Messages -->
                    <?php if (isset($success)): ?>
                        <div class="alert alert-success alert-dismissible fade show" role="alert">
                            <i class="fas fa-check-circle me-2"></i>
                            <?php echo htmlspecialchars($success); ?>
                            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                        </div>
                    <?php endif; ?>

                    <?php if (isset($error)): ?>
                        <div class="alert alert-danger alert-dismissible fade show" role="alert">
                            <i class="fas fa-exclamation-triangle me-2"></i>
                            <?php echo htmlspecialchars($error); ?>
                            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                        </div>
                    <?php endif; ?>

                    <!-- Stats Cards -->
                    <div class="row mb-4">
                        <div class="col-md-3">
                            <div class="card stat-card info">
                                <div class="card-body text-center">
                                    <i class="fas fa-tags fa-2x mb-2"></i>
                                    <h4><?= number_format($totalCategories) ?></h4>
                                    <p class="mb-0">کل دسته‌بندی‌ها</p>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="card stat-card success">
                                <div class="card-body text-center">
                                    <i class="fas fa-check-circle fa-2x mb-2"></i>
                                    <h4><?= number_format(array_reduce($categories, function($carry, $item) { return $carry + ($item['is_active'] ? 1 : 0); }, 0)) ?></h4>
                                    <p class="mb-0">دسته‌بندی‌های فعال</p>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="card stat-card warning">
                                <div class="card-body text-center">
                                    <i class="fas fa-box fa-2x mb-2"></i>
                                    <h4><?= number_format(array_reduce($categories, function($carry, $item) { return $carry + $item['product_count']; }, 0)) ?></h4>
                                    <p class="mb-0">کل محصولات</p>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="card stat-card">
                                <div class="card-body text-center">
                                    <i class="fas fa-gift fa-2x mb-2"></i>
                                    <h4><?= number_format(array_reduce($categories, function($carry, $item) { return $carry + ($item['is_free_coins_category'] ? 1 : 0); }, 0)) ?></h4>
                                    <p class="mb-0">دسته‌بندی‌های رایگان</p>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Categories Table -->
                    <div class="table-container">
                        <?php if (empty($categories)): ?>
                            <div class="text-center py-5">
                                <i class="fas fa-tags fa-3x text-muted mb-3"></i>
                                <h5 class="text-muted">هیچ دسته‌بندی‌ای یافت نشد</h5>
                                <p class="text-muted">برای شروع، اولین دسته‌بندی را اضافه کنید</p>
                                <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addCategoryModal">
                                    <i class="fas fa-plus me-2"></i>افزودن دسته‌بندی
                                </button>
                            </div>
                        <?php else: ?>
                            <div class="table-responsive">
                                <table class="table table-hover mb-0">
                        <thead>
                            <tr>
                                <th>#</th>
                                <th>نام دسته‌بندی</th>
                                <th>توضیحات</th>
                                <th>اولویت</th>
                                <th>تعداد محصولات</th>
                                <th>نوع سکه</th>
                                <th>وضعیت</th>
                                <th>تاریخ ایجاد</th>
                                <th>عملیات</th>
                            </tr>
                        </thead>
                                                        <tbody>
                                    <?php foreach ($categories as $category): ?>
                                        <tr>
                                            <td>
                                                <div class="d-flex align-items-center">
                                                    <div class="bg-primary text-white rounded-circle d-flex align-items-center justify-content-center" style="width: 40px; height: 40px;">
                                                        <i class="fas fa-tag"></i>
                                                    </div>
                                                    <span class="ms-3 fw-bold"><?php echo htmlspecialchars($category['id']); ?></span>
                                                </div>
                                            </td>
                                            <td>
                                                <div>
                                                    <h6 class="mb-1 fw-bold"><?php echo htmlspecialchars($category['name']); ?></h6>
                                                    <?php if ($category['description']): ?>
                                                        <small class="text-muted"><?php echo htmlspecialchars($category['description']); ?></small>
                                                    <?php endif; ?>
                                                </div>
                                            </td>
                                            <td>
                                                <span class="badge-custom bg-secondary text-white">
                                                    <i class="fas fa-sort-numeric-up me-1"></i><?php echo $category['priority'] ?? 0; ?>
                                                </span>
                                            </td>
                                            <td>
                                                <span class="badge-custom bg-info text-white">
                                                    <i class="fas fa-box me-1"></i><?php echo $category['product_count']; ?> محصول
                                                </span>
                                            </td>
                                            <td>
                                                <?php if ($category['is_free_coins_category']): ?>
                                                    <span class="badge-custom bg-warning text-white">
                                                        <i class="fas fa-gift me-1"></i>سکه‌های رایگان
                                                    </span>
                                                <?php else: ?>
                                                    <span class="badge-custom bg-primary text-white">
                                                        <i class="fas fa-coins me-1"></i>سکه‌های معمولی
                                                    </span>
                                                <?php endif; ?>
                                            </td>
                                            <td>
                                                <?php if ($category['is_active']): ?>
                                                    <span class="badge-custom bg-success text-white">
                                                        <i class="fas fa-check me-1"></i>فعال
                                                    </span>
                                                <?php else: ?>
                                                    <span class="badge-custom bg-secondary text-white">
                                                        <i class="fas fa-times me-1"></i>غیرفعال
                                                    </span>
                                                <?php endif; ?>
                                            </td>
                                            <td>
                                                <div class="text-muted">
                                                    <i class="fas fa-calendar me-1"></i>
                                                    <?php echo date('Y/m/d', strtotime($category['created_at'])); ?>
                                                </div>
                                            </td>
                                            <td>
                                                <div class="btn-group" role="group">
                                                    <button class="btn btn-action btn-outline-primary" 
                                                            onclick="editCategory(<?php echo htmlspecialchars(json_encode($category)); ?>)"
                                                            title="ویرایش">
                                                        <i class="fas fa-edit"></i>
                                                    </button>
                                                    <?php if ($category['product_count'] == 0): ?>
                                                        <button class="btn btn-action btn-outline-danger" 
                                                                onclick="deleteCategory(<?php echo $category['id']; ?>, '<?php echo htmlspecialchars($category['name']); ?>')"
                                                                title="حذف">
                                                            <i class="fas fa-trash"></i>
                                                        </button>
                                                    <?php endif; ?>
                                                </div>
                                            </td>
                                        </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                        <?php endif; ?>
                    </div>

                <!-- Pagination -->
                <?php if ($totalPages > 1): ?>
                    <div class="d-flex justify-content-center mt-4">
                        <nav aria-label="صفحه‌بندی">
                            <ul class="pagination">
                                <?php if ($page > 1): ?>
                                    <li class="page-item">
                                        <a class="page-link" href="?page=<?php echo $page - 1; ?>">
                                            <i class="fas fa-chevron-right"></i>
                                        </a>
                                    </li>
                                <?php endif; ?>
                                
                                <?php for ($i = max(1, $page - 2); $i <= min($totalPages, $page + 2); $i++): ?>
                                    <li class="page-item <?php echo $i == $page ? 'active' : ''; ?>">
                                        <a class="page-link" href="?page=<?php echo $i; ?>"><?php echo $i; ?></a>
                                    </li>
                                <?php endfor; ?>
                                
                                <?php if ($page < $totalPages): ?>
                                    <li class="page-item">
                                        <a class="page-link" href="?page=<?php echo $page + 1; ?>">
                                            <i class="fas fa-chevron-left"></i>
                                        </a>
                                    </li>
                                <?php endif; ?>
                            </ul>
                        </nav>
                    </div>
                <?php endif; ?>
        </div>
    </main>

    <!-- Add Category Modal -->
    <div class="modal fade" id="addCategoryModal" tabindex="-1">
        <div class="modal-dialog modal-dialog-centered">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">
                        <i class="fas fa-plus me-2"></i>افزودن دسته‌بندی جدید
                    </h5>
                    <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
                </div>
                <form method="POST">
                    <div class="modal-body">
                        <input type="hidden" name="action" value="add_category">
                        
                        <div class="mb-3">
                            <label for="name" class="form-label fw-bold">
                                <i class="fas fa-tag me-2"></i>نام دسته‌بندی *
                            </label>
                            <input type="text" class="form-control" id="name" name="name" 
                                   placeholder="مثال: الکترونیک" required>
                        </div>
                        
                        <div class="mb-3">
                            <label for="description" class="form-label fw-bold">
                                <i class="fas fa-align-left me-2"></i>توضیحات
                            </label>
                            <textarea class="form-control" id="description" name="description" 
                                      rows="3" placeholder="توضیحات دسته‌بندی (اختیاری)"></textarea>
                        </div>
                        
                        <div class="mb-3">
                            <label for="priority" class="form-label fw-bold">
                                <i class="fas fa-sort-numeric-up me-2"></i>اولویت نمایش
                            </label>
                            <input type="number" class="form-control" id="priority" name="priority" 
                                   placeholder="0" value="0" min="0">
                            <small class="form-text text-muted">
                                عدد بالاتر = اولویت بیشتر در نمایش. دسته‌بندی با اولویت بالاتر اول نمایش داده می‌شود
                            </small>
                        </div>
                        
                        <div class="mb-3">
                            <div class="form-check">
                                <input class="form-check-input" type="checkbox" id="is_free_coins_category" name="is_free_coins_category">
                                <label class="form-check-label fw-bold" for="is_free_coins_category">
                                    <i class="fas fa-gift me-2"></i>دسته‌بندی سکه‌های رایگان
                                </label>
                                <small class="form-text text-muted d-block mt-1">
                                    محصولات این دسته‌بندی فقط با سکه‌های رایگان قابل خرید هستند
                                </small>
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
                            <i class="fas fa-times me-2"></i>انصراف
                        </button>
                        <button type="submit" class="btn btn-primary">
                            <i class="fas fa-plus me-2"></i>افزودن
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <!-- Edit Category Modal -->
    <div class="modal fade" id="editCategoryModal" tabindex="-1">
        <div class="modal-dialog modal-dialog-centered">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">
                        <i class="fas fa-edit me-2"></i>ویرایش دسته‌بندی
                    </h5>
                    <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
                </div>
                <form method="POST">
                    <div class="modal-body">
                        <input type="hidden" name="action" value="edit_category">
                        <input type="hidden" name="category_id" id="edit_category_id">
                        
                        <div class="mb-3">
                            <label for="edit_name" class="form-label fw-bold">
                                <i class="fas fa-tag me-2"></i>نام دسته‌بندی *
                            </label>
                            <input type="text" class="form-control" id="edit_name" name="name" required>
                        </div>
                        
                        <div class="mb-3">
                            <label for="edit_description" class="form-label fw-bold">
                                <i class="fas fa-align-left me-2"></i>توضیحات
                            </label>
                            <textarea class="form-control" id="edit_description" name="description" rows="3"></textarea>
                        </div>
                        
                        <div class="mb-3">
                            <label for="edit_priority" class="form-label fw-bold">
                                <i class="fas fa-sort-numeric-up me-2"></i>اولویت نمایش
                            </label>
                            <input type="number" class="form-control" id="edit_priority" name="priority" 
                                   placeholder="0" min="0">
                            <small class="form-text text-muted">
                                عدد بالاتر = اولویت بیشتر در نمایش. دسته‌بندی با اولویت بالاتر اول نمایش داده می‌شود
                            </small>
                        </div>
                        
                        <div class="mb-3">
                            <div class="form-check">
                                <input class="form-check-input" type="checkbox" id="edit_is_active" name="is_active">
                                <label class="form-check-label fw-bold" for="edit_is_active">
                                    <i class="fas fa-check-circle me-2"></i>فعال
                                </label>
                            </div>
                        </div>
                        
                        <div class="mb-3">
                            <div class="form-check">
                                <input class="form-check-input" type="checkbox" id="edit_is_free_coins_category" name="is_free_coins_category">
                                <label class="form-check-label fw-bold" for="edit_is_free_coins_category">
                                    <i class="fas fa-gift me-2"></i>دسته‌بندی سکه‌های رایگان
                                </label>
                                <small class="form-text text-muted d-block mt-1">
                                    محصولات این دسته‌بندی فقط با سکه‌های رایگان قابل خرید هستند
                                </small>
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
                            <i class="fas fa-times me-2"></i>انصراف
                        </button>
                        <button type="submit" class="btn btn-primary">
                            <i class="fas fa-save me-2"></i>ذخیره تغییرات
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <!-- Delete Category Modal -->
    <div class="modal fade" id="deleteCategoryModal" tabindex="-1">
        <div class="modal-dialog modal-dialog-centered">
            <div class="modal-content">
                <div class="modal-header bg-danger text-white">
                    <h5 class="modal-title">
                        <i class="fas fa-exclamation-triangle me-2"></i>حذف دسته‌بندی
                    </h5>
                    <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body text-center py-4">
                    <div class="mb-3">
                        <i class="fas fa-trash-alt fa-3x text-danger"></i>
                    </div>
                    <h6 class="mb-3">آیا مطمئن هستید که می‌خواهید دسته‌بندی زیر را حذف کنید؟</h6>
                    <div class="alert alert-warning">
                        <strong id="delete_category_name"></strong>
                    </div>
                    <p class="text-danger mb-0">
                        <i class="fas fa-exclamation-circle me-2"></i>
                        این عملیات قابل بازگشت نیست!
                    </p>
                </div>
                <form method="POST">
                    <input type="hidden" name="action" value="delete_category">
                    <input type="hidden" name="category_id" id="delete_category_id">
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
                            <i class="fas fa-times me-2"></i>انصراف
                        </button>
                        <button type="submit" class="btn btn-danger">
                            <i class="fas fa-trash me-2"></i>حذف
                        </button>
                    </div>
                </form>
            </div>
        </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <?php include 'includes/admin_shell_scripts.php'; ?>
    <script>
        function editCategory(category) {
            document.getElementById('edit_category_id').value = category.id;
            document.getElementById('edit_name').value = category.name;
            document.getElementById('edit_description').value = category.description || '';
            document.getElementById('edit_priority').value = category.priority || 0;
            document.getElementById('edit_is_active').checked = category.is_active == 1;
            document.getElementById('edit_is_free_coins_category').checked = category.is_free_coins_category == 1;
            
            new bootstrap.Modal(document.getElementById('editCategoryModal')).show();
        }

        function deleteCategory(categoryId, categoryName) {
            document.getElementById('delete_category_id').value = categoryId;
            document.getElementById('delete_category_name').textContent = categoryName;
            
            new bootstrap.Modal(document.getElementById('deleteCategoryModal')).show();
        }
    </script>
</body>
</html>
