<?php 
ini_set('display_errors', 1);
error_reporting(E_ALL);

require_once($_SERVER['DOCUMENT_ROOT'] . '/hethong/ketnoi.php'); 

// Xử lý Thêm tin
if (isset($_POST['add_news'])) {
    $title = mysqli_real_escape_string($ketnoi, $_POST['title']);
    $category = mysqli_real_escape_string($ketnoi, $_POST['category']);
    $image = mysqli_real_escape_string($ketnoi, $_POST['image']);
    $content = mysqli_real_escape_string($ketnoi, $_POST['content']);
    
    if(empty($image)) { $image = 'assets/img/default-news.png'; }

    if (!empty($title) && !empty($content)) {
        $insert = mysqli_query($ketnoi, "INSERT INTO `news` (`title`, `category`, `image`, `content`, `created_at`) VALUES ('$title', '$category', '$image', '$content', NOW())");
        if ($insert) {
            echo "<script>alert('Đăng tin thành công!'); window.location.href='add-news.php';</script>";
            exit;
        }
    } else {
        echo "<script>alert('Vui lòng điền tiêu đề và nội dung!');</script>";
    }
}
?>
<!DOCTYPE html>
<html lang="vi">
<head>
    <?php require_once('head.php');?>
    <title>Quản Lý Tin Tức</title>
    <?php require_once('nav.php');?>
</head>
<body class="hold-transition sidebar-mini layout-fixed">
<div class="wrapper">
    <div class="content-wrapper p-3">
        <section class="content">
            <div class="row">
                <div class="col-md-12">
                    <div class="card card-primary">
                        <div class="card-header"><h3 class="card-title">Viết bài mới</h3></div>
                        <form action="" method="post">
                            <div class="card-body">
                                <div class="form-group">
                                    <label>Tiêu đề</label>
                                    <input type="text" class="form-control" name="title" required>
                                </div>
                                <div class="form-group">
                                    <label>Danh mục</label>
                                    <select class="form-control" name="category">
                                        <option value="Tin tức">Tin tức</option>
                                        <option value="Hướng dẫn">Hướng dẫn</option>
                                    </select>
                                </div>
                                <div class="form-group">
                                    <label>Ảnh đại diện (URL)</label>
                                    <input type="url" class="form-control" name="image">
                                </div>
                                <div class="form-group">
                                    <label>Nội dung</label>
                                    <textarea class="form-control" name="content" rows="5" required></textarea>
                                </div>
                            </div>
                            <div class="card-footer">
                                <button type="submit" name="add_news" class="btn btn-primary">Đăng bài</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </section>
    </div>
</div>
<?php require_once('foot.php');?>
</body>
</html>
