<?php
// session_start();
// session_regenerate_id(true);
// define('BASEPATH', true);
// require '../functions/config.php';
// require_once '../functions/config.php';
require_once '../functions/enDecpt.php';

// $firstname=  $_SESSION['current_session']['user']['firstname'];
// $email=  $_SESSION['current_session']['user']['email'];    
if (isset($_SESSION['admin'])) {
    $dbHandler = DbHandler();
    // include_once 'notify.php'; 
    if (isset($_POST['submit']) != "") {
        if ($_FILES['file']['name']) {
            $name = $_FILES['file']['name'];
            $size = $_FILES['file']['size'];
            $type = $_FILES['file']['type'];
            $temp = $_FILES['file']['tmp_name'];
            $fname = date("YmdHis") . '_' . $name;

            $a_title = $_POST['title'];
            $a_body = $_POST['body'];
            $created_at = date("YmdHis");
            // $status = "pending"; 

            $status = "Pending";
            // move image into upload directory 
            $move =  move_uploaded_file($temp, "upload/" . $fname);
            if ($move) {
                $sql = "INSERT INTO tbl_blog(name, fname, a_title, a_body, created_at, status)values(:name,:fname,:a_title,:a_body,:created_at,:status)";
                $stmt = $dbHandler->prepare($sql);

                $stmt->bindParam(':name', $name, PDO::PARAM_STR);
                $stmt->bindParam(':fname', $fname, PDO::PARAM_STR);
                $stmt->bindParam(':a_title', $a_title, PDO::PARAM_STR);
                $stmt->bindParam(':a_body', $a_body, PDO::PARAM_STR);
                $stmt->bindParam(':created_at', $created_at, PDO::PARAM_STR);
                $stmt->bindParam(':status', $status, PDO::PARAM_STR);

                $updateArticleTable =  $stmt->execute();

                if ($updateArticleTable) {
                    $_SESSION['success'] = "Article submitted successfully ";
                    header("Location: ./blog-manager.php");
                } else {
                    $_SESSION['success'] = "Something went wrong, please try again!";
                    header("Location: ./post-article.php");
                }
                exit();
            }
        }
    }
} else {
    echo "<script>window.location='../logout.php'</script>";
}
