<?php
session_start();
include ('../config/conn.php');
include ('../config/function.php');

if(isset($_POST['tambah'])){
    $nama_divisi = $_POST['nama_divisi'];


    $insert = mysqli_query($con,"INSERT INTO divisi (nama_divisi) VALUES ('$nama_divisi')") or die (mysqli_error($con));
    if($insert){
        $success = 'Berhasil menambahkan data divisi';
    }else{
        $error = 'Gagal menambahkan data divisi';
    }
    $_SESSION['success'] = $success;
    $_SESSION['error'] = $error;
    header('Location:../dashboard.php?divisi');
}

if(isset($_POST['ubah'])){
    $id = $_POST['id_divisi'];
    $nama_divisi = $_POST['nama_divisi'];

    $update = mysqli_query($con,"UPDATE divisi SET nama_divisi='$nama_divisi' WHERE id_divisi='$id'") or die (mysqli_error($con));
    
    // var_dump($update);die;
    if($update){
        $success = 'Berhasil mengubah data divisi';
    }else{
        $error = 'Gagal mengubah data divisi';
    }
    $_SESSION['success'] = $success;
    $_SESSION['error'] = $error;
    header('Location:../dashboard.php?divisi');
}

if(decrypt($_GET['act'])=='delete' && isset($_GET['id'])!=""){
    // echo $_GET['act'];die;
    $id = decrypt($_GET['id']);
    $delete = mysqli_query($con, "DELETE FROM divisi WHERE id_divisi='$id'")or die(mysqli_error($con));
    if ($delete) {
        $success = "Data divisi berhasil dihapus";
    }else{
        $error = "Data divisi gagal dihapus";
    }
    $_SESSION['success'] = $success;
    $_SESSION['error'] = $error;
    header('Location:../dashboard.php?divisi');
}
?>