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

<div class="panel panel-primary">
  <div class="panel-heading">
    <h3 class="panel-title">แก้ไขสมาชิก</h3>
  </div>
  <div class="panel-body">

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

 <?php
	
	include("../config/mysql_connect.php");

	$edit_id = $_REQUEST['id'];
	
    if(isset($_POST["save"])){

    $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'];

    $sql_update = "update tb_account set
		acc_fname='$fname', 
		acc_lname='$lname', 
		acc_phone='$phone', 
		acc_status='$status',
		acc_commission='$commission',
		rate1='$rate1',
		rate2='$rate2',
		rate3='$rate3',
		rate4='$rate4',
		rate5='$rate5',
		rate6='$rate6' where acc_id='$edit_id'";
		$objQuery = mysqli_query($link,$sql_update);

		if($objQuery){
			echo "<meta http-equiv=refresh content=0;url=member.php>";
		}
		else{
			echo "<div class='alert alert-danger'>
			<button type='button' class='close' data-dismiss='alert'>&times;</button>
			<strong>Alerts!</strong> ล้มเหลว </div>";
		}

    }else{

	$strSQL = "SELECT * FROM tb_account WHERE acc_id = '$edit_id'";
	$objQuery = mysqli_query($link,$strSQL);
	$Result = mysqli_fetch_array($objQuery);

	$acc_id = $Result['acc_id'];
	$acc_user = $Result['acc_user'];
	$acc_status = $Result['acc_status'];
	$acc_fname = $Result['acc_fname'];
	$acc_lname = $Result['acc_lname'];
	$acc_phone = $Result['acc_phone'];
	$acc_commission = $Result['acc_commission'];

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

	$status = '';

	if($acc_status == 'admin'){
		$status = 'selected';
	}

	?>

<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" value="<?php echo"$acc_user"; ?>" disabled>
      </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" value="<?php echo"$acc_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" value="<?php echo"$acc_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" value="<?php echo"$acc_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" value="<?php echo"$acc_commission"; ?>" placeholder="คอมมิชชั่น"><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="<?php echo"$rate1"; ?>">
      </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="<?php echo"$rate2"; ?>">
      </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="<?php echo"$rate3"; ?>">
      </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="<?php echo"$rate4"; ?>">
      </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="<?php echo"$rate5"; ?>">
      </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="<?php echo"$rate6"; ?>">
      </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" <?php echo"$status"; ?> >Admin</option>
        </select>
      </div>
    </div>

    <div class="form-group">
      <div class="col-lg-10 col-lg-offset-2">
      	<a href='member.php' class='btn btn-default'>ย้อนกลับ</a>
        <button type="submit" class="btn btn-primary" name="save">บันทึก</button>
        <a href='setpwd.php?id=<?php echo"$acc_id"; ?>' class='btn btn-info'>เปลี่ยนรหัสผ่านใหม่</a>
      </div>
    </div>

  </fieldset>
</form>
<?php } ?>

  </div>
</div>

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