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

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

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

if(isset($_POST['ubah'])){
    $id = $_POST['idmerek'];
    $nama_merek = $_POST['nama_merek'];

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

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