<?php
class Temple_expenses extends REF_Controller {
    public $controller;
    
    function __construct() {
        parent::__construct();
        
        $segments = $this->uri->segments;
        $this->controller = $segments[1];
        
        $this->load->model('temple_expenses_model');
    }
    
    function index( $id='' ) {
        
        check_permission();
        $actions = $this->actions();
        
        $data = array(
            'title'          => 'Temple Expenses',
            'controller'     => $this->controller,
            'actions'        => $actions,            
            'success'        => $this->session->flashdata('success'),
            'error'          => $this->session->flashdata('error'),
        );
        
        
        $data['add_btn']    = 'New Temple Expenses';
        $data['view_link']  = $actions['view'] . '/';
        $data['add_link']   = $actions['add'] . '/'.$id;
        $data['edit_link']  = $actions['edit'] . '/';
        $data['delete_link']= $actions['delete'] . '/';
        
        $data['results'] = $this->temple_expenses_model->list_all();
        //echo "<pre>Data: "; print_r( $data['results'] ); die;
        
        // Load Views
        $this->render( $data, 'temple_expenses/index' );
    }
    
    function add( $id='' ) {
        $this->manipulate( $id );
    }
    
    function edit( $id ) {
        $this->manipulate( $id, array( 'page' => 'edit' ) );
    }
    
    function view( $id ) {
        $this->manipulate( $id, array( 'page' => 'view' ) );
    }
    
    function manipulate( $id, $args = array() ) {
        $actions 		= $this->actions();
        $post 			= $this->input->post();
        #echo '<pre>'; print_r($post);die;
        $session_data 	= $this->session->userdata(TBL_PREFIX . 'user_logged_in');
        $token 			= $this->session->userdata('rtoken-'.$session_data['user_id'] );
        
        $page 			= $page_file_name = !empty( $args['page'] ) ? $args['page'] : 'add';
        $page_file_name = $page == 'pdf' ? 'view' : $page_file_name;
        
        $data = array(
            'id'                => $id,
            'title'             => 'Temple Expenses',
            'is_'.$page         => TRUE,
            'controller'        => $this->controller,
            'actions'           => $actions,
            'form_action'       => base_url() . $this->controller .'/'.$page.'/'.$id,
            'success'           => $this->session->flashdata('success'),
            'error'             => $this->session->flashdata('error'),            
            'token'             => $this->auto_generation_code( rand( 999, 9999 ) ),
        );
        
        $this->session->set_userdata( 'rtoken-'.$session_data['user_id'], $data['token'] );
        
        if( !empty( $post['submit'] ) && $post['token'] == $token ) :
            // insert
            $insert_id 	= $this->temple_expenses_model->manipulate();

            $msg 		= 'Temple Expense is '. ( $page == 'add' ? 'added' : 'updated' ) .' successfully.';
            $this->session->set_flashdata('success', $msg);

            if($page == 'add') :
                redirect( $this->controller . '/add/' );
            else :
                // Redirect
                redirect( $this->controller . '/index/' );
            endif;

        endif;
        
        if( !empty( $id ) ) :
           $data['edit_data'] = $this->temple_expenses_model->get_ex_by_id($id);
        endif;
		
        $data['expense_types'] 	= $this->temple_expenses_model->get_types();
        $data['exp_types'] 		= $this->temple_expenses_model->get(TBL_TEMPLE_EXPENSES, '', array('select' => 'DISTINCT(exp_type_id), exp_type_name', 'order_by' => 'exp_type_id'));
        // Load Views
        $this->render( $data, 'temple_expenses/'.$page_file_name);
    }
    
    function delete( $id ) {
        $this->temple_expenses_model->delete($id);
        // Redirect
        $this->session->set_flashdata('success', 'The record is deleted successfully');
        redirect( $this->controller . '/index/' );        
    }
    
    function ajax() {
        $results =  $this->temple_expenses_model->ajax();
        echo json_encode( $results );
    }
    
}