{{ header }}{{ column_left }}
<div id="content">
  <div class="page-header">
    <div class="container-fluid">
      <div class="float-end">
        <button type="submit" form="form-translation" data-bs-toggle="tooltip" title="{{ button_save }}" class="btn btn-primary"><i class="fa-solid fa-floppy-disk"></i></button>
        <a href="{{ back }}" data-bs-toggle="tooltip" title="{{ button_back }}" class="btn btn-light"><i class="fa-solid fa-reply"></i></a>
      </div>
      <h1>{{ heading_title }}</h1>
      <ol class="breadcrumb">
        {% for breadcrumb in breadcrumbs %}
          <li class="breadcrumb-item"><a href="{{ breadcrumb.href }}">{{ breadcrumb.text }}</a></li>
        {% endfor %}
      </ol>
    </div>
  </div>
  <div class="container-fluid">
    <div class="card">
      <div class="card-header"><i class="fa-solid fa-pencil"></i> {{ text_form }}</div>
      <div class="card-body">
        <form id="form-translation" action="{{ save }}" method="post" data-oc-toggle="ajax">
          <div class="row mb-3">
            <label for="input-store" class="col-sm-2 col-form-label">{{ entry_store }}</label>
            <div class="col-sm-10">
              <select name="store_id" id="input-store" class="form-select">
                <option value="0">{{ text_default }}</option>
                {% for store in stores %}
                  <option value="{{ store.store_id }}"{% if store.store_id == store_id %} selected{% endif %}>{{ store.name }}</option>
                {% endfor %}
              </select>
            </div>
          </div>
          <div class="row mb-3">
            <label for="input-language" class="col-sm-2 col-form-label">{{ entry_language }}</label>
            <div class="col-sm-10">
              <select name="language_id" id="input-language" class="form-select">
                {% for language in languages %}
                  <option value="{{ language.language_id }}"{% if language.language_id == language_id %} selected{% endif %}>{{ language.name }}</option>
                {% endfor %}
              </select>
            </div>
          </div>
          <div class="row mb-3">
            <label for="input-route" class="col-sm-2 col-form-label">{{ entry_route }}</label>
            <div class="col-sm-10">
              <select name="route" id="input-route" class="form-select"></select>
            </div>
          </div>
          <div class="row mb-3">
            <label for="input-key" class="col-sm-2 col-form-label">{{ entry_key }}</label>
            <div class="col-sm-10">
              <select id="input-keys" class="form-select"></select>
              <input type="text" name="key" value="{{ key }}" placeholder="{{ entry_key }}" id="input-key" class="form-control"/>
              <div id="error-key" class="invalid-feedback"></div>
            </div>
          </div>
          <div class="row mb-3">
            <label for="input-default" class="col-sm-2 col-form-label">{{ entry_default }}</label>
            <div class="col-sm-10">
              <textarea name="default" rows="5" placeholder="{{ entry_default }}" id="input-default" class="form-control" disabled="disabled">{% if default %}{{ default }}{% endif %}</textarea>
            </div>
          </div>
          <div class="row mb-3">
            <label for="input-value" class="col-sm-2 col-form-label">{{ entry_value }}</label>
            <div class="col-sm-10">
              <textarea name="value" rows="5" placeholder="{{ entry_value }}" id="input-value" class="form-control">{{ value }}</textarea>
            </div>
          </div>
          <input type="hidden" name="translation_id" value="{{ translation_id }}" id="input-translation-id"/>
        </form>
      </div>
    </div>
  </div>
</div>
<script type="text/javascript"><!--
$('#input-language').on('change', function() {
    $.ajax({
        url: 'index.php?route=design/translation.path&user_token={{ user_token }}&language_id=' + $('#input-language').val(),
        dataType: 'json',
        beforeSend: function() {
            $('#input-language').prop('disabled', true);
            $('#input-route').prop('disabled', true);
            $('#input-key').prop('disabled', true);
        },
        complete: function() {
            $('#input-language').prop('disabled', false);
            $('#input-route').prop('disabled', false);
            $('#input-key').prop('disabled', false);
        },
        success: function(json) {
            html = '';

            if (json) {
                for (i = 0; i < json.length; i++) {
                    if (json[i] == '{{ route }}') {
                        html += '<option value="' + json[i] + '" selected>' + json[i] + '</option>';
                    } else {
                        html += '<option value="' + json[i] + '">' + json[i] + '</option>';
                    }
                }
            }

            $('#input-route').html(html);

            $('#input-route').trigger('change');
        },
        error: function(xhr, ajaxOptions, thrownError) {
            console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });
});

$('#input-route').on('change', function() {
    $.ajax({
        url: 'index.php?route=design/translation.translation&user_token={{ user_token }}&language_id=' + $('#input-language').val() + '&path=' + $('#input-route').val(),
        dataType: 'json',
        beforeSend: function() {
            $('#input-language').prop('disabled', true);
            $('#input-route').prop('disabled', true);
            $('#input-key').prop('disabled', true);
        },
        complete: function() {
            $('#input-language').prop('disabled', false);
            $('#input-route').prop('disabled', false);
            $('#input-key').prop('disabled', false);
        },
        success: function(json) {
            translation = [];

            html = '<option value=""></option>';

            if (json) {
                for (i = 0; i < json.length; i++) {
                    if (json[i]['key'] == $('#input-key').val()) {
                        html += '<option value="' + json[i]['key'] + '" selected>' + json[i]['key'] + '</option>';
                    } else {
                        html += '<option value="' + json[i]['key'] + '">' + json[i]['key'] + '</option>';
                    }

                    translation[json[i]['key']] = json[i]['value'];
                }
            }

            $('#input-keys').html(html);

            $('#input-keys').trigger('change');
        },
        error: function(xhr, ajaxOptions, thrownError) {
            console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });
});

$('#input-keys').on('change', function() {
    if (translation[$('#input-keys').val()]) {
        $('#input-default').val(translation[$('#input-keys').val()]);

        $('#input-key').val($('#input-keys').val());
    } else {
        $('#input-default').val('');
    }
});

$('#input-language').trigger('change');
//--></script>
{{ footer }}