<?
/* index.php: $pubmodule, $pubtask, $pubitemid */
/*
$pubsubjectid = $_REQUEST["subjectid"];
$pubtypeid = $_REQUEST["typeid"];
$pubgroupid = $_REQUEST["groupid"];
*/

$pubparentid = $_REQUEST["pid"];
$publevel = $_REQUEST["level"];

// filter forms..
$filtertradedesc=$_POST['tradedesc'];
?>

<script type="text/javascript">

// ................................................................................
// user-interaction handlers..
// ................................................................................

function trade_button_search() {
  document.tradeform.job.value='trade_search';
  document.tradeform.submit();
}

function trade_button_add(parentpkey,tradelevel) {
  document.location='index.php?module=trade&task=add&pid='+parentpkey+'&level='+tradelevel;
}

function trade_button_edit(tradepkey) {
  document.location='index.php?module=trade&task=edit&itemid='+tradepkey;
}

function trade_button_view(tradepkey) {
  document.location='index.php?module=trade&task=view&itemid='+tradepkey;
}

function trade_button_delete(tradepkey) {
  document.location='index.php?module=trade&task=delete&itemid='+tradepkey;
}

function trade_button_save() {
  if (validateForm()==true) {
    document.tradeform.job.value='trade_add'; document.tradeform.submit();
  }
}

function trade_button_update() {
  if (validateForm()==true) {
    document.tradeform.job.value='trade_edit'; document.tradeform.submit();
  }
}

function trade_button_purge() {
  document.tradeform.job.value='trade_remove'; document.tradeform.submit();
}

function trade_tree_open(node) {
  document.getElementById(node).style.cssText="display: block;";
  document.getElementById('open'+node).style.cssText="display: none; cursor: pointer;";
  document.getElementById('close'+node).style.cssText="display: block; cursor: pointer;";
}

function trade_tree_close(node) {
  document.getElementById(node).style.cssText="display: none;";
  document.getElementById('open'+node).style.cssText="display: block; cursor: pointer;";
  document.getElementById('close'+node).style.cssText="display: none; cursor: pointer;";
}

function trade_tree_edit(tradepkey) {
  document.location='index.php?module=trade&task=edit&itemid='+tradepkey;
}

function trade_tree_delete(tradepkey) {
  document.location='index.php?module=trade&task=delete&itemid='+tradepkey;
}

function trade_button_back() {
  history.go(-1);
}

</script>

<?

// ................................................................................
// model..
// ................................................................................

include_once('models/tradeModel.php');
$mytradeModel=new tradeModel;

?>

<?

// ................................................................................
// default to list view if none specified..
// ................................................................................
if ($pubtask=="") $pubtask="main";

// ................................................................................
// display page header..
// ................................................................................

$pagetitle="Master Data";
if (($pubtask=="main")||($pubtask=="submenu")) {
  $subtitle="Categories List";
} else if ($pubtask=="view") {
  $subtitle="Category Information";
} else if ($pubtask=="add") {
  $subtitle="Add Category";
} else if ($pubtask=="edit") {
  $subtitle="Edit Category";
} else if ($pubtask=="delete") {
  $subtitle="Delete Category";
} else {
  $subtitle="Error: Invalid Option";
}

$headercaption=$pagetitle." ··» ".$subtitle;
include_once('header.php');

include_once('views/general-header.tpl');


// TODO: Later to show list of available variables here if in DEBUG mode!!

// ................................................................................
// display main area..
// ................................................................................

if (($pubtask=="main")||($pubtask=="submenu")) {

  //*$trade_list1 = $mytradeModel->getlist_tradesbylevel(1,"","tradecode");
  //$trade_list2 = $mytradeModel->getlist_tradesbylevel(2,"","tradecode");
  //$trade_list3 = $mytradeModel->getlist_tradesbylevel(3,"","tradecode");
  //$trade_list4 = $mytradeModel->getlist_tradesbylevel(4,"","tradecode");
  //$trade_list5 = $mytradeModel->getlist_tradesbylevel(5,"","tradecode");

  $trade_list1 = $mytradeModel->getlist_tradesbypkeys("'456b685e3475eb3c9cb1948e3d430318'","tradecode");
  //$trade_list2 = $mytradeModel->getlist_tradesbyparent("456b685e3475eb3c9cb1948e3d430318","","tradecode");

  //$trade_list = $mytradeModel->getlist_alltrades("tradecode");
  include_once('views/trade-list.tpl');

} else if ((($pubtask=="add")||($pubtask=="edit"))&&(($pubuseraccess['access_trade_edit']==1)||($pubuserrole>=2))) {

  $trade_data = $mytradeModel->getdata_trade($pubitemid);
  if ($pubtask=="add") $trade_data['tradelevel']=$publevel;
  include_once('views/trade-edit.tpl');

} else if ($pubtask=="view") {

  $trade_data = $mytradeModel->getdata_trade($pubitemid);
  $parent_data = $mytradeModel->getdata_trade($trade_data['tradeparentpkey']);
  include_once('views/trade-view.tpl');

} else if (($pubtask=="delete")&&(($pubuseraccess['access_trade_edit']==1)||($pubuserrole>=2))) {

  $trade_data = $mytradeModel->getdata_trade($pubitemid);
  $parent_data = $mytradeModel->getdata_trade($trade_data['tradeparentpkey']);
  include_once('views/trade-view.tpl');

} else {
  include_once('views/general-invalidtask.php');
}

// ...............................................................................
// display button options area..
// ...............................................................................

$prevurl = explode('/',$_SESSION['Xpreviousurl']);
$prevfile = $prevurl[count($prevurl)-1];
if ($prevfile=='inputController.php') $backstatus="style='display: none;'";

echo "<br>";

if (($pubtask=="main")||($pubtask=="submenu")) {

  echo "<br><hr size=1>";
  //echo "<button name=\"print\" id=\"print\" onClick=\"trade_button_printlist();\">&nbsp;<img src='gfx/options/b_print1.png' class='actionbutton'>&nbsp;&nbsp;Print&nbsp;</button>&nbsp;";
  
  // edit 17-08-2010
  //echo "<button name=\"back\" id=\"back\" ".$backstatus." onClick=\"trade_button_back();\">&nbsp;<img src='gfx/options/b_back.png' class='actionbutton'>&nbsp;&nbsp;Back&nbsp;</button>&nbsp;";
  echo "<button name=\"back\" id=\"back\" ".$backstatus." onClick=\"window.location.reload()\">&nbsp;<img src='gfx/options/b_back.png' class='actionbutton'>&nbsp;&nbsp;Back&nbsp;</button>&nbsp;";
  
  echo "<br><hr size=1><br>";

} else if (($pubtask=="add")&&(($pubuseraccess['access_trade_edit']==1)||($pubuserrole>=2))) {

  echo "<br><hr size=1>";
  echo "<button name=\"save\" id=\"save\" onClick=\"trade_button_save();\"><img src='gfx/options/b_save.png' class='actionbutton'>&nbsp;&nbsp;Save Category</button>&nbsp;";
  echo "<button name=\"cancel\" id=\"cancel\" onClick=\"trade_button_back();\"><img src='gfx/options/b_cancel.png' class='actionbutton'>&nbsp;&nbsp;Back</button>&nbsp;";
  echo "<br><hr size=1><br>";

} else if (($pubtask=="edit")&&(($pubuseraccess['access_trade_edit']==1)||($pubuserrole>=2))) {

  echo "<br><hr size=1>";
  echo "<button name=\"update\" id=\"update\" onClick=\"trade_button_update();\"><img src='gfx/options/b_save.png' class='actionbutton'>&nbsp;&nbsp;Update Category</button>&nbsp;";
  echo "<button name=\"cancel\" id=\"cancel\" onClick=\"trade_button_back();\"><img src='gfx/options/b_cancel.png' class='actionbutton'>&nbsp;&nbsp;Back</button>&nbsp;";
  echo "<br><hr size=1><br>";

} else if ($pubtask=="view") {

  echo "<br><hr size=1>";
  echo "<button name=\"edit\" id=\"edit\" onClick=\"trade_button_edit('".$trade_data['tradepkey']."');\">&nbsp;<img src='gfx/options/b_edit.png' class='actionbutton'>&nbsp;&nbsp;Edit&nbsp;</button>&nbsp;";
  echo "<button name=\"delete\" id=\"delete\" onClick=\"trade_button_delete('".$trade_data['tradepkey']."');\"><img src='gfx/options/b_drop.png' class='actionbutton'>&nbsp;&nbsp;Delete</button>&nbsp;";
  echo "<button name=\"back\" id=\"back\" ".$backstatus." onClick=\"trade_button_back();\">&nbsp;<img src='gfx/options/b_back.png' class='actionbutton'>&nbsp;&nbsp;Back&nbsp;</button>&nbsp;";
  echo "<br><hr size=1><br>";

} else if (($pubtask=="delete")&&(($pubuseraccess['access_trade_edit']==1)||($pubuserrole>=2))) {

  echo "<br><hr size=1>";
  echo "<font color='#ff0000'><b>ARE YOU SURE TO PERMANENTLY DELETE THIS CATEGORY?</b></font><br><br>";
  echo "<button name=\"delete\" id=\"delete\" onClick=\"trade_button_purge();\">&nbsp;Yes, Delete Category&nbsp;</button>&nbsp;";
  echo "<button name=\"cancel\" id=\"cancel\" onClick=\"trade_button_back();\"><img src='gfx/options/b_cancel.png' class='actionbutton'>&nbsp;&nbsp;Back</button>&nbsp;";
  echo "<brp><hr size=1><br>";

}

// ................................................................................
// display page footer..
// ................................................................................

include_once('views/general-footer.tpl');

?>