<?php

namespace App\Controllers\Admin;

use App\Controllers\BaseController;
use App\Models\BlogModel;
use App\Models\SettingModel;
use App\Models\UploadModel;
use App\Models\EmailModel;

class Blog extends BaseController
{
    public function blog_category()
    {
        $blog = new BlogModel();
        $setting = new SettingModel();
        $data['app_setting'] = $setting->get_application_setting();
        $data['all_category'] = $blog->get_all_category();
        echo view('admin/include/header', $data);
        echo view('admin/blog/category');
        echo view('admin/include/footer');
    }

    function add_new_category()
    {
        $blog = new BlogModel();
        $upload = new UploadModel();

        $data['category_name'] = $this->request->getVar('category_name');
        $data['category_slug'] = slug($this->request->getVar('category_name'));
        if($this->request->getFile('category_image')->isValid() && !$this->request->getFile('category_image')->hasMoved())
        {
            $data['category_image'] = $upload->upload_image($this->request->getFile('category_image'));
        }
        if($blog->add_new_category($data))
        {
            session()->set('success', "Category Successfully Saved");
            return redirect()->route('admin/blog/blog_category');
        }
        else
        {
            session()->set('error', "Category Saved Failed");
            return redirect()->route('admin/blog/blog_category');
        }
    }

    function update_blog_category($id)
    {
        $blog = new BlogModel();
        $upload = new UploadModel();

        $data['category_name'] = $this->request->getVar('category_name');
        $data['category_slug'] = slug($this->request->getVar('category_name'));
        if($this->request->getFile('category_image')->isValid() && !$this->request->getFile('category_image')->hasMoved())
        {
            $category = $blog->get_category_by_id($id);
            if(file_exists(substr($category->category_image, 1)))
            {
                unlink(substr($category->category_image, 1));
            }
            $data['category_image'] = $upload->upload_image($this->request->getFile('category_image'));
        }
        if($blog->update_category($data, $id))
        {
            session()->set('success', "Category Successfully Saved");
            return redirect()->route('admin/blog/blog_category');
        }
        else
        {
            session()->set('error', "Category Saved Failed");
            return redirect()->route('admin/blog/blog_category');
        }
    }

    function delete_category($id)
    {
        $blog = new BlogModel();

        $category = $blog->get_category_by_id($id);
        if(file_exists(substr($category->category_image, 1)))
        {
            unlink(substr($category->category_image, 1));
        }
        if($blog->delete_category($id))
        {
            session()->set('success', "Category Successfully Deleted");
            return redirect()->route('admin/blog/blog_category');
        }
        else
        {
            session()->set('error', "Category Deleted Failed");
            return redirect()->route('admin/blog/blog_category');
        }
    }

    public function add_new_blog()
    {
        $blog = new BlogModel();
        $setting = new SettingModel();
        $data['app_setting'] = $setting->get_application_setting();
        $data['all_category'] = $blog->get_all_category();
        echo view('admin/include/header', $data);
        echo view('admin/blog/add_new_blog');
        echo view('admin/include/footer');
    }

    function new_blog_add()
    {
        $blog = new BlogModel();
        $upload = new UploadModel();
        $email = new EmailModel();
        $setting = new SettingModel();

        $data['title'] = $this->request->getVar('title');
        $data['title_slug'] = slug($this->request->getVar('title'));
        $data['short_description'] = $this->request->getVar('short_description');
        $data['keywords'] = $this->request->getVar('keywords');
        $data['breaking'] = $this->request->getVar('breaking') == 1 ? 1 : 0;
        $data['tag'] = $this->request->getVar('tag');
        $data['content'] = $this->request->getVar('content');
        $data['category'] = $this->request->getVar('category');
        $data['date'] = date('Y-m-d h:i:s');
        if($this->request->getFile('image')->isValid() && !$this->request->getFile('image')->hasMoved())
        {
            $data['image'] = $upload->upload_image($this->request->getFile('image'));
        }
        if($blog->add_new_blog($data))
        {
            if($this->request->getVar('send_post_to_subscribes') == 1)
            {
                $all_subscribers = $setting->get_all_subscribers_and_volunteer();
                
                $subject = $this->request->getVar('title');
                $email_data['content'] = $this->request->getVar('content');
                $email_data['title'] = $this->request->getVar('title');
                $email_data['app_setting'] = $setting->get_application_setting();
                $msg = view('email/send_email_template', $email_data);

                foreach ($all_subscribers as $subscribers) 
                {
                    $email_id = $subscribers->subscribers_email;
                    $email->send_email($email_id, $subject, $msg, $mailType = 'html', $subject);
                }
                
            }

            session()->set('success', "Blog Successfully Saved");
            return redirect()->route('admin/blog/view_all_blog');
        }
        else
        {
            session()->set('error', "Blog Saved Failed");
            return redirect()->route('admin/blog/view_all_blog');
        }
    }

    public function view_all_blog()
    {
        $blog = new BlogModel();
        $setting = new SettingModel();
        $data['app_setting'] = $setting->get_application_setting();
        $data['all_blog'] = $blog->get_all_blog();
        echo view('admin/include/header', $data);
        echo view('admin/blog/view_all_blog');
        echo view('admin/include/footer');
    }

    public function update_blog($id)
    {
        $blog = new BlogModel();
        $setting = new SettingModel();
        $data['app_setting'] = $setting->get_application_setting();
        $data['all_category'] = $blog->get_all_category();
        $data['blog'] = $blog->get_blog_by_id($id);
        echo view('admin/include/header', $data);
        echo view('admin/blog/update_blog');
        echo view('admin/include/footer');
    }

    function update_blog_new($id)
    {
        $blog = new BlogModel();
        $upload = new UploadModel();

        $data['title'] = $this->request->getVar('title');
        $data['title_slug'] = slug($this->request->getVar('title'));
        $data['short_description'] = $this->request->getVar('short_description');
        $data['keywords'] = $this->request->getVar('keywords');
        $data['breaking'] = $this->request->getVar('breaking') == 1 ? 1 : 0;
        // $data['send_post_to_subscribes'] = $this->request->getVar('send_post_to_subscribes')== 1 ? 1 : 0;
        $data['tag'] = $this->request->getVar('tag');
        $data['content'] = $this->request->getVar('content');
        $data['category'] = $this->request->getVar('category');

        if($this->request->getFile('image')->isValid() && !$this->request->getFile('image')->hasMoved())
        {
            $blog_image = $blog->get_blog_by_id($id);
            if(file_exists(substr($blog_image->image, 1)))
            {
                unlink(substr($blog_image->image, 1));
            }
            $data['image'] = $upload->upload_image($this->request->getFile('image'));
        }
        if($blog->update_blog_details($data, $id))
        {
            session()->set('success', "Blog Successfully Saved");
            return redirect()->route('admin/blog/view_all_blog');
        }
        else
        {
            session()->set('error', "Blog Saved Failed");
            return redirect()->route('admin/blog/view_all_blog');
        }
    }

    function delete_blog($id)
    {
        $blog = new BlogModel();

        $blog_img = $blog->get_blog_by_id($id);
        if(file_exists(substr($blog_img->image, 1)))
        {
            unlink(substr($blog_img->image, 1));
        }
        if($blog->delete_blog($id))
        {
            session()->set('success', "Blog Successfully Deleted");
            return redirect()->route('admin/blog/view_all_blog');
        }
        else
        {
            session()->set('error', "Blog Deleted Failed");
            return redirect()->route('admin/blog/view_all_blog');
        }
    }

    public function view_all_comment()
    {
        $blog = new BlogModel();
        $setting = new SettingModel();
        $data['app_setting'] = $setting->get_application_setting();
        $data['all_blog_review'] = $blog->get_all_blog_review();
        echo view('admin/include/header', $data);
        echo view('admin/blog/view_all_comment');
        echo view('admin/include/footer');
    }

    function delete_blog_review($id)
    {
        $blog = new BlogModel();

        if($blog->delete_blog_review($id))
        {
            session()->set('success', "Review Successfully Deleted");
            return redirect()->route('admin/blog/view_all_comment');
        }
        else
        {
            session()->set('error', "Review Deleted Failed");
            return redirect()->route('admin/blog/view_all_comment');
        }
    }

    
    

    

    

    

    

    

    


    
}
