<?php include("../templates/header.php"); ?>
<?php include("check_admin.php"); ?>

<!-- ############## -->
<!-- Create Account -->
<!--       PHP      -->
<!-- ############## -->

 <?php
	
	include("../config/mysql_connect.php");
	
    if(isset($_POST["save"])){

    	$username = $_POST['username'];
    	$password = $_POST['password'];
    	$fname = $_POST['fname'];
    	$lname = $_POST['lname'];
    	$phone = $_POST['phone'];
    	$commission = $_POST['commission'];
    	$status = $_POST['status'];

    	$rate1 = $_POST['rate1'];
    	$rate2 = $_POST['rate2'];
    	$rate3 = $_POST['rate3'];
    	$rate4 = $_POST['rate4'];
    	$rate5 = $_POST['rate5'];
    	$rate6 = $_POST['rate6'];

    $str = "select acc_user from tb_account where acc_user = '".trim($_POST['username'])."' ";
		$data = mysqli_query($link,$str);
		$num = mysqli_num_rows($data);

		if($num>0){

			echo "<div class='alert alert-dismissible alert-danger'>
  			<button type='button' class='close' data-dismiss='alert'>&times;</button>
 			<strong>Error Username</strong> โปรดตรวจสอบ username ใหม่อีกครั้ง .
			</div>";

		}
		else{

			echo "<div class='alert alert-dismissible alert-success'>
  		<button type='button' class='close' data-dismiss='alert'>&times;</button>
 			<strong>Create Succes</strong> เพิ่มบัญชีใหม่เข้าสู่ระบบเรียบร้อย.
			</div>";

			$salt = 'fq7wrt3ygio8p5axd4h9j0kl2zcevb1nm6su';
		  $hash_pwd = hash_hmac('sha256', $password, $salt);
		 	$strSQL = "INSERT INTO tb_account";
		 	$strSQL .="(acc_user,acc_pass,acc_fname,acc_lname,acc_phone,acc_commission,acc_status,rate1,rate2,rate3,rate4,rate5,rate6) ";
		  $strSQL .="VALUES ";
		  $strSQL .="('$username','$hash_pwd','$fname','$lname','$phone','$commission','$status','$rate1','$rate2','$rate3','$rate4','$rate5','$rate6')";
		  $objQuery = mysqli_query($link,$strSQL);

		}

    }

?>

<!-- ############## -->
<!-- ############## -->

<div class="panel panel-primary">
  <div class="panel-heading">
    <h3 class="panel-title">สร้างบัญชีใหม่</h3>
  </div>
  <div class="panel-body">

<form class="form-horizontal" method="post">
<br>
  <fieldset>
    <div class="form-group">
      <label for="inputUsername" class="col-lg-2 control-label">Username</label>
      <div class="col-lg-9">
        <input type="text" class="form-control" name="username" placeholder="Username" required>
      </div>
    </div>
    
    <div class="form-group">
      <label for="inputPassword" class="col-lg-2 control-label">Password</label>
      <div class="col-lg-9">
        <input type="password" class="form-control" name="password" placeholder="Password" required>
      </div>
    </div>

    <div class="form-group">
      <label for="inputName" class="col-lg-2 control-label">Name</label>
      <div class="col-lg-9">
        <input type="text" class="form-control" name="fname" placeholder="ชื่อ">
      </div>
    </div>

    <div class="form-group">
      <label for="inputName" class="col-lg-2 control-label">Surname</label>
      <div class="col-lg-9">
        <input type="text" class="form-control" name="lname" placeholder="นามสกุล">
      </div>
    </div>

    <div class="form-group">
      <label for="inputPhone" class="col-lg-2 control-label">Phone</label>
      <div class="col-lg-9">
        <input type="text" class="form-control" name="phone" placeholder="เบอร์ติดต่อ">
      </div>
    </div>

    <div class="form-group">
      <label for="inputCommission" class="col-lg-2 control-label">Commission</label>
      <div class="col-lg-9"><div class="input-group">
        <input type="number" class="form-control" name="commission" placeholder="คอมมิชชั่น" value="25"><span class="input-group-addon">%</span>
      </div></div>
    </div>

    <div class="form-group">
      <label for="input" class="col-lg-2 control-label">เรท 2 หลัก</label>
      <div class="col-lg-3">
        <input type="number" class="form-control" name="rate1" value="70">
      </div>
       <label for="input" class="col-lg-2 control-label">เรท 3 หลัก</label>
      <div class="col-lg-3">
        <input type="number" class="form-control" name="rate2" value="400">
      </div>
    </div>

    <div class="form-group">
      <label for="input" class="col-lg-2 control-label">เรท 3 โต้ด</label>
      <div class="col-lg-3">
        <input type="number" class="form-control" name="rate3" value="70">
      </div>
       <label for="input" class="col-lg-2 control-label">เรทเลขใบ</label>
      <div class="col-lg-3">
        <input type="number" class="form-control" name="rate4" value="1000">
      </div>
    </div>

    <div class="form-group">
      <label for="input" class="col-lg-2 control-label">เรทวิ่งบน</label>
      <div class="col-lg-3">
        <input type="number" class="form-control" name="rate5" value="3">
      </div>
       <label for="input" class="col-lg-2 control-label">เรทวิ่งล่าง</label>
      <div class="col-lg-3">
        <input type="number" class="form-control" name="rate6" value="4">
      </div>
    </div>

     <div class="form-group">
      <label for="select" class="col-lg-2 control-label">สถานะ</label>
      <div class="col-lg-9">
        <select class="form-control" name="status">
          <option value="user">User</option>
          <option value="admin">Admin</option>
        </select>
      </div>
    </div>

    <div class="form-group">
      <div class="col-lg-10 col-lg-offset-2">
        <button type="reset" class="btn btn-default">ยกเลิก</button>
        <button type="submit" class="btn btn-primary" name="save">บันทึก</button>
      </div>
    </div>

  </fieldset>
</form>

  </div>
</div>

<?php include("../templates/footer.php"); ?>