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

class Add_category extends CI_Controller {

	
	public function __construct()
        {
                parent::__construct();
                $this->load->library('session');
                $this->load->model('admin/Add_category_model');
                if(!isset($_SESSION['admin'])){redirect('admin/Login');}
        }

	

	public function index()
	{
		$this->load->view('admin/header');
		$this->load->view('admin/add-category');
		$this->load->view('admin/footer');
	}

	public function insert()
	{
		// 
		$data = $this->Add_category_model->db_insert();
		if($data == 1){
			$this->session->set_flashdata("success"," Data sucessfully Saved.");
				$this->output->set_header('refresh:1; url='.site_url("admin/Add_category"));
		}elseif($data == 2){
			echo 'Not Inserted';
		}else{
			$this->session->set_flashdata("same"," This Category Already Exist.");
				$this->output->set_header('refresh:1; url='.site_url("admin/Add_category"));
		}
	}

	public function view(){
		$data['view_all'] = $this->Add_category_model->get_all();
		$this->load->view('admin/header');
		$this->load->view('admin/category-table', $data);
		$this->load->view('admin/footer');
	}

	public function edit($id){
		$data['data'] = $this->Add_category_model->select($id);
		$data['id'] = $id;
		$this->load->view('admin/header');
		$this->load->view('admin/add-category', $data);
		$this->load->view('admin/footer');
	}

	public function update()
	{
		$data = $this->Add_category_model->update($_POST);
		if($data == 1){
			$this->session->set_flashdata("same"," This Category Already Exist Your Other Data is Updated.");
				$this->output->set_header('refresh:1; url='.site_url("admin/Add_category/view"));
		}elseif($data == 2){
			$this->session->set_flashdata("error"," Something is Wrong");
				$this->output->set_header('refresh:1; url='.site_url("admin/Add_category/view"));
		}elseif ($data == 3) {
			$this->session->set_flashdata("success"," Your All Date Sucessfuly Updated");
				$this->output->set_header('refresh:1; url='.site_url("admin/Add_category/view"));
		}else{
			$this->session->set_flashdata("error"," Something is Wrong");
				$this->output->set_header('refresh:1; url='.site_url("admin/Add_category/view"));
		}
	}

	public function delete($id)
	{
		$data = $this->Add_category_model->delete($id);
		if($data == 1){
			$this->session->set_flashdata("success"," Category Deleted Done");
				$this->output->set_header('refresh:1; url='.site_url("admin/Add_category/view"));
		}else{
			$this->session->set_flashdata("error"," Something is Wrong");
				$this->output->set_header('refresh:1; url='.site_url("admin/Add_category/view"));
		}
	}


	public function toindex($id)
	{
		$data = $this->Add_category_model->add_to_index($id);
		if($data == 1){
			$this->session->set_flashdata("success"," Category Added To Index Page");
				$this->output->set_header('refresh:1; url='.site_url("admin/Add_category/view"));
		}else{
			$this->session->set_flashdata("error"," Something is Wrong");
				$this->output->set_header('refresh:1; url='.site_url("admin/Add_category/view"));
		}
	}

	public function removeindex($id)
	{
		$data = $this->Add_category_model->remove_from_index($id);
		if($data == 1){
			$this->session->set_flashdata("success"," Category Remove from Index Page");
				$this->output->set_header('refresh:1; url='.site_url("admin/Add_category/view"));
		}else{
			$this->session->set_flashdata("error"," Something is Wrong");
				$this->output->set_header('refresh:1; url='.site_url("admin/Add_category/view"));
		}
	}
}
