<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">{{ text_edit_value }} "{{ option_name }}"</h4>
</div>
<div class="modal-body">
  <form action="{{ action }}" method="post" enctype="multipart/form-data" id="form-option-value" class="form-horizontal">
	<fieldset>
      <legend>{{ text_option }}</legend>
	  <div class="form-group">
        <label class="col-sm-2 control-label" for="input-type">{{ entry_type }}</label>
        <div class="col-sm-10">
          <select name="type" id="input-type" class="form-control">
            <optgroup label="{{ text_choose }}">
              {% if type == 'select' %}
                <option value="select" selected="selected">{{ text_select }}</option>
                {% else %}
                <option value="select">{{ text_select }}</option>
              {% endif %}
              {% if type == 'radio' %}
                <option value="radio" selected="selected">{{ text_radio }}</option>
                {% else %}
                <option value="radio">{{ text_radio }}</option>
              {% endif %}
              {% if type == 'checkbox' %}
                <option value="checkbox" selected="selected">{{ text_checkbox }}</option>
                {% else %}
                <option value="checkbox">{{ text_checkbox }}</option>
              {% endif %}
            </optgroup>
            <optgroup label="{{ text_input }}">
              {% if type == 'text' %}
                <option value="text" selected="selected">{{ text_text }}</option>
                {% else %}
                <option value="text">{{ text_text }}</option>
              {% endif %}
              {% if type == 'textarea' %}
                <option value="textarea" selected="selected">{{ text_textarea }}</option>
                {% else %}
                <option value="textarea">{{ text_textarea }}</option>
              {% endif %}
            </optgroup>
            <optgroup label="{{ text_file }}">
              {% if type == 'file' %}
                <option value="file" selected="selected">{{ text_file }}</option>
                {% else %}
                <option value="file">{{ text_file }}</option>
              {% endif %}
            </optgroup>
            <optgroup label="{{ text_date }}">
              {% if type == 'date' %}
                <option value="date" selected="selected">{{ text_date }}</option>
                {% else %}
                <option value="date">{{ text_date }}</option>
              {% endif %}
              {% if type == 'time' %}
                <option value="time" selected="selected">{{ text_time }}</option>
                {% else %}
                <option value="time">{{ text_time }}</option>
              {% endif %}
              {% if type == 'datetime' %}
                 <option value="datetime" selected="selected">{{ text_datetime }}</option>
                {% else %}
                <option value="datetime">{{ text_datetime }}</option>
              {% endif %}
            </optgroup>
          </select>
        </div>
      </div>
    </fieldset>
    <fieldset>
      <legend>{{ text_value }}</legend>
      <table id="option-value" class="table table-striped table-bordered table-hover">
        <thead>
          <tr>
            <td class="text-left required">{{ entry_option_value }}</td>
            <td class="text-center">{{ entry_image }}</td>
            <td class="text-right">{{ entry_sort_order }}</td>
            <td></td>
          </tr>
        </thead>
        <tbody>
          {% set option_value_row = 0 %}
          {% for option_value in option_values %}
            <tr id="option-value-row{{ option_value_row }}">
              <td class="text-center"><input type="hidden" name="option_value[{{ option_value_row }}][option_value_id]" value="{{ option_value.option_value_id }}" />
                {% for language in languages %}
                  <div class="input-group"><span class="input-group-addon"><img src="language/{{ language.code }}/{{ language.code }}.png" title="{{ language.name }}" /></span>
                    <input type="text" name="option_value[{{ option_value_row }}][option_value_description][{{ language.language_id }}][name]" value="{{ option_value.option_value_description[language.language_id] ? option_value.option_value_description[language.language_id].name }}" placeholder="{{ entry_option_value }}" class="form-control" />
                  </div>
                  {% if error_option_value[option_value_row][language.language_id] %}
                  <div class="text-danger">{{ error_option_value[option_value_row][language.language_id] }}</div>
                  {% endif %}
                {% endfor %}
			  </td>
              <td class="text-center"><a href="" id="thumb-image{{ option_value_row }}" data-toggle="image" class="img-thumbnail"><img src="{{ option_value.thumb }}" alt="" title="" data-placeholder="{{ placeholder }}" /></a>
                <input type="hidden" name="option_value[{{ option_value_row }}][image]" value="{{ option_value.image }}" id="input-image{{ option_value_row }}" /></td>
              <td class="text-right"><input type="text" name="option_value[{{ option_value_row }}][sort_order]" value="{{ option_value.sort_order }}" class="form-control" /></td>
              <td class="text-right"><button type="button" onclick="$('#option-value-row{{ option_value_row }}').remove();" data-toggle="tooltip" title="{{ button_remove }}" class="btn btn-danger"><i class="fa fa-minus-circle"></i></button></td>
            </tr>
            {% set option_value_row = option_value_row + 1 %}
          {% endfor %}
        </tbody>
        <tfoot>
          <tr>
            <td colspan="3"></td>
            <td class="text-right"><button type="button" onclick="addOptionValue();" data-toggle="tooltip" title="{{ button_option_value_add }}" class="btn btn-primary"><i class="fa fa-plus-circle"></i></button></td>
          </tr>
        </tfoot>
      </table>
    </fieldset>
  </form>
</div>
<div class="modal-footer">
  <div class="text-center"><a data-form="form-option-value" 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: $('#' + $(this).data('form') + ' input[type=\'text\'], #' + $(this).data('form') + ' input[type=\'hidden\'], #' + $(this).data('form') + ' select'),
		success: function(json) {
			if (json['error']) {
				$('#form-option-value').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-option-value').before('<div class="alert alert-success"><i class="fa fa-exclamation-circle"></i> ' + json['success'] + '</div>');			
									
				setTimeout(function(){
					$('#modal-option-edit').modal('hide');
				}, 1000)
			}
		}
	});
});   
//--></script>
<script type="text/javascript"><!--
$('select[name=\'type\']').on('change', function() {
	if (this.value == 'select' || this.value == 'radio' || this.value == 'checkbox' || this.value == 'image') {
		$('#option-value').parent().show();
	} else {
		$('#option-value').parent().hide();
	}
});

$('select[name=\'type\']').trigger('change');

var option_value_row = {{ option_value_row }};

function addOptionValue() {
	html  = '<tr id="option-value-row' + option_value_row + '">';
    html += '  <td class="text-left"><input type="hidden" name="option_value[' + option_value_row + '][option_value_id]" value="" />';
	{% for language in languages %}
	html += '    <div class="input-group">';
	html += '      <span class="input-group-addon"><img src="language/{{ language.code }}/{{ language.code }}.png" title="{{ language.name }}" /></span><input type="text" name="option_value[' + option_value_row + '][option_value_description][{{ language.language_id }}][name]" value="" placeholder="{{ entry_option_value }}" class="form-control" />';
    html += '    </div>';
	{% endfor %}
	html += '  </td>';
    html += '  <td class="text-center"><a href="" id="thumb-image' + option_value_row + '" data-toggle="image" class="img-thumbnail"><img src="{{ placeholder }}" alt="" title="" data-placeholder="{{ placeholder }}" /></a><input type="hidden" name="option_value[' + option_value_row + '][image]" value="" id="input-image' + option_value_row + '" /></td>';
	html += '  <td class="text-right"><input type="text" name="option_value[' + option_value_row + '][sort_order]" value="" placeholder="{{ entry_sort_order }}" class="form-control" /></td>';
	html += '  <td class="text-right"><button type="button" onclick="$(\'#option-value-row' + option_value_row + '\').remove();" data-toggle="tooltip" title="{{ button_remove }}" class="btn btn-danger"><i class="fa fa-minus-circle"></i></button></td>';
	html += '</tr>';

	$('#option-value tbody').append(html);

	option_value_row++;
}
//--></script>