<?php
require_once "common/header.php";
require_once "include/db_handler.php";
$db = new DbHandler();

if (isset($_POST['submit_art'])) {

    $artTitle = $_POST['title'];
    $catID = $_POST['category'];
    $status = $_POST['status'];
    $story = $_POST['story'];
    $authorID = $_POST['author_id'];
    $summary = $_POST['summary'];

    $postPhoto = $_FILES['post_photo']['name'];
    move_uploaded_file($_FILES['post_photo']['tmp_name'], 'assets/img/post_pic/' . $_FILES['post_photo']['name']);

    if ($artTitle != '' && $postPhoto != '') {

        $response = $db->addArticles($artTitle, $story, $summary, $authorID, $catID, $postPhoto, $status);
        //$response = $db->uploadEditorImage();

        if ($response["error"] == false) {

            $response["message"] = '<div class="alert alert-success" style="text-align:center;">
                    <a href="published_article.php" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                    <strong>Success!</strong> Article added successfully
                  </div>';
        }

    } else {
        $response["message"] = '<div class="alert alert-danger" style="text-align:center;">
                    <a href="published_article.php" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                    <strong>Sorry!</strong> All fields are required 
                                
                  </div>';
    }
}
?>

<body>

<div class="container-fluid">
    <div class="row">
        <?php
        require_once "common/sidebar.php";
        ?>
        <div class="right-column">
            <?php
            require_once "common/navbar.php";
            ?>
            <main class="main-content p-5" role="main">

                <div class="row mb-4">
                    <div class="col-md-12">
                        <div class="card">
                            <div class="card-mailbox">
                                <div class="row mx-0">

                                    <div class="col-lg-12">

                                        <h2>Add Article</h2>

                                        <?php if (isset($response['message'])) echo $response["message"];?>

                                        <div class="mailbox-container mailbox-message-compose">

                                            <form method="post" enctype="multipart/form-data" action="add_article.php">
                                                <div class="modal-body">

                                                    <div class="form-group my_form">
                                                        <label for="email">Title:</label>
                                                        <input type="text" class="form-control" name="title">
                                                    </div>

                                                    <div class="form-group my_form">
                                                        <label for="email">Category:</label>
                                                        <select class="form-control" name="category">
                                                            <?php
                                                            $categories = $db->getDropCategories();
                                                            while ($category = $categories->fetch_assoc()) : ?>
                                                                <option value="<?php echo $category['id']; ?>">
                                                                    <?php echo $category['name']; ?>
                                                                </option>
                                                            <?php endwhile; ?>
                                                        </select>
                                                    </div>

                                                    <div class="form-group my_form">
                                                        <label for="email">Status:</label>
                                                        <select class="form-control" name="status">
                                                            <option value="published">Published</option>
                                                            <option value="inactive">Inactive</option>
                                                        </select>
                                                    </div>

                                                    <div class="form-group my_form">
                                                        <label for="email">Author:</label>
                                                        <input type="text" class="form-control" name="author_id">
                                                    </div>

                                                    <div class="form-group my_form">
                                                        <label for="email">Summary:</label>
                                                        <input type="text" class="form-control" name="summary">
                                                    </div>

                                                    <div class="form-group my_form">
                                                        <label>Content:</label>
                                                        <textarea name="story" id="mytextarea"></textarea>
                                                    </div>

                                                    <div class="form-group" style="margin-top: 20px!important;">
                                                        <label for="exampleFormControlFile1">Upload Photo</label>
                                                        <input type="file" class="form-control-file" name="post_photo">
                                                    </div>

                                                </div>
                                                <div class="modal-footer">
                                                    <button type="submit" name="submit_art" class="btn btn-info">
                                                        Submit
                                                    </button>
                                                </div>
                                            </form>

                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

            </main>
        </div>
    </div>
</div>

<?php
require_once "modals/add_article.php";
require_once "modals/logout.php";
require_once "modals/add_category.php";
require_once "common/footer.php";
?>

