
<%= simple_form_for(@user) do |f| %> 
<nav class="breadcrumb">
    <a class="breadcrumb-item" href="<%= dashboard_path %>">Home</a>
    <a class="breadcrumb-item active" href="<%= users_path %>">User</a>
    <a class="breadcrumb-item active" href="#">
        <% if !@user.id.nil? %>
            Edit
        <% else %>
            New
        <% end %>
    </a>
</nav> 
<div class="content">
  <% if @user.id.nil? %>
    <div class="row">    
        <div class="col-md-5" >
            <div class="form-group">
                <label for="email" class="string optional control-label">Registered Email:</label>
                <input type="text" id="registered_email" name="registered_email" class="form-control" placeholder="Please enter member mail" value="<%= @email %>">          
            </div> 
        </div>
        <div class="col-md-1">  
        <label for="email" class="string optional control-label"> &nbsp;&nbsp;&nbsp;</label>    
          <button type="button" class="btn btn-primary" style="float:right;" id="check_email"  >Check</button>  
        </div>
    </div>
  <% end %>
  <div class="row">
      <div class="col-md-6" id="textbox_group">
          <div class="form-group">
                <label for="user_type" class="string optional control-label">User Type:</label>
                  
                <%= f.select(:user_type, ['User', "Seller"], {}, { :class => 'form-control' })  %>       
          </div> 
          <div class ="form-group" >
             <label for="name" class="string optional control-label">Name:</label>
             <%= f.input :name ,:label =>false,:error => false,:placeholder =>'Please enter user name',input_html: { class: "form-control" } %>
             <%= f.error :name ,style: 'color: red' %> 
          </div>   
          <div class ="form-group" >
              <label for="nrc" class="string optional control-label">NRC:</label>
              <%= f.input :nrc ,:error=>false,:label =>false,:placeholder =>'Please enter  user NRC',input_html: { class: "form-control" }  %>           
          </div>
          <div class ="form-group" >
              <label for="email" class="string optional control-label">Email:</label>
              <%= f.input :email ,:error=>false,:label =>false ,:placeholder =>'Please enter user email',input_html: { class: "form-control" }  %>
          </div>
          <div class ="form-group" >
              <label for="phone" class="string optional control-label">Phone:</label>
              <%= f.input :phone,:error=>false,:label =>false,:placeholder =>'Please enter user phone',input_html: { class: "form-control" }  %>
          </div>
          <div class ="form-group" >
            <label for="address" class="string optional control-label">Address:</label>
            <%= f.input :address,:error=>false,:label =>false,:placeholder =>'Please enter user address',input_html: { class: "form-control" }  %>
          </div>                      
          <% if !@user.id.nil? %>             
              <div class ="form-group" >
                <label for="is_active" class="string optional control-label">Active:</label>
                <%= f.check_box :is_active %>
              </div>
          <% end %>
          <div class ="form-group" >
             <label></label>
              <div class="actions">
                 <%= f.button :submit, :class => 'btn btn-primary',:id =>'btn_submit' %>   
                 <%= link_to 'Cancel', users_path ,:class => 'btn btn-primary',:id => 'btnback' %>
              </div>
          </div>
      </div>
  </div>   
</div>

<% end %>
<script >
$(document).ready(function() {  
    error_count="<%= @user.errors.count %>"  
    if (error_count>0){
      status='' 
    }else{
      status='disabled' 
    }
       
    name=$("#user_name").val()
    if (name.trim() !=""){        
        status=''
    }      
    enable_control(status)
    
    $("#check_email").click(function(){  
        registered_email = $("#registered_email").val();
        $.ajax({
              type:'get',
              url: '<%= check_registered_email_path %>',
              data:'registered_email='+ registered_email ,
              success:function(result){   
                 // $("#show-data").html('')
                  if (result.status == 'true'){
                     enable_control(false);                                         
                  }
                  else
                  {
                    if (result.data === null){                      
                        enable_control(true); 
                    }
                    else
                    {                     
                      $(result.data).each(function(i){                      
                          name = result.data.name
                          nrc =result.data.nrc
                          phone =result.data.phone
                          address =result.data.address
                          email = result.data.email                      
                        
                          $("#user_name").val(name)
                          $("#user_email").val(email)
                          $("#user_nrc").val(nrc)
                          $("#user_phone").val(email)
                          $("#user_address").val(address)
                          $("#btn_submit").hide()
                        
                      });
                    }                     
                  }
              }
        });
     });
    function enable_control(status){      
        $("#user_name").prop('disabled',status);
        $("#user_email").prop('disabled',status);
        $("#user_nrc").prop('disabled',status);
        $("#user_phone").prop('disabled',status);
        $("#user_address").prop('disabled',status);       
        $("#btn_submit").prop('disabled',status)
    }

})
</script>