<?php
//error_reporting(0);
ob_start();
include "../../../../model/viewuser.php";
include "../../../../controller/upload.php";
include "../../../../konfig/dbconnect.php";
$aksi = $_GET[aksi];
if($aksi=='tambah'){
    $user = new viewuser();
    $upload = new upload();
    
    $lokasi_file    = $_FILES['gambar']['tmp_name'];
    $tipe_file      = $_FILES['gambar']['type'];
    $nama_file      = $_FILES['gambar']['name'];
    $acak           = rand(1,99);
    $nama_unik = $acak.$nama_file;
    
    if(empty($nama_file)){
        $nama = '';
    }else{
        $nama = $nama_unik;
    }

    $upload->profile($nama_unik);
    
    if($user->searchuser($_POST[username])>0){
        header('location:../../../../index.php?module=admin');
    }else{
        if($_POST[password]==$_POST[rpassword]){
            $pass = md5($_POST[password]);
            
            $user->adduser($_POST[username], $pass, 'admin', $_POST[blokir], $_POST[nama], '', $_POST[level], '', '', '', '', '', '', '', '', '', '', $nama, $_POST[nip], $_POST[email_admin], $_POST[alamat_admin], $_POST[telpon_admin], $_POST[lab]);
            
            header('location:../../../../index.php?module=admin');
        }else{
            header('location:../../../../index.php?module=admin');
        }
    }
}
elseif($aksi=='delete'){
    $user = new viewuser();
    $user->id = $_GET[id];
    $u = $user->user();
    
    $user->deleteuser($u[username]);
    
    header('location:../../../../index.php?module=admin');
}
elseif($aksi=='edit'){
    $user = new viewuser();
    $upload = new upload();
    
    $lokasi_file    = $_FILES['gambar']['tmp_name'];
    $tipe_file      = $_FILES['gambar']['type'];
    $nama_file      = $_FILES['gambar']['name'];
    $acak           = rand(1,99);
    $nama_unik      = $acak.$nama_file;

    $upload->profile($nama_unik);
    
    if($nama_file == ''){
        $nama = "".$_POST['logo']."";
    }else{
        $nama = $nama_unik;
    }
    
    $user->id = $_POST[id];
    
    if(empty($_POST[password])){
        $user->edituser($_POST[nama], '', '', '', '', '', '', '', '', '', '', '', $nama, $_POST[nip], $_POST[email_admin], $_POST[alamat_admin], $_POST[telpon_admin], $_POST[lab]);
    
        $user->aktifuser($_POST[username], $_POST[blokir]);

        $user->editlevel($_POST[level], $_POST[username]);
        
        header("location:../../../../index.php?module=admin&act=edit&id=$_POST[id]");
    }else{
        if($_POST[password]==$_POST[rpassword]){
            $pass = md5($_POST[password]);
            
            $user->edituser($_POST[nama], '', '', '', '', '', '', '', '', '', '', '', $nama, $_POST[nip], $_POST[email_admin], $_POST[alamat_admin], $_POST[telpon_admin], $_POST[lab]);
    
            $user->aktifuser($_POST[username], $_POST[blokir]);

            $user->editlevel($_POST[level], $_POST[username]);
            
            $user->editpassword($_POST[password], $_POST[username]);
            
            header("location:../../../../index.php?module=admin&act=edit&id=$_POST[id]");
        }else{
            header("location:../../../../index.php?module=admin&act=edit&id=$_POST[id]");
        }
    }
}