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

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

//MAIN CONDITION
if (!authen())
{
    header("location: login.php");
}
else
{
    //QUERY SQL
    $wm="SELECT * FROM $db_wilayahotr";
    $connect=mysqli_query($dbConnection, $wm);
    $dataTaken=mysqli_num_rows($connect);

    //CONDITION
    if (mysqli_num_rows($connect)>0)
    {
        $count=1;
        $wilayahOtrList="";

        while ($loopWilayahOtr=mysqli_fetch_array($connect))
        {
            //sub-condition (0=tidak tersedia 1=tersedia)
            if ($loopWilayahOtr['tersedia']==1)
            {
                $status="Tersedia";
            }
            else
            {
                $status="Tidak Tersedia";
            }

            //LIST VAR FIELD (value take from db)
            $wilayahOtrList.="<tr>
                                    <td align='center' valign='top' nowrap>
                                        <b>".$count++."</b>
                                    </td>
                                    <td>
                                        ".$loopWilayahOtr['namawilayahotr']."
                                    </td>
                                    <td>
                                        ".$loopWilayahOtr['kodewilayahotr']."
                                    </td>
                                    <td>
                                        $status
                                    </td>";


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

                        //DELETE BUTTON
                        if (fullaccess($_GET['menuid'], $_SESSION['SES_GROUPID']) || deleteaccess($_GET['menuid'], $_SESSION['SES_GROUPID']))
                        {
                            $wilayahOtrList.="<td align='center' valign='top'>
                                            <a href='wilayah_delete.php?id=".$loopWilayahOtr['idwilayahotr']."&menuid=".$_GET['menuid']."'><img src='images/database_delete.png' align='absmiddle' title='Hapus'></a>
                                        </td>";
                        }
                        else
                        {
                            $wilayahOtrList.="<td></td>";
                        }
            $wilayahOtrList.="</tr>";
        }
    }
    else
    {
        $wilayahOtrList="<tr>
                            <td>
                                <b>Data Wilayah OTR 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 Wilayah OTR' onclick=\"document.location='wilayah_add.php?menuid=".$_GET['menuid']."'\"><br><br>"; 
                                        }

                                        //MAIN CONTENT
                                        $content.="<table id='' class='display'>
                                                        <thead>
                                                            <tr>
                                                                <th align='center' valign='middle' width='120px' nowrap>
                                                                    <b>No</b>
                                                                </th>
                                                                <th align='center' valign='middle' width='120px' nowrap>
                                                                    <b>Wilayah OTR</b>
                                                                </th>
                                                                <th align='center' valign='middle' width='120px' nowrap>
                                                                    <b>Kode Wilayah OTR</b>
                                                                </th>
                                                                <th align='center' valign='middle' width='120px' nowrap>
                                                                    <b>Status Tersedia</b>
                                                                </th>
                                                                <th align='center' valign='middle' width='120px' nowrap>
                                                                    <b>Edit</b>
                                                                </th>
                                                                <th align='center' valign='middle' width='120px' nowrap>
                                                                    <b>Hapus</b>
                                                                </th>
                                                            </tr>
                                                        </thead>
                                                        <tbody>
                                                            ".$wilayahOtrList."
                                                        </tbody>
                                                    </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();

?>