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

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

include "../../../../model/viewmenuweb.php";

$aksi = $_GET[aksi];
if($aksi=='tambah'){
    $menu = new viewmenuweb();

    if ($_POST[key]==1) {
        if ($_POST[pagesub]=='home' || $_POST[pagesub]=='galery' || $_POST[pagesub]=='kontak') {
            $menu->addtempsubmenu($_POST[user],'','',$_POST[pagesub],$_POST[namasub],$_POST[ordersub]);
        }elseif ($_POST[pagesub]=='list') {
            $menu->addtempsubmenu($_POST[user],'',$_POST[kategorisub],$_POST[pagesub],$_POST[namasub],$_POST[ordersub]);
        }else{
            $menu->addtempsubmenu($_POST[user],$_POST[pagesub],'','page',$_POST[namasub],$_POST[ordersub]);
        }
        

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

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

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

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

        header("location:../../../../index.php?module=menu&act=tambah");
        
    }elseif ($_POST[key]==2) {
        if ($_POST[page]=='home' || $_POST[page]=='galery' || $_POST[page]=='kontak') {
            $menu->addmenu('','',$_POST[page],$_POST[nama],$_POST[order]);
        }elseif ($_POST[pagesub]=='list') {
            $menu->addmenu('',$_POST[kategori],$_POST[page],$_POST[nama],$_POST[order]);
        }else{
            $menu->addmenu($_POST[page],'','page',$_POST[nama],$_POST[order]);
        }

        $id_menu = $menu->lastmenu();

        $m = $menu->listtempsubmenu($_SESSION[nama]);

        while($i<count($m)){
            $menu->addsubmenu($id_menu,$m[$i][id_page],$m[$i][id_katpost],$m[$i][module],$m[$i][nama],$m[$i][submenu_order]);
            $menu->deletetempsubmenu($m[$i][id]);
            $i++;
        }
        unset($i);

        unset($_SESSION[nama]);
        unset($_SESSION[page]);
        unset($_SESSION[kategori]);
        unset($_SESSION[order]);

        header("location:../../../../index.php?module=menu");
    } 
}
elseif($aksi=='edit'){
    $menu = new viewmenuweb();

    if ($_POST[key]==1) {
        if ($_POST[page]=='home' || $_POST[page]=='galery' || $_POST[page]=='kontak') {
            $menu->addsubmenu($_POST[id],'','',$_POST[pagesub],$_POST[namasub],$_POST[ordersub]);
        }elseif ($_POST[pagesub]=='list') {
            $menu->addsubmenu($_POST[id],'',$_POST[kategorisub],$_POST[pagesub],$_POST[namasub],$_POST[ordersub]);
        }else{
            $menu->addsubmenu($_POST[id],$_POST[pagesub],'','page',$_POST[namasub],$_POST[ordersub]);
        }

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

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

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

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

        header("location:../../../../index.php?module=menu&act=edit&id=$_POST[id]");
        
    }elseif ($_POST[key]==2) {
        $menu->id = $_POST[id];

        if ($_POST[page]=='home' || $_POST[page]=='galery' || $_POST[page]=='kontak') {
            $menu->editmenu('','',$_POST[page],$_POST[nama],$_POST[order]);
        }elseif ($_POST[pagesub]=='list') {
            $menu->editmenu('',$_POST[kategori],$_POST[page],$_POST[nama],$_POST[order]);
        }else{
            $menu->editmenu($_POST[page],'','page',$_POST[nama],$_POST[order]);
        }
        

        unset($_SESSION[nama]);
        unset($_SESSION[page]);
        unset($_SESSION[kategori]);
        unset($_SESSION[order]);

        header("location:../../../../index.php?module=menu");
    } 
}
elseif($aksi=='delete'){
    $menu = new viewmenuweb();
    
    $menu->id = $_GET[id];
    $menu->deletemenu();
    
    header("location:../../../../index.php?module=menu");
}
