<?php

if($_REQUEST['ajax']) {

  switch($_REQUEST['code']) {

    case "urutan":

           $ssql="SELECT MAX(fnurut) FROM tlmenu where ftmenu_category='{$_REQUEST['ftmenu_category']}' and ftmenu_type ='{$_REQUEST['ftmenu_type']}'";


           $urutan=RetRstField($ssql);
           $urutan = $urutan+1;

           ?>
                   document.getElementById('fnurut').value='<?php echo $urutan; ?>';
           <?php


      break;


    case "ftmenu_code":

      if(RetRstField("SELECT ftmenu_code FROM tlmenu WHERE ftmenu_code='{$_REQUEST['ftmenu_code']}'"))
      {
        ?>
          alert('Kode Menu sudah dipakai... ');
        <?php
      }
      else
      {
        ?>
          submitbutton('Simpan','','','','');
        <?php
      }
    break;

  }
}

// $panjang="document.getElementById('fnurut').value='';";
// $objAxpDB->FormJS($panjang);

$objAxpDB->ViewName("Daftar Menu System");

$objAxpDB->ViewSQL("SELECT
                      	     `fnid`,
                             `ftmenu_code`,
                             `ftmenu_name`,
                             `ftmenu_category`,
                             `ftmenu_type`,
                             `a_href` ,
                             `fnurut`,
                            case when fnstatus=0 then 'Tidak Aktif' else 'Aktif' end as Status
                       FROM tlmenu ORDER BY ftmenu_code DESC");

$objAxpDB->ViewCaption("id,Kode Menu,Nama Menu,Nama Module, Jenis Menu , Link Menu,No Urut, Status");

$objAxpDB->FormName("Menu System");

$objAxpDB->FormSQL("SELECT   `fnid`,
                             `ftmenu_code`,
                             `ftmenu_name`,
                             `ftmenu_category`,
                             `ftmenu_type`,
                             `a_href` ,
                             `img_alt` ,
                             `fnurut`,
                             `fnstatus`,
                             `fticon`
                       FROM tlmenu");

$objAxpDB->FormCaption("id,Kode Menu,Nama Menu,Nama Module, Jenis Menu , Link Menu, Keterangan,No Urut, Status, Icon");

$objAxpDB->FormPosition("HD,TL, TL, TL, TL, TL, TL, TR, TR, TR");

$objAxpDB->SetStatus("id,ftstock_category_code");

$objAxpDB->GridSQL(0, "SELECT '' as test from tlmenu");
$objAxpDB->GridCaption(0, "No.");
$objAxpDB->GridHidden(0,'1');


$objAxpDB->Lookup("ftmenu_category", "SELECT ftmodule_code,ftmodule_name FROM tsmodule WHERE fnstatus = 1","ftmodule_code","ftmodule_name", -1,true);

$objAxpDB->Lookup("ftmenu_type", "SELECT DISTINCT ftmenu_type, ftmenu_type FROM tlmenu","ftmenu_type","ftmenu_type", -1,true);

$objAxpDB->Lookup('fnstatus', 'SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status where fntrans_status_code<>2 and fntrans_group_code=9', 'fntrans_status_code', 'fttrans_status_name', 1,true);

 $kategori = $_SESSION['ftmenu_category'];

$sql = "WHERE ftmenu_category='{$kategori['urut']}'";
// print_r($_SESSION['ftmenu_category']);

$objAxpDB->Lookup('fnurut', "SELECT fnurut, ftmenu_name,ftmenu_category  FROM tlmenu  order by fnurut", 'fnurut', 'ftmenu_category', 3,false);

$objAxpDB->Lookup('ftmenu_code', "SELECT ftmenu_code,ftmenu_name FROM tlmenu ", 'ftmenu_code', 'ftmenu_name  ', 3,false);

$ajaxurutan="ajax_do_grid0('setting.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=urutan&ftmenu_category='+ document.getElementById('ftmenu_category').value + '&ftmenu_type=' + document.getElementById('ftmenu_type').value);";

// $ajaxmodule="ajax_do('setting.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=module&ftcustomer_code='+ document.getElementById('ftmenu_category');";
// $objAxpDB->FormScript(3, "onChange=\"{$ajaxcustomer}\"");

$name = "document.getElementById('ftmenu_name').value.split(' ').join('_').toLowerCase()";

$module = "document.getElementById('ftmenu_category').value.concat('.php?form='+{$name})";

$objAxpDB->FormScript(4, "onChange=\"document.getElementById('a_href').value={$module};{$ajaxurutan}\"");

$objAxpDB->FormScript(2, "onChange=\"document.getElementById('img_alt').value=document.getElementById('ftmenu_name').value;document.getElementById('a_href').value={$module};\"");



if ($objAxpDB->GetStatus("id") == "") {

    $objAxpDB->ButtonShow("baru");

  }elseif($objAxpDB->GetStatus("id") == 0){

    $objAxpDB->ButtonShow("reset, simpan, tutup");

  }elseif($objAxpDB->GetStatus("id") != 0) {

    $objAxpDB->ButtonShow("update, hapus, tutup");

  }

  if($_REQUEST['ajax']) {

  switch($_REQUEST['code']) {
    case "urutan":
            ob_start();

            $test = $_REQUEST['ftmenu_category'];

            $_SESSION['ftmenu_category'] = array('urut'=> $test);

            $objAxpDB->GridSQL(0, "SELECT '' as test from tlmenu");
            $objAxpDB->GridCaption(0, "No.");
            $objAxpDB->GridHidden(0,'1');


      $objAxpDB->Process();
      $objAxpDB->setGrid2(0);
      $html = ob_get_contents();
      ob_end_clean();

      ?>

      div = document.getElementById('txtHint0');
      var m='<?php echo js_esc($html); ?>';
      m= m.replace(/^\s*|\s*$/g,"");

      div.innerHTML =m;
      RowIndex0();
      reSize();
      ElementCurrencyALL();
      <?php


    break;
  }
          die("");
          exit;
  }



$objAxpDB->Process();
$objAxpDB->WriteHtmlTable();
