
$(function() {


/*Append New Input fields */
var index_main = 1;
if(index_main == 1)
{
$('.select-container_main .removeField').hide();
index_main = 1;
console.log(index_main);
}
else if (index_main >= 2 || index_main <= 15)
{
$('.select-container_main:first-child .removeField').show();
$('.select-container_main .removeField').show();
$('.select-container_main:last-child .removeField').hide();
}
$(document).on("focusout",".inc_main",function() {
    //var modifierData = null
    var val = $(this).val();
	if(val.length != 0 && index_main <= 15)
    {
		$(this).removeClass("inc_main");
		$(this).removeClass("append_field");

        var newRow = $("<div class='row select-container_main mt-3'><div class='col-md-6 col-xl-4'> <label class='d-block d-xl-none'>NDC Number <span class='text-red fs_12'>*</span></label> <div class='form_label mb-3 input-parent input-parent-cpt'>  <input type='text' class='form-control inc_main cptcode text-uppercase append_field  form-input' name='singleReport["+index_main+"][cptcode]' placeholder='NDC Number' value='' maxLength='13'/> <div class='invalid-feedback'>Fill NDC Number</div> </div> </div> <div class='col-md-6 col-xl-4'> <label class='d-block d-xl-none'>Units <span class='text-red fs_12'>*</span></label> <div class='form_label mb-3 input-parent input-parent-unit'>  <input type='text' class='form-control units  form-input' name='singleReport["+index_main+"][units]' placeholder='0' value='1' maxLength='3' required/> <div class='invalid-feedback'>Fill unit</div> </div> </div><div class='col-md-6 col-xl-4'> <label class='d-block d-xl-none'>Charges <span class='text-red fs_12'></span></label> <div class='form_label mb-3 input-parent input-parent-charges'>  <div class='last_field'> <input type='text' class='form-control charges  form-input' name='singleReport["+index_main+"][payment_total]' placeholder='0.00'  value='0' maxLength='5' /> <div class='removeField'><a href='javascript:void(0);' class='remove_button'><i class='fa fa-remove' aria-hidden='true'></i></a></div> </div> </div> </div></div>");
        $(".row.select-container_main").addClass("border-bottom");
        // backup
        // var newRow = $("<div class='row select-container_main mt-3'><div class='col-6 col-sm-3 col-md-3'> <div class='form_label mb-3 input-parent input-parent-cpt'> <label class='d-block d-sm-none'>Procedure Code <span class='text-red fs_12'>*</span></label> <input type='text' class='form-control inc_main cptcode text-uppercase append_field  form-input' name='singleReport["+index_main+"][cptcode]' placeholder='CPT code' value='' pattern='[a-zA-Z0-9]{1,5}'' maxLength='5'/> <div class='invalid-feedback'>Select procedure code</div> </div> </div><div class='col-6 col-sm-3 col-md-3'> <div class='form_label mb-3 input-parent input-parent-cpt'> <label class='d-block d-sm-none'>Modifiers <span class='text-red fs_12'></span></label> <div class='mb-3 mod_selector_main select2-simple_select_div select2-modifiers'></div> </div> </div><div class='col-6 col-sm-3 col-md-3'> <div class='form_label mb-3 input-parent input-parent-unit'> <label class='d-block d-sm-none'>Units <span class='text-red fs_12'>*</span></label> <input type='text' class='form-control units  form-input' name='singleReport["+index_main+"][units]' placeholder='0' value='1' maxLength='3' required/> <div class='invalid-feedback'>Select unit</div> </div> </div><div class='col-6 col-sm-3 col-md-3'> <div class='form_label mb-3 input-parent input-parent-charges'> <label class='d-block d-sm-none'>Charges <span class='text-red fs_12'></span></label> <div class='last_field'> <input type='text' class='form-control charges  form-input' name='singleReport["+index_main+"][payment_total]' placeholder='0.00'  value='0' maxLength='5' /> <div class='removeField'><a href='javascript:void(0);' class='remove_button'><i class='fa fa-remove' aria-hidden='true'></i></a></div> </div> </div> </div></div>");

        $(newRow).appendTo(".field_wrapper_main");
		index_main++;
        $('.cptcode').mask('#####-####-##');
		// $('.charges').mask("###.##", {reverse: false});
		$('.units').mask('000', {reverse: true});
		$('.select-container_main .removeField').show();
		$('.select-container_main:last-child .removeField').hide();


    }
});

//Once remove button is clicked
$('.field_wrapper_main').on('click', '.remove_button', function(e){
    e.preventDefault();
    $(this).parent().parent().parent().parent().parent().remove(); //Remove row
    index_main--; //Decrement field counter

   $(".field_wrapper_main .select-container_main:last-child .append_field").addClass("inc_main");
console.log(index_main);
 if(index_main <= 1)
{
$(".cptcode").addClass("inc_main");
$(".cptcode.append_field").attr('required', true);
}
});

/*End Append New Input fields*/



});
