<?php
/**
 * لوحة تحكم المنشورات - نسخة مبسطة
 */

session_start();
require_once '../../config/database.php';

$database = new Database();
$conn = $database->getConnection();

// معالجة الحذف
if (isset($_GET['delete'])) {
    $id = intval($_GET['delete']);
    $stmt = $conn->prepare("DELETE FROM posts WHERE id = ?");
    $stmt->execute([$id]);
    header('Location: index.php?msg=deleted');
    exit;
}

// جلب المنشورات
$search = $_GET['search'] ?? '';
$where = ['1=1'];
$params = [];

if ($search) {
    $where[] = "(caption LIKE ? OR title LIKE ?)";
    $params[] = "%$search%";
    $params[] = "%$search%";
}

$where_clause = implode(' AND ', $where);

try {
    $stmt = $conn->prepare("SELECT * FROM posts WHERE $where_clause ORDER BY created_at DESC");
    $stmt->execute($params);
    $posts = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    // إحصائيات
    $total = count($posts);
} catch (Exception $e) {
    $posts = [];
    $total = 0;
    $error = $e->getMessage();
}
?>
<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>إدارة المنشورات</title>
    
    <link href="https://fonts.googleapis.com/css2?family=Tajawal:wght@400;500;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Tajawal', sans-serif;
        }
        
        body {
            background: #f5f7fa;
            padding: 20px;
        }
        
        .container {
            max-width: 1400px;
            margin: 0 auto;
        }
        
        .header {
            background: white;
            padding: 30px;
            border-radius: 15px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            margin-bottom: 30px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        .header h1 {
            font-size: 32px;
            color: #333;
        }
        
        .btn {
            padding: 12px 24px;
            border-radius: 8px;
            text-decoration: none;
            font-weight: 600;
            display: inline-flex;
            align-items: center;
            gap: 8px;
            transition: all 0.3s;
            border: none;
            cursor: pointer;
        }
        
        .btn-primary {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
        }
        
        .btn-primary:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
        }
        
        .btn-danger {
            background: #ef4444;
            color: white;
            font-size: 14px;
            padding: 8px 16px;
        }
        
        .btn-secondary {
            background: #6b7280;
            color: white;
        }
        
        .stats {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 20px;
            margin-bottom: 30px;
        }
        
        .stat-card {
            background: white;
            padding: 25px;
            border-radius: 15px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        
        .stat-card h3 {
            color: #666;
            font-size: 14px;
            margin-bottom: 10px;
        }
        
        .stat-card .number {
            font-size: 36px;
            font-weight: 700;
            color: #667eea;
        }
        
        .search-box {
            background: white;
            padding: 20px;
            border-radius: 15px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            margin-bottom: 30px;
        }
        
        .search-box input {
            width: 100%;
            padding: 12px 20px;
            border: 2px solid #e5e7eb;
            border-radius: 8px;
            font-size: 16px;
        }
        
        .search-box input:focus {
            outline: none;
            border-color: #667eea;
        }
        
        .posts-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
            gap: 25px;
        }
        
        .post-card {
            background: white;
            border-radius: 15px;
            overflow: hidden;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            transition: transform 0.3s, box-shadow 0.3s;
        }
        
        .post-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 5px 20px rgba(0,0,0,0.15);
        }
        
        .post-image {
            width: 100%;
            height: 200px;
            object-fit: cover;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 48px;
        }
        
        .post-content {
            padding: 20px;
        }
        
        .post-title {
            font-size: 18px;
            font-weight: 700;
            margin-bottom: 10px;
            color: #333;
        }
        
        .post-caption {
            color: #666;
            font-size: 14px;
            line-height: 1.6;
            margin-bottom: 15px;
        }
        
        .post-meta {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding-top: 15px;
            border-top: 1px solid #e5e7eb;
            color: #999;
            font-size: 13px;
        }
        
        .alert {
            padding: 15px 20px;
            border-radius: 8px;
            margin-bottom: 20px;
        }
        
        .alert-success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        
        .alert-error {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        
        .empty-state {
            background: white;
            padding: 60px 20px;
            border-radius: 15px;
            text-align: center;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        
        .empty-state i {
            font-size: 64px;
            color: #e5e7eb;
            margin-bottom: 20px;
        }
        
        .empty-state h3 {
            font-size: 24px;
            color: #333;
            margin-bottom: 10px;
        }
        
        .empty-state p {
            color: #666;
            margin-bottom: 20px;
        }
    </style>
</head>
<body>
    <div class="container">
        <!-- Header -->
        <div class="header">
            <div>
                <h1>📝 إدارة المنشورات</h1>
                <p style="color: #666; margin-top: 5px;">إجمالي المنشورات: <?php echo $total; ?></p>
            </div>
            <div style="display: flex; gap: 10px;">
                <a href="../dashboard.php" class="btn btn-secondary">
                    <i class="fas fa-arrow-right"></i> رجوع
                </a>
                <a href="create.php" class="btn btn-primary">
                    <i class="fas fa-plus"></i> منشور جديد
                </a>
            </div>
        </div>

        <!-- Messages -->
        <?php if (isset($_GET['msg'])): ?>
            <?php if ($_GET['msg'] == 'created'): ?>
                <div class="alert alert-success">
                    ✅ تم إضافة المنشور بنجاح
                </div>
            <?php elseif ($_GET['msg'] == 'deleted'): ?>
                <div class="alert alert-success">
                    ✅ تم حذف المنشور بنجاح
                </div>
            <?php endif; ?>
        <?php endif; ?>

        <?php if (isset($error)): ?>
            <div class="alert alert-error">
                ❌ خطأ: <?php echo $error; ?>
            </div>
        <?php endif; ?>

        <!-- Search -->
        <div class="search-box">
            <form method="GET">
                <input type="text" name="search" placeholder="🔍 ابحث في المنشورات..." value="<?php echo htmlspecialchars($search); ?>">
            </form>
        </div>

        <!-- Posts Grid -->
        <?php if (empty($posts)): ?>
            <div class="empty-state">
                <i class="fas fa-inbox"></i>
                <h3>لا توجد منشورات</h3>
                <p>ابدأ بإضافة أول منشور الآن!</p>
                <a href="create.php" class="btn btn-primary">
                    <i class="fas fa-plus"></i> إضافة منشور
                </a>
            </div>
        <?php else: ?>
            <div class="posts-grid">
                <?php foreach ($posts as $post): ?>
                    <div class="post-card">
                        <?php 
                        $image = $post['featured_image'] ?: $post['image'];
                        if (!empty($image)): 
                        ?>
                            <img src="../../<?php echo htmlspecialchars($image); ?>" class="post-image" alt="">
                        <?php else: ?>
                            <div class="post-image">📝</div>
                        <?php endif; ?>
                        
                        <div class="post-content">
                            <?php if (!empty($post['title'])): ?>
                                <div class="post-title"><?php echo htmlspecialchars($post['title']); ?></div>
                            <?php endif; ?>
                            
                            <div class="post-caption">
                                <?php echo htmlspecialchars(substr($post['caption'], 0, 150)); ?>
                                <?php echo strlen($post['caption']) > 150 ? '...' : ''; ?>
                            </div>
                            
                            <div class="post-meta">
                                <span>📅 <?php echo date('Y-m-d', strtotime($post['created_at'])); ?></span>
                                <a href="?delete=<?php echo $post['id']; ?>" 
                                   class="btn btn-danger"
                                   onclick="return confirm('هل أنت متأكد من الحذف؟')">
                                    <i class="fas fa-trash"></i>
                                </a>
                            </div>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
    </div>
</body>
</html>
