<?php
//error_reporting(0);
ob_start();
include "../../../../konfig/dbconnect.php";

$db = new database();
$db->dbsql();

include "../../../../model/viewlevel.php";
include "../../../../model/viewmenu.php";

$aksi = $_GET[aksi];
if($aksi=='tambah'){
    $level = new viewlevel();
    $menu = new viewmenu();
    
    $acak    = rand(1,99);
    $session = $acak.$_POST[nama];
    
    $level->addlevel($_POST[nama], '', '', $_POST[aktif], $session);
    
    $id = $level->searchid($session);
    $level->id = $id; 
    
    $m = $menu->menu('admin','','admin');
                                            
    $no = 1;
    while($i<count($m)){
        $s = $menu->submenu($m[$i][id],'','admin','');

        $nos=1;
        while($n<count($s)){
            if($_POST["check$no$nos"]==$s[$n][id]){
                $level->addotoritas($_POST["check$no"], $_POST["check$no$nos"]);
            }
            $n++; 
            $nos++;
        }
        unset($n);
                                                
        $i++;
        $no++;
    }
    
    header('location:../../../../index.php?module=otoritas');
}
elseif($aksi=='edit'){
    $level = new viewlevel();
    $menu = new viewmenu();
    
    $level->id = $_POST[id]; 
    
    $level->editlevel($_POST[nama], '', '', $_POST[aktif]);
    
    $level->deleteotoritas();
    
    $m = $menu->menu('admin','','admin');
                                            
    $no = 1;
    while($i<count($m)){
        $s = $menu->submenu($m[$i][id],'','admin','');

        $nos=1;
        while($n<count($s)){
            if($_POST["check$no$nos"]==$s[$n][id]){
                $level->addotoritas($_POST["check$no"], $_POST["check$no$nos"]);
            }
            $n++; 
            $nos++;
        }
        unset($n);
                                                
        $i++;
        $no++;
    }
    
    header("location:../../../../index.php?module=otoritas&act=edit&id=$_POST[id]");
}
elseif($aksi=='delete'){
    $level = new viewlevel();
    
    $level->id = $_GET[id];
    
    $level->deletelevel();
    $level->deleteotoritas();
    
    header('location:../../../../index.php?module=otoritas');
}
