<?php
//error_reporting(0);
session_start();
ob_start();
include "../../../../konfig/dbconnect.php";

$db = new database();
$db->dbsql();   

include "../../../../model/viewuser.php";
include "../../../../model/viewgambar.php";
include "../../../../controller/upload.php";

$aksi = $_GET[aksi];
if($aksi=='tambah'){
    $produk = new viewproduk();
    $gambar = new viewgambar();
    $upload = new upload();

    if($_POST[key]==1){
        $lokasi_file    = $_FILES['gambar']['tmp_name'];
        $tipe_file      = $_FILES['gambar']['type'];
        $nama_file      = $_FILES['gambar']['name'];
        $acak           = rand(1,99);
        $nama_unik      = $acak.$nama_file;

        $upload->produk($nama_unik);
        $gambar->addgambartemp($_SESSION[id_user],$nama_unik);

        if (empty($_SESSION[nama])) {
            $_SESSION[nama] = $_POST[nama];
        }else{
            unset($_SESSION[nama]);
            $_SESSION[nama] = $_POST[nama];
        }

        if (empty($_SESSION[ukuran])) {
            $_SESSION[ukuran] = $_POST[ukuran];
        }else{
            unset($_SESSION[ukuran]);
            $_SESSION[ukuran] = $_POST[ukuran];
        }

        if (empty($_SESSION[berat])) {
            $_SESSION[berat] = $_POST[berat];
        }else{
            unset($_SESSION[berat]);
            $_SESSION[berat] = $_POST[berat];
        }

        if (empty($_SESSION[stock])) {
            $_SESSION[stock] = $_POST[stock];
        }else{
            unset($_SESSION[stock]);
            $_SESSION[stock] = $_POST[stock];
        }

        if (empty($_SESSION[harga])) {
            $_SESSION[harga] = $_POST[harga];
        }else{
            unset($_SESSION[harga]);
            $_SESSION[harga] = $_POST[harga];
        }

        if (empty($_SESSION[diskon])) {
            $_SESSION[diskon] = $_POST[diskon];
        }else{
            unset($_SESSION[diskon]);
            $_SESSION[diskon] = $_POST[diskon];
        }

        if (empty($_SESSION[detail])) {
            $_SESSION[detail] = $_POST[detail];
        }else{
            unset($_SESSION[detail]);
            $_SESSION[detail] = $_POST[detail];
        }

        if (empty($_SESSION[kategori])) {
            $_SESSION[kategori] = $_POST[kategori];
        }else{
            unset($_SESSION[kategori]);
            $_SESSION[kategori] = $_POST[kategori];
        }

        if (empty($_SESSION[brand])) {
            $_SESSION[brand] = $_POST[brand];
        }else{
            unset($_SESSION[brand]);
            $_SESSION[brand] = $_POST[brand];
        }

        if (empty($_SESSION[aktif])) {
            $_SESSION[aktif] = $_POST[aktif];
        }else{
            unset($_SESSION[aktif]);
            $_SESSION[aktif] = $_POST[aktif];
        }

        header("location:../../../../index.php?module=produk&act=tambah");
    }
    elseif ($_POST[key]==2) {

        $produk->addproduk($_POST[kategori],$_POST[brand],$_POST[nama],$_POST[detail],$_POST[ukuran],$_POST[berat],$_POST[stock],$_POST[harga],$_POST[diskon],$_POST[aktif]);

        $id_produk = $produk->lastproduk();

        $g = $gambar->listgambartemp($_SESSION[id_user]);

        while($i<count($g)){
            $gambar->addgambar($id_produk,$g[$i][nama]);
            $gambar->id = $g[$i][id];
            $gambar->deletegambartemp();
            $i++;
        }
        unset($i);

        unset($_SESSION[nama]);
        unset($_SESSION[ukuran]);
        unset($_SESSION[berat]);
        unset($_SESSION[stock]);
        unset($_SESSION[harga]);
        unset($_SESSION[diskon]);
        unset($_SESSION[detail]);
        unset($_SESSION[kategori]);
        unset($_SESSION[brand]);
        unset($_SESSION[aktif]);

        header("location:../../../../index.php?module=produk");
    } 
}
elseif($aksi=='edit'){
    $produk = new viewproduk();
    $gambar = new viewgambar();
    $upload = new upload();

    if($_POST[key]==1){
        $lokasi_file    = $_FILES['gambar']['tmp_name'];
        $tipe_file      = $_FILES['gambar']['type'];
        $nama_file      = $_FILES['gambar']['name'];
        $acak           = rand(1,99);
        $nama_unik      = $acak.$nama_file;

        $upload->produk($nama_unik);
        $gambar->addgambar($_POST[id],$nama_unik);

        if (empty($_SESSION[nama])) {
            $_SESSION[nama] = $_POST[nama];
        }else{
            unset($_SESSION[nama]);
            $_SESSION[nama] = $_POST[nama];
        }

        if (empty($_SESSION[ukuran])) {
            $_SESSION[ukuran] = $_POST[ukuran];
        }else{
            unset($_SESSION[ukuran]);
            $_SESSION[ukuran] = $_POST[ukuran];
        }

        if (empty($_SESSION[berat])) {
            $_SESSION[berat] = $_POST[berat];
        }else{
            unset($_SESSION[berat]);
            $_SESSION[berat] = $_POST[berat];
        }

        if (empty($_SESSION[stock])) {
            $_SESSION[stock] = $_POST[stock];
        }else{
            unset($_SESSION[stock]);
            $_SESSION[stock] = $_POST[stock];
        }

        if (empty($_SESSION[harga])) {
            $_SESSION[harga] = $_POST[harga];
        }else{
            unset($_SESSION[harga]);
            $_SESSION[harga] = $_POST[harga];
        }

        if (empty($_SESSION[diskon])) {
            $_SESSION[diskon] = $_POST[diskon];
        }else{
            unset($_SESSION[diskon]);
            $_SESSION[diskon] = $_POST[diskon];
        }

        if (empty($_SESSION[detail])) {
            $_SESSION[detail] = $_POST[detail];
        }else{
            unset($_SESSION[detail]);
            $_SESSION[detail] = $_POST[detail];
        }

        if (empty($_SESSION[kategori])) {
            $_SESSION[kategori] = $_POST[kategori];
        }else{
            unset($_SESSION[kategori]);
            $_SESSION[kategori] = $_POST[kategori];
        }

        if (empty($_SESSION[brand])) {
            $_SESSION[brand] = $_POST[brand];
        }else{
            unset($_SESSION[brand]);
            $_SESSION[brand] = $_POST[brand];
        }

        if (empty($_SESSION[aktif])) {
            $_SESSION[aktif] = $_POST[aktif];
        }else{
            unset($_SESSION[aktif]);
            $_SESSION[aktif] = $_POST[aktif];
        }

        header("location:../../../../index.php?module=produk&act=edit&id=$_POST[id]");
    }
    elseif ($_POST[key]==2) {
        $produk->id = $_POST[id];
        $produk->editproduk($_POST[kategori],$_POST[brand],$_POST[nama],$_POST[detail],$_POST[ukuran],$_POST[berat],$_POST[stock],$_POST[harga],$_POST[diskon],$_POST[aktif]);

        unset($_SESSION[nama]);
        unset($_SESSION[ukuran]);
        unset($_SESSION[berat]);
        unset($_SESSION[stock]);
        unset($_SESSION[harga]);
        unset($_SESSION[diskon]);
        unset($_SESSION[detail]);
        unset($_SESSION[kategori]);
        unset($_SESSION[brand]);
        unset($_SESSION[aktif]);

        header("location:../../../../index.php?module=produk");
    } 
}
elseif($aksi=='delete'){
    $produk = new viewproduk();
    $gambar = new viewgambar();
    
    $produk->id = $_POST[id];
    $produk->deleteproduk();

    $gambar->deletegambarproduk($_POST[id]);
    
    header("location:../../../../index.php?module=produk");
}
elseif($aksi=='deletetemp'){
    $gambar = new viewgambar();

    if (empty($_SESSION[nama])) {
            $_SESSION[nama] = $_POST[nama];
        }else{
            unset($_SESSION[nama]);
            $_SESSION[nama] = $_POST[nama];
        }

        if (empty($_SESSION[ukuran])) {
            $_SESSION[ukuran] = $_POST[ukuran];
        }else{
            unset($_SESSION[ukuran]);
            $_SESSION[ukuran] = $_POST[ukuran];
        }

        if (empty($_SESSION[berat])) {
            $_SESSION[berat] = $_POST[berat];
        }else{
            unset($_SESSION[berat]);
            $_SESSION[berat] = $_POST[berat];
        }

        if (empty($_SESSION[stock])) {
            $_SESSION[stock] = $_POST[stock];
        }else{
            unset($_SESSION[stock]);
            $_SESSION[stock] = $_POST[stock];
        }

        if (empty($_SESSION[harga])) {
            $_SESSION[harga] = $_POST[harga];
        }else{
            unset($_SESSION[harga]);
            $_SESSION[harga] = $_POST[harga];
        }

        if (empty($_SESSION[diskon])) {
            $_SESSION[diskon] = $_POST[diskon];
        }else{
            unset($_SESSION[diskon]);
            $_SESSION[diskon] = $_POST[diskon];
        }

        if (empty($_SESSION[detail])) {
            $_SESSION[detail] = $_POST[detail];
        }else{
            unset($_SESSION[detail]);
            $_SESSION[detail] = $_POST[detail];
        }

        if (empty($_SESSION[kategori])) {
            $_SESSION[kategori] = $_POST[kategori];
        }else{
            unset($_SESSION[kategori]);
            $_SESSION[kategori] = $_POST[kategori];
        }

        if (empty($_SESSION[brand])) {
            $_SESSION[brand] = $_POST[brand];
        }else{
            unset($_SESSION[brand]);
            $_SESSION[brand] = $_POST[brand];
        }

        if (empty($_SESSION[aktif])) {
            $_SESSION[aktif] = $_POST[aktif];
        }else{
            unset($_SESSION[aktif]);
            $_SESSION[aktif] = $_POST[aktif];
        }

    $gambar->id = $_GET[id];
    $gambar->deletegambartemp();
    
    header("location:../../../../index.php?module=produk&act=tambah");
}
elseif($aksi=='deletegambar'){
    $gambar = new viewgambar();

        if (empty($_SESSION[nama])) {
            $_SESSION[nama] = $_POST[nama];
        }else{
            unset($_SESSION[nama]);
            $_SESSION[nama] = $_POST[nama];
        }

        if (empty($_SESSION[ukuran])) {
            $_SESSION[ukuran] = $_POST[ukuran];
        }else{
            unset($_SESSION[ukuran]);
            $_SESSION[ukuran] = $_POST[ukuran];
        }

        if (empty($_SESSION[berat])) {
            $_SESSION[berat] = $_POST[berat];
        }else{
            unset($_SESSION[berat]);
            $_SESSION[berat] = $_POST[berat];
        }

        if (empty($_SESSION[stock])) {
            $_SESSION[stock] = $_POST[stock];
        }else{
            unset($_SESSION[stock]);
            $_SESSION[stock] = $_POST[stock];
        }

        if (empty($_SESSION[harga])) {
            $_SESSION[harga] = $_POST[harga];
        }else{
            unset($_SESSION[harga]);
            $_SESSION[harga] = $_POST[harga];
        }

        if (empty($_SESSION[diskon])) {
            $_SESSION[diskon] = $_POST[diskon];
        }else{
            unset($_SESSION[diskon]);
            $_SESSION[diskon] = $_POST[diskon];
        }

        if (empty($_SESSION[detail])) {
            $_SESSION[detail] = $_POST[detail];
        }else{
            unset($_SESSION[detail]);
            $_SESSION[detail] = $_POST[detail];
        }

        if (empty($_SESSION[kategori])) {
            $_SESSION[kategori] = $_POST[kategori];
        }else{
            unset($_SESSION[kategori]);
            $_SESSION[kategori] = $_POST[kategori];
        }

        if (empty($_SESSION[brand])) {
            $_SESSION[brand] = $_POST[brand];
        }else{
            unset($_SESSION[brand]);
            $_SESSION[brand] = $_POST[brand];
        }

        if (empty($_SESSION[aktif])) {
            $_SESSION[aktif] = $_POST[aktif];
        }else{
            unset($_SESSION[aktif]);
            $_SESSION[aktif] = $_POST[aktif];
        }

    $gambar->id = $_GET[idg];
    $gambar->deletegambar();
    
    header("location:../../../../index.php?module=produk&act=edit&id=$_POST[id]");
}