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

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

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

$aksi = $_GET[aksi];
if($aksi=='tambah'){
    
    $user = new viewuser();
    
    if($user->searchuser($_POST[username])>0){
        header('location:../../../../index.php?module=petugas');
    }else{
        if($_POST[password1]==$_POST[password2]){
            $pass = md5($_POST[password1]);
            
            $user->adduser($_POST[username],$pass,'admin',$_POST[blokir],$_POST[nama],$_POST[level],$_POST[nip],$_POST[email],$_POST[alamat],$_POST[telpon],$_POST[keterangan]);
            
            header('location:../../../../index.php?module=petugas');
        }else{
            header('location:../../../../index.php?module=petugas');
        }
    }
}
elseif($aksi=='edit'){
    $user = new viewuser();

    $user->id = $_POST[id];
    
    if(empty($_POST[password])){
        $user->edituser('admin',$_POST[nama],$_POST[level],$_POST[nip],$_POST[email],$_POST[alamat],$_POST[telpon],$_POST[keterangan]);
    
        $user->aktifuser($_POST[username], $_POST[blokir]);

        $user->editlevel($_POST[level], $_POST[username]);
        
        header("location:../../../../index.php?module=petugas");
    }else{
        if($_POST[password1]==$_POST[password2]){
            $pass = md5($_POST[password1]);
            
            $user->edituser('admin',$_POST[nama],$_POST[level],$_POST[nip],$_POST[email],$_POST[alamat],$_POST[telpon],$_POST[keterangan]);
    
            $user->aktifuser($_POST[username], $_POST[blokir]);

            $user->editlevel($_POST[level], $_POST[username]);
            
            $user->editpassword($pass, $_POST[username]);
            
            header("location:../../../../index.php?module=petugas");
        }else{
            header("location:../../../../index.php?module=petugas");
        }
    }
}
elseif($aksi=='blokir'){
    $user = new viewuser();
    
    $user->id = $_GET[id];
    $u = $user->user();
    
    if($u[blokir]=='Y'){
        $user->aktifuser($u[username], 'N');
    }else{
        $user->aktifuser($u[username], 'Y');
    }
    
    header("location:../../../../index.php?module=petugas");
}
elseif($aksi=='delete'){
    $user = new viewuser();
    
    $user->id = $_POST[id];
    $u = $user->user();
    
    $user->deleteuser($u[username]);
    
    header("location:../../../../index.php?module=petugas");
}