<?php
// add_blog_post.php - Add Blog Post
// File location: /bidwheels.net/admin/add_blog_post.php

require_once '../includes/auth.php';
require_once '../includes/functions.php';
require_once 'includes/admin_header.php';

requireAdmin();

$error = '';
$success = '';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $title = sanitize($_POST['title']);
    $content = $_POST['content'];
    $category = sanitize($_POST['category']);
    $tags = sanitize($_POST['tags']);
    
    if (empty($title) || empty($content)) {
        $error = 'Title and content are required.';
    } else {
        $featured_image = '';
        
        // Handle featured image upload
        if (isset($_FILES['featured_image']) && $_FILES['featured_image']['error'] == 0) {
            $upload_dir = $_SERVER['DOCUMENT_ROOT'] . '/bidwheels/uploads/blog/';
            if (!file_exists($upload_dir)) {
                mkdir($upload_dir, 0777, true);
            }
            
            $ext = pathinfo($_FILES['featured_image']['name'], PATHINFO_EXTENSION);
            $filename = 'blog_' . time() . '_' . rand(1000, 9999) . '.' . $ext;
            $filepath = $upload_dir . $filename;
            
            if (move_uploaded_file($_FILES['featured_image']['tmp_name'], $filepath)) {
                $featured_image = $filename;
            }
        }
        
        $post_id = insert('blog_posts', [
            'title' => $title,
            'content' => $content,
            'featured_image' => $featured_image,
            'category' => $category,
            'tags' => $tags,
            'views' => 0
        ]);
        
        if ($post_id) {
            $success = 'Blog post published successfully!';
        } else {
            $error = 'Failed to save post.';
        }
    }
}
?>

<div class="admin-page">
    <h1 class="page-title">Add Blog Post</h1>
    
    <?php if ($success): ?>
        <div class="alert alert-success"><?php echo $success; ?> <a href="../blog/post.php?id=<?php echo $post_id; ?>">View Post</a></div>
    <?php endif; ?>
    
    <?php if ($error): ?>
        <div class="alert alert-danger"><?php echo $error; ?></div>
    <?php endif; ?>
    
    <form method="POST" action="" enctype="multipart/form-data" class="admin-form">
        <div class="form-group">
            <label>Post Title *</label>
            <input type="text" name="title" class="form-control" required>
        </div>
        
        <div class="form-row">
            <div class="form-group">
                <label>Category</label>
                <input type="text" name="category" class="form-control" placeholder="e.g., Car Reviews, Buying Tips">
            </div>
            <div class="form-group">
                <label>Tags (comma separated)</label>
                <input type="text" name="tags" class="form-control" placeholder="cars, auctions, tips">
            </div>
        </div>
        
        <div class="form-group">
            <label>Featured Image</label>
            <input type="file" name="featured_image" accept="image/*" class="form-control">
        </div>
        
        <div class="form-group">
            <label>Post Content *</label>
            <textarea name="content" rows="15" class="form-control" id="editor" required></textarea>
        </div>
        
        <div class="form-actions">
            <button type="submit" class="btn btn-primary">Publish Post</button>
            <a href="blog_posts.php" class="btn btn-secondary">Cancel</a>
        </div>
    </form>
</div>

<style>
.form-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 20px;
    margin-bottom: 20px;
}
#editor {
    font-family: monospace;
    font-size: 14px;
}
</style>

<?php require_once 'includes/admin_footer.php'; ?>