<script type="text/javascript">

$(document).ready(function(){
    
    var maxField = 30; //Input fields increment limitation
    var addButton = $('.add_button'); //Add button selector
    var wrapper = $('.field_wrapper'); //Input field wrapper
    var wrapper_remove = $('.field_wrapper_remove');
    var fieldHTML = '<div><br/><input type="text" name="module_page[]" value="" placeholder="Page Name"/>&nbsp;&nbsp;&nbsp;&nbsp;<a href="javascript:void(0);" class="remove_button"><img src="<?Php echo SITE_URL."x-admin/assets/images/clinic_setting/remove-icon.png" ?>"/></a></div>'; //New input field html 
    var x = 1; //Initial field counter is 1
    //<input type="text" name="field_setting2[]" value=""/>
    //Once add button is clicked
    $(addButton).click(function(){
        //Check maximum number of input fields
        if(x < maxField){ 
            x++; //Increment field counter
            $(wrapper).append(fieldHTML); //Add field html
        }
    });
    
    //Once remove button is clicked
    $(wrapper).on('click', '.remove_button', function(e){
        e.preventDefault();
        $(this).parent('div').remove(); //Remove field html
        x--; //Decrement field counter
    });

    $(wrapper_remove).on('click', '.remove_button', function(e){
        e.preventDefault();
        $(this).parent('div').remove(); //Remove field html
        x--; //Decrement field counter
    });
});

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

    if($("input[name='module_page[]']").val() == '')
    {
        $("#module_page").addClass('error');
        $("#module_page").focus();
        $("#msg_module_page").html('Please atleast one page name').show();
        return false;
    }else{
        $("#module_page").removeClass('error');
        $("#module_page").hide();
    }
  
	
	<?php if(!isset($_REQUEST['id'])){ ?>

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

</script>


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

    </ul>
</div>


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

<div class="text-sm-right">
     <span><a href="modulelist.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 Module</div>
                <!-- onsubmit="return validate_module();" -->
                <form action="" method="post"  enctype="multipart/form-data">
                	<?php if(isset($moduleDetails->id) && $moduleDetails->id!=0) { ?>
                       <input type="hidden" id="module_id" name="module_id" value="<?php echo $moduleDetails->id ?>" />
                    <?php } ?>
                    <div class="row">

                        <div class="col-md-6 form-group mb-3">
                            <label for="module_name">Module Name <span class="asterisk">*</span></label>
                            <input type="text" class="form-control" id="module_name" name="module_name" value="<?php echo $moduleDetails->name; ?>" placeholder="Enter your module name">
                            <div class="invalid-tooltip" id="msg_module_name"></div>
                        </div>

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

                        <?php
                            if(isset($modulepageDetails) && count($modulepageDetails) > 0)
                            {
                                foreach ($modulepageDetails as $skey => $pagevalue) 
                                { 
                                    $modpagename = $pagevalue->page;
                                    $modpageid = $pagevalue->id;

                                    ?>
                                    <div class="col-md-8 form-group mb-3 field_wrapper_remove">
                                        <input type="text"  name="module_page[]" value="<?php echo $modpagename; ?>"/>&nbsp;&nbsp;&nbsp;
                                        <input type="hidden"  name="module_pageid[]" value="<?php echo $modpageid; ?>"/>&nbsp;&nbsp;&nbsp;

                                        <a href="javascript:void(0);" class="remove_button"><img src="<?Php echo SITE_URL."x-admin/assets/images/clinic_setting/remove-icon.png" ?>"/></a>
                                    </div>
                               <?php }
                            }
                        ?>
                        
                        <div class="col-md-6 form-group mb-3 field_wrapper">
                            <input type="text" name="module_page[]" value="" placeholder="Page Name" />&nbsp;&nbsp;&nbsp;&nbsp;
                            <!-- <input type="text" name="field_setting2[]" value="" placeholder="info@dmo.com"/> -->
                            <a href="javascript:void(0);" class="add_button" title="Add field"><img src=<?Php echo SITE_URL."x-admin/assets/images/clinic_setting/add-icon.png" ?>></a>
                            <div class="invalid-tooltip" id="msg_module_page"></div>
                        </div>

                   
                        <!-- <div class="col-md-6 form-group mb-3">
			                <label class="control-label">Status</label>
			                <div class="controls">

		                    <label class="switch switch-primary mr-3">
                                <input type="checkbox" id="module_status" name="module_status" <?php echo ($moduleDetails->status=='1') ? "checked='checked'" : "" ?>>
                                <span class="slider"></span>
                            </label>
			                </div>
	                     </div> -->
						

                        <div class="col-md-12">
                            <button class="btn btn-primary" id="submit" name="submit">Submit</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
  
  $( "form" ).on( "submit", function(e) {
      e.preventDefault();
        var module_name = $("#module_name").val();
        var module_page = []; 
        var module_pageid = [];
        $("input[name='module_page[]']").each(function(){
            if($(this).prop("value") != '')
            {
                module_page.push($(this).prop("value"));    
            }
        });

        $("input[name='module_pageid[]']").each(function(){
            if($(this).prop("value") != '')
            {
                module_pageid.push($(this).prop("value"));    
            }
        });    
        
        
        if($("#module_name").val()=='')
        {
            $("#module_name").addClass('error');
            $("#module_name").focus();
            $("#msg_module_name").html('Please enter name').show();
            return false;
        }else if($("input[name='module_page[]']").val() == '')
        {
            if($("#module_name").val()!='')
            {
                $("#module_name").removeClass('error');
                $("#msg_module_name").hide();
            }

            $("#module_page").addClass('error');
            $("#module_page").focus();
            $("#msg_module_page").html('Please atleast one page name').show();
            return false;
            
        }
        else
        {
          if($("input[name='module_page[]']").val() !== '')
          {
            $("#module_page").removeClass('error');
            $("#module_page").hide();
          }

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

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

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

                          $.ajax({      
                                    url: "moduleadd.php",
                                    data: { act:"load",
                                          module_name:module_name,module_page:module_page,module_pageid:module_pageid,module_id:<?php echo $_REQUEST['id'];?> },
                                    method: 'POST',
                                    success: function(msg){
                                        if(msg!=''){
                                            window.location='modulelist.php';
                                        }
                                    }
                                });
                        }
                      }
                  });
         <?php }
          ?>
      }

  });
</script>