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

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

  $strSQL = "SELECT * FROM setting_default";
  $objQuery = mysqli_query($link,$strSQL);
  $Result = mysqli_fetch_array($objQuery);
  $def_status = $Result['def_status'];

  if($def_status == 'Off'){

        echo "<div class='alert alert-dismissible alert-danger'>
        <button type='button' class='close' data-dismiss='alert'>&times;</button>
        <strong>ปิดการซื้อขาย</strong> : ขออภัยระบบได้ปิดการซื้อขายแล้ว <a href='page3.php'>ย้อนกลับ</a></div>";

  }else{
	
    if(isset($_POST["save"])){

    	$number_a = $_POST['number_a'];
    	$number_b = $_POST['number_b'];
    	$number_c = $_POST['number_c'];

    	foreach ( $_POST['number_a'] as $i => $value ){

    	$strSQL = "INSERT INTO tb_number";
    	$strSQL .="(num_buy,num_one,num_two,num_type,num_created,acc_id)";
    	$strSQL .="VALUES ";
    	$strSQL .="('$number_a[$i]','$number_b[$i]','$number_c[$i]','c',now(),'$session_id')";
    	$objQuery = mysqli_query($link,$strSQL);

      echo "<div class='alert alert-dismissible alert-success'>
      <button type='button' class='close' data-dismiss='alert'>&times;</button>
      <strong>[ <b>$number_a[$i]</b> : $number_b[$i] : $number_c[$i] ]</strong> บันทึกข้อมูลเข้าสู่ระบบเรียบร้อย.</div>";

      } //foreach

    }
 ?>

<script>
$(document).ready(function() {
    var max_fields      = 10; //maximum input boxes allowed
    var wrapper         = $(".input_fields_wrap"); //Fields wrapper
    var add_button      = $(".add_field_button"); //Add button ID
    
    var x = 1; //initlal text box count
    $(add_button).click(function(e){ //on add input button click
        e.preventDefault();
        if(x < max_fields){ //max input box allowed
            x++; //text box increment
            $(wrapper).append('<div class="form-group"><label for="input" class="col-lg-1 control-label"></label><div class="col-lg-3"><input type="number" class="form-control" name="number_a[]" maxlength="1" placeholder="*ใส่เลขที่ซื้อ 1 หลักเท่านั้น" required></div><div class="col-lg-3"><input type="number" class="form-control" name="number_b[]" placeholder="ใส่ราคาเลขวิ่งบน default = 0"></div><div class="col-lg-3"><input type="number" class="form-control" name="number_c[]" placeholder="ใส่ราคาเลขวิ่งล่าง default = 0"></div><a href="#" class="remove_field btn btn-danger btn-xs"><i class="fa fa-close"></i> ลบ</a></div>'); //add input box
        }
    });
    
    $(wrapper).on("click",".remove_field", function(e){ //user click on remove text
        e.preventDefault(); $(this).parent('div').remove(); x--;
    })
});
</script>

<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">
  <fieldset><br>
  	<div class="input_fields_wrap">
  		<button class="add_field_button btn btn-success" type="submit"><span class="glyphicon glyphicon-plus"></span> เพิ่มช่อง</button><hr>

    <div class="form-group">
     <label for="text" class="col-lg-3 control-label">เลขที่ซื้อ</label>
     <label for="text" class="col-lg-3 control-label">เลขวิ่งบน</label>
     <label for="text" class="col-lg-3 control-label">เลขวิ่งล่าง</label>
    </div>

    <div class="form-group">
      <label for="input" class="col-lg-1 control-label"></label>
      <div class="col-lg-3">
        <input type="number" class="form-control" name="number_a[]" maxlength="1" placeholder="*ใส่เลขที่ซื้อ 1 หลักเท่านั้น" required>
      </div>
      <div class="col-lg-3">
        <input type="number" class="form-control" name="number_b[]" placeholder="ใส่ราคาเลขวิ่งบน default = 0">
      </div>
      <div class="col-lg-3">
        <input type="number" class="form-control" name="number_c[]" placeholder="ใส่ราคาเลขวิ่งล่าง default = 0">
      </div>
    </div>

    </div>

    <button type="reset" class="btn btn-default">ยกเลิก</button>
    <button type="submit" class="btn btn-primary" name="save">บันทึก</button>
</fieldset>
</form>

  </div>
</div>

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