<?php
session_start();
include ('../config/conn.php');
include ('../config/function.php');
//proses tambah
if(isset($_POST['tambah'])){
    $id = $_POST['id_peminjaman'];
    $barang_id = $_POST['barang_id'];
    $jumlah = $_POST['jumlah'];
    $tgl_pinjam = $_POST['tgl_pinjam'];
    $tgl_kembali = $_POST['tgl_kembali'];
    $konsumen = $_POST['konsumen_id'];
    $divisi = $_POST['divisi_id'];

    $insert = mysqli_query($con,"INSERT INTO peminjaman (id_peminjaman, barang_id, jumlah, tgl_pinjam, tgl_kembali, konsumen_id, divisi_id) 
    VALUES ('$id','$barang_id','$jumlah','$tgl_pinjam','$tgl_kembali','$konsumen','$divisi')") or die (mysqli_error($con));
    $update = mysqli_query($con,"UPDATE barang SET stok = stok - '$jumlah' WHERE idbarang='$barang_id'") or die (mysqli_error($con));
    if($insert){
        $success = 'Berhasil menambahkan data peminjaman';
    }else{
        $error = 'Gagal menambahkan data peminjaman';
    }
    $_SESSION['success'] = $success;
    $_SESSION['error'] = $error;
    header('Location:../dashboard.php?peminjaman');
}

if(decrypt($_GET['act'])=='delete' && isset($_GET['id'])!=""){
    // echo $_GET['act'];die;
    $id = decrypt($_GET['id']);
    $delete = mysqli_query($con, "DELETE FROM peminjaman WHERE id_peminjaman='$id'")or die(mysqli_error($con));
    if($insert & $delete) {
        $success = "Barang berhasil dikembalikan";
    }else{
        $success = "Barang berhasil dikembalikan";
    }
    $_SESSION['success'] = $success;
    header('Location:../dashboard.php?peminjaman');
}
?>
