{{ header }}{{ column_left }}
<div id="content">
  <div class="page-header">
    <div class="container-fluid">
      <div class="pull-right">
        <button type="submit" form="form-etsy-settings" data-toggle="tooltip" title="{{ button_save }}" class="btn btn-primary" onclick="validateForm(); return false;"><i class="fa fa-check-circle"></i></button>
        <a href="{{ cancel }}" data-toggle="tooltip" title="{{ button_cancel }}" class="btn btn-default"><i class="fa fa-reply"></i></a>
      </div>
      <h1>{{ heading_title }}</h1>
      <ul class="breadcrumb">
        {% for breadcrumb in breadcrumbs %}
        <li><a href="{{ breadcrumb.href }}">{{ breadcrumb.text }}</a></li>
        {% endfor %}
      </ul>
    </div>
  </div>
  <div class="container-fluid">
    {% if error_warning %}
      <div class="alert alert-danger alert-dismissible"><i class="fa fa-exclamation-circle"></i> {{ error_warning }}</div>
    {% endif %}
    {% if account_info is not empty %}
      {% if account_info.header_code == 200 %}
        <div class="alert alert-success alert-dismissible"><i class="fa fa-check"></i> {{ text_account_ok }}</div>
      {% else %}
        <div class="alert alert-danger alert-dismissible"><i class="fa fa-exclamation-circle"></i> {{ error_account_info }} ({{ account_info.header_code }})</div>
      {% endif %}
    {% endif %}
    <div class="panel panel-default">
      <div class="panel-heading">
        <h3 class="panel-title"><i class="fa fa-etsy"></i> {{ text_edit }}</h3>
      </div>
      <div class="panel-body">
        <form action="{{ action }}" method="post" enctype="multipart/form-data" id="form-etsy-settings" class="form-horizontal">
          {% if etsy_token is empty %}
          <a href="{{ link_signup }}" target="_blank"><div class="alert alert-info"><h5 style="font-size: 14px;"><i class="fa fa-user-plus fa-2x"></i>&nbsp;&nbsp;{{ text_register_banner }}</h5></div></a>
          {% endif %}
          <ul class="nav nav-tabs">
            <li class="active"><a href="#tab-general" data-toggle="tab">{{ tab_api_info }}</a></li>
          </ul>
          <div class="tab-content">
            <div class="tab-pane active" id="tab-general">
              <div class="form-group">
                <label class="col-sm-2 control-label" for="etsy_status">{{ text_status }}</label>
                <div class="col-sm-10">
                  <select name="etsy_status" id="etsy_status" class="form-control ftpsetting">
                    {% if etsy_status == 1 %}
                      <option value="1" selected="selected">{{ text_enabled }}</option>
                      <option value="0">{{ text_disabled }}</option>
                    {% else %}
                      <option value="1">{{ text_enabled }}</option>
                      <option value="0" selected="selected">{{ text_disabled }}</option>
                    {% endif %}
                  </select>
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label" for="etsy_token">{{ entry_token }}</label>
                <div class="col-sm-10">
                  <input type="text" name="etsy_token" value="{{ etsy_token }}" placeholder="{{ entry_token }}" id="etsy_token" class="form-control credentials" />
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label" for="etsy_encryption_key">{{ entry_encryption_key }}</label>
                <div class="col-sm-10">
                  <input type="text" name="etsy_encryption_key" value="{{ etsy_encryption_key }}" placeholder="{{ entry_encryption_key }}" id="etsy_encryption_key" class="form-control credentials" />
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label" for="etsy_encryption_iv">{{ entry_encryption_iv }}</label>
                <div class="col-sm-10">
                  <input type="text" name="etsy_encryption_iv" value="{{ etsy_encryption_iv }}" placeholder="{{ entry_encryption_iv }}" id="etsy_encryption_iv" class="form-control credentials" />
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label" for="etsy_logging">{{ entry_debug }}</label>
                <div class="col-sm-10">
                  <select name="etsy_logging" id="etsy_logging" class="form-control">
                    {% if etsy_logging == 1 %}
                      <option value="1" selected="selected">{{ text_yes }}</option>
                      <option value="0">{{ text_no }}</option>
                    {% else %}
                      <option value="1">{{ text_yes }}</option>
                      <option value="0" selected="selected">{{ text_no }}</option>
                    {% endif %}
                  </select>
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label" for="etsy_address_format"><span data-toggle="tooltip" data-container="#tab-general" title="{{ help_address_format }}">{{ entry_address_format }}</span></label>
                <div class="col-sm-10">
                  <textarea name="etsy_address_format" class="form-control" rows="3" id="etsy_address_format">{{ etsy_address_format }}</textarea>
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label" for="etsy_order_status_new">{{ entry_import_def_id }}</label>
                <div class="col-sm-10">
                  <select name="etsy_order_status_new" id="etsy_order_status_new" class="form-control">
                    {% if etsy_order_status_new is empty %}{% set etsy_order_status_new = 1 %}{% endif %}
                    {% for status in order_statuses %}
                      <option value="{{ status.order_status_id }}"{% if etsy_order_status_new == status.order_status_id %} selected=selected{% endif %}>{{ status.name }}</option>
                    {% endfor %}
                  </select>
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label" for="etsy_order_status_paid">{{ entry_import_paid_id }}</label>
                <div class="col-sm-10">
                  <select name="etsy_order_status_paid" id="etsy_order_status_paid" class="form-control">
                    {% if etsy_order_status_paid is empty %}{% set etsy_order_status_paid = 2 %}{% endif %}
                    {% for status in order_statuses %}
                      <option value="{{ status.order_status_id }}"{% if etsy_order_status_paid == status.order_status_id %} selected=selected{% endif %}>{{ status.name }}</option>
                    {% endfor %}
                  </select>
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label" for="etsy_order_status_shipped">{{ entry_import_shipped_id }}</label>
                <div class="col-sm-10">
                  <select name="etsy_order_status_shipped" id="etsy_order_status_shipped" class="form-control">
                    {% if etsy_order_status_shipped is empty %}{% set etsy_order_status_shipped = 3 %}{% endif %}
                    {% for status in order_statuses %}
                      <option value="{{ status.order_status_id }}"{% if etsy_order_status_shipped == status.order_status_id %} selected=selected{% endif %}>{{ status.name }}</option>
                    {% endfor %}
                  </select>
                </div>
              </div>
              {% if account_info.header_code == 200 %}
                <div class="form-group">
                  <label class="col-sm-2 control-label" for="button-import"><span data-toggle="tooltip" data-container="#tab-general" title="{{ help_pull_orders }}">{{ text_pull_orders }}</span></label>
                  <div class="col-sm-10"> <a class="btn btn-primary" id="button-import"><i class="fa fa-refresh"></i></a> </div>
                </div>
                <div class="form-group">
                  <label class="col-sm-2 control-label" for="button-settings"><span data-toggle="tooltip" data-container="#tab-general" title="{{ help_sync_settings }}">{{ text_sync_settings }}</span></label>
                  <div class="col-sm-10"> <a class="btn btn-primary" id="button-settings"><i class="fa fa-refresh"></i></a> </div>
                </div>
              {% endif %}
            </div>
          </div>
        </form>
      </div>
    </div>
  </div>
</div>
<script type="text/javascript"><!--
  function validateForm() {
      $('#form-etsy-settings').submit();
  }

  $('#button-import').bind('click', function() {
    $.ajax({
      url: 'index.php?route=extension/openbay/etsy/getorders&user_token={{ user_token }}',
      beforeSend: function(){
        $('#button-import').removeClass('btn-success').removeClass('btn-danger').addClass('btn-primary').empty().html('<i class="fa fa-cog fa-lg fa-spin"></i>').attr('disabled','disabled');
      },
      type: 'post',
      dataType: 'json',
      success: function(json) {
        if (json.header_code == 200) {
          $('#button-import').empty().removeClass('btn-primary').addClass('btn-success').html('{{ text_complete }}');
          alert('{{ text_orders_imported }}');
        } else {
          $('#button-import').empty().removeClass('btn-primary').addClass('btn-danger').html('{{ text_failed }}').removeAttr('disabled');
          alert(json.data.error + '(' + json.data.code + ')');
        }
      },
      error: function (xhr, ajaxOptions, thrownError) {
        $('#button-import').empty().removeClass('btn-primary').addClass('btn-danger').html('{{ text_failed }}').removeAttr('disabled');
        if (xhr.status != 0) { alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText); }
      }
    });
  });

  $('#button-settings').bind('click', function() {
    $.ajax({
      url: 'index.php?route=extension/openbay/etsy/settingsupdate&user_token={{ user_token }}',
      beforeSend: function(){
        $('#button-settings').removeClass('btn-success').removeClass('btn-danger').addClass('btn-primary').empty().html('<i class="fa fa-cog fa-lg fa-spin"></i>').attr('disabled','disabled');
      },
      type: 'get',
      dataType: 'json',
      success: function(json) {
        if (json.header_code == 200) {
          $('#button-settings').empty().removeClass('btn-primary').addClass('btn-success').html('{{ text_complete }}');
        } else {
          $('#button-settings').empty().removeClass('btn-primary').addClass('btn-danger').html('{{ text_failed }}').removeAttr('disabled');
        }
      },
      error: function (xhr, ajaxOptions, thrownError) {
        $('#button-settings').empty().removeClass('btn-primary').addClass('btn-danger').html('{{ text_failed }}').removeAttr('disabled');
        if (xhr.status != 0) { alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText); }
      }
    });
  });
//--></script>
{{ footer }}
