<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Inventory_general extends REF_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->library('upload');
        $this->load->model('inv_general_model', '', TRUE);
        $this->uri_key_index = 0;
    }
    
    public function index() {
       
        $data = array();
        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']    = 'New General Inventory';
        $data['view_link']  = $actions['view'] . '/';
        $data['edit_link']  = $actions['edit'] . '/';
        $data['add_link']   = $actions['add'];
        $data['delete_link']= $actions['delete'] . '/';
        $data['results'] = $this->inv_general_model->list_all();
        
        $service_menu_id = get_inventory_controller_id($actions['controller']);
        $data['service_menu_id'] = array('service_menu_id' => $service_menu_id, 'menu_type' => '3');
        
        //echo "<pre>"; print_r( $data['results'] ); die;
        
        $this->render($data, 'inv_general/list');
    }
    
    private function get_general_url_key($name, $id = '') {
        $url_key = get_url_key($name);
        if ($this->inv_general_model->general_url_key_exists($url_key, $id)) {
            $this->uri_key_index ++;
            return $this->get_general_url_key($name . '_' . $this->uri_key_index, $id);
        }
        
        return $url_key;
    }
    
    public function add() {
        $this->manipulate();
    }
    
    function edit( $inv_item_id ){
        $this->manipulate( $inv_item_id, array('page' => 'edit') );
    }
    
    function view( $inv_item_id ) {
        $this->manipulate( $inv_item_id, array('page' => 'view') );
        
    }
    
    function manipulate($inv_item_id, $args){
        $actions = $this->actions();
        
        $data = array();
        $post = $this->input->post();
        $page = empty( $args['page'] ) ? 'add' : $args['page'];
        
        if( !empty( $post['submit'] ) ) :
            $db_id = $this->inv_general_model->manipulate($inv_item_id);
            if( $db_id ) :
                $this->session->set_flashdata('success', 'General Inventory details '. ( $page == 'add' ? 'added' : 'updated' ) .' successfully');
            else :
                $this->session->set_flashdata('error', 'General Inventory details are failed to '. ( $page == 'add' ? 'add' : 'update' ) .'!');
            endif;
            
            redirect('inventory_general', 'refresh');
        endif;
        
        if( !empty($inv_item_id) ) :
            $data['result'] = $this->inv_general_model->get_gen_inv($inv_item_id); //_get( TBL_INV_LOG, array('inv_item_id' => $inv_item_id), array('is_single' => TRUE ));
        else :
            $data['categories'] = $this->inv_general_model->category_list();
        endif;
        
        $this->render($data, 'inv_general/'. $page);
    }
    
    
    public function delete($inv_item_id = '') {
        if (empty($inv_item_id)) {
            show_400_error();
        }
        
        $this->inv_general_model->delete($inv_item_id);
        $this->session->set_flashdata('success', 'General Inventory details deleted successfully');
        
        redirect( 'inventory_general', 'refresh');
    }
    
    public function get_items_by_cat_id(){
        $data['items'] =  $this->inv_general_model->get_items_by_cat_id($_POST['category_id']);
        echo json_encode($data);
        exit(0);
    }
    
    public function get_unit_by_item_id() {
        $data['item'] =  $this->inv_general_model->get_unit_by_item_id($_POST['item_id']);
        //echo '<pre>'; print_r( $data ); die;
        
        echo json_encode($data);
        exit(0);
    }
    
    function ajax(){
        
        echo json_encode( $this->inv_general_model->ajax() );
    }
}
?>