<?php

session_start() ;
if(!session_is_registered("login_true_admin")){
echo "<meta http-equiv='refresh' content='0; url = index.php'>" ;
exit() ;
}
include("../config.inc.php") ;
mysql_select_db($db) ;
@mysql_select_db($db) or die( "Unable to select database");
mysql_db_query($db,"SET NAMES tis620");// 

mysql_query("SET character_set_results=tis620");
mysql_query("SET character_set_client=''tis620''");
mysql_query("SET character_set_connection=''tis620''");
mysql_query("collation_connection = tis620_thai_ci");
mysql_query("collation_database = tis620_thai_ci");
mysql_query("collation_server = tis620_thai_ci");
?>
<style type="text/css">
<!--
body,td,th {
	color: #FFFF66;
}
body {
	background-color: #000000;
}
a:link {
	color: #FFCC00;
}
a:visited {
	color: #FFCC00;
}
a:hover {
	color: #FFCC33;
}
a:active {
	color: #FFCC33;
}
-->
</style>
<script type="text/JavaScript">
<!--
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
//-->
</script>

<html>
<head>
<title>Admin ผู้ดูแลระบบ</title>
</head><body ><center>
<?php

if(  isset($_GET["all"])  )
{
?>
<form action="" method="get">
<table width="402" cellspacing="0" cellpadding="0">
  <tr>
    <td width="123">ชื่อลูำกค้า</td>
    <td width="277"><input name="fname" type="text" id="fname"></td>
  </tr>
  <tr>
    <td>รหัสสั่งของ</td>
    <td><input name="fid" type="text" id="fid"></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><input type="submit" name="Submit" value="ค้นหา">
      <input type="reset" name="Submit2" value="Reset"><input name="all" type="hidden" value="yes"></td>
  </tr>
</table>

</form>



<?php
if(($fid=="")&&($fname==""))
{
     $sql="SELECT * FROM `order` ORDER BY `order_time` DESC "; 
}elseif($fname=="")
{ 
     $sql="SELECT * FROM `order` WHERE id = $fid  ORDER BY  `order_time` DESC "; 
}elseif($fid=="")
{ 
      $sql="SELECT * FROM `member`  WHERE name LIKE '%".$fname."%'"; 
	  echo $sql;
      $result2 = mysql_query($sql) ;
      $dbarr2 = mysql_fetch_array($result2);


     $sql="SELECT * FROM `order` WHERE user = '".$dbarr2['user']."'  ORDER BY `order_time` DESC "; 
}


$result = mysql_query($sql) ;
echo "<br><strong>รายการสั่งของ</strong> <br><br><table cellspacing=1 cellpadding=3 bgcolor=555555>";			
echo " <tr>
<td bgcolor=880000 align=\"center\">รหัสสั่งของ</td>
<td bgcolor=880000  align=\"center\">ผู้สั่ง</td>
<td bgcolor=880000  align=\"center\">วันสั่งของ</td>
<td bgcolor=880000  align=\"center\">วันส่งของ</td>
<td bgcolor=880000  align=\"center\">สถานะ</td>
<td bgcolor=880000  align=\"center\"> รายละเอียด </td>
</tr>";
while($dbarr = mysql_fetch_array($result)) {
$sql="SELECT * FROM `member` WHERE user='".$dbarr['user']."'"; 
$result2 = mysql_query($sql) ;
$dbarr2 = mysql_fetch_array($result2);

if($dbarr['status']==0)$stat="รอการอนุมัติ";
if($dbarr['status']==1)$stat="อนุมัติเรียบร้อยแล้ว";
if($dbarr['status']==2)$stat="ส่งของเรียบร้อยแล้ว";
 				  echo 
" <tr><td bgcolor=442222 align=\"center\">".$dbarr['id'].
"</td><td bgcolor=442222  align=\"center\">".$dbarr2['name'].
"</td><td bgcolor=442222  align=\"center\">".$dbarr['order_time'].
"</td><td bgcolor=442222  align=\"center\">".$dbarr['date'].
"</td><td bgcolor=442222  align=\"center\">".$stat.
"</td><td bgcolor=442222  align=\"center\">
<a href=\"".$PHP_SELF."?order_detail=yes&id=".$dbarr['id']."\">รายละเ้อียด</a> | 
<a href=\"".$PHP_SELF."?order_delete=1&id=".$dbarr['id']."\">ลบ</a> ".
"</td></tr>";
						  
}
echo "</table>";
}elseif( isset($_GET["order_detail"]))  ///// รายละเอียด
{
    $sql="SELECT * FROM `order` WHERE id=$id"; 
    $result = mysql_query($sql) ;
    $dbarr = mysql_fetch_array($result);

$sql="SELECT * FROM `member` WHERE user='".$dbarr['user']."'"; 
$result2 = mysql_query($sql) ;
$dbarr2 = mysql_fetch_array($result2);

if($dbarr['status']==-1)$stat="ยกเลิก";
if($dbarr['status']==0)$stat="รอการอนุมัติ";
if($dbarr['status']==1)$stat="อนุมัติเรียบร้อยแล้ว";
if($dbarr['status']==2)$stat="ส่งของเรียบร้อยแล้ว";
echo "<br><strong>รายการสั่งของ</strong> <br><br><table cellspacing=1 cellpadding=3 bgcolor=555555>";			
echo " <tr>
<td bgcolor=880000 align=\"left\">รหัสสั่งของ</td>
<td bgcolor=442222 align=\"left\">".$dbarr['id']."</td>
</tr>
<tr>
<td bgcolor=880000  align=\"left\">ผู้สั่ง</td>
<td bgcolor=442222  align=\"left\">".$dbarr2['name']."</td>
</tr>
<tr>
<td bgcolor=880000  align=\"left\">ที่อยู่</td>
<td bgcolor=442222  align=\"left\">".$dbarr2['address']."</td>
</tr>
<tr>
<td bgcolor=880000  align=\"left\">อำเภอ/เขต</td>
<td bgcolor=442222  align=\"left\">".$dbarr2['ampher']."</td>
</tr>
<tr>
<td bgcolor=880000  align=\"left\">จังหวัด</td>
<td bgcolor=442222  align=\"left\">".$dbarr2['province']."</td>
</tr>
<tr>
<td bgcolor=880000  align=\"left\">เบอร์โทรศัพท์</td>
<td bgcolor=442222  align=\"left\">".$dbarr2['phone']."</td>
</tr>
<tr>
<td bgcolor=880000  align=\"left\">อีเมล์</td>
<td bgcolor=442222  align=\"left\">".$dbarr2['email']."</td>
</tr>
<tr>
<td bgcolor=880000  align=\"left\">วันสั่งของ</td>
<td bgcolor=442222  align=\"left\">".$dbarr['order_time']."</td>
</tr>
<tr>
<td bgcolor=880000  align=\"left\">วันส่งของ</td>
<td bgcolor=442222  align=\"left\">".$dbarr['date']."</td>
</tr>
<form id=\"form1\" name=\"form1\" method=\"get\" action=\"$PHP_SELF\">
<tr>
<td bgcolor=880000  align=\"left\">สถานะ</td>
<td bgcolor=442222  align=\"left\">".$stat."  
  <input name=\"confirm\" type=\"submit\" id=\"confirm\" value=\"อนุมัติ\" />
    <input name=\"id\" type=\"hidden\" value=\"$id\" />

</td>
</tr>
</form>
<tr>
<td bgcolor=880000  align=\"left\">Note</td>
<td bgcolor=442222  align=\"left\">".$dbarr['detail']."</td>
</tr>
<tr>
<td bgcolor=880000  align=\"left\"></td><td bgcolor=442222  align=\"left\">
<a href=\"".$PHP_SELF."?order_delete=1&id=".$dbarr['id']."\">ลบ</a> ".
"</td></tr></table>";


echo "<br><strong>รายการสั่งของ</strong> <br><br><table cellspacing=1 cellpadding=3 bgcolor=555555>";			
echo " <tr>
<td bgcolor=228822 align=\"center\">ลำดับ</td>
<td bgcolor=228822 align=\"center\">รหัส</td>
<td bgcolor=228822  align=\"center\">ประเภท</td>
<td bgcolor=228822  align=\"center\">ชื่อสินค้า</td>
<td bgcolor=228822  align=\"center\">น้ำหนัก</td>
<td bgcolor=228822  align=\"center\"> ขนาด </td>
<td bgcolor=228822  align=\"center\">  จำนวน (ชิ้น)  </td></tr>";

	$sql="SELECT * FROM `ordered_carts` WHERE orderid='$id'"; 
	$result = mysql_query($sql) ;
	while($dbarr = mysql_fetch_array($result) )
	{  
	
$sql="SELECT * FROM `product` WHERE id='".$dbarr['productid']."'"; 
$result2 = mysql_query($sql) ;
$dbarr2 = mysql_fetch_array($result2);

$query3="SELECT * FROM productsize WHERE id = '".$dbarr['detail']."' ";
$result3=mysql_query($query3);
$num3=mysql_numrows($result3);
  $idsize =mysql_result($result3,0,"id");
  $name =mysql_result($result3,0,"name");
  $weight =mysql_result($result3,0,"weight");
  $size =mysql_result($result3,0,"size");
  $unit =mysql_result($result3,0,"unit");
  $unit2 =mysql_result($result3,0,"unit2");

 echo " <tr>
 <td bgcolor=223322 align=\"center\">".$dbarr2['model']."</td>
 <td bgcolor=223322  align=\"center\">".$dbarr2['type']."</td>
 <td bgcolor=223322  align=\"center\">".$dbarr2['name']."</td>
 <td bgcolor=223322  align=\"center\" > $weight </td>
 <td bgcolor=223322  align=\"center\" > $size $unit2</td>
  <td bgcolor=223322  align=\"center\">".$dbarr['amount']."</td>
 </tr>";

//echo " <tr><td bgcolor=880000  align=\"center\">".$dbarr2['model']."</td><td bgcolor=880000  align=\"center\">".$dbarr['amount']."</td></tr>";
	      
	}
	echo "</table>";
}elseif(isset($_GET['confirm']))
{
   $sql="UPDATE `order` SET `status` = '1' WHERE `id` = '".$_GET['id']."' LIMIT 1 ";
   
   $result2 = mysql_query($sql) ;
   echo "  อนุมัติเ้รียบร้อยแล้วครับ  <br> รอสักครู่";
   echo "<meta http-equiv='refresh' content='3; url =order_detail.php?all=yes'>" ;
}elseif(isset($_GET['order_delete']))
{
     echo "คุณแน่ใจ <a href=\"".$PHP_SELF."?order_delete=yes&id=".$id."\">ใช่่</a> หรือ <a href=\"".$PHP_SELF."?all=yes\">ไม่<a>";
}


?>
  <p><strong><font size="3" face="MS Sans Serif, Tahoma, sans-serif"><a href="menu.php">กลับเมนูหลัก</a> |&nbsp;<a href="order_detail.php?all=yes">รายการสั่งของ</a> |&nbsp;<a href="logout.php">ออกจากระบบ </a></font></strong></p>
  </center>
</body >
</html>
