<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Admin extends CI_Controller
{

        public function index()
        {
                $this->load->helper('url');
                $this->load->view('header');
                $this->load->view('admin');
                $this->load->view('footer');
        }

        public function add_user()
        {
                $this->load->helper('url');
                $this->load->view('header');
                $this->load->view('add_user');
                $this->load->view('footer');
        }

        public function add_message()
        {
                $this->load->helper('url');
                $this->load->model('Services_model');
                $services = $this->Services_model->get_all_services();
                $_SESSION["services"] = $services;
                $this->load->view('header');
                $this->load->view('add_message');
                $this->load->view('footer');
        }
        
                public function add_post()
        {
                $this->load->helper('url');
                $this->load->view('header');
                $this->load->view('add_post');
                $this->load->view('footer');
        }

        public function edit_message()
        {
                $this->load->helper('url');
                $this->load->view('header');
                $id = $_REQUEST['message_id'];
                
                $this->load->model('Message_archive');
                $edit_message = $this->Message_archive->get_messages_by_id($id);
                $_SESSION['edit_message'] = $edit_message;

                $this->load->model('Services_model');
                $services = $this->Services_model->get_all_services();
                $_SESSION["services"] = $services;
                foreach ($services as $s) {
                        $_SESSION["service" . $s->id] = $s->name;
                }

                $this->load->view('edit_message');
                $this->load->view('footer');
        }

        public function edit_user()
        {
                $this->load->helper('url');
                $this->load->view('header');
                $id = $_REQUEST['user_id'];

                $this->load->model('User_model');
                $_SESSION['edit_user'] = $this->User_model->get_user_by_id($id);

                $this->load->view('edit_user');
                $this->load->view('footer');
        }

        public function edit_post()
        {
                $this->load->helper('url');
                $this->load->view('header');
                $id = $_REQUEST['post_id'];

                $this->load->model('Post_model');
                $_SESSION['edit_post'] = $this->Post_model->get_posts_by_id($id);

                $this->load->view('edit_post');
                $this->load->view('footer');
        }

        public function messages()
        {
                $this->load->helper('url');
                $this->load->view('header');
                $this->load->model('Message_archive');
                $_SESSION['sermons'] = $this->Message_archive->get_all_msgs();
                $this->load->model('Services_model');
                $services = $this->Services_model->get_all_services();
                foreach ($services as $s) {
                        $_SESSION["service" . $s->id] = $s->name;
                }

                $this->load->view('messages');
                $this->load->view('footer');
        }

        public function users()
        {
                $this->load->helper('url');
                $this->load->view('header');
                $this->load->model('User_model');
                $_SESSION['users'] = $this->User_model->get_all_users();
                $this->load->view('users');
                $this->load->view('footer');
        }

        public function services()
        {
                $this->load->helper('url');
                $this->load->view('header');
                $this->load->model('Services_model');
                $_SESSION['services'] = $this->Services_model->get_all_services();
                $this->load->view('services');
                $this->load->view('footer');
        }

        public function posts()
        {
                $this->load->helper('url');
                $this->load->view('header');
                $this->load->model('Post_model');
                $_SESSION['posts'] = $this->Post_model->get_all_posts();
                $this->load->view('posts');
                $this->load->view('footer');
        }

        public function upload_sermon()
        {
                $this->load->model('Message_archive');
                $this->load->model('Services_model');
                $this->load->helper('url');
                $this->load->helper('file');
                $this->load->helper('form');

                $config['upload_path'] = './uploads/';
                $config['allowed_types'] = 'jpeg|jpg|png|mp3|mpeg|mp4|wma';
                $config['max_size'] = 10000;
                $this->load->library('upload', $config);
                $title = $this->input->post('title');
                $m = base_url().'uploads/'.$title;
                $media_file_url = $m;
                $preacher = $this->input->post('preacher');
                $service_date =$this->input->post('service_date');
                $uploaded_by = $this->input->post('uploaded_by');
                $updated_by = $this->input->post('updated_by');
                $img_url = $this->input->post('image_url');
                $service = $this->input->post('service');

                // redirect('admin/add_message?msg=Try!%20Check%20Error%20and%20try%20again%20later!&type=error');
                $img_up = $this->upload->do_upload('img_url');
                $media_up = $this->upload->do_upload('media_file_url');
                if ($img_up) {

                        $msg_response = $this->Message_archive->save_msg($title, $media_file_url, $preacher, $service_date, $uploaded_by, $updated_by, $img_url, $service);
                        $msg =$msg_response->message;
                        if($msg_response->data){
                        $error = 'information';
                        } else{
                                $error = 'error';
                        }
                        // $_SESSION['services'] = $this->Services_model->get_all_services();
                        redirect('admin/add_message?msg='.$msg.'!&type='.$error);
                }
                else{
                        redirect('admin/add_message?msg=Error trying to upload  '.$m.'!%20Check%20Error%20and%20try%20again%20later!&type=error');
                }
        }

        public function delete_user()
        {
                $this->load->model('User_model');
                $id = $_REQUEST['user_id'];
                $delete = $this->User_model->deactivate_user($id);
                if($delete){
                        redirect('admin/users?user_id='.$id);
                }
                else{
                        redirect('admin/users'); 
                }
        }
        public function activate_user()
        {
                $this->load->model('User_model');
                $id = $_REQUEST['user_id'];
                $delete = $this->User_model->activate_user($id);
                if($delete){
                        redirect('admin/users?user_id='.$id);
                }
                else{
                        redirect('admin/users'); 
                }
        }
        
                public function delete_message()
        {
                $this->load->model('Message_archive');
                $id = $_REQUEST['message_id'];
                $delete = $this->Message_archive->delete_message($id);
                if($delete){
                        redirect('admin/messages?type=confirmation&msg=Sermon deleted successfully!');
                }
                else{
                        redirect('admin/messages?type=error&msg=Unsuccessful! Try Again'); 
                }
        }
        
                public function archive_message()
        {
                $this->load->model('Message_archive');
                $id = $_REQUEST['message_id'];
                $delete = $this->Message_archive->archive_message($id);
                if($delete){
                        redirect('admin/messages?type=confirmation&msg=Sermon archived successfully!');
                }
                else{
                        redirect('admin/messages?type=error&msg=Unsuccessful! Try Again'); 
                }
        }
        
                public function archive_post()
        {
                $this->load->model('Post_model');
                $id = $_REQUEST['post_id'];
                $delete = $this->Post_model->archive_post($id);
                if($delete){
                        redirect('admin/posts?type=confirmation&msg=Post archived successfully!');
                }
                else{
                        redirect('admin/posts?type=error&msg=Unsuccessful! Try Again'); 
                }
        }
        
                public function restore_post()
        {
                $this->load->model('Post_model');
                $id = $_REQUEST['post_id'];
                $delete = $this->Post_model->restore_post($id);
                if($delete){
                        redirect('admin/posts?type=confirmation&msg=Post restored successfully!');
                }
                else{
                        redirect('admin/posts?type=error&msg=Unsuccessful! Try Again'); 
                }
        }
}
