<?php require_once('Connections/food.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO food (f_name, f_price, f_size, f_unit, t_id, f_status) VALUES (%s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['f_name'], "text"),
                       GetSQLValueString($_POST['f_price'], "double"),
                       GetSQLValueString($_POST['f_size'], "text"),
                       GetSQLValueString($_POST['f_unit'], "text"),
                       GetSQLValueString($_POST['t_id'], "int"),
                       GetSQLValueString($_POST['f_status'], "text"));

  mysql_select_db($database_food, $food);
  $Result1 = mysql_query($insertSQL, $food) or die(mysql_error());
}

mysql_select_db($database_food, $food);
$query_type = "SELECT * FROM typefood";
$type = mysql_query($query_type, $food) or die(mysql_error());
$row_type = mysql_fetch_assoc($type);
$totalRows_type = mysql_num_rows($type);

$maxRows_food = 5;
$pageNum_food = 0;
if (isset($_GET['pageNum_food'])) {
  $pageNum_food = $_GET['pageNum_food'];
}
$startRow_food = $pageNum_food * $maxRows_food;

mysql_select_db($database_food, $food);
$query_food = "SELECT * FROM food";
$query_limit_food = sprintf("%s LIMIT %d, %d", $query_food, $startRow_food, $maxRows_food);
$food = mysql_query($query_limit_food, $food) or die(mysql_error());
$row_food = mysql_fetch_assoc($food);

if (isset($_GET['totalRows_food'])) {
  $totalRows_food = $_GET['totalRows_food'];
} else {
  $all_food = mysql_query($query_food);
  $totalRows_food = mysql_num_rows($all_food);
}
$totalPages_food = ceil($totalRows_food/$maxRows_food)-1;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>บันทึกเมนูอาหาร</title>
<style type="text/css">
body,td,th {
	font-family: "Angsana New";
	font-size: 16pt;
}
body {
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
}
</style>
<script src="SpryAssets/SpryMenuBar.js" type="text/javascript"></script>
<link href="SpryAssets/SpryMenuBarHorizontal.css" rel="stylesheet" type="text/css" />
</head>

<body>
<table width="1000" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td align="center" valign="middle"><img src="pic/banner.png" width="1000" height="274" /></td>
  </tr>
  <tr>
    <td align="center" valign="middle"><ul id="MenuBar1" class="MenuBarHorizontal">
      <li><a href="index.php">หน้าแรก</a>        </li>
      <li><a href="#" class="MenuBarItemSubmenu">บันทึกข้อมูล</a>
        <ul>
          <li><a href="add_typefood.php">บันทึกประเภทอาหาร</a></li>
          <li><a href="add_food.php">บันทึกอาหาร</a></li>
        </ul>
      </li>
      <li><a class="MenuBarItemSubmenu" href="#">ค้นหาข้อมูล</a>
        <ul>
<li><a href="search_typefood.php">ค้นหาประเภทอาหาร</a></li>
          <li><a href="search_food.php">ค้นหาอาหาร</a></li>
        </ul>
      </li>
</ul></td>
  </tr>
  <tr>
    <td align="center" valign="middle">&nbsp;<img src="pic/add.png" width="119" height="119" />
      <form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
        <table align="center">
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">ชื่ออาหาร :</td>
            <td><input type="text" name="f_name" value="" size="50" /></td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">ราคา :</td>
            <td><input type="text" name="f_price" value="" size="50" /></td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">ขนาด :</td>
            <td><select name="f_size">
              <option value="เล็ก" <?php if (!(strcmp("เล็ก", ""))) {echo "SELECTED";} ?>>เล็ก</option>
              <option value="กลาง" <?php if (!(strcmp("กลาง", ""))) {echo "SELECTED";} ?>>กลาง</option>
              <option value="ใหญ่" <?php if (!(strcmp("ใหญ่", ""))) {echo "SELECTED";} ?>>ใหญ่</option>
            </select></td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">หน่วยนับ :</td>
            <td><select name="f_unit">
              <option value="ถ้วย" <?php if (!(strcmp("ถ้วย", ""))) {echo "SELECTED";} ?>>ถ้วย</option>
              <option value="ชาม" <?php if (!(strcmp("ชาม", ""))) {echo "SELECTED";} ?>>ชาม</option>
              <option value="จาน" <?php if (!(strcmp("จาน", ""))) {echo "SELECTED";} ?>>จาน</option>
              <option value="แก้ว" <?php if (!(strcmp("แก้ว", ""))) {echo "SELECTED";} ?>>แก้ว</option>
            </select></td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">รหัสประเภทสินค้า :</td>
            <td><select name="t_id">
              <?php 
do {  
?>
              <option value="<?php echo $row_type['t_id']?>" ><?php echo $row_type['t_name']?></option>
              <?php
} while ($row_type = mysql_fetch_assoc($type));
?>
            </select></td>
          </tr>
          <tr> </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">สถานะ :</td>
            <td><select name="f_status">
              <option value="มี" <?php if (!(strcmp("มี", ""))) {echo "SELECTED";} ?>>มี</option>
              <option value="ไม่มี" <?php if (!(strcmp("ไม่มี", ""))) {echo "SELECTED";} ?>>ไม่มี</option>
            </select></td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">&nbsp;</td>
            <td><input type="submit" value="บันทึกข้อมูล" onclick="return confirm('คุณค้องการบันทึกข้อมูลใช่หรือไม่')"/></td>
          </tr>
        </table>
        <input type="hidden" name="MM_insert" value="form1" />
      </form>
      <table width="950" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td width="97" align="center" valign="middle" bgcolor="#FFFF99"><strong>รหัสอาหาร</strong></td>
          <td width="234" align="center" valign="middle" bgcolor="#FFFF99"><strong>ชื่ออาหาร</strong></td>
          <td width="66" align="center" valign="middle" bgcolor="#FFFF99"><strong>ราคา</strong></td>
          <td width="81" align="center" valign="middle" bgcolor="#FFFF99"><strong>ขนาด</strong></td>
          <td width="97" align="center" valign="middle" bgcolor="#FFFF99"><strong>หน่วยนับ</strong></td>
          <td width="149" align="center" valign="middle" bgcolor="#FFFF99"><strong>รหัสประเภทสินค้า</strong></td>
          <td width="74" align="center" valign="middle" bgcolor="#FFFF99"><strong>สถานะ</strong></td>
          <td colspan="2" align="center" valign="middle" bgcolor="#FFFF99"><strong>จัดการระบบ</strong></td>
        </tr>
        <?php do { ?>
          <tr>
            <td align="center" valign="middle"><?php echo $row_food['f_id']; ?></td>
            <td align="center" valign="middle"><?php echo $row_food['f_name']; ?></td>
            <td align="center" valign="middle"><?php echo $row_food['f_price']; ?></td>
            <td align="center" valign="middle"><?php echo $row_food['f_price']; ?></td>
            <td align="center" valign="middle"><?php echo $row_food['f_unit']; ?></td>
            <td align="center" valign="middle"><?php echo $row_food['t_id']; ?></td>
            <td align="center" valign="middle"><?php echo $row_food['f_status']; ?></td>
            <td width="74" align="center" valign="middle"><a href="edit_food.php?f_id=<?php echo $row_food['f_id']; ?>"><img src="pic/edit.png" width="35" height="35" /></a></td>
            <td width="78" align="center" valign="middle"><a href="delete_food.php?f_id=<?php echo $row_food['f_id']; ?>"><img src="pic/delete.png" width="29" height="35" /></a></td>
          </tr>
          <?php } while ($row_food = mysql_fetch_assoc($food)); ?>
      </table>
      <p>| <a href="<?php printf("%s?pageNum_type=%d%s", $currentPage, 0, $queryString_type); ?>">หน้าแรก</a> | <a href="<?php printf("%s?pageNum_type=%d%s", $currentPage, max(0, $pageNum_type - 1), $queryString_type); ?>">ก่อนหน้า</a> | <a href="<?php printf("%s?pageNum_type=%d%s", $currentPage, min($totalPages_type, $pageNum_type + 1), $queryString_type); ?>">หน้าถัดไป</a> | <a href="<?php printf("%s?pageNum_type=%d%s", $currentPage, $totalPages_type, $queryString_type); ?>">หน้าสุดท้าย</a> |</p>
    <p>&nbsp;</p></td>
  </tr>
  <tr>
    <td align="center" valign="middle"><img src="pic/footer.png" width="1000" height="135" /></td>
  </tr>
</table>
<script type="text/javascript">
var MenuBar1 = new Spry.Widget.MenuBar("MenuBar1", {imgDown:"SpryAssets/SpryMenuBarDownHover.gif", imgRight:"SpryAssets/SpryMenuBarRightHover.gif"});
</script>
</body>
</html>
<?php
mysql_free_result($type);

mysql_free_result($food);
?>
