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

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

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

if(isset($_POST['ubah'])){
    $id = $_POST['id_konsumen'];
    $nama_konsumen = $_POST['nama_konsumen'];
    $lokasi = $_POST['lokasi'];

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

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