<div class="modal-header">
  <button type="button" class="pull-right btn btn-danger btn-sm" data-dismiss="modal" aria-hidden="true"><i class="fa fa-close"></i></button>
  <h4 class="modal-title">{{ tab_attribute }}</h4>
</div>
<div class="modal-body" id="body-mass-product-attributes">
  <form action="{{ action }}" method="post" enctype="multipart/form-data" id="form-mass-product-attributes" class="form-horizontal">	
	<div class="table-responsive">
	  <table id="attribute" class="table table-striped table-bordered table-hover">
		<thead>
		  <tr>
			<td class="text-left" width="30%">{{ entry_attribute }}</td>
			<td class="text-left">{{ entry_text }}</td>
			<td width="1%"></td>
		  </tr>
		</thead>
		<tbody>
		  {% set attribute_row = 0 %}
		  <tr id="attribute-row{{ attribute_row }}"></tr>
		  {% set attribute_row = attribute_row + 1 %}
		</tbody>
		<tfoot>
		  <tr>
			<td colspan="2"></td>
			<td class="text-right"><button type="button" onclick="addAttribute();" class="btn btn-primary"><i class="fa fa-plus-circle"></i></button></td>
		  </tr>
		</tfoot>
	  </table>
	</div>
	<div class="form-group">	
	  <label class="col-sm-2 control-label" for="input-attribute-method">{{ entry_method_edit }}</label>
	  <div class="col-sm-10">
		<select name="attribute_method_edit" id="input-attribute-method" class="form-control">
		  <option value="all_new">{{ text_all_new }}</option>
		  <option value="add_selected">{{ text_add_selected }}</option>
		  <option value="delete_selected">{{ text_delete_selected }}</option>
		</select>
	  </div>
	</div>
  </form>
</div>
<div class="modal-footer">
  <div class="text-center"><a data-form="form-mass-product-attributes" class="button-save btn btn-primary"><i class="fa fa-save"></i> {{ button_save }}</a></div>
</div>
<script type="text/javascript"><!--
$(".button-save").on('click', function(){		
	$.ajax({
		url: $('#' + $(this).data('form')).attr('action'),
		type: 'post',
		dataType: 'json',
		data: $('#product-list input[type=\'checkbox\']:checked, #' + $(this).data('form') + ' input[type=\'text\'], #' + $(this).data('form') + ' input[type=\'hidden\'], #' + $(this).data('form') + ' textarea, #' + $(this).data('form') + ' select'),
		success: function(json) {				
			if (json['error']) {
				$('#form-mass-product-attributes').before('<div class="alert alert-danger"><i class="fa fa-exclamation-circle"></i> ' + json['error'] + '</div>');
				
				$('.alert-danger').delay(1500).fadeOut(500);
			}
								
			if (json['success']) {
				$('#form-mass-product-attributes').before('<div class="alert alert-success"><i class="fa fa-check-circle"></i> ' + json['success'] + '</div>');	
				
				setTimeout(function(){
					$('#modal-product-edit').modal('hide');
				}, 1000)
			}
		}
	});
});  
//--></script>
<script type="text/javascript"><!--
var attribute_row = {{ attribute_row }};

function addAttribute() {
    html  = '<tr id="attribute-row' + attribute_row + '">';
	html += '  <td class="text-left"><input type="text" name="product_attribute[' + attribute_row + '][name]" value="" placeholder="{{ entry_attribute }}" class="form-control" /><input type="hidden" name="product_attribute[' + attribute_row + '][attribute_id]" value="" /></td>';
	html += '  <td class="text-left">';
	{% for language in languages %}
	html += '<div class="input-group"><span class="input-group-addon"><img src="language/{{ language.code }}/{{ language.code }}.png" title="{{ language.name }}" /></span><textarea name="product_attribute[' + attribute_row + '][product_attribute_description][{{ language.language_id }}][text]" rows="2" placeholder="{{ entry_text }}" class="form-control"></textarea></div>';
    {% endfor %}
	html += '  </td>';
	html += '  <td class="text-right"><button type="button" onclick="$(\'#attribute-row' + attribute_row + '\').remove();" class="btn btn-danger"><i class="fa fa-minus-circle"></i></button></td>';
    html += '</tr>';
	
	$('#attribute tbody').append(html);
	
	attributeautocomplete(attribute_row);
	
	attribute_row++;
}

function attributeautocomplete(attribute_row) {
	$('input[name=\'product_attribute[' + attribute_row + '][name]\']').autocomplete({
		'source': function(request, response) {
			$.ajax({
				url: 'index.php?route=editors/attribute/autocomplete&user_token={{ user_token }}&filter_name=' +  encodeURIComponent(request),
				dataType: 'json',			
				success: function(json) {
					response($.map(json, function(item) {
						return {
							category: item.attribute_group,
							label: item.name,
							value: item.attribute_id
						}
					}));
				}
			});
		},
		'select': function(item) {
			$('input[name=\'product_attribute[' + attribute_row + '][name]\']').val(item['label']);
			$('input[name=\'product_attribute[' + attribute_row + '][attribute_id]\']').val(item['value']);
		}
	});
}

$('#attribute tbody tr').each(function(index, element) {
	attributeautocomplete(index);
});
//--></script>