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

class Menu extends CI_Controller 
{
	public function __construct()
	{
	 	parent::__construct();
		cek_login();
		$this->load->model('Menu_model', 'menu');
		$this->menu->aksesUser($this->session->userdata('login_session')['role']);
	}

	public function index()
	{
		// menampilkan data Menu
		$data['title'] = 'Daftar Menu';
		$data['user'] = $this->db->get_where('user', ['id_user' => $this->session->userdata('login_session')['user']]);
		$data['profile'] = $this->db->get_where('profile', ['id_profile' => 1])->row_array();

		// menampilkan dari model semua jabatan
		$data['menu'] = $this->menu->getAllData();

        $this->template->load('templates/dashboard', 'menu/index', $data);
	}

	public function tambah()
	{
		// menambah menu
		$this->menu->insert();
		$this->session->set_flashdata('message', '<div class="alert alert-success" role="alert">Menu baru berhasil ditambahkan!
				<button type="button" class="close" data-dismiss="alert" aria-label="Close">
			    	<span aria-hidden="true">&times;</span>
			  	</button></div>');
    		redirect('menu/');
	}

	public function ubah()
	{
		// menambah menu
		$this->menu->update();
		$this->session->set_flashdata('message', '<div class="alert alert-success" role="alert">Menu berhasil diubah!
				<button type="button" class="close" data-dismiss="alert" aria-label="Close">
			    	<span aria-hidden="true">&times;</span>
			  	</button></div>');
    		redirect('menu/');
	}

	public function hapus()
	{
		// menambah menu
		$this->menu->delete();
		$this->session->set_flashdata('message', '<div class="alert alert-danger" role="alert">Menu berhasil dihapus!
				<button type="button" class="close" data-dismiss="alert" aria-label="Close">
			    	<span aria-hidden="true">&times;</span>
			  	</button></div>');
    		redirect('menu/');
	}

	// mengatasi submenu aplikasi
	public function submenu($id_menu){
		// menampilkan data Menu
		$data['title'] = 'Daftar Menu';
		$data['user'] = $this->db->get_where('user', ['id_user' => $this->session->userdata('login_session')['user']]);
		$data['profile'] = $this->db->get_where('profile', ['id_profile' => 1])->row_array();

		// menampilkan dari model semua jabatan
		$data['menu'] = $this->menu->getMenuById($id_menu);
		$data['sub_menu'] = $this->menu->getSubmenuById($id_menu);


        $this->template->load('templates/dashboard', 'menu/sub_menu', $data);
	}

	public function tambah_sub_menu()
	{
		// menambah menu
		$id_menu = $this->input->post('id_menu', true);

		$this->menu->insert_sub_menu();
		$this->session->set_flashdata('message', '<div class="alert alert-success" role="alert">Submenu baru berhasil ditambahkan!
				<button type="button" class="close" data-dismiss="alert" aria-label="Close">
			    	<span aria-hidden="true">&times;</span>
			  	</button></div>');
    		redirect('menu/submenu/'.$id_menu);
	}

	public function ubah_sub_menu()
	{
		// menambah menu
		$id_menu = $this->input->post('id_menu', true);
		
		$this->menu->update_sub_menu();
		$this->session->set_flashdata('message', '<div class="alert alert-success" role="alert">Submenu berhasil diubah!
				<button type="button" class="close" data-dismiss="alert" aria-label="Close">
			    	<span aria-hidden="true">&times;</span>
			  	</button></div>');
    		redirect('menu/submenu/'.$id_menu);
	}

	public function hapus_sub_menu()
	{
		// menambah menu
		$id_menu = $this->input->post('id_menu', true);
		$this->menu->delete_sub_menu();
		$this->session->set_flashdata('message', '<div class="alert alert-danger" role="alert">Submenu berhasil dihapus!
				<button type="button" class="close" data-dismiss="alert" aria-label="Close">
			    	<span aria-hidden="true">&times;</span>
			  	</button></div>');
    		redirect('menu/submenu/'.$id_menu);
	}

	public function list()
	{
		$data['title'] = 'Daftar Menu Aplikasi';
		$data['user'] = $this->db->get_where('user', ['email' => $this->session->userdata('email')])->row_array();
		$data['profile'] = $this->db->get_where('profile', ['id_profile' => 1])->row_array();

		// menampilkan dari model semua jabatan
		$data['dt_menu'] = $this->menu->listMenuApp();

        $this->template->load('templates/dashboard', 'menu/list', $data);
	}

	public function ubah_stts()
	{
		$data['user'] = $this->db->get_where('user', ['email' => $this->session->userdata('email')])->row_array();

		$id_menu = $this->input->post('id_menu', true);
		$is_admin = $this->input->post('is_admin', true);
		$is_petugas = $this->input->post('is_petugas', true);
		$is_user = $this->input->post('is_user', true);
		// eksekusi admin aktif
		if($is_admin) {
			foreach($is_admin as $row){
				// panggil dari menu modal
				$this->menu->ubah_stts_role($row, 1, 1);
			}
		}
		// eksekusi member aktif
		if($is_petugas) {
			foreach($is_petugas as $row_){
				// panggil dari menu modal
				$this->menu->ubah_stts_role($row_, 1, 2);
			}
		}

		// eksekusi member aktif
		if($is_user) {
			foreach($is_user as $row_){
				// panggil dari menu modal
				$this->menu->ubah_stts_role($row_, 1, 3);
			}
		}

		$dt_submenu = $this->menu->getSubmenuById($id_menu);
		$array_sub_menu = array();
		foreach($dt_submenu as $row__){
			$array_sub_menu[] = $row__['id_sub_menu'];
		}


		// is_admin
		if (!empty($is_admin)) {
		    // jika ada data admin
		    $result_admin = array_values(array_diff($array_sub_menu, $is_admin));
		} else {
		    // jika null / kosong, tampilkan semua submenu
		    $result_admin = $array_sub_menu;
		}
		// is_petugas
		if (!empty($is_petugas)) {
		    // jika ada data admin
		    $result_petugas = array_values(array_diff($array_sub_menu, $is_petugas));
		} else {
		    // jika null / kosong, tampilkan semua submenu
		    $result_petugas = $array_sub_menu;
		}
		// is_user
		if (!empty($is_user)) {
		    // jika ada data admin
		    $result_user = array_values(array_diff($array_sub_menu, $is_user));
		} else {
		    // jika null / kosong, tampilkan semua submenu
		    $result_user = $array_sub_menu;
		}

		// eksekusi admin non aktif
		if($result_admin) {
			foreach($result_admin as $row___){
				// panggil dari menu modal
				$this->menu->ubah_stts_role($row___, 0, 1);
			}
		}


		// eksekusi member non aktif
		if($result_petugas) {
			foreach($result_petugas as $row____){
				// panggil dari menu modal
				$this->menu->ubah_stts_role($row____, 0, 2);
			}
		}
		// eksekusi member non aktif
		if($result_user) {
			foreach($result_user as $row____){
				// panggil dari menu modal
				$this->menu->ubah_stts_role($row____, 0, 3);
			}
		}

		$this->session->set_flashdata('message', '<div class="alert alert-success" role="alert">Role berhasil disimpan!
				<button type="button" class="close" data-dismiss="alert" aria-label="Close">
			    	<span aria-hidden="true">&times;</span>
			  	</button></div>');
    		redirect('menu/submenu/'.$id_menu);
	}
}