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

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


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

if(isset($_POST['ubah'])){
    $id = $_POST['id_satuan'];
    $nama_satuan = $_POST['nama_satuan'];

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

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