<?php

session_name("SIGNADMAUTH");
session_start();

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";

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

if (!authen())
{
    header("location: login.php");
}
else 
{
    //QUERY SQL
     $qmm="SELECT * FROM $db_merek"; //ORDER BY here...
     $conn=mysqli_query($dbConnection, $qmm);
    //  echo $conn."<br>".mysqli_error($dbConnection); exit; //cek query err
     $dataTaken=mysqli_num_rows($conn);

    //CONDITION 1
    if (mysqli_num_rows($conn)>0)
    {
        $count=1;
        $merekList="";
        
        while ($loopMerek=mysqli_fetch_array($conn))
        { 
            //sub-condition (0=tidak tersedia 1=tersedia)
            if ($loopMerek['status_tersedia_merek']==1)
            {
                $status="Tersedia";
            }
            else
            {
                $status="Tidak Tersedia";
            }

            //LIST VAR FIELD
           $merekList.="<tr>
                            <td align='center' valign='top' nowarp>
                                <b>".$count++."</b>
                            </td>
                            <td align='center' valign='top' nowarp>
                                ".$loopMerek['kodemerek']."
                            </td>
                            <td align='center' valign='top' nowarp>
                                ".$loopMerek['namamerek']."
                            </td>
                            <td align='center' valign='top' nowarp>
                                $status
                            </td>";

                //EDIT BUTTON
                if (fullaccess($_GET['menuid'], $_SESSION['SES_GROUPID']) || editaccess($_GET['menuid'], $_SESSION['SES_GROUPID']))
                {
                  $merekList.="<td align='center' valign='middle'>
                                    <a href='merek_edit.php?id=".$loopMerek['idmerek']."&menuid=".$_GET['menuid']."'><img src='images/database_edit.png' align='absmiddle' title='Edit'></a>
                                </td>";  
                }
                else
                {
                    $merekList.="<td></td>";
                }

                //DELETE BUTTON
                if (fullaccess($_GET['menuid'], $_SESSION['SES_GROUPID']) || deleteaccess($_GET['menuid'], $_SESSION['SES_GROUPID']))
                {
                  $merekList.="<td align='center' valign='top'>
                                    <a href='merek_delete.php?id=".$loopMerek['idmerek']."&menuid=".$_GET['menuid']."'><img src='images/database_delete.png' align='absmiddle' title='Hapus'></a>
                                </td>";
                }
                else
                {
                    $merekList.="<td></td>";
                }
            $merekList.="</tr>";             
         }
    }
    else
    {
        $merekList="<tr>
                        <td>
                            <b>Data Merek tidak ditemukan.</b>
                        </td>
                    </tr>";
    }

    //----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    
    //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' widht='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'>";

                                        //ADD BUTTON
                                        if (fullaccess($_GET['menuid'], $_SESSION['SES_GROUPID']) || addaccess($_GET['menuid'], $_SESSION['SES_GROUPID']))
                                        {
                                           $content.="<input type='button' value=' [+] Tambah Merek Produk' onclick=\"document.location='merek_add.php?menuid=".$_GET['menuid']."'\"><br><br>"; 
                                        }

                                        //MAIN CONTENT
                                        $content.="<table id='' class='display'>
                                                        <thead>
                                                            <tr>
                                                                <th align='center' valign='middle' width='130px' nowrap>
                                                                    <b>No</b>
                                                                </th>
                                                                <th align='center' valign='middle' width='130px' nowrap>
                                                                    <b>Kode Merek</b>
                                                                </th>
                                                                <th align='center' valign='middle' width='130px' nowrap>
                                                                    <b>Nama Merek</b>
                                                                </th>
                                                                <th align='center' valign='middle' width='130px' nowrap>
                                                                    <b>Status</b>
                                                                </th>
                                                                <th align='center' valign='middle' width='130px' nowrap>
                                                                    <b>Edit</b>
                                                                </th>
                                                                <th align='center' valign='middle' width='130px' nowrap>
                                                                    <b>Hapus</b>
                                                                </th>
                                                            </tr>
                                                        </thead>
                                                        <tbody>
                                                            ".$merekList."
                                                        </tbody>
                                                    </table>
                                        
                                    </td>
                                </tr>
                            </table>

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

}

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

//TEMPLATE
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();

?>