<?php

/* 
 * Author       : Jai K
 * Purpose      : The controller for Expenses
 * Created On   : 2017-01-25 18:34
 */

class Expenses extends REF_Controller {
    public $controller;
    
    function __construct() {
        parent::__construct();
        
        $segments = $this->uri->segments;
        $this->controller = $segments[1];
        
        $this->load->model('expenses_model');
    }
    
    function index( $id='' ) {
        
        check_permission();
        $actions = $this->actions();
        
        $data = array(
            'title'          => 'Expense',
            'controller'     => $this->controller,
            'actions'        => $actions,
            
            'success'        => $this->session->flashdata('success'),
            'error'          => $this->session->flashdata('error'),
        );
        
        
        $data['add_btn']    = 'New Expense';
        $data['view_link']  = $actions['view'] . '/';
        $data['add_link']   = $actions['add'] . '/'.$id;
        $data['edit_link']  = $actions['edit'] . '/';
        $data['delete_link']= $actions['delete'] . '/';
        
        $data['results'] = $this->expenses_model->list_all();
        //echo "<pre>Data: "; print_r( $data['results'] ); die;
        
        // Load Views
        $this->render( $data, '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'             => 'Expense',
            '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->expenses_model->manipulate();

            $msg = 'Expense Type 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->expenses_model->get_ex_by_id($id);
        endif;
        $data['expense_types'] = $this->expenses_model->get_types();
        $data['exp_types'] = $this->expenses_model->get(TBL_EXPENSES, '', array('select' => 'DISTINCT(exp_type), exp_type_slug', 'order_by' => 'exp_type'));
        // Load Views
        $this->render( $data, 'expenses/'.$page_file_name);
    }
    
    function delete( $id ) {
        $this->expenses_model->delete($id);
                
        // Redirect
        $this->session->set_flashdata('success', 'The record is deleted successfully');
        redirect( $this->controller . '/index/' );
        
    }
    
    function ajax() {
        $results =  $this->expenses_model->ajax();
        echo json_encode( $results );
    }
    
}