<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Item_group_materials extends AdminController
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('item_group_materials_model');
        $this->load->model('item_groups_model');
        if (!is_admin()) {
            access_denied('item_group_materials');
        }
    }

    /**
     * Lista todos os materiais (Ajax)
     */
    public function index()
    {
        if ($this->input->is_ajax_request()) {
            $this->app->get_table_data('item_group_materials');
        }
        $data['title'] = _l('item_group_materials');
        $data['item_groups'] = $this->item_groups_model->get();
        $this->load->view('admin/item_group_materials/manage', $data);
    }

    /**
     * Form de criação/edição de material.
     */
    public function material($id = '')
    {
        if ($this->input->post() && !$this->input->is_ajax_request()) {
            $post = $this->input->post();
            if ($id == '') {
                $id = $this->item_group_materials_model->add($post);
                if ($id) {
                    set_alert('success', _l('added_successfully', _l('material')));
                    redirect(admin_url('item_group_materials'));
                }
                set_alert('danger', _l('problem_adding', _l('material')));
                redirect(admin_url('item_group_materials/material'));
            } else {
                $updated = $this->item_group_materials_model->edit($id, $post);
                if ($updated) {
                    set_alert('success', _l('updated_successfully', _l('material')));
                } else {
                    set_alert('danger', _l('problem_updating', _l('material')));
                }
                redirect(admin_url('item_group_materials/material/' . $id));
            }
        }

        if ($id != '') {
            $data['material'] = $this->item_group_materials_model->get($id);
            if (!$data['material']) {
                set_alert('warning', _l('material_not_found'));
                redirect(admin_url('item_group_materials'));
            }
            $title = _l('edit_material');
        } else {
            $title = _l('new_material');
        }

        $data['title']       = $title;
        $data['item_groups'] = $this->item_groups_model->get();
        $this->load->view('admin/item_group_materials/material', $data);
    }

    /**
     * Exclui um material.
     */
    public function delete($id)
    {
        $rec = $this->item_group_materials_model->get($id);
        if (!$rec) {
            set_alert('warning', _l('material_not_found'));
            redirect(admin_url('item_group_materials'));
        }
        $deleted = $this->item_group_materials_model->delete($id);
        if ($deleted) {
            set_alert('success', _l('deleted', _l('material')));
        } else {
            set_alert('danger', _l('problem_deleting', _l('material')));
        }
        redirect(admin_url('item_group_materials'));
    }
}