<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Inventory_temple extends REF_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->library('upload');
        $this->load->model('inv_temple_model', '', TRUE);
        $this->uri_key_index = 0;
    }
    
    public function index() {
        $data = array();
        $data['results'] = $this->inv_temple_model->list_all();
        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 Temple Inventory';
        $data['view_link']  = $actions['view'] . '/';
        $data['add_link']   = $actions['add'];
        $data['edit_link']  = $actions['edit'] . '/';
        $data['delete_link']= $actions['delete'] . '/';
        
        $service_menu_id = get_inventory_controller_id($actions['controller']);
        $data['service_menu_id'] = array('service_menu_id' => $service_menu_id, 'menu_type' => '3');
        $this->render($data, 'inv_temple/list');
    }
    
    public function add() {
        $data = array();
        $actions = $this->actions();
        $data['form_action'] = $actions['insert'];
        $this->render($data, 'inv_temple/add');
    }
    
    public function insert() {
        $insert = array(
            'type' => $this->input->post('type'),
            'quantity' => $this->input->post('quantity'),
            'desc' => $this->input->post('desc'),
            'created_by' => $this->user_data['user_id'],
            'created_on' => date('Y-m-d H:i:s')
        );
        
        $insert_id = $this->inv_temple_model->insert($insert);  
        if ($insert_id > 0)
            $this->session->set_flashdata('success', 'Temple Inventory details added successfully');    
        
        else
            $this->session->set_flashdata('error', 'Temple Inventory details not added!');
        redirect('inventory_temple', 'refresh');
    }
    
    public function edit($id = '') {
        if (empty($id)) {
            show_400_error();
        }
        
        $data = array();
        $data['temple'] = $this->inv_temple_model->get_by_id($id);
        $actions = $this->actions();
        $data['form_action'] = $actions['update'] . '/' . $id;
        $this->render($data, 'inv_temple/edit');
    }
    
    public function update($id = '') {
        if (empty($id)) {
            show_400_error();
        }
        
        $update = array(
            'type' => $this->input->post('type'),
            'quantity' => $this->input->post('quantity'),
            'desc' => $this->input->post('desc'),
            'updated_by' => $this->user_data['user_id'],
            'updated_on' => date('Y-m-d H:i:s')
        );
        
        
        if ( $this->inv_temple_model->update($update, $id) > 0)
            $this->session->set_flashdata('success', 'Temple Inventory details updated successfully');  
       
        else
            $this->session->set_flashdata('error', 'Temple Inventory details not updated!');    
        
        redirect('inventory_temple','refresh');
    }
    
    public function view($id = '') {
        if (empty($id)) {
            show_400_error();
        }
        
        $data = array();
        $data['temple'] = $this->inv_temple_model->get_by_id($id);
        $this->render($data, 'inv_temple/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->inv_temple_model->update($update,$id) > 0)
            $this->session->set_flashdata('success', 'Temple Inventory details deleted successfully');
            
        else 
            $this->session->set_flashdata('error', 'Temple Inventory details not deleted!');
        
        redirect( 'inventory_temple', 'refresh');
    }
    
    public function type_exists(){
        $data = $this->inv_temple_model->type_exists($_POST['id'],$_POST['type']);
        echo json_encode($data);
        exit(0);
    }
}
?>