<?php

class DbHandler
{
    private $conn;

    function __construct()
    {
        require_once dirname(__FILE__) . '/db_connect.php';

        // opening db connection
        $db = new DbConnect();
        $this->conn = $db->connect();
    }

    public function redirect($url)
    {
        header("Location: $url");
    }

    public function isUserExists($email, $userPassword)
    {
        $stmt = $this->conn->prepare("SELECT * from users WHERE email = ? AND password = ?");
        $stmt->bind_param("ss", $email, $userPassword);
        $stmt->execute();
        $stmt->store_result();
        $num_rows = $stmt->num_rows;
        $stmt->close();
        return $num_rows > 0;
    }

    public function emailExists($email)
    {
        $stmt = $this->conn->prepare("SELECT * from users WHERE email = ?");
        $stmt->bind_param("s", $email);
        $stmt->execute();
        $stmt->store_result();
        $num_rows = $stmt->num_rows;
        $stmt->close();
        return $num_rows > 0;
    }

    public function addUser($userName, $email, $phoneNumber, $userType, $userPhoto, $userPassword)
    {
        $response = array();
        $stmt = $this->conn->prepare("INSERT INTO users (name, email, phone, userType, photo_url, password) values(?, ?, ?, ?, ?, ?)");
        $stmt->bind_param("ssssss", $userName, $email, $phoneNumber, $userType, $userPhoto, $userPassword);
        if ($result = $stmt->execute()) {
            $response['error'] = false;

        } else {
            $response['error'] = true;
            $response['message'] = 'Failed saving product' . $stmt->error;
        }
        return $response;
    }

    public function getUserByUserID($userID)
    {
        $stmt = $this->conn->prepare("SELECT id, name, email, phone, userType, photo_url, created_at FROM users WHERE email = ?");
        $stmt->bind_param("s", $userID);
        if ($stmt->execute()) {
            $stmt->bind_result($userID, $fName, $email, $phoneNumber, $entry, $imageUrl, $createdAt);
            $stmt->fetch();
            $user = array();
            $user["id"] = $userID;
            $user["name"] = $fName;
            $user["email"] = $email;
            $user["phone"] = $phoneNumber;
            $user["userType"] = $entry;
            $user["photo_url"] = $imageUrl;
            $user["created_at"] = $createdAt;
            $stmt->close();
            return $user;
        } else {
            return NULL;
        }
    }

    public function getAllUsers($query)
    {
        $users = array();
        $stmt = $this->conn->prepare($query);
        $stmt->execute();
        $result = $stmt->get_result();
        $stmt->close();

        while ($user = $result->fetch_assoc()) {
            $tmp = array();
            $tmp["id"] = $user["id"];
            $tmp["name"] = $user["name"];
            $tmp["email"] = $user["email"];
            $tmp["phone"] = $user["phone"];
            $tmp["userType"] = $user["userType"];
            $tmp["photo_url"] = $user["photo_url"];
            $tmp["created_at"] = $user["created_at"];
            array_push($users, $tmp);
        }
        return $users;
    }

    public function getSingleUser($id)
    {
        $users = array();
        $stmt = $this->conn->prepare("SELECT * FROM users WHERE id = ?");
        $stmt->bind_param("i", $id);
        $stmt->execute();
        $result = $stmt->get_result();
        $stmt->close();

        while ($user = $result->fetch_assoc()) {
            $tmp = array();
            $tmp["id"] = $user["id"];
            $tmp["name"] = $user["name"];
            $tmp["email"] = $user["email"];
            $tmp["phone"] = $user["phone"];
            $tmp["userType"] = $user["userType"];
            $tmp["photo_url"] = $user["photo_url"];
            $tmp["created_at"] = $user["created_at"];
            array_push($users, $tmp);
        }
        return $users;
    }


    public function editUser($id, $userName, $email, $phone)
    {
        $response = array();
        $stmt = $this->conn->prepare("UPDATE users SET name = ?, email = ?, phone = ? WHERE id = ?");
        $stmt->bind_param("sssi", $userName, $email, $phone, $id);
        if ($stmt->execute()) {
            // User successfully updated
            $response["error"] = false;
            $response["message"] = 'Details updated successfully';
        } else {
            // Failed to update user
            $response["error"] = true;
            $response["message"] = "Failed to update Details";
            $stmt->error;
        }
        $stmt->close();

        return $response;
    }

    public function deleteUser($userID)
    {
        $stmt = $this->conn->prepare("DELETE FROM users WHERE id = ? ");
        $stmt->bind_param("s", $userID);
        $result = $stmt->execute();
        $stmt->close();

        if ($result) {
            $response["error"] = false;
            $response["message"] = "User removed.";
        } else {
            $response["error"] = true;
            $response["message"] = "Oops! An error occurred while adding category";
        }

        return $response;
    }

    function countCategories()
    {
        $stmt = $this->conn->prepare("SELECT * FROM tbl_categories");
        $stmt->execute();
        $stmt->store_result();
        $num_rows = $stmt->num_rows;
        $stmt->close();
        return $num_rows;
    }

    function countAllArticles()
    {
        $stmt = $this->conn->prepare("SELECT * FROM tbl_articles");
        $stmt->execute();
        $stmt->store_result();
        $num_rows = $stmt->num_rows;
        $stmt->close();
        return $num_rows;
    }

    public function countAllArticlesByDate($date)
    {
        $categories = array();
        $stmt = $this->conn->prepare("SELECT * FROM tbl_articles WHERE created_at = ?");
        $stmt->bind_param("s", $date);
        $stmt->execute();
        $result = $stmt->get_result();
        $stmt->close();

        while ($category = $result->fetch_assoc()) {
            $tmp = array();
            $tmp["created_at"] = $category["created_at"];
            array_push($categories, $tmp);
        }
        return $categories;
    }

    function countPublishedArticles()
    {
        $stmt = $this->conn->prepare("SELECT * FROM tbl_articles WHERE status = 'published'");
        $stmt->execute();
        $stmt->store_result();
        $num_rows = $stmt->num_rows;
        $stmt->close();
        return $num_rows;
    }

    function countUser()
    {
        $stmt = $this->conn->prepare("SELECT * FROM users WHERE userType = 'user' ");
        $stmt->execute();
        $stmt->store_result();
        $num_rows = $stmt->num_rows;
        $stmt->close();
        return $num_rows;
    }

    public function getAllCategories($query)
    {
        $categories = array();
        $stmt = $this->conn->prepare($query);
        $stmt->execute();
        $result = $stmt->get_result();
        $stmt->close();

        while ($category = $result->fetch_assoc()) {
            $tmp = array();
            $tmp["id"] = $category["id"];
            $tmp["name"] = $category["name"];
            $tmp["created_at"] = $category["created_at"];
            array_push($categories, $tmp);
        }
        return $categories;
    }

    public function addCat($catName)
    {
        $response = array();
        $stmt = $this->conn->prepare("INSERT INTO tbl_categories (name) values(?)");
        $stmt->bind_param("s", $catName);
        if ($result = $stmt->execute()) {
            $response['error'] = false;

        } else {
            $response['error'] = true;
            $response['message'] = 'Failed saving category' . $stmt->error;
        }
        return $response;
    }

    public function getDropCategories()
    {
        $stmt = $this->conn->prepare("SELECT * FROM tbl_categories");
        $stmt->execute();
        $tasks = $stmt->get_result();
        $stmt->close();
        return $tasks;
    }

    public function getCategory($id)
    {
        $categories = array();
        $stmt = $this->conn->prepare("SELECT * FROM tbl_categories WHERE id = ?");
        $stmt->bind_param("i", $id);
        $stmt->execute();
        $result = $stmt->get_result();
        $stmt->close();

        while ($category = $result->fetch_assoc()) {
            $tmp = array();
            $tmp["id"] = $category["id"];
            $tmp["id"] = $category["id"];
            $tmp["name"] = $category["name"];
            $tmp["created_at"] = $category["created_at"];
            array_push($categories, $tmp);
        }
        return $categories;
    }

    public function editCat($catID, $catName)
    {
        $response = array();
        $stmt = $this->conn->prepare("UPDATE tbl_categories SET name = ? WHERE id = ?");
        $stmt->bind_param("ss", $catName, $catID);
        if ($stmt->execute()) {
            // User successfully updated
            $response["error"] = false;
            $response["message"] = 'Details updated successfully';
        } else {
            // Failed to update user
            $response["error"] = true;
            $response["message"] = "Failed to update Details";
            $stmt->error;
        }
        $stmt->close();

        return $response;
    }

    public function deleteCategory($categoryID)
    {
        $stmt = $this->conn->prepare("DELETE FROM tbl_categories WHERE id = ? ");
        $stmt->bind_param("s", $categoryID);
        $result = $stmt->execute();
        $stmt->close();

        if ($result) {
            $response["error"] = false;
            $response["message"] = "Category removed.";
        } else {
            $response["error"] = true;
            $response["message"] = "Oops! An error occurred while adding category";
        }

        return $response;
    }

    public function addArticles($artTitle, $story, $summary, $authorID, $catID, $postPhoto, $status)
    {
        $response = array();
        $stmt = $this->conn->prepare("INSERT INTO tbl_articles (title, body, summary, author, category, image, status) values(?, ?, ?, ?, ?, ?, ?)");
        $stmt->bind_param("sssssss", $artTitle, $story, $summary, $authorID, $catID, $postPhoto, $status);
        if ($result = $stmt->execute()) {
            $response['error'] = false;

        } else {
            $response['error'] = true;
            $response['message'] = 'Failed saving article' . $stmt->error;
        }
        return $response;
    }

    public function getAllArticles($query)
    {
        $articles = array();
        $stmt = $this->conn->prepare($query);
        $stmt->execute();
        $result = $stmt->get_result();
        $stmt->close();

        while ($article = $result->fetch_assoc()) {
            $tmp = array();
            $tmp["id"] = $article["id"];
            $tmp["title"] = $article["title"];
            $tmp["body"] = $article["body"];
            $tmp["author"] = $article["author"];
            $tmp["image"] = $article["image"];
            $tmp["category"] = $article["category"];
            $tmp["summary"] = $article["summary"];
            $tmp["created_at"] = $article["created_at"];
            $tmp["status"] = $article["status"];
            array_push($articles, $tmp);
        }
        return $articles;
    }

    public function getSingleArticle($id)
    {
        $articles = array();
        $stmt = $this->conn->prepare("SELECT * FROM tbl_articles WHERE id = ?");
        $stmt->bind_param("i", $id);
        $stmt->execute();
        $result = $stmt->get_result();
        $stmt->close();

        while ($article = $result->fetch_assoc()) {
            $tmp = array();
            $tmp["id"] = $article["id"];
            $tmp["title"] = $article["title"];
            $tmp["body"] = $article["body"];
            $tmp["author"] = $article["author"];
            $tmp["image"] = $article["image"];
            $tmp["category"] = $article["category"];
            $tmp["summary"] = $article["summary"];
            $tmp["created_at"] = $article["created_at"];
            $tmp["status"] = $article["status"];
            array_push($articles, $tmp);
        }
        return $articles;
    }

    public function editArt($id, $artTitle, $story, $catID, $postPhoto)
    {
        $response = array();
        $stmt = $this->conn->prepare("UPDATE tbl_articles SET title = ?, body = ?, category = ?, image = ? WHERE id = ?");
        $stmt->bind_param("sssss", $artTitle, $story, $catID, $postPhoto, $id);
        if ($stmt->execute()) {
            // User successfully updated
            $response["error"] = false;
            $response["message"] = 'Details updated successfully';
        } else {
            // Failed to update user
            $response["error"] = true;
            $response["message"] = "Failed to update Details";
            $stmt->error;
        }
        $stmt->close();

        return $response;
    }

    public function updateStatus($artID, $status)
    {
        $response = array();
        $stmt = $this->conn->prepare("UPDATE tbl_articles SET status = ? WHERE id = ?");
        $stmt->bind_param("ss", $status, $artID);
        if ($stmt->execute()) {
            //successfully updated
            $response["error"] = false;
            $response["message"] = 'Details updated successfully';
        } else {
            // Failed to update user
            $response["error"] = true;
            $response["message"] = "Failed to update Details";
            $stmt->error;
        }
        $stmt->close();

        return $response;
    }

    public function deleteArticle($articleID)
    {
        $stmt = $this->conn->prepare("DELETE FROM tbl_articles WHERE id = ? ");
        $stmt->bind_param("s", $articleID);
        $result = $stmt->execute();
        $stmt->close();

        if ($result) {
            $response["error"] = false;
            $response["message"] = "Article removed.";
        } else {
            $response["error"] = true;
            $response["message"] = "Oops! An error occurred while adding category";
        }

        return $response;
    }


    public function timeAgo($time_ago)
    {
        $cur_time = time();
        $time_elapsed = $cur_time - $time_ago;
        $seconds = $time_elapsed;
        $minutes = round($time_elapsed / 60);
        $hours = round($time_elapsed / 3600);
        $days = round($time_elapsed / 86400);
        $weeks = round($time_elapsed / 604800);
        $months = round($time_elapsed / 2600640);
        $years = round($time_elapsed / 31207680);
// Seconds
        if ($seconds <= 60) {
            echo "$seconds seconds ago";
        } //Minutes
        else if ($minutes <= 60) {
            if ($minutes == 1) {
                echo "one minute ago";
            } else {
                echo "$minutes minutes ago";
            }
        } //Hours
        else if ($hours <= 24) {
            if ($hours == 1) {
                echo "an hour ago";
            } else {
                echo "$hours hours ago";
            }
        } //Days
        else if ($days <= 7) {
            if ($days == 1) {
                echo "yesterday";
            } else {
                echo "$days days ago";
            }
        } //Weeks
        else if ($weeks <= 4.3) {
            if ($weeks == 1) {
                echo "a week ago";
            } else {
                echo "$weeks weeks ago";
            }
        } //Months
        else if ($months <= 12) {
            if ($months == 1) {
                echo "a month ago";
            } else {
                echo "$months months ago";
            }
        } //Years
        else {
            if ($years == 1) {
                echo "one year ago";
            } else {
                echo "$years years ago";
            }
        }
    }

    public function addGallery($imageTitle, $postPhoto1, $postPhoto2, $postPhoto3, $postPhoto4, $postPhoto5, $postPhoto6, $status)
    {
        $response = array();
        $stmt = $this->conn->prepare("INSERT INTO tbl_images (title, image1, image2, image3, image4, image5, image6, status) values(?, ?, ?, ?, ?, ?, ?, ?)");
        $stmt->bind_param("ssssssss", $imageTitle, $postPhoto1, $postPhoto2, $postPhoto3, $postPhoto4, $postPhoto5, $postPhoto6, $status);
        if ($result = $stmt->execute()) {
            $response['error'] = false;

        } else {
            $response['error'] = true;
            $response['message'] = 'Failed saving image' . $stmt->error;
        }
        return $response;
    }


    public function getAllGalleries($query)
    {
        $galleries = array();
        $stmt = $this->conn->prepare($query);
        $stmt->execute();
        $result = $stmt->get_result();
        $stmt->close();

        while ($gallery = $result->fetch_assoc()) {
            $tmp = array();
            $tmp["id"] = $gallery["id"];
            $tmp["title"] = $gallery["title"];
            $tmp["image1"] = $gallery["image1"];
            $tmp["image2"] = $gallery["image2"];
            $tmp["image3"] = $gallery["image3"];
            $tmp["image4"] = $gallery["image4"];
            $tmp["image5"] = $gallery["image5"];
            $tmp["image6"] = $gallery["image6"];
            $tmp["status"] = $gallery["status"];
            $tmp["dateCreated"] = $gallery["dateCreated"];
            array_push($galleries, $tmp);
        }
        return $galleries;
    }

    public function getSingleGallery($id)
    {
        $galleries = array();
        $stmt = $this->conn->prepare("SELECT * FROM tbl_images WHERE id = ?");
        $stmt->bind_param("i", $id);
        $stmt->execute();
        $result = $stmt->get_result();
        $stmt->close();

        while ($gallery = $result->fetch_assoc()) {
            $tmp = array();
            $tmp["id"] = $gallery["id"];
            $tmp["title"] = $gallery["title"];
            $tmp["image1"] = $gallery["image1"];
            $tmp["image2"] = $gallery["image2"];
            $tmp["image3"] = $gallery["image3"];
            $tmp["image4"] = $gallery["image4"];
            $tmp["image5"] = $gallery["image5"];
            $tmp["image6"] = $gallery["image6"];
            $tmp["status"] = $gallery["status"];
            $tmp["dateCreated"] = $gallery["dateCreated"];
            array_push($galleries, $tmp);
        }
        return $galleries;
    }

    public function updateGalleryStatus($galleryID, $status)
    {
        $response = array();
        $stmt = $this->conn->prepare("UPDATE tbl_images SET status = ? WHERE id = ?");
        $stmt->bind_param("ss", $status, $galleryID);
        if ($stmt->execute()) {
            //successfully updated
            $response["error"] = false;
            $response["message"] = 'Details updated successfully';
        } else {
            // Failed to update user
            $response["error"] = true;
            $response["message"] = "Failed to update Details";
            $stmt->error;
        }
        $stmt->close();

        return $response;
    }

    public function editGallery($id, $videoTitle, $videoUrl)
    {
        $response = array();
        $stmt = $this->conn->prepare("UPDATE tbl_videos SET title = ?, video_url = ? WHERE id = ?");
        $stmt->bind_param("sss", $videoTitle, $videoUrl, $id);
        if ($stmt->execute()) {
            // User successfully updated
            $response["error"] = false;
            $response["message"] = 'Details updated successfully';
        } else {
            // Failed to update user
            $response["error"] = true;
            $response["message"] = "Failed to update Details";
            $stmt->error;
        }
        $stmt->close();

        return $response;
    }

    public function addVideo($videoTitle, $videoUrl, $status)
    {
        $response = array();
        $stmt = $this->conn->prepare("INSERT INTO tbl_videos (title, video_url, status) values(?, ?, ?)");
        $stmt->bind_param("sss", $videoTitle, $videoUrl, $status);
        if ($result = $stmt->execute()) {
            $response['error'] = false;

        } else {
            $response['error'] = true;
            $response['message'] = 'Failed saving video' . $stmt->error;
        }
        return $response;
    }

    public function getAllVideos($query)
    {
        $articles = array();
        $stmt = $this->conn->prepare($query);
        $stmt->execute();
        $result = $stmt->get_result();
        $stmt->close();

        while ($article = $result->fetch_assoc()) {
            $tmp = array();
            $tmp["id"] = $article["id"];
            $tmp["title"] = $article["title"];
            $tmp["video_url"] = $article["video_url"];
            $tmp["dateCreated"] = $article["dateCreated"];
            $tmp["status"] = $article["status"];
            array_push($articles, $tmp);
        }
        return $articles;
    }

    public function getSingleVideo($id)
    {
        $videos = array();
        $stmt = $this->conn->prepare("SELECT * FROM tbl_videos WHERE id = ?");
        $stmt->bind_param("i", $id);
        $stmt->execute();
        $result = $stmt->get_result();
        $stmt->close();

        while ($video = $result->fetch_assoc()) {
            $tmp = array();
            $tmp["id"] = $video["id"];
            $tmp["title"] = $video["title"];
            $tmp["video_url"] = $video["video_url"];
            $tmp["status"] = $video["status"];
            array_push($videos, $tmp);
        }
        return $videos;
    }

    public function updateVideoStatus($videoID, $status)
    {
        $response = array();
        $stmt = $this->conn->prepare("UPDATE tbl_videos SET status = ? WHERE id = ?");
        $stmt->bind_param("ss", $status, $videoID);
        if ($stmt->execute()) {
            //successfully updated
            $response["error"] = false;
            $response["message"] = 'Details updated successfully';
        } else {
            // Failed to update user
            $response["error"] = true;
            $response["message"] = "Failed to update Details";
            $stmt->error;
        }
        $stmt->close();

        return $response;
    }

    public function editVideo($id, $videoTitle, $videoUrl)
    {
        $response = array();
        $stmt = $this->conn->prepare("UPDATE tbl_videos SET title = ?, video_url = ? WHERE id = ?");
        $stmt->bind_param("sss", $videoTitle, $videoUrl, $id);
        if ($stmt->execute()) {
            // User successfully updated
            $response["error"] = false;
            $response["message"] = 'Details updated successfully';
        } else {
            // Failed to update user
            $response["error"] = true;
            $response["message"] = "Failed to update Details";
            $stmt->error;
        }
        $stmt->close();

        return $response;
    }

    public function deleteVideo($videoID)
    {
        $stmt = $this->conn->prepare("DELETE FROM tbl_videos WHERE id = ? ");
        $stmt->bind_param("s", $videoID);
        $result = $stmt->execute();
        $stmt->close();

        if ($result) {
            $response["error"] = false;
            $response["message"] = "Video removed.";
        } else {
            $response["error"] = true;
            $response["message"] = "Oops! An error occurred while adding category";
        }

        return $response;
    }

    public function addContact($name, $email, $recipient, $subject, $message, $status)
    {
        $response = array();
        $stmt = $this->conn->prepare("INSERT INTO tbl_messages (name, email, recipient, subject, message, status) values(?, ?, ?, ?, ?, ?)");
        $stmt->bind_param("ssssss", $name, $email, $recipient, $subject, $message, $status);
        if ($result = $stmt->execute()) {
            $response['error'] = false;

        } else {
            $response['error'] = true;
            $response['message'] = 'Failed saving category' . $stmt->error;
        }
        return $response;
    }

    public function addAbout($about, $aboutPhoto)
    {
        $response = array();
        $stmt = $this->conn->prepare("INSERT INTO tbl_about (description, about_photo) values(?, ?)");
        $stmt->bind_param("ss", $about, $aboutPhoto);
        if ($result = $stmt->execute()) {
            $response['error'] = false;

        } else {
            $response['error'] = true;
            $response['message'] = 'Failed saving' . $stmt->error;
        }
        return $response;
    }

    public function getAboutMe($query)
    {
        $articles = array();
        $stmt = $this->conn->prepare($query);
        $stmt->execute();
        $result = $stmt->get_result();
        $stmt->close();

        while ($article = $result->fetch_assoc()) {
            $tmp = array();
            $tmp["id"] = $article["id"];
            $tmp["description"] = $article["description"];
            $tmp["about_photo"] = $article["about_photo"];
            array_push($articles, $tmp);
        }
        return $articles;
    }

    public function addWorkWithUs($name, $email, $message)
    {
        $response = array();
        $stmt = $this->conn->prepare("INSERT INTO tbl_work_with_us (userName, email, message) values(?, ?, ?)");
        $stmt->bind_param("sss", $name, $email, $message);
        if ($result = $stmt->execute()) {
            $response['error'] = false;

        } else {
            $response['error'] = true;
            $response['message'] = 'Failed saving' . $stmt->error;
        }
        return $response;
    }

    public function getAllMessages($query)
    {
        $messages = array();
        $stmt = $this->conn->prepare($query);
        $stmt->execute();
        $result = $stmt->get_result();
        $stmt->close();

        while ($message = $result->fetch_assoc()) {
            $tmp = array();
            $tmp["id"] = $message["id"];
            $tmp["name"] = $message["name"];
            $tmp["email"] = $message["email"];
            $tmp["recipient"] = $message["recipient"];
            $tmp["subject"] = $message["subject"];
            $tmp["message"] = $message["message"];
            $tmp["status"] = $message["status"];
            $tmp["dateCreated"] = $message["dateCreated"];
            array_push($messages, $tmp);
        }
        return $messages;
    }

    public function getSingleMessage($id)
    {
        $messages = array();
        $stmt = $this->conn->prepare("SELECT *, UNIX_TIMESTAMP(dateCreated) as dateCreated FROM tbl_messages WHERE id = ?");
        $stmt->bind_param("i", $id);
        $stmt->execute();
        $result = $stmt->get_result();
        $stmt->close();

        while ($message = $result->fetch_assoc()) {
            $tmp = array();
            $tmp["id"] = $message["id"];
            $tmp["name"] = $message["name"];
            $tmp["email"] = $message["email"];
            $tmp["recipient"] = $message["recipient"];
            $tmp["subject"] = $message["subject"];
            $tmp["message"] = $message["message"];
            $tmp["status"] = $message["status"];
            $tmp["dateCreated"] = $message["dateCreated"];
            array_push($messages, $tmp);
        }
        return $messages;
    }


    public function isSubscribed($email)
    {
        $stmt = $this->conn->prepare("SELECT * from tbl_newsletters WHERE email = ?");
        $stmt->bind_param("s", $email);
        $stmt->execute();
        $stmt->store_result();
        $num_rows = $stmt->num_rows;
        $stmt->close();
        return $num_rows > 0;
    }

    public function subscribeUser($email, $code, $status)
    {
        $response = array();
        $stmt = $this->conn->prepare("INSERT INTO tbl_newsletters (email, code, status) values(?, ?, ?)");
        $stmt->bind_param("sss", $email, $code, $status);
        if ($result = $stmt->execute()) {
            $response['error'] = false;
            $response["message"] = "You have been successfully added to our newsletter list.";

        } else {
            $response['error'] = true;
            $response['message'] = 'Failed send message ' . $stmt->error;
        }

        return $response;
    }

    public function subscriptionExist($email, $code)
    {
        $stmt = $this->conn->prepare("SELECT * from tbl_newsletters WHERE email = ? AND code = ?");
        $stmt->bind_param("ss", $email, $code);
        $stmt->execute();
        $stmt->store_result();
        $num_rows = $stmt->num_rows;
        $stmt->close();
        return $num_rows > 0;
    }

    public function unSubscribeUser($email)
    {
        $response = array();
        $disable = "disable";
        $stmt = $this->conn->prepare("UPDATE tbl_newsletters SET status = ? WHERE email = ?");
        $stmt->bind_param("ss", $disable, $email);
        if ($stmt->execute()) {
            $response["error"] = false;

            $response["message"] = '<div class="alert alert-success" style="text-align:center;">
                    <a href="../subscribe.php" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                    <strong>Success!</strong> You have been unsubscribed from our newsletter. You will no longer receive email from us. Thank you
                                
                  </div>';
        } else {
            $response["error"] = true;
            $response["message"] = "An error occurred while subscribing please try again.";
            $stmt->error;
        }
        $stmt->close();
        return $response;
    }


    public function getAllApplications($query)
    {
        $applications = array();
        $stmt = $this->conn->prepare($query);
        $stmt->execute();
        $result = $stmt->get_result();
        $stmt->close();

        while ($application = $result->fetch_assoc()) {
            $tmp = array();
            $tmp["id"] = $application["id"];
            $tmp["userName"] = $application["userName"];
            $tmp["email"] = $application["email"];
            $tmp["message"] = $application["message"];
            $tmp["date"] = $application["date"];
            array_push($applications, $tmp);
        }
        return $applications;
    }

    public function deleteApplication($applicationID)
    {
        $stmt = $this->conn->prepare("DELETE FROM tbl_work_with_us WHERE id = ? ");
        $stmt->bind_param("s", $applicationID);
        $result = $stmt->execute();
        $stmt->close();

        if ($result) {
            $response["error"] = false;
            $response["message"] = "application removed.";
        } else {
            $response["error"] = true;
            $response["message"] = "Oops! An error occurred while adding application";
        }

        return $response;
    }


    function uploadEditorImage()
    {
        // Allowed extentions.
        $allowedExts = array("gif", "jpeg", "jpg", "png");
        $temp = explode(".", $_FILES["image_param"]["name"]);
        $extension = end($temp);

        if (in_array($extension, $allowedExts)) {
            $imageName = sha1(microtime()) . "." . $extension;
            move_uploaded_file($_FILES["image_param"]["tmp_name"], getcwd() .
                "/front_assets/img/post_pic/" . $imageName);

            $response = new StdClass;
        }
        return $response;
    }
    
    
    public function deleteGallery($galleryID)
    {
        $stmt = $this->conn->prepare("DELETE FROM tbl_images WHERE id = ? ");
        $stmt->bind_param("s", $galleryID);
        $result = $stmt->execute();
        $stmt->close();

        if ($result) {
            $response["error"] = false;
            $response["message"] = "Gallery removed.";
        } else {
            $response["error"] = true;
            $response["message"] = "Oops! An error occurred while adding category";
        }

        return $response;
    }
}
