<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Menu extends CI_Controller
{

    function __construct()
    {
        parent::__construct();
        $this->load->model(array('Menu_model', 'Ion_auth_model', 'Web_style_model'));
        $this->load->library(array('ion_auth', 'form_validation', 'upload'));
    }

    public function index()
    {
        $menu = $this->Menu_model->get_all();
        $web_style = $this->Web_style_model->get_style();

        $data = array(
            'menu_data' => $menu,
            'web_meta_color' => $web_style->meta_color,
            'web_navbar_color' => $web_style->navbar_color,
            'web_box_color' => $web_style->box_color,
            'web_box_bottom' => $web_style->box_bottom,
        );

        $this->template->load('template', 'menu/menu_list', $data);
    }

    public function master_data()
    {
        $menu = $this->Menu_model->get_master();
        $web_style = $this->Web_style_model->get_style();

        $data = array(
            'menu_data' => $menu,
            'web_meta_color' => $web_style->meta_color,
            'web_navbar_color' => $web_style->navbar_color,
            'web_box_color' => $web_style->box_color,
            'web_box_bottom' => $web_style->box_bottom,
        );

        $this->template->load('template', 'menu/menu_master', $data);
    }

    public function read($id)
    {
        $row = $this->Menu_model->get_by_id($id);
        $web_style = $this->Web_style_model->get_style();
        if ($row) {
            $data = array(
                'id' => $row->id,
                'name' => $row->name,
                'link' => $row->link,
                'icon' => $row->icon,
                'is_active' => $row->is_active,
                'is_parent' => $row->is_parent,
                'web_meta_color' => $web_style->meta_color,
                'web_navbar_color' => $web_style->navbar_color,
                'web_box_color' => $web_style->box_color,
                'web_box_bottom' => $web_style->box_bottom,
            );
            $this->template->load('template', 'menu/menu_read', $data);
        } else {
            $this->session->set_flashdata('message', '4444');
            redirect(site_url('menu'));
        }
    }

    public function create()
    {
        $web_style = $this->Web_style_model->get_style();
        $data = array(
            'button' => 'Simpan',
            'action' => site_url('menu/create_action'),
            'id' => set_value('id'),
            'name' => set_value('name'),
            'link' => set_value('link'),
            'icon' => set_value('icon'),
            'is_active' => set_value('is_active'),
            'is_parent' => set_value('is_parent'),
            'web_meta_color' => $web_style->meta_color,
            'web_navbar_color' => $web_style->navbar_color,
            'web_box_color' => $web_style->box_color,
            'web_box_bottom' => $web_style->box_bottom,
        );
        $this->template->load('template', 'menu/menu_form', $data);
    }

    public function create_action()
    {
        $this->_rules();

        if ($this->form_validation->run() == FALSE) {
            $this->create();
        } else {
            $data = array(
                'name' => $this->input->post('name', TRUE),
                'link' => $this->input->post('link', TRUE),
                'icon' => $this->input->post('icon', TRUE),
                'is_active' => $this->input->post('is_active', TRUE),
                'is_parent' => $this->input->post('is_parent', TRUE),
            );

            $this->Menu_model->insert($data);
            $this->session->set_flashdata('message', '1111');
            redirect(site_url('menu'));
        }
    }

    public function update($id)
    {
        $row = $this->Menu_model->get_by_id($id);
        $web_style = $this->Web_style_model->get_style();

        if ($row) {
            $data = array(
                'button' => 'Update',
                'action' => site_url('menu/update_action'),
                'id' => set_value('id', $row->id),
                'name' => set_value('name', $row->name),
                'link' => set_value('link', $row->link),
                'icon' => set_value('icon', $row->icon),
                'is_active' => set_value('is_active', $row->is_active),
                'is_parent' => set_value('is_parent', $row->is_parent),
                'web_meta_color' => $web_style->meta_color,
                'web_navbar_color' => $web_style->navbar_color,
                'web_box_color' => $web_style->box_color,
                'web_box_bottom' => $web_style->box_bottom,
            );
            $this->template->load('template', 'menu/menu_form', $data);
        } else {
            $this->session->set_flashdata('message', '4444');
            redirect(site_url('menu'));
        }
    }

    public function update_action()
    {
        $this->_rules();

        if ($this->form_validation->run() == FALSE) {
            $this->update($this->input->post('id', TRUE));
        } else {
            $data = array(
                'name' => $this->input->post('name', TRUE),
                'link' => $this->input->post('link', TRUE),
                'icon' => $this->input->post('icon', TRUE),
                'is_active' => $this->input->post('is_active', TRUE),
                'is_parent' => $this->input->post('is_parent', TRUE),
            );

            $this->Menu_model->update($this->input->post('id', TRUE), $data);
            $this->session->set_flashdata('message', '2222');
            redirect(site_url('menu'));
        }
    }

    public function delete($id)
    {
        $row = $this->Menu_model->get_by_id($id);

        if ($row) {
            $this->Menu_model->delete($id);
            $this->session->set_flashdata('message', '3333');
            redirect(site_url('menu'));
        } else {
            $this->session->set_flashdata('message', '4444');
            redirect(site_url('menu'));
        }
    }

    public function _rules()
    {
        $this->form_validation->set_rules('name', 'name', 'trim|required');
        $this->form_validation->set_rules('link', 'link', 'trim|required');
        $this->form_validation->set_rules('icon', 'icon', 'trim|required');
        $this->form_validation->set_rules('is_active', 'is active', 'trim|required');
        $this->form_validation->set_rules('is_parent', 'is parent', 'trim|required');

        $this->form_validation->set_rules('id', 'id', 'trim');
        $this->form_validation->set_error_delimiters('<span class="text-danger">', '</span>');
    }
}
