<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="description" content="">
<meta name="author" content="">
<link rel="stylesheet" href="bootstrap/css/footer.css">
<link rel="stylesheet" href="bootstrap/css/base.css">
<link rel="stylesheet" href="bootstrap/css/skeleton.css">
<link rel="stylesheet" href="bootstrap/less/datepicker.less">
<link rel="stylesheet" href="bootstrap/css/layout.css">
<link href="bootstrap/css/datepicker.css" rel="stylesheet">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="bootstrap/css/bootstrap.css" rel="stylesheet"/>
<link type"text/css" rel="Stylesheet" href="bootstrap/css/bootstrap.css" />
<meta charset="UTF-8">
<title>AFM ASS</title>
</head>

<body>
                                    <div class="row clearfix">
                                        <div class="col-md-12 column">
                                        </div>
                                    </div>
                                        <div class="row clearfix">
                                        <div class="col-md-12 column">
                                        </div>
                                    </div>
                                        <div class="row clearfix">
                                        <div class="col-md-12 column">
                                        </div>
                                    </div>
<div class="container">
	<div class="row clearfix">
		<div class="col-md-12 column">
			<div class="page-header">
				<h1>
					AFM Asset Management <small>ระบบการจัดการสินทรัพย์บริษัท เอ เอฟ เอ็ม จำกัด</small>
				</h1>
			</div>
			<?php include("navbartop.php"); ?>
		</div>
	</div>
<form role="form" name="frmMain" method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
<?php include("connect.php");
//*** Add Condition ***//
if(isset($_POST['hdnCmd']) && $_POST['hdnCmd'] == "Add")
{
	$strSQL = "INSERT INTO tb_department ";
	$strSQL .="(Departmentcode,Department) ";
	$strSQL .="VALUES ";
	$strSQL .="('".$_POST["adddepartmentcode"]."','".$_POST["adddepartment"]."')";
	$objQuery = mysql_query($strSQL);
	if(!$objQuery)
	{
		echo "Error Save [".mysql_error()."]";
	}
	//header("location:$_SERVER[PHP_SELF]");
	//exit();
}

//*** Update Condition ***//
if(isset($_POST['hdnCmd']) && $_POST['hdnCmd'] == "Update")
{
	$strSQL = "UPDATE tb_department SET ";
	$strSQL .="Departmentcode = '".$_POST["txtAddCustomerID"]."' ";
	$strSQL .=",Department = '".$_POST["txtEditName"]."' ";
	$strSQL .="WHERE Department_id = '".$_POST["hdnEditCustomerID"]."' ";
	$objQuery = mysql_query($strSQL);
	if(!$objQuery)
	{
		echo "Error Update [".mysql_error()."]";
	}
	//header("location:$_SERVER[PHP_SELF]");
	//exit();
}

//*** Delete Condition ***//
if(isset($_GET['Action']) && $_GET['Action'] == "Del")
{
	$strSQL = "DELETE FROM tb_department ";
	$strSQL .="WHERE Department_id = '".$_GET["CusID"]."' ";
	$objQuery = mysql_query($strSQL);
	if(!$objQuery)
	{
		echo "Error Delete [".mysql_error()."]";
	}
	//header("location:$_SERVER[PHP_SELF]");
	//exit();
}

$strSQL = "SELECT * FROM tb_department";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
?>
<div class="container">
<table width="79%" height="148" class="table table-striped table-bordered">
  <input type="hidden" name="hdnCmd" value="">
  <tr>
    <th align="center">#</th>
    <th align="center">รหัสแผนก</th>
    <th align="center">แผนก</th>
    <th align="center">แก้ไข</th>
    <th align="center">ลบ</th>
  </tr>
  <?php
	$i = 1;
while($objResult = mysql_fetch_array($objQuery))
{
?>
  <?php
		$CusID = (isset($_GET["CusID"])) ? $_GET["CusID"] : '';
	    $Action = (isset($_GET["Action"])) ? $_GET["Action"] : '';
	if($objResult["Department_id"] == $CusID and $Action == "Edit")
	{
  ?>
  <tr>
    <td>&nbsp;</td>
    <td><input type="hidden" name="hdnEditCustomerID" value="<?php echo $objResult["Department_id"];?>">
        <input type="text"  name="txtAddCustomerID" class="form-control" 
        value="<?php echo $objResult["Departmentcode"];?>"></td>
    <td><input type="text" name="txtEditName" class="form-control" 
    value="<?php echo $objResult["Department"];?>"></td>
    <td><input name="btnAdd" type="button" id="btnUpdate" value="ปรับปรุง" onClick="frmMain.hdnCmd.value='Update';frmMain.submit();" class="btn btn-primary"></td>
    <td><input name="btnAdd" type="button" id="btnCancel" value="ย้อนกลับ" onClick="window.location='<?php echo $_SERVER["PHP_SELF"];?>';" class="btn btn-warning">    </td>
  </tr>
  <?php
	}
  else
	{
  ?>
  <tr>
    <td><?php echo $i;?></td>
    <td><?php echo $objResult["Departmentcode"];?></td>
    <td><?php echo $objResult["Department"];?></td>
    <td>
    <a href="<?php echo $_SERVER["PHP_SELF"];?>?Action=Edit&CusID=<?php echo $objResult["Department_id"];?>">
    แก้ไข
    </a>
    </td>
    <td>
    <a href="JavaScript:if(confirm('Confirm Delete?')==true){window.location='<?php echo $_SERVER["PHP_SELF"];?>?Action=Del&CusID=<?php echo $objResult["Department_id"];?>';}">
    ลบแผนก
    </a>
    </td>
  </tr>
  <?php
	} $i++
  ?>
  <?php
} 
?>
  <tr>
    <td>#</td>
    <td><input type="text" name="adddepartmentcode"></td>
    <td><input type="text" name="adddepartment"></td>
    <td colspan="2" align="center"><input name="btnAdd" type="button" class="btn btn-info"btnAdd" value="Add" onClick="frmMain.hdnCmd.value='Add';frmMain.submit();"></td>
  </tr>
</table>
</form>
</div>
<?php include("footer.php"); ?>

<script src="bootstrap/js/jquery.js"></script>
<script src="bootstrap/js/bootstrap-datepicker.js"></script>
<script src="bootstrap/js/bootstrap.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.js"></script>
<script>window.jQuery || document.write("<script src='js/jquery-1.5.1.min.js'>\x3C/script>")</script>
<script src="js/app.js"></script>
</body>
</html>