<html>
<head>
<link rel="stylesheet" href="/style/style.css" type="text/css" />
</head>
<body>
<script language="JavaScript" src="js/tree_tpl.js" ></script>
	<script language="JavaScript" src="js/tree.js" ></script> 
<?php 
  $fh = fopen('login.cfg', "r") or die("Can't open file!");
$login = fread($fh, filesize('login.cfg'));
fclose($fh);
$fh = fopen('pass.cfg', "r") or die("Can't open file!");
$pass = fread($fh, filesize('pass.cfg'));
fclose($fh);
$fh = fopen('sid.cfg', "r") or die("Can't open file!");
$sid = fread($fh, filesize('sid.cfg'));
fclose($fh);
  
$c=OCILogon($login, $pass, $sid);
  if ( ! $c ) {
    echo "Невозможно подключится к базе: " . var_dump( OCIError() );
    die();
  } 
//--------------------------------------------------------
//отделы
  $s = OCIParse($c, "select * from departament where deleted=1 order by 1");
  OCIExecute($s, OCI_DEFAULT); 
  $dep="";
  while (OCIFetch($s)) {
    $dep=$dep."['".ociresult($s,"NAME")."','dep.php?id=".ociresult($s,"ID_DEP")."'],";
  }
  $s = OCIParse($c, "select * from departament where deleted=0 order by 1");
  OCIExecute($s, OCI_DEFAULT); 
  $dep_del="";
  while (OCIFetch($s)) {
    $dep_del=$dep_del."['".ociresult($s,"NAME")."','dep.php?id=".ociresult($s,"ID_DEP")."'],";
  }
  if($dep_del!='')
	$dep=$dep."['Удаленные','dep.php?deleted',".$dep_del."],";
//--------------------------------------------------------
//сотрудники
  $s = OCIParse($c, "select * from staff where deleted=1 order by 1");
  OCIExecute($s, OCI_DEFAULT); 
  $staff="";
  while (OCIFetch($s)) {
    $staff=$staff."['".ociresult($s,"FIO")."','staff.php?id=".ociresult($s,"ID_STAFF")."'],";
  }
  $s = OCIParse($c, "select * from staff where deleted=0 order by 1");
  OCIExecute($s, OCI_DEFAULT); 
  $staff_del="";
  while (OCIFetch($s)) {
    $staff_del=$staff_del."['".ociresult($s,"FIO")."','staff.php?id=".ociresult($s,"ID_STAFF")."'],";
  }
  if($staff_del!='')
	$staff=$staff."['Уволенные','staff.php?deleted',".$staff_del."],";
//--------------------------------------------------------
//типы устройств
  $s = OCIParse($c, "select * from products_type where deleted=1 order by 1");
  OCIExecute($s, OCI_DEFAULT); 
  $prtype="";
  while (OCIFetch($s)) {
    $prtype=$prtype."['".ociresult($s,"TYPE_NAME")."','nomen.php?id=".ociresult($s,"ID_TYPE")."'],";
  }
  $s = OCIParse($c, "select * from products_type where deleted=0 order by 1");
  OCIExecute($s, OCI_DEFAULT); 
  $prtype_del="";
  while (OCIFetch($s)) {
    $prtype_del=$prtype_del."['".ociresult($s,"TYPE_NAME")."','nomen.php?id=".ociresult($s,"ID_TYPE")."'],";
  }
  if($prtype_del!='')
	$prtype=$prtype."['Удаленные','nomen.php?deleted',".$prtype_del."],";
//---------------------------------------------------------
//тех. процесс
  $s = OCIParse($c, "select * from products_type where deleted=1 order by 1");
  OCIExecute($s, OCI_DEFAULT); 
  $tp="";
  while (OCIFetch($s)) {
    $tp=$tp."['Т/П ".ociresult($s,"TYPE_NAME")."','tp.php?id=".ociresult($s,"ID_TYPE")."'],";
  }
  $s = OCIParse($c, "select * from products_type where deleted=0 order by 1");
  OCIExecute($s, OCI_DEFAULT); 
  $tp_del="";
  while (OCIFetch($s)) {
    $tp_del=$tp_del."['Т/П ".ociresult($s,"TYPE_NAME")."','tp.php?id=".ociresult($s,"ID_TYPE")."'],";
  }
  if($tp_del!='')
	$tp=$tp."['Удаленные','tp.php?deleted',".$tp_del."],";
//операции
//---------------------------------------------------------
$s = OCIParse($c, "select * from List_of_operation where deleted=1 order by 1");
  OCIExecute($s, OCI_DEFAULT); 
  $oper="";
  while (OCIFetch($s)) {
    $oper=$oper."['".ociresult($s,"NAME")."','oper.php?id=".ociresult($s,"ID_LISTOP")."'],";
  }  
  $s = OCIParse($c, "select * from List_of_operation where deleted=0 order by 1");
  OCIExecute($s, OCI_DEFAULT); 
  $oper_del="";
  while (OCIFetch($s)) {
    $oper_del=$oper_del."['".ociresult($s,"NAME")."','oper.php?id=".ociresult($s,"ID_LISTOP")."'],";
  } 
  if($oper_del!='')
	$oper=$oper."['Удаленные','oper.php?deleted',".$oper_del."],";
//---------------------------------------------------------
//текущее производство
  $s = OCIParse($c, "select * from products_type  order by 1");
  OCIExecute($s, OCI_DEFAULT); 
  $product="";
  while (OCIFetch($s)) {
	$s_2 = OCIParse($c, "select lop.*, tp.nom from list_of_operation lop join tp on lop.id_listop=tp.id_listop where tp.id_type=".ociresult($s,"ID_TYPE")." order by tp.nom ");
	OCIExecute($s_2, OCI_DEFAULT); 
	$op="";
	$op=$op."['Готовые', 'current.php?id=".ociresult($s,"ID_TYPE")."&ready'],";
	$op=$op."['БРАК', 'current.php?id=".ociresult($s,"ID_TYPE")."&brak'],";
	
	 while (OCIFetch($s_2)) {
		$op=$op."['".ociresult($s_2,"NAME")."','current.php?id_pr=".ociresult($s,"ID_TYPE")."&nom=".ociresult($s_2,"NOM")."'],";
	 }
	 $op=$op."['УДАЛЕННЫЕ', 'current.php?deleted=".ociresult($s,"ID_TYPE")."'],";
    $product=$product."['".ociresult($s,"TYPE_NAME")."','current.php?id=".ociresult($s,"ID_TYPE")."',".$op."],";
  }

//----------------------------------------------------------
//Оборудование
$s = OCIParse($c, "select * from equipment where deleted=1 order by 1");
  OCIExecute($s, OCI_DEFAULT); 
  $eq="";
  while (OCIFetch($s)) {
    $eq=$eq."['".ociresult($s,"NAME")."','equip.php?id=".ociresult($s,"ID_EQUIPMENT")."'],";
  }
  $s = OCIParse($c, "select * from equipment where deleted=0 order by 1");
  OCIExecute($s, OCI_DEFAULT); 
  $eq_del="";
  while (OCIFetch($s)) {
    $eq_del=$eq_del."['".ociresult($s,"NAME")."','equip.php?id=".ociresult($s,"ID_EQUIPMENT")."'],";
  }
  if($eq_del!='')
	$eq=$eq."['Удаленные','equip.php?deleted',".$eq_del."],";
//----------------------------------------------------------  
//Типы ресурсов
$s = OCIParse($c, "select * from resources where deleted=1 order by 1");
  OCIExecute($s, OCI_DEFAULT); 
  $res="";
  while (OCIFetch($s)) {
    $res=$res."['".ociresult($s,"NAME")."','res.php?id=".ociresult($s,"ID_RES")."'],";
  }  
  $s = OCIParse($c, "select * from resources where deleted=0 order by 1");
  OCIExecute($s, OCI_DEFAULT); 
  $res_del="";
  while (OCIFetch($s)) {
    $res_del=$res_del."['".ociresult($s,"NAME")."','res.php?id=".ociresult($s,"ID_RES")."'],";
  } 
  if($res_del!='')
	$res=$res."['Удаленные','res.php?deleted',".$res_del."],";
//----------------------------------------------------------
//Спецификация ресурсов
 $s = OCIParse($c, "select res_usage.id_resus as id_resus, resources.name as name from res_usage join resources on resources.id_res=res_usage.id_res where res_usage.deleted=1 order by 1");
  OCIExecute($s, OCI_DEFAULT); 
  $res_us="";
  while (OCIFetch($s)) {
    $res_us=$res_us."['".ociresult($s,"NAME")."','res_us.php?id=".ociresult($s,"ID_RESUS")."'],";
  }  
  $s = OCIParse($c, "select res_usage.id_resus as id_resus, resources.name as name from res_usage join resources on resources.id_res=res_usage.id_res where res_usage.deleted=0 order by 1");
  OCIExecute($s, OCI_DEFAULT); 
  $res_del="";
  while (OCIFetch($s)) {
    $res_del=$res_del."['".ociresult($s,"NAME")."','res_us.php?id=".ociresult($s,"ID_RESUS")."'],";
  } 
  if($res_del!='')
	$res_us=$res_us."['Удаленные','res_us.php?deleted',".$res_del."],";
//----------------------------------------------------------
  
  $s = OCIParse($c, "select slots.id_slots, slots.slot_name from slots");
  OCIExecute($s, OCI_DEFAULT); 
  $slots="";
  while (OCIFetch($s)) {
    $slots=$slots."['".ociresult($s,"SLOT_NAME")."','slots.php?id=".ociresult($s,"ID_SLOTS")."'],";
  }  
  
$tree_items = "[ ['АСУ', 'index.php',
					['Производство','',
						['Текущее производство','current.php',".$product."],
						['Операции','oper.php',".$oper."],
						['Тех. процессы','tp.php',".$tp."],
						['Номанклатура','nomen.php',".$prtype."]	
					],
					['Обеспечение','',
						['Спецификация ресурсов','res_us.php',".$res_us."],
						['Ресурсы','res.php',".$res."],
						['Оборудование','equip.php',".$eq."]				
					],
					['Фирма','',
						['Отделы','dep.php',".$dep."],
						['Сотрудники','staff.php',".$staff."]
					],	
";
$tree_items .= "], ['Экспертная система','',
						['Генерировать ТП','generate_tp.php'],
						['Слоты','slots.php',".$slots.",
						],
						['Фреймы','',
							['Прототипы','frames.php'],
							['Экземпляры','frame_exmpl.php'],
						]
					]
 ]";
print '<script language="JavaScript">
	
	
	var TREE_ITEMS = '.$tree_items.'; //оздаём массив и подставляем в него сформированные ранее данные
   new tree(TREE_ITEMS, TREE_TPL); //вызываем функцию яваскрипт, которая разберёт нашу структуру и отрисует наше меню
	
   </script>';
   OCICommit($c);
   OCILogoff($c);
?>
</body>
</html>