<?php
class Adminpanel extends CI_Controller {


	public function dashboard()
	{   
		$this->load->library('pagination');
		$this->load->helper('form');	
		$this->load->model('articlesmodel', 'articles'); 		
		$config = [
				'base_url' => base_url('admin/dashboard'),				
				'per_page' => 20,
				'total_rows'=> $this->articles->num_rows(),
				];
		$this->pagination->initialize($config); 		
		$articles = $this->articles->articles_list( $config['per_page'], $this->uri->segment(3) );
		$this->load->view('admin/dashboard',['articles'=>$articles]);

	}

	public function add_article()
	{
        $this->load->helper('form');	
        $this->load->view('admin/add_article');

	}

	public function store_article()
	{
		$this->load->library('form_validation');
		if ($this->form_validation->run('add_article_rules')) {

				$post = $this->input->post();
                unset($post['submit']);
				$this->load->model('articlesmodel', 'articles');  
				if ($this->articles->add_article($post)) {
					
					$this->session->set_flashdata('feedback','Article Added Successfully.');
					$this->session->set_flashdata('feedback_class','alert-success');
				}

				else

				{
					$this->session->set_flashdata('feedback','Article is not added, Please try again.');
					$this->session->set_flashdata('feedback_class','alert-danger');
				}

				return redirect(base_url().'adminpanel/dashboard');

		}
		else
		{
			 $this->load->view('admin/add_article');
			
		}

	}

	public function editarticle($article_id)
	{
		$this->load->helper('form');	
		$this->load->model('articlesmodel', 'articles');
		$article = $this->articles->find_article($article_id);
		$this->load->view('admin/edit_article',['article'=>$article]);
	}

	public function updatearticle()
	{

		$this->load->library('form_validation');
		if ($this->form_validation->run('add_article_rules')) {

				$post = $this->input->post();
				$article_id = $post['article_id'];
                unset($post['submit'], $post['article_id']);
				$this->load->model('articlesmodel', 'articles');  
				if ($this->articles->update_article($article_id,$post)) {
					
					$this->session->set_flashdata('feedback','Article Upadted Successfully.');
					$this->session->set_flashdata('feedback_class','alert-success');
				}

				else

				{
					$this->session->set_flashdata('feedback','Article is not updated, Please try again.');
					$this->session->set_flashdata('feedback_class','alert-danger');
				}

				return redirect(base_url().'adminpanel/dashboard');

		}
		else
		{
			 $this->load->view('admin/edit_article');
			
		}
	}

	public function deletearticle()
	{
		$article_id = $this->input->post('article_id');
		$this->load->model('articlesmodel', 'articles');		
			if ($this->articles->delete_article($article_id)) {
					
					$this->session->set_flashdata('feedback','Article Deleted Successfully.');
					$this->session->set_flashdata('feedback_class','alert-danger');
				}

				else

				{
					$this->session->set_flashdata('feedback','Article is not deleted, Please try again.');
					$this->session->set_flashdata('feedback_class','alert-danger');
				}

				return redirect(base_url().'adminpanel/dashboard');
		
	}

	public function __construct()

	{
       
		parent::__construct();
		if( ! $this->session->userdata('user_id') )
		return redirect(base_url().'admin');

	}

}

?>