<script type="text/javascript">

$(document).ready(function(){
    /*$("#submit").click(function(e){
            if(!validate_module()){
                e.preventDefault();
            }
            $("#frm-role").submit();
  });*/

    /*$("#role_name").blur(function(){
            
            var rolename = $("#role_name").val();
            //alert(rolename);
            //$("#submit").prop('disabled', true);
            $.ajax({
                url: "roleadd.php",
                data: 'act=validate_email&role_name='+rolename,
                method: 'POST',
                success: function(msg){
                    if(msg!=''){
                        $("#role_name").addClass('error');
                        $("#role_name").focus();
                        $("#msg_role_name").html('Role name alredy exists').show();
                        return false;
                    }else{
                        $("#submit").prop('disabled', false);
                        $("#role_name").removeClass('error');
                        $("#msg_role_name").hide();
                    }
                }
            });
    });*/

    var module_access = $('#module_access:checked').val();
    if(module_access === 'Full')
    {
      //$("input:roledata").attr("checked", false);
      $("input.roledata").attr("disabled", true);
    }

    $('#module_access').click(function() {
        
        if ($(this).is(':checked')) 
        {
            $("input.roledata").attr("disabled", true);
        }else{
            $("input.roledata").removeAttr("disabled");
        }
    
    });
});


function validate_module(){
      
   if($("#role_name").val()=='')
   {
      $("#role_name").addClass('error');
      $("#role_name").focus();
      $("#msg_role_name").html('Please enter name').show();
      return false;
    }
    else
    {
      $("#role_name").removeClass('error');
      $("#msg_role_name").hide();
    }

    if ($('input:checkbox:checked').length == 0)
    {
      $("#module_access").addClass('error');
      $("#module_access").focus();
      $("#msg_module_access").html('Please atleast one select').show();
      return false;
    }else
    {
      $("#module_access").removeClass('error');
      $("#msg_module_access").hide();
    }

    
    /*var rolename = $("#role_name").val();
    var moduleaccess = $('input:checkbox:checked').length;
    if(rolename != '' && moduleaccess > 0)
    {
        var t;
        $.ajax({
          url: "roleadd.php",
          data: 'act=validate_name&role_name='+rolename,
          method: 'POST',
          success: function(msg){
              if(msg==='success'){
                //alert(msg);  
                //return true;
                $("#frm-role").submit();
              }else{
               $("#role_name").addClass('error');
               $("#role_name").focus();
               $("#msg_role_name").html(msg).show();
               return false;
              }
          }
      });
        
    }*/
    //return true;
    /*if(t === 'noerror')
    {
      return true;
    }else{
      return false;
    }*/
    
    return true;
    
  <?php //if(!isset($_REQUEST['id'])){ ?>

        /*if($("#role_name").val()==''){
            $("#role_name").addClass('error');
            $("#role_name").focus();
            $("#msg_role_name").html('Please enter name').show();
          return false;
          
        }*/
  <?php //} ?>
}


</script>

<div class="breadcrumb">
    <h1>Role</h1>
    <ul>
        <li><a href="<?php echo SITE_URL.ADMIN_DIR ?>">Dashboard</a></li>
        <li>Add New Role</li>

    </ul>
</div>


<div class="separator-breadcrumb border-top"></div>

<div class="text-sm-right">
     <span><a href="rolelist.php">Back</a></span>       
</div>

<div class="row">
    <div class="col-md-12">
        <div class="card mb-4">
            <div class="card-body">
                <div class="card-title mb-3">Add New Role</div>
                <!-- onsubmit="return validate_module();" -->
                <form action="" id='frm-role' method="post"  enctype="multipart/form-data">
                  <?php if(isset($roleDetails->role_id) && $roleDetails->role_id!=0) { ?>
                       <input type="hidden" id="role_id" name="role_id" value="<?php echo $roleDetails->role_id ?>" />
                    <?php } ?>
                    <div class="row">
                          <div id="rolenamesucc"></div>
                        <div class="col-md-6 form-group mb-3">
                            <label for="role_name">Role Name <span class="asterisk">*</span></label>
                            <input type="text" class="form-control" id="role_name" name="role_name" value="<?php echo $roleDetails->role_name; ?>" placeholder="Enter your role name">
                            <div class="invalid-tooltip" id="msg_role_name"></div>
                        </div>

                        <div class="col-md-6 form-group mb-3">
                            
                        </div>

                        <?php if($roleDetails->role_id!='1')
                        { ?>
                        
                            <?php $roleaccessArr = array();
                                  if(isset($roleDetails->role_access) && $roleDetails->role_access!=''){
                                    $roleaccessArr = explode(',',$roleDetails->role_access);   
                              } ?>

                              <div class="col-md-12 form-group mb-3">
                                <label class="control-label">Module permission</label>
                                <div class="controls">

                          
                                <?php if($allModules && count($allModules) > 0)
                                { ?>
                                    <label class="checkbox checkbox-outline-primary inline-checkbox">

                                            <input type="checkbox" name="module_access[]" id="module_access"  value="Full" <?php echo ($roleaccessArr && in_array("Full",$roleaccessArr)) ? "checked='checked'" : '' ?>>
                                            <span>Full</span>
                                            <span class="checkmark"></span>
                                     </label>

                                   <?php 
                                   $count = 0;
                                   foreach ($allModules as $mkey => $modvalue)
                                    { 
                                      if ($count && $count % 7 == 0){ echo '</br></br></br>'; }  
                                      ?>
                                        
                                     <label class="checkbox checkbox-outline-primary inline-checkbox">

                                            <input class = "roledata" type="checkbox" name="module_access[]" id="module_access"  value="<?php echo $modvalue->id ?>" <?php echo ($roleaccessArr && in_array($modvalue->id,$roleaccessArr)) ? "checked='checked'" : '' ?>>
                                            <span><?php echo $modvalue->name; ?></span>
                                            <span class="checkmark"></span>
                                     </label>
                                     <?php $count++; ?>
                                     
                                <?php } 
                                } ?>    
                                  <div class="invalid-tooltip" id="msg_module_access"></div>                
                                </div>
                              </div>
                             
                        <?php 
                        }
                        ?> 

                        <div class="col-md-12">
                            <br/>
                            <!-- <button class="btn btn-primary"  id="submit" name="submit">Submit</button> -->
                            <input type="submit" class="btn btn-primary" id="submit" name="submit" value="Submit">
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">
  
  $( "form" ).on( "submit", function(e) {
      e.preventDefault();
      var role_name = $("#role_name").val();
      var module_access = new Array();
        $("input:checked").each(function() {
           module_access.push($(this).val());
        });
      

      if($("#role_name").val()=='')
       {
          $("#role_name").addClass('error');
          $("#role_name").focus();
          $("#msg_role_name").html('Please enter name').show();
          return false;
        }else if($('input:checkbox:checked').length == 0)
        {
            if($("#role_name").val()!='')
            {
              $("#role_name").removeClass('error');
              $("#msg_role_name").hide();
            }

            $("#module_access").addClass('error');
            $("#module_access").focus();
            $("#msg_module_access").html('Please atleast one select').show();
            return false;
        }
        else
        {
          if($('input:checkbox:checked').length > 0)
          {
            $("#module_access").removeClass('error');
            $("#msg_module_access").hide();
          }

          <?php if(!isset($_REQUEST['id']))
          { ?>
              $.ajax({
                  url: "roleadd.php",
                  data: 'act=validate_name&role_name='+role_name,
                  method: 'POST',
                  success: function(msg){
                      if(msg!='success'){
                          $("#role_name").addClass('error');
                          $("#role_name").focus();
                          $("#msg_role_name").html(msg).show();
                          return false;
                      }else{
                          $("#role_name").removeClass('error');
                          $("#msg_role_name").hide();

                          $.ajax({
                              url: "roleadd.php",
                              data: { act:"load",
                                    role_name:role_name,module_access:module_access },
                              method: 'POST',
                              success: function(msg){
                                  if(msg!=''){
                                      window.location='rolelist.php';
                                      //return false;
                                  }
                              }
                          });
                      }
                  }
              });
          <?php 
          }else
          {  ?>

            $.ajax({
                  url: "roleadd.php",
                  data: 'act=validate_name&role_name='+role_name+"&role_id="+<?php echo $_REQUEST['id'];?>,
                  method: 'POST',
                  success: function(msg){
                      if(msg!='success'){
                          $("#role_name").addClass('error');
                          $("#role_name").focus();
                          $("#msg_role_name").html(msg).show();
                          return false;
                      }else{
                          $("#role_name").removeClass('error');
                          $("#msg_role_name").hide();

                          $.ajax({      
                                    url: "roleadd.php",
                                    data: { act:"load",
                                          role_name:role_name,module_access:module_access,role_id:<?php echo $_REQUEST['id'];?> },
                                    method: 'POST',
                                    success: function(msg){
                                        if(msg!=''){
                                            window.location='rolelist.php';
                                        }
                                    }
                                });
                        }
                      }
                  });
         <?php }
          ?>
      }

  });
</script>