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

class IncomeCategorySetup extends OWN_Controller{
    //put your code here
    function __construct() {
        parent::__construct();
        $this->admin_login_check();
        $this->load->model('incomecategorymodel/incomecategorymodel','incomecategorymodel');
    }
    
    public function index(){
        $data['allIncomeCategory']=$this->incomecategorymodel->getCategoryInfo();
        $this->load->view('common/header');
        $this->load->view('common/menu');
        $this->load->view('admin/Category/incomeCategory',$data);
        $this->load->view('common/footer');
    }
    
    public function addCategory(){
        $admin_id=$this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id !="")) 
        {
        $this->load->helper(array('url','form'));
        $this->load->library('form_validation');
        $config = array(
            array(
                'field' => 'data[category_name]',
                'label' => 'Category Name',
                'rules' => 'required|xss_clean|trim|alpha_spaces'
                )
            );
        $this->form_validation->set_rules($config);
        if ($this->form_validation->run() == FALSE) {
            $errormessage['errormessage'] = "category Info Missing......!";
            $this->session->set_userdata($errormessage);
            $data['allIncomeCategory']=$this->incomecategorymodel->getCategoryInfo();
            $this->load->view('common/header');
            $this->load->view('common/menu');
            $this->load->view('admin/Category/incomeCategory',$data);
            $this->load->view('common/footer');
        }else {
            $data = $this->input->post('data', TRUE);
            if(!empty($data)){
                $excution=  $this->incomecategorymodel->saveIncomecategory($data);
                if(!empty($excution)){
                    $message['message'] = "Category saved......!";
                    $this->session->set_userdata($message);
                    redirect(admin_Url().'/incomecategorysetup/index', 'refresh');  //redirect(admin_Url().'/courseassignsetup/courseAssign','refresh');
                }  else {
                    $errormessage['errormessage'] = "category Not saved......!";
                    $this->session->set_userdata($errormessage);
                    redirect('admin/incomecategorysetup/index', 'refresh');
                }
            }
            else{
                $errormessage['errormessage'] = "category Info Missing......!";
                $this->session->set_userdata($errormessage);
                redirect('admin/incomecategorysetup/index', 'refresh');
            }
        }
    }
    else{
            redirect(base_url('admin','refresh'));
        }
    }

    public function edit_Category($category_id){
        $admin_id=$this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id !="")) 
        {
            $category_id=(int)$category_id;
            $data['categoryById']=  $this->incomecategorymodel->getincomeCategoryById($category_id);
            //print_r($data);die();
            if(!empty($data['categoryById']))
            {
                $this->load->view('common/header');
                $this->load->view('common/menu');
                $this->load->view('admin/Category/editIncomeCtagory',$data);
                $this->load->view('common/footer');
            }
            else
            {
                redirect(admin_Url().'/incomecategorysetup/index', 'refresh');
            }
        }
        else{
            redirect(base_url('admin','refresh'));
        }
    }

    public function update_Category($category_id){
        $admin_id=$this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id !="")) 
        {
            if(isset($_POST['btnUpdate']))
            {
            $this->load->helper(array('url','form'));
            $this->load->library('form_validation');
            $config = array(
            array(
                'field' => 'data[category_name]',
                'label' => 'Category Name',
                'rules' => 'required|xss_clean|trim|alpha_spaces'
                )
            );
            $this->form_validation->set_rules($config);
            if ($this->form_validation->run() == FALSE) {
                $errormessage['errormessage'] = "category Info Missing......!";
                $this->session->set_userdata($errormessage);
                $data['allIncomeCategory']=$this->incomecategorymodel->getCategoryInfo();
                $this->load->view('common/header');
                $this->load->view('common/menu');
                $this->load->view('admin/Category/incomeCategory',$data);
                $this->load->view('common/footer');
            }
            else
            {
                $data = $this->input->post('data', TRUE);
                if(!empty($data))
                {
                    $excution=  $this->incomecategorymodel->updateIncomecategory($data,$category_id);
                    if(!empty($excution)){
                        $message['message'] = "Category Updated......!";
                        $this->session->set_userdata($message);
                        redirect(admin_Url().'/incomecategorysetup/index', 'refresh');  //redirect(admin_Url().'/courseassignsetup/courseAssign','refresh');
                    }  else {
                        $errormessage['errormessage'] = "Category Not Updated......!";
                        $this->session->set_userdata($errormessage);
                        redirect('admin/incomecategorysetup/index', 'refresh');
                    }
                }
            else
                {
                    $errormessage['errormessage'] = "Category Info Missing......!";
                    $this->session->set_userdata($errormessage);
                    redirect('admin/incomecategorysetup/index', 'refresh');
                }
            }
        }
        else{
                        redirect(admin_Url().'/incomecategorysetup/index', 'refresh'); 
        }
        }
        else{
            redirect(base_url('admin','refresh'));
        }
    }


    public function delete_Category($category_id)
    {
        $admin_id=$this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id !="")) 
        {
            $execution=$this->incomecategorymodel->deleteIncomecategory($category_id);
            if(!empty($execution)){
                $message['message'] = "Category Deleted......!";
                 $this->session->set_userdata($message);
                redirect(admin_Url().'/incomecategorysetup/index', 'refresh');
            }
            else
            {
                 $errormessage['errormessage'] = "Category Not Deleted......!";
                 $this->session->set_userdata($errormessage);
                redirect('admin/incomecategorysetup/index', 'refresh');
            }
        }
        else
        {
            redirect(base_url('admin','refresh'));
        }
    }
    
}
