<?php

//SESSION
session_name("SIGNADMAUTH");
session_start();

//INCLUDE
include "common/config/config.php";
include "common/config/dbconfig.php";
include "common/functions/funcconnect.php";
openDB();

include "common/functions/funcauthen.php";
include "common/functions/funcselectdb.php";
include "common/functions/funcinsertdb.php";

//MAIN CONDITION
if (!authen())
{
    header("location: login.php");
}
else if (!fullaccess($_GET['menuid'], $_SESSION['SES_GROUPID']))
{
    header("location: index.php");
}
else
{
    //CALL VARIABLE BY NAME (in form tag)
    $kodeMerek=$_POST['kodeMerek'];
    $namaMerek=$_POST['namaMerek'];
    $merekTersedia=$_POST['status'];
    // $kodeKategori=$_POST['kodeKategori'];
    // $namaKategori=$_POST['namaKategori'];
    // $kategoriTersedia=$_POST['kategoriTersedia'];

    if (empty($kodeMerek))
    {
        $msg="Kode Merek tidak boleh kosong.<br><br>".$linkback;
    }
    else if (empty($namaMerek))
    {
        $msg="Nama Merek tidak boleh kosong.<br><br>".$linkback;
    }
    else if (empty($merekTersedia))
    {
        $msg="Merek Tersedia tidak boleh kosong.<br><br>".$linkback;
    }
    // else if (empty($kodeKategori))
    // {
    //     $msg="Kode Kategori tidak boleh kosong.<br><br>".$linkback;
    // }
    // else if (empty($namaKategori))
    // {
    //     $msg="Nama Kategori tidak boleh kosong.<br><br>".$linkback;
    // }
    // else if (empty($kategoriTersedia))
    // {
    //     $msg="Kategori Tersedia tidak boleh kosong.<br><br>".$linkback;
    // }
    else
    {
        $qim="INSERT INTO $db_merek (kodemerek, namamerek, status_tersedia_merek)  ";
        $qim.= "VALUES ('".mysqli_real_escape_string($dbConnection, $kodeMerek)."', '".mysqli_real_escape_string($dbConnection, $namaMerek)."', '".mysqli_real_escape_string($dbConnection, $merekTersedia)."')";
            

        // $qm.="INSERT INTO $db_kategoribarang (kodekategoribarang, namakategoribarang, status_tersedia_kat) 
        // VALUES ('".mysqli_real_escape_string($dbConnection, $kodeKategori)."', '".mysqli_real_escape_string($dbConnection, $namaKategori)."', '".mysqli_real_escape_string($dbConnection, $kategoriTersedia)."')
        // ";

        $rim=mysqli_query($dbConnection, $qim);

        // echo $qim."<br>".mysqli_error($dbConnection); exit;

        //sub-condition
        if ($rim)
        {
            $msg="Menambah Merek Produk sukses.<br><br>".$linkback;
            $logdesc="TAMBAH MEREK PRODUK";
            include "logakses.php";
        }
        else
        {
            $msg="Menambah Merek Produk gagal.<br><br>".$linkback;
        }
    }

    //------------------------------------------------------------------------------------------------------------------------------------------------------------

    //QUERY APP MENU
    $qq="SELECT a.appmenuname, b.parentmenuname FROM $db_appmenu a, $db_parent_menu b ";
	$qq.="WHERE a.appmenuid='".mysqli_real_escape_string($dbConnection, $_GET['menuid'])."' AND b.parentmenuid=a.parentid";
	$rr=mysqli_query($dbConnection, $qq);
	$dd=mysqli_fetch_array($rr);

    //CONTENT
    $content="<table border='0' cellpadding='6px' cellspacing='0' width='100%'>
                    <tr>
                        <td align='left' valign='top' width='100%'>
                            <b class='title'>".$dd['parentmenuname']." &gt; ".$dd['appmenuname']."</b>
                            <br><hr size='1' color='#000000'><br>

                            <table border='0' cellpadding='0' cellspacing='0'>
                                <tr>
                                    <td align='left' valign='top' width='50%'>
                                        ".$msg."
                                    </td>
                                </tr>
                            </table>

                        </td>
                    </tr>
            </table>";
}

//------------------------------------------------------------------------------------------------------------------------------------------------------------

include "webmenu.php";
include "common/include/template.php";
$temp=new template;
$temp->define_theme("themes/theme1.html");
$temp->define_tag("{PAGETITLE}", $namaaplikasi);
$temp->define_tag("{NAMAUSER}", strtoupper($_SESSION['SES_FULLNAME']));
$temp->define_tag("{APPMENU}", $webmenu);
$temp->define_tag("{APPCONTENT}", $content);
$temp->parse();
$temp->printproses();

closeDB();

?>