<?php 

require_once("config/configDB.php"); 
require "userPermission.php";

$sql_InvoiceQuery   = mysqli_query($conn,"SELECT count(*) as code FROM customer_master");
$rowInvoiceCount    = mysqli_fetch_array($sql_InvoiceQuery,MYSQLI_ASSOC);
$code_count      = $rowInvoiceCount['code']+1;

$createdby      = $_SESSION['username'];
    $userID         = $_SESSION['user_id'];
	
	//echo $createdby;
	//echo $userID;

?>

<!DOCTYPE html>
<html>
<head>
    
    <?php 
      require 'meta.php'; 
      require 'header.php'; 
    ?>

</head>
<body>
    <?php require 'menu.php' ?>
<div class="container-fluid">
<h2 class="text-center">Customer Master Details</h2><br>
    <form class="form-horizontal" action="customerMasterInsert.php" method="post" id="customerMasterForm" onSubmit="return checkForm(this);">
      
	  <input type="hidden" value="<?php echo $createdby;?>" name="createdby">
	  <input type="hidden" value="<?php echo $userID;?>" name="userID">

    <div class="form-group">
      <label class="control-label col-sm-2" for="email">Customer Name *</label>
      <div class="col-sm-4">
        <input type="text" class="form-control input-lg" placeholder="Enter Customer Name" name="customername" autocomplete="off" required> 
      </div>
  
    
   
      <label class="control-label col-sm-2" for="email">Customer Name Tamil*</label>
      <div class="col-sm-4">
        <input type="text" class="form-control input-lg tamil" placeholder="Enter Customer Name" name="cname_tamil" id="cname_tamil" autocomplete="off" required> <br>
        <a href="https://www.google.com/intl/ta/inputtools/try/" target="_blank">Tamil Type</a>
      </div>
    </div>

    <div class="form-group">
      <label class="control-label col-sm-2" for="email">Address </label>
      <div class="col-sm-4">
        <textarea  class="form-control input-lg" placeholder="Enter Address" name="address1" rows="4"></textarea>
      </div>

      <label class="control-label col-sm-2" for="email"> Customer Code *</label>
        <div class="col-sm-4">
          <input type="text" class="form-control input-lg" placeholder="Enter Customer Code" name="customercode" autocomplete="off" value="<?php echo "CUS-".$code_count; ?>" required readonly>
        </div> 
      
    </div>

    <div class="form-group">

    <label class="control-label col-sm-2" for="email"> Area</label>
      <div class="col-sm-4">
        <select  class="form-control input-lg "  name="area" id="area" required>
        <option value="">Select Area</option>
        <?php
		$sql_qry=mysqli_query($conn,"select * from area_master");
		while($row_area=mysqli_fetch_array($sql_qry))
		{
		?>
        <option value="<?php echo $row_area['areaname'];?> "><?php echo $row_area['areaname'];?></option>
        <?php
		}
		?>
        </select>
      </div>

     <label class="control-label col-sm-2" for="email"> Area Tamil</label>
      <div class="col-sm-4">
        <input type="text" class="form-control input-lg tamil" placeholder="Enter area" name="area_tamil" id="area_tamil" reqyured readonly>
      </div>
    
     
    </div>
    
    <div class="form-group">
     
      <label class="control-label col-sm-2" for="email"> Pincode</label>
      <div class="col-sm-4">
        <input type="text" class="form-control input-lg" placeholder="Enter Pincode" name="pincode">
      </div>
    
      <label class="control-label col-sm-2" for="email">Mobile *</label>
      <div class="col-sm-4">
        <input type="number" class="form-control input-lg" placeholder="Enter mobile Number" name="mobile" autocomplete="off" required>
      </div>

      
            
    </div>
        
    
     <div class="form-group">
     
      <label class="control-label col-sm-2" for="email"> Opening Balance</label>
      <div class="col-sm-4">
        <input type="number" class="form-control input-lg"  name="opening_balance" value="0" required>
      </div>
    
      
    </div>   
    
        
    
        
    
    
    
    <div class="form-group">        
      <div class="col-sm-12 text-center">
        <button type="submit" name="myButton" class="btn btn-default btn-lg">Save</button>
      </div>
    </div>
  </form>   
</div>


    <?php require 'footer.php' ?>
    
    <script>

  var checkForm = function(form) {

    //
    // validate form fields
    //

    form.myButton.disabled = true;
    return true;
  };


</script>

<SCRIPT language=JavaScript src="js/utf.js"></SCRIPT>
		<SCRIPT language=JavaScript src="js/tamil.js"></SCRIPT>
		
		<script>
			$(document).ready(function(){
				$('.tamil').on('keydown',function(event){
					if(event.which==121){
						$(this).toggleClass('tamil');
						return false;
					}
					if($(this).hasClass('tamil')){
						toggleKBMode(event);
					}else{
						return true;
					}
				});
				$('.tamil').on('keypress',function(event){
					if($(this).hasClass('tamil')){
						convertThis(event);
					}
				});
			});
		</script>

<script>

$(document).ready(function(){  
  
      $('#area').change(function(){  
	   
	  var query=$(this).val();
	 //alert(query);
	 
	
	            $.ajax({
            type:'POST',
            url:'pick_area.php',
            data:{area:query},
            success:function(data){
                console.log(data);
                var output= data.split("||");
                console.log(output);
            $("#area_tamil").val(output[0]);
       
           
          
            } 
           });  
      }); 
	  
	  
	  });   
	  </script>
    
    <script>
      $('#customerMasterForm').bootstrapValidator({
        // To use feedback icons, ensure that you use Bootstrap v3.1.0 or later
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
          customername: {
                message: 'The customer name is not valid',
                validators: {
                    notEmpty: {
                        message: 'The customer name is required and cannot be empty'
                    }
                }
            },
            mobile: {
                message: 'The mobile is not valid',
                validators: {
                    notEmpty: {
                        message: 'The mobile is required and cannot be empty'
                    },
                    stringLength: {
                        min: 10,
                        max: 10,
                        message: 'The mobile must be more than 6 and less than 30 characters long'
                    },
                    regexp: {
                        regexp: /^[0-9]+$/,
                        message: 'The mobile can only consist of number'
                    },
                }
            }

        }
    });
    
    </script>
</body>
</html>