<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Temple_expenses_type extends REF_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->library('upload');
        $this->load->model('temple_expenses_type_model', '', TRUE);
        $this->uri_key_index = 0;
    }
    
    public function index() {
        $data = array();
        $data['results'] = $this->temple_expenses_type_model->list_all_types();
        if (!empty($this->session->flashdata('success'))) 
            $data['success'] = $this->session->flashdata('success');
        else 
            $data['error'] = $this->session->flashdata('error');
        
        $actions = $this->actions();
        $data['add_btn']    = 'Create New Expense Type';
        $data['view_link']  = $actions['view'] . '/';
        $data['add_link']   = $actions['add'];
        $data['edit_link']  = $actions['edit'] . '/';
        $data['delete_link']= $actions['delete'] . '/';
        
        $service_menu_id = get_master_controller_id($actions['controller']);
        $data['service_menu_id'] = array('service_menu_id' => $service_menu_id, 'menu_type' => '1');
        $this->render($data, 'temple_expense_type/list');
    }
    
    public function add() {
        $data = array();
        $actions = $this->actions();
        $data['form_action'] = $actions['insert'];
        $this->render($data, 'temple_expense_type/add');
    }
    
    public function insert() {
        $insert = array(
            'type_name' => $this->input->post('type'),
            'slug'  => get_var_name($this->input->post('type')),
            'status' => $this->input->post('status'),
            'created_by' => $this->user_data['user_id'],
            'created_on' => date('Y-m-d H:i:s')
        );
        
        $insert_id = $this->temple_expenses_type_model->insert($insert);  
        if ($insert_id > 0) {
            $this->session->set_flashdata('success', 'Temple Expense Type added successfully');    
        }
        else{
            $this->session->set_flashdata('error', 'Temple Expense Type not added!');   
        }
        
        redirect('temple_expenses_type', 'refresh');
    }
    
     public function edit($id = '') {
        if (empty($id)) {
            show_400_error();
        }
        
        $data = array();
        $data['type'] = $this->temple_expenses_type_model->get_by_id($id);
        $actions = $this->actions();
        $data['form_action'] = $actions['update'] . '/' . $id;
        $this->render($data, 'temple_expense_type/edit');
    }
    
    public function update($id = '') {
        if (empty($id)) {
            show_400_error();
        }

        $update = array(
            'type_name' => $this->input->post('type'),
            'slug'  => get_var_name($this->input->post('type')),
            'status' => $this->input->post('status'),
            'updated_by' => $this->user_data['user_id'],
            'updated_on' => date('Y-m-d H:i:s')
        );
        
        if ($this->temple_expenses_type_model->update($update, $id) > 0)
            $this->session->set_flashdata('success', 'Temple Expense Type updated successfully');  
        
        else
            $this->session->set_flashdata('error', 'Temple Expense Type not updated!');    
        
        redirect('temple_expenses_type','refresh');
    }
    
   public function view($id = '') {
        if (empty($id)) {
            show_400_error();
        }
        
        $data = array();
        $data['type'] = $this->temple_expenses_type_model->get_by_id($id);
        $this->render($data, 'temple_expense_type/view');
    }
    
    public function delete($id = '') {
        if (empty($id)) {
            show_400_error();
        }
        
        $update = array ( 
            'status' => 2,
            'updated_by' => $this->user_data['user_id'],
            'updated_on' => date('Y-m-d H:i:s')
        );
        
        if ($this->temple_expenses_type_model->update($update,$id) > 0) {
            $this->session->set_flashdata('success', 'Temple Expense Type deleted successfully');
        }
        else 
            $this->session->set_flashdata('error', 'Temple Expense Type not deleted!');
        
        redirect( 'temple_expenses_type', 'refresh');
    }
    
    public function type_name_exists() {
        $data =  $this->temple_expenses_type_model->type_name_exists($_POST['name'], $_POST['id']);
        echo json_encode($data);
        exit(0);
    } 
}
?>