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

class Menu_model extends CI_Model
{
	public function aksesUser($id_role)
	{
		$menu_value = $this->uri->segment(1); // Mengambil nilai ke-3 dari URL
	    $list_value = $this->uri->segment(2); // Mengambil nilai ke-4 dari URL

	    $url_terdeteksi = $menu_value.'/'.$list_value;


	    // cek apakah url tersebut diizinkan untuk menu
	    $dt_list_menu = $this->getSubmenuByUrl($url_terdeteksi);

	    // return $dt_list_menu;
	    $this->load->model('Admin_model', 'admin');

	    if($dt_list_menu) {

		    // return $id_role;
	    	//role id
	    	if($id_role == 1) {

	    		if($dt_list_menu['is_admin'] == 0) {
	    			return redirect('auth/blocked');
	    		}
	    	} else if($id_role == 2) {
	    		if($dt_list_menu['is_petugas'] == 0) {
	    			return redirect('auth/blocked');
	    		} 
	    	} else if($id_role == 3) {
	    		if($dt_list_menu['is_user'] == 0) {
	    			return redirect('auth/blocked');
	    		} 
	    	} else {
	    		return redirect('auth/blocked');
	    	}
	    } else {
	    	// menu belum diset
	    	return redirect('auth/blocked');
	    }
	}

	private function getSubmenuByUrl($url_terdeteksi)
	{
		$this->db->select('b.nama, a.*');
		$this->db->from('user_sub_menu a');
		$this->db->join('user_menu b', 'a.id_menu = b.id_menu');
		$this->db->where_in('a.link', $url_terdeteksi);
		return $this->db->get()->row_array();
	}

	public function ubah_stts_role($id_sub_menu, $stts, $role)
	{
		if($role == 1) {
			$data = [
	        	"is_admin" => $stts
			];
		} else if($role == 2) {
			$data = [
	        	"is_petugas" => $stts
			];
		} else if($role == 3) {
			$data = [
	        	"is_user" => $stts
			];
		}

		$this->db->where('id_sub_menu', $id_sub_menu);
		$this->db->update('user_sub_menu', $data);
	}	

	public function getAllData()
	{
		// isi query disini
		$this->db->select('*');
		$this->db->from('user_menu');
		$this->db->order_by("id_menu", "asc");
		return $this->db->get()->result_array();
	}

	public function listMenuApp()
	{
		$Allmenu = $this->getAllData();
		$result = array();
		foreach($Allmenu as $row){
			$idData = $this->getSubmenuById($row['id_menu']);

			$result[] = array(
				'nm_menu' => $row['nama'],
				'dt_sub_menu' => $idData,
			);
		}

		return $result;
	}

	public function getMenuById($id_menu)
	{
		// isi query disini
		$this->db->select('*');
		$this->db->from('user_menu');
		$this->db->where_in('id_menu', $id_menu);
		return $this->db->get()->row_array();
	}
	
	public function insert()
	{
		// isi query disini
		$data = [
        	"nama" => $this->input->post('nama', true)
		];

		$this->db->insert('user_menu', $data);
	}

	public function update()
	{
		// isi query disini
		$data = [
        	"nama" => $this->input->post('nama', true)
		];

		$this->db->where('id_menu', $this->input->post('id_menu'));
		$this->db->update('user_menu', $data);
	}

	public function delete()
	{
		// isi query disini
		return $this->db->delete('user_menu', ['id_menu' => $this->input->post('id_menu')]);
	}

	// submenu
	public function getAllSubMenu()
	{
		$this->db->select('menu.nama, sub_menu.*');
		$this->db->from('sub_menu');
		$this->db->join('user_menu', 'sub_menu.id_menu = menu.id_menu');
		return $this->db->get()->result_array();
	}

	public function getSubmenuById($id_menu)
	{
		// isi query disini
		$this->db->select('b.nama, a.*');
		$this->db->from('user_sub_menu a');
		$this->db->join('user_menu b', 'a.id_menu = b.id_menu');
		$this->db->where_in('a.id_menu', $id_menu);
		return $this->db->get()->result_array();
	}

	public function insert_sub_menu()
	{
		$data = [
        	"id_menu" => $this->input->post('id_menu', true),
        	"nm_sub_menu" => $this->input->post('nm_sub_menu', true),
        	"link" => $this->input->post('link', true),
        	"is_active" => $this->input->post('is_aktif', true),
		];

		$this->db->insert('user_sub_menu', $data);
	}

	public function update_sub_menu()
	{
		$data = [
        	"nm_sub_menu" => $this->input->post('nm_sub_menu', true),
        	"link" => $this->input->post('link', true),
        	"is_active" => $this->input->post('is_aktif', true),
		];

		$this->db->where('id_sub_menu', $this->input->post('id_sub_menu'));
		$this->db->update('user_sub_menu', $data);
	}

	public function delete_sub_menu()
	{
		// isi query disini
		return $this->db->delete('user_sub_menu', ['id_sub_menu' => $this->input->post('id_sub_menu')]);
	}

}