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

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

    	$number = $_POST['number'];
    	$status = $_POST['status'];
    	$text_sta = "";

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

    	$num = $number[$i];
    	$error_t = "";

    	if($status[$i] == 'A' || $status[$i] == 'B'){
    		$text_sta = "[ เลข 2 หลัก ]";
    		$num =  substr("00".$num,-2,2);
    	}elseif($status[$i] == 'C' || $status[$i] == 'D'){
    		$text_sta = "[ เลข 3 หลัก ]";
    		$num =  substr("000".$num,-3,3);
    	}elseif($status[$i] == 'E' || $status[$i] == 'F'){
        $text_sta = "[ เลข 1 หลัก ]";
        $num =  substr("".$num,-1,1);
      }
      else{

    	}
      	
      	$sql = "select * from tb_locknum where loc_num='$num' and loc_type='$status[$i]'"; 
	     	$Query = mysqli_query($link,$sql);
	     	$Result = mysqli_fetch_array($Query);
	      $loc_num = $Result['loc_num'];
	      $loc_created = $Result['loc_created'];

        if($loc_num == $num){

          echo "<div class='alert alert-dismissible alert-danger'>
          <button type='button' class='close' data-dismiss='alert'>&times;</button>
          <strong>หมายเลข $num</strong> ได้ถูกล็อคไปแล้วเมื่อ $loc_created</div>";

        }
        else{

        	if($error_t == 'error'){
        		echo "<div class='alert alert-dismissible alert-danger'>
          		<button type='button' class='close' data-dismiss='alert'>&times;</button>
          		<strong>หมายเลข $num</strong> เกินจำนวน 2 หลัก</div>";
        	}
        	else{
    			$strSQL = "INSERT INTO tb_locknum";
    			$strSQL .="(loc_num,loc_type)";
    			$strSQL .="VALUES ";
    			$strSQL .="('$num','$status[$i]')";
    			$objQuery = mysqli_query($link,$strSQL);

	        echo "<div class='alert alert-dismissible alert-success'>
	        <button type='button' class='close' data-dismiss='alert'>&times;</button>
	        <strong>หมายเลข $num</strong> ได้ถูกล็อคเรียบร้อยแล้ว $text_sta</div>";
	    	}

        } //else

      } //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="text" class="col-lg-2 control-label">เลขที่ล็อค</label><div class="col-lg-4"><input type="number" class="form-control" name="number[]" maxlength="3" oninput="maxLengthCheck(this)" placeholder="ใส่เลขที่ต้องการล็อค" required></div><div class="col-lg-5"><select class="form-control" name="status[]"><option value="A">A : ล็อคเลขบน (เลข 2 ตัว)</option><option value="B">B : ล็อคเลขล่าง (เลข 2 ตัว)</option><option value="C">C : ล็อคเลขเต็ง (เลข 3 ตัว)</option><option value="D">D : ล็อคเลขโต็ด (เลข 3 ตัว)</option><option value="E">E : ล็อคเลขวิ่งบน (เลข 1 ตัว)</option><option value="F">F : ล็อคเลขวิ่ิ่งล่าง (เลข 1 ตัว)</option></select></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>

<!-- Number Lock -->
<!-- Number Lock -->
<!-- Number Lock -->

<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-2 control-label">เลขที่ล็อค</label>
     <div class="col-lg-4">
        <input type="number" class="form-control" name="number[]" maxlength="3" oninput="maxLengthCheck(this)" placeholder="ใส่เลขที่ต้องการล็อค" required>
      </div>
      <div class="col-lg-5">
        <select class="form-control" name="status[]">
          <option value="A">A : ล็อคเลขบน (เลข 2 ตัว)</option>
          <option value="B">B : ล็อคเลขล่าง (เลข 2 ตัว)</option>
          <option value="C">C : ล็อคเลขเต็ง (เลข 3 ตัว)</option>
          <option value="D">D : ล็อคเลขโต็ด (เลข 3 ตัว)</option>
          <option value="E">E : ล็อคเลขวิ่งบน (เลข 1 ตัว)</option>
          <option value="F">F : ล็อคเลขวิ่ิ่งล่าง (เลข 1 ตัว)</option>
        </select>
      </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>

<!-- Show Number Locked -->
<!-- Show Number Locked -->
<!-- Show Number Locked -->

<!-- Show Lock -->
    <div class="col-lg-12">

      <div class="col-lg-6">
        <div class="panel panel-primary">
          <div class="panel-heading">
            <h3 class="panel-title">A : เลขบน</h3>
            </div>
            <div class='panel-body'>
            <br><center><a href='unlock_num.php?num=A&id=0' class='btn btn-warning btn-xs'><i class='fa fa-unlock-alt' aria-hidden='true'></i> ปลดล็อค</a></center><br><br>

            <?php

            $sql_show = "select * from tb_locknum where loc_type='A' order by loc_num ASC"; 
            $objQuery = mysqli_query($link,$sql_show);
            while($objResult = mysqli_fetch_array($objQuery)){
            $locid = $objResult['loc_id'];
            $locnum = $objResult['loc_num'];

            echo "<a href='unlock_num.php?id=$locid&num=0' class='numlock btn btn-danger btn-xs'>$locnum <i class='fa fa-times' aria-hidden='true'></i></a> ";
            }

            ?>
            </div>
          </div>
      </div>

      <div class="col-lg-6">
        <div class="panel panel-primary">
          <div class="panel-heading">
            <h3 class="panel-title">B : เลขล่าง</h3>
            </div>
            <div class='panel-body'>
            <br><center><a href='unlock_num.php?num=B&id=0' class='btn btn-warning btn-xs'><i class='fa fa-unlock-alt' aria-hidden='true'></i> ปลดล็อค</a></center><br><br>

            <?php

            $sql_show = "select * from tb_locknum where loc_type='B' order by loc_num ASC"; 
            $objQuery = mysqli_query($link,$sql_show);
            while($objResult = mysqli_fetch_array($objQuery)){
            $locid = $objResult['loc_id'];
            $locnum = $objResult['loc_num'];

            echo "<a href='unlock_num.php?id=$locid&num=0' class='numlock btn btn-danger btn-xs'>$locnum <i class='fa fa-times' aria-hidden='true'></i></a> ";
            }

            ?>
            </div>
          </div>
      </div>

    </div>


    <div class="col-lg-12">
      <div class="col-lg-6">
        <div class="panel panel-primary">
          <div class="panel-heading">
            <h3 class="panel-title">C : เลขเต็ง</h3>
            </div>
            <div class='panel-body'>
            <br><center><a href='unlock_num.php?num=C&id=0' class='btn btn-warning btn-xs'><i class='fa fa-unlock-alt' aria-hidden='true'></i> ปลดล็อค</a></center><br><br>

            <?php

            $sql_show = "select * from tb_locknum where loc_type='C' order by loc_num ASC"; 
            $objQuery = mysqli_query($link,$sql_show);
            while($objResult = mysqli_fetch_array($objQuery)){
            $locid = $objResult['loc_id'];
            $locnum = $objResult['loc_num'];

            echo "<a href='unlock_num.php?id=$locid&num=0' class='numlock btn btn-danger btn-xs'>$locnum <i class='fa fa-times' aria-hidden='true'></i></a> ";
            }

            ?>
            </div>
          </div>
      </div>

      <div class="col-lg-6">
        <div class="panel panel-primary">
          <div class="panel-heading">
            <h3 class="panel-title">D : เลขโต็ด</h3>
            </div>
            <div class='panel-body'>
            <br><center><a href='unlock_num.php?num=D&id=0' class='btn btn-warning btn-xs'><i class='fa fa-unlock' aria-hidden='true'></i> ปลดล็อค</a></center><br><br>

            <?php

            $sql_show = "select * from tb_locknum where loc_type='D' order by loc_num ASC"; 
            $objQuery = mysqli_query($link,$sql_show);
            while($objResult = mysqli_fetch_array($objQuery)){
            $locid = $objResult['loc_id'];
            $locnum = $objResult['loc_num'];

            echo "<a href='unlock_num.php?id=$locid&num=0' class='numlock btn btn-danger btn-xs'>$locnum <i class='fa fa-times' aria-hidden='true'></i></a> ";
            }

            ?>
            </div>
          </div>
        </div>

      <div class="col-lg-6">
        <div class="panel panel-primary">
          <div class="panel-heading">
            <h3 class="panel-title">E : เลขวิ่งบน</h3>
            </div>
            <div class='panel-body'>
            <br><center><a href='unlock_num.php?num=E&id=0' class='btn btn-warning btn-xs'><i class='fa fa-unlock-alt' aria-hidden='true'></i> ปลดล็อค</a></center><br><br>

            <?php

            $sql_show = "select * from tb_locknum where loc_type='E' order by loc_num ASC"; 
            $objQuery = mysqli_query($link,$sql_show);
            while($objResult = mysqli_fetch_array($objQuery)){
            $locid = $objResult['loc_id'];
            $locnum = $objResult['loc_num'];

            echo "<a href='unlock_num.php?id=$locid&num=0' class='numlock btn btn-danger btn-xs'>$locnum <i class='fa fa-times' aria-hidden='true'></i></a> ";
            }

            ?>
            </div>
          </div>
        </div>

      <div class="col-lg-6">
        <div class="panel panel-primary">
          <div class="panel-heading">
            <h3 class="panel-title">F : เลขวิ่งล่าง</h3>
            </div>
            <div class='panel-body'>
            <br><center><a href='unlock_num.php?num=F&id=0' class='btn btn-warning btn-xs'><i class='fa fa-unlock-alt' aria-hidden='true'></i> ปลดล็อค</a></center><br><br>

            <?php

            $sql_show = "select * from tb_locknum where loc_type='F' order by loc_num ASC"; 
            $objQuery = mysqli_query($link,$sql_show);
            while($objResult = mysqli_fetch_array($objQuery)){
            $locid = $objResult['loc_id'];
            $locnum = $objResult['loc_num'];

            echo "<a href='unlock_num.php?id=$locid&num=0' class='numlock btn btn-danger btn-xs'>$locnum <i class='fa fa-times' aria-hidden='true'></i></a> ";
            }

            ?>
            </div>
          </div>
        </div>

<!-- End Show Lock -->

  <?php

  $sql_show = "select * from tb_locknum where loc_type='a' order by loc_num ASC"; 
	$objQuery = mysqli_query($link,$sql_show);
	while($objResult = mysqli_fetch_array($objQuery)){
	$locid = $objResult['loc_id'];
  $locnum = $objResult['loc_num'];

	//echo "<a href='unlock_num.php?id=$locid&num=0' class='btn btn-warning btn-xs'>$locnum <i class='fa fa-times' aria-hidden='true'></i></a> ";
	}

	//echo "</div><hr><div class='panel-body'><a href='unlock_num.php?num=b&id=0' class='btn btn-success btn-xs'><i class='fa fa-unlock-alt' aria-hidden='true'></i> ปลดล็อคเลข 3 หลักทั้งหมด</a><br><br>";
	$sql_show = "select * from tb_locknum where loc_type='b' order by loc_num ASC"; 
	$objQuery = mysqli_query($link,$sql_show);
	while($objResult = mysqli_fetch_array($objQuery)){
  $locid = $objResult['loc_id'];
	$locnum = $objResult['loc_num'];

	//echo "<a href='unlock_num.php?id=$locid&num=0' class='btn btn-danger btn-xs'>$locnum <i class='fa fa-times' aria-hidden='true'></i></a> ";
	}

  ?>


</div>

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

<style>
  .numlock {
  margin-bottom: 5px;

}
</style>