{{ header }}{{ column_left }}
<!-- Content Wrapper. Contains page content -->
<div id="content" class="content-wrapper">
  <!-- Content Header (Page header) -->
  <section class="content-header">
    <div>
      <div class="pull-right">
        <a href="{{ cancel }}" data-toggle="tooltip" title="{{ button_cancel }}" class="btn btn-default btn-sm"><i class="fa fa-reply"></i> {{ button_cancel }}</a>
      </div>
      <h1>
        {{ heading_title }}
      </h1>
    </div>
    <div class="row">
      <div class="col-sm-12">
        <ul class="breadcrumb">
          {% for breadcrumb in breadcrumbs %}
          <li><a href="{{ breadcrumb.href }}">{{ breadcrumb.text }}</a></li>
          {% endfor %}
        </ul>
      </div>
    </div>
  </section>

  <!-- Alert -->
  <section class="content-header">
    {% for warning in error_warning %}
      <div class="alert alert-danger"><i class="fa fa-exclamation-circle"></i> {{ warning }}</div>
    {% endfor %}
  </section>
  <!-- /.alert -->

  <!-- Main content -->
  <section class="content">

    <!-- Default box -->
    <div class="box">
      <div class="box-header with-border">
        <h3 class="box-title">{{ text_bulk }}</h3>

        <div class="box-tools pull-right">
          <button type="button" class="btn btn-box-tool" data-widget="collapse" data-toggle="tooltip" title="{{ text_collapse }}">
            <i class="fa fa-minus"></i></button>
        </div>
      </div>
      <div class="box-body" id="page-listing">
        {% set i = 0 %}
        {% if error_fail is empty %}
        <form action="{{ action }}" method="post" enctype="multipart/form-data" id="form" class="form-horizontal">
          <div class="well">
            <div class="row">
              <div class="col-sm-12 text-right">
                <a class="btn btn-primary" id="button-verify"><i class="fa fa-check"></i> {{ text_preview_all }}</a>
                <a class="btn btn-primary" id="button-edit" style="display:none;"><i class="fa fa-pencil"></i> {{ button_edit }}</a>
                <a class="btn btn-primary" id="button-submit" style="display:none;"><i class="fa fa-plus-circle"></i> {{ button_submit }}</a>
              </div>
            </div>
          </div>
          {% if products %}
            {% for product in products %}
              {% set price_original = product.price * ((default.defaults.tax / 100) + 1) %}

              <div class="well listingBox" id="p_row_{{ i }}">
                <input type="hidden" class="product_id openbay_data_{{ i }}" name="product_id" value="{{ i }}" />
                <input type="hidden" class="openbay_data_{{ i }}" name="product_id" value="{{ product.product_id }}" id="product-id-{{ i }}" />
                <input type="hidden" name="price_original" id="price_original_{{ i }}" value="{{ price_original|number_format(2, '.', '') }}" />
                <input type="hidden" class="openbay_data_{{ i }}" name="catalog_epid" id="catalog_epid_{{ i }}" value="0" />
                <div class="row">
                  <div class="col-sm-7">
                    <h4 id="product_title_{{ i }}" style="display:none;"></h4>
                  </div>
                  <div class="col-sm-5 form-group text-right" id="p_row_buttons_{{ i }}">
                    <a class="btn btn-primary" onclick="showCategory('{{ i }}');" id="editCategory_{{ i }}" ><i class="fa fa-pencil"></i> {{ text_category }}</a>
                    <a class="btn btn-primary" onclick="showProfiles('{{ i }}');" id="editProfiles_{{ i }}" ><i class="fa fa-pencil"></i> {{ text_profile }}</a>
                    <a class="btn btn-primary" style="display:none;" onclick="showCatalog('{{ i }}');" id="editCatalog_{{ i }}" ><i class="fa fa-pencil"></i> {{ text_catalog }}</a>
                    <a class="btn btn-primary" style="display:none;" onclick="showFeatures('{{ i }}');" id="editFeature_{{ i }}"><i class="fa fa-pencil"></i> {{ text_features }}</a>
                    <a class="btn btn-danger" onclick="removeBox('{{ i }}')"> <i class="fa fa-minus-circle"></i> {{ button_remove }}</a>
                  </div>
                </div>
                <div class="row">
                  <div class="col-sm-12">
                    <div class="row" id="product_messages_{{ i }}" style="display:none;"></div>
                    <div class="row product_content_{{ i }}">
                      <div class="col-sm-2">
                        <div class="row">
                          <div class="col-sm-12 form-group text-center">
                            <img class="img-thumbnail" src="{{ product.image }}" alt="{{ product.name }}" />
                          </div>
                        </div>
                        <div class="row">
                          <div class="col-sm-12 form-group">
                            <h4 class="text-center"><span class="label label-success">{{ text_stock }}: {{ product.quantity }}</span></h4>
                            <input type="hidden" name="qty" value="{{ product.quantity }}" class="openbay_data_{{ i }}" />
                          </div>
                        </div>
                        <div class="modal fade" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true" id="overlay-feature-{{ i }}" data-backdrop="static" data-keyboard="false">
                          <div class="modal-dialog modal-lg">
                            <div class="modal-content">
                              <div class="modal-body">
                                <div class="page-header">
                                  <div class="container-fluid">
                                    <div class="pull-right">
                                      <a onclick="overlayHide();" class="btn btn-default" data-toggle="tooltip" title="{{ text_close }}"><i class="fa fa-reply"></i></a>
                                    </div>
                                    <h1 class="panel-title">{{ text_features }}</h1>
                                  </div>
                                </div>
                                <div class="container-fluid" style="display: none;" id="product_identifier_container_{{ i }}">
                                  <div class="panel panel-default">
                                    <div class="panel-body">
                                      <div class="well">
                                        <div class="row">
                                          <div class="form-group" id="product_identifier_ean_container_{{ i }}" style="display:none;">
                                            <label class="col-sm-2 control-label">{{ text_ean }}</label>
                                            <div class="col-sm-10">
                                              <input type="hidden" id="identifier_ean_required_{{ i }}" class="product_identifier_required_{{ i }}" value="0" />
                                              <input type="hidden" id="identifier_ean_original_{{ i }}" value="{{ product.ean }}" />
                                              <input type="text" name="identifier_ean" value="{{ product.ean }}" id="identifier_ean_{{ i }}" class="form-control openbay_data_{{ i }}" />
                                            </div>
                                          </div>
                                          <div class="form-group" id="product_identifier_isbn_container_{{ i }}" style="display:none;">
                                            <label class="col-sm-2 control-label">{{ text_isbn }}</label>
                                            <div class="col-sm-10">
                                              <input type="hidden" id="identifier_isbn_required_{{ i }}" class="product_identifier_required_{{ i }}" value="0" />
                                              <input type="hidden" id="identifier_isbn_original_{{ i }}" value="{{ product.isbn }}" />
                                              <input type="text" name="identifier_isbn" value="{{ product.isbn }}" id="identifier_isbn_{{ i }}" class="form-control openbay_data_{{ i }}" />
                                            </div>
                                          </div>
                                          <div class="form-group" id="product_identifier_upc_container_{{ i }}" style="display:none;">
                                            <label class="col-sm-2 control-label">{{ text_upc }}</label>
                                            <div class="col-sm-10">
                                              <input type="hidden" id="identifier_upc_required_{{ i }}" class="product_identifier_required" value="0" />
                                              <input type="hidden" id="identifier_upc_original_{{ i }}" value="{{ product.upc }}" />
                                              <input type="text" name="identifier_upc" value="{{ product.upc }}" id="identifier_upc_{{ i }}" class="form-control openbay_data_{{ i }}" />
                                            </div>
                                          </div>
                                          <div class="form-group">
                                            <label class="col-sm-2 control-label">{{ text_identifier_not_required }}</label>
                                            <div class="col-sm-10">
                                              <input type="checkbox" name="identifier_not_required" value="1" id="identifier_not_required_{{ i }}" class="form-control" onclick="identifierNotRequired({{ i }});"/>
                                            </div>
                                          </div>
                                        </div>
                                      </div>
                                    </div>
                                  </div>
                                </div>
                                <div id="feature-data-{{ i }}"></div>
                              </div>
                            </div>
                          </div>
                        </div>
                        <div class="modal fade" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true" id="overlay-category-{{ i }}" data-backdrop="static" data-keyboard="false">
                          <div class="modal-dialog modal-lg">
                            <div class="modal-content">
                              <div class="modal-body">
                                <div class="page-header">
                                  <div class="container-fluid">
                                    <div class="pull-right">
                                      <a onclick="overlayHide();" class="btn btn-default" data-toggle="tooltip" title="{{ text_close }}"><i class="fa fa-reply"></i></a>
                                    </div>
                                    <h1 class="panel-title">{{ text_category }}</h1>
                                  </div>
                                </div>
                                <div class="container-fluid">
                                  <div class="panel panel-default">
                                    <div class="panel-body">
                                    <div class="well">
                                      <div class="row">
                                        <div class="form-group">
                                          <label class="col-sm-2 control-label">{{ text_suggested }}</label>
                                          <div class="col-sm-10">
                                            <div class="alert alert-info" id="loadingSuggestedCat_{{ i }}"><i class="fa fa-cog fa-lg fa-spin"></i> {{ text_category }}</div>
                                            <div id="suggestedCat_{{ i }}"></div>
                                            <input type="hidden" name="finalCat" id="finalCat_{{ i }}" class="openbay_data_{{ i }}" />
                                          </div>
                                        </div>
                                      </div>
                                      <div class="row">
                                        <div class="form-group" id="cSelections_{{ i }}" style="display:none;">
                                          <label class="col-sm-2 control-label">{{ text_category_choose }}</label>
                                          <div class="col-sm-10">
                                          <div class="alert alert-info" id="imageLoading_{{ i }}"><i class="fa fa-cog fa-lg fa-spin"></i> {{ text_loading_categories }}</div>
                                          <div class="row form-group">
                                            <div class="col-sm-12">
                                              <select id="catsSelect1_{{ i }}" class="form-control" onchange="loadCategories(2, false, {{ i }});"></select>
                                            </div>
                                          </div>
                                          <div class="row form-group">
                                            <div class="col-sm-12">
                                              <select id="catsSelect2_{{ i }}" class="form-control" onchange="loadCategories(3, false, {{ i }});" style="display:none;"></select>
                                            </div>
                                          </div>
                                          <div class="row form-group">
                                            <div class="col-sm-12">
                                              <select id="catsSelect3_{{ i }}" class="form-control" onchange="loadCategories(4, false, {{ i }});" style="display:none;"></select>
                                            </div>
                                          </div>
                                          <div class="row form-group">
                                            <div class="col-sm-12">
                                              <select id="catsSelect4_{{ i }}" class="form-control" onchange="loadCategories(5, false, {{ i }}, false, {{ i }});" style="display:none;"></select>
                                            </div>
                                          </div>
                                          <div class="row form-group">
                                            <div class="col-sm-12">
                                              <select id="catsSelect5_{{ i }}" class="form-control" onchange="loadCategories(6, false, {{ i }});" style="display:none;"></select>
                                            </div>
                                          </div>
                                          <div class="row form-group">
                                            <div class="col-sm-12">
                                              <select id="catsSelect6_{{ i }}" class="form-control" onchange="loadCategories(7, false, {{ i }});" style="display:none;"></select>
                                            </div>
                                          </div>
                                        </div>
                                        </div>
                                      </div>
                                    </div>
                                  </div>
                                  </div>
                                </div>
                              </div>
                            </div>
                          </div>
                        </div>
                        <div class="modal fade" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true" id="overlay-catalog-{{ i }}" data-backdrop="static" data-keyboard="false">
                          <div class="modal-dialog modal-lg">
                            <div class="modal-content">
                              <div class="modal-body">
                                <div class="page-header">
                                  <div class="container-fluid">
                                    <div class="pull-right">
                                      <a onclick="overlayHide();" class="btn btn-default" data-toggle="tooltip" title="{{ text_close }}"><i class="fa fa-reply"></i></a>
                                    </div>
                                    <h1 class="panel-title">{{ text_catalog_search }}</h1>
                                  </div>
                                </div>
                                <div class="container-fluid">
                                  <div class="panel panel-default">
                                    <div class="panel-body">
                                      <div class="well">
                                        <div class="row">
                                          <div class="form-group">
                                            <label class="col-sm-2 control-label">{{ text_search_term }}</label>
                                            <div class="col-sm-10">
                                              <input type="text" name="catalog_search" id="catalog_search_{{ i }}" value="" class="form-control"/>
                                            </div>
                                          </div>
                                        </div>
                                        <div class="row">
                                          <div class="col-sm-12 text-right">
                                            <a onclick="searchEbayCatalog('{{ i }}');" class="btn btn-primary" id="button-catalog-search-{{ i }}">{{ text_search }}</a>
                                          </div>
                                        </div>
                                      </div>
                                      <div id="catalog-results-{{ i }}" style="display:none;"></div>
                                    </div>
                                  </div>
                                </div>
                              </div>
                            </div>
                          </div>
                        </div>
                        <div class="modal fade" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true" id="overlay-profile-{{ i }}" data-backdrop="static" data-keyboard="false">
                          <div class="modal-dialog modal-lg">
                            <div class="modal-content">
                              <div class="modal-body">
                                <div class="page-header">
                                  <div class="container-fluid">
                                    <div class="pull-right">
                                      <a onclick="overlayHide();" class="btn btn-default" data-toggle="tooltip" title="{{ text_close }}"><i class="fa fa-reply"></i></a>
                                    </div>
                                    <h1 class="panel-title">{{ text_profile }}</h1>
                                  </div>
                                </div>
                                <div class="container-fluid">
                                  <div class="panel panel-default">
                                    <div class="panel-body">
                                    <div class="well">
                                      <div class="form-group">
                                        <label class="col-sm-2 control-label">{{ text_profile_theme }}</label>
                                        <div class="col-sm-10">
                                          <select name="theme_profile" class="openbay_data_{{ i }} form-control">
                                            {% for profile in default.profiles_theme %}
                                              <option value="{{ profile.ebay_profile_id }}"{% if default.profiles_theme_def == profile.ebay_profile_id %} selected{% endif %}>{{ profile.name }}</option>
                                            {% endfor %}
                                          </select>
                                        </div>
                                      </div>
                                      <div class="form-group">
                                        <label class="col-sm-2 control-label">{{ text_profile_shipping }}</label>
                                        <div class="col-sm-10">
                                          <select name="shipping_profile" class="openbay_data_{{ i }} form-control">
                                            {% for profile in default.profiles_shipping %}
                                              <option value="{{ profile.ebay_profile_id }}"{% if default.profiles_shipping_def == profile.ebay_profile_id %} selected{% endif %}>{{ profile.name }}</option>
                                            {% endfor %}
                                          </select>
                                        </div>
                                      </div>
                                      <div class="form-group">
                                        <label class="col-sm-2 control-label">{{ text_profile_generic }}</label>
                                        <div class="col-sm-10">
                                          <select name="generic_profile" id="generic_profile_{{ i }}" class="openbay_data_{{ i }} form-control" onchange="genericProfileChange({{ i }});">
                                            {% for profile in default.profiles_generic %}
                                              <option value="{{ profile.ebay_profile_id }}"{% if default.profiles_generic_def == profile.ebay_profile_id %} selected{% endif %}>{{ profile.name }}</option>
                                            {% endfor %}
                                          </select>
                                        </div>
                                      </div>
                                      <div class="form-group">
                                        <label class="col-sm-2 control-label">{{ text_profile_returns }}</label>
                                        <div class="col-sm-10">
                                          <select name="return_profile" class="openbay_data_{{ i }} form-control">
                                            {% for profile in default.profiles_returns %}
                                              <option value="{{ profile.ebay_profile_id }}"{% if default.profiles_returns_def == profile.ebay_profile_id %} selected{% endif %}>{{ profile.name }}</option>
                                            {% endfor %}
                                          </select>
                                        </div>
                                      </div>
                                    </div>
                                  </div>
                                  </div>
                                </div>
                              </div>
                            </div>
                          </div>
                        </div>
                      </div>
                      <div class="col-sm-10">
                        <div class="form-group">
                          <label class="col-sm-2 control-label">{{ text_title }}</label>
                          <div class="col-sm-10">
                            <input type="text" name="title" class="openbay_data_{{ i }} form-control" value="{{ product.name }}" id="title_{{ i }}" />
                          </div>
                        </div>
                        <div class="form-group">
                          <label class="col-sm-2 control-label">{{ text_price }}</label>
                          <div class="col-sm-10">
                            {% set price_new = product.price * ((default.defaults.tax / 100) + 1) %}
                            <input id="price_{{ i }}" type="text" name="price" class="openbay_data_{{ i }} form-control" value="{{ price_new|number_format(2, '.', '') }}" />
                          </div>
                        </div>
                        <div class="alert alert-info" id="conditionLoading_{{ i }}"><i class="fa fa-cog fa-lg fa-spin"></i> {{ text_loading_condition }}</div>
                        <div class="form-group" id="conditionContainer_{{ i }}" style="display:none;">
                          <label class="col-sm-2 control-label">{{ entry_condition }}</label>
                          <div class="col-sm-10">
                            <select name="condition" class="openbay_data_{{ i }} form-control" id="conditionRow_{{ i }}"></select>
                          </div>
                        </div>
                        <div class="alert alert-info" id="durationLoading_{{ i }}"><i class="fa fa-cog fa-lg fa-spin"></i> {{ text_loading_duration }}</div>
                        <div class="form-group" id="durationContainer_{{ i }}" style="display:none;">
                          <label class="col-sm-2 control-label">{{ text_duration }}</label>
                          <div class="col-sm-10">
                            <select name="duration" class="openbay_data_{{ i }} form-control" id="durationRow_{{ i }}"></select>
                          </div>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
              {% set i = i + 1 %}
              {% endfor %}
          {% else %}
            <div class="alert alert-danger alert-dismissible"><i class="fa fa-exclamation-circle"></i> {{ text_no_results }}</div>
          {% endif %}
        </form>
        <div class="modal fade" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true" id="overlay-loading" data-backdrop="static" data-keyboard="false">
          <div class="modal-dialog modal-lg">
            <div class="modal-content">
              <div class="modal-body">
                <div class="progress">
                  <div class="progress-bar progress-bar-striped active" role="progressbar" aria-valuenow="1" aria-valuemin="0" aria-valuemax="100" style="width: 0%" id="loading-bar"></div>
                </div>
                <p class="text-center">{{ text_preparing0 }} <span id="ajax-count-complete-display">0</span> {{ text_preparing1 }} <span id="ajax-count-total-display">0</span> {{ text_preparing2 }} </p>
              </div>
            </div>
          </div>
        </div>
        {% else %}
          {% for fail in error_fail %}
            <div class="alert alert-danger alert-dismissible"><i class="fa fa-exclamation-circle"></i> {{ fail }}</div>
          {% endfor %}
        {% endif %}
      </div>
      <!-- /.box-body -->
    </div>
    <!-- /.box -->

  </section>
  <!-- /.content -->
</div>

<input type="hidden" id="total-items" value="{{ count }}" name="total-items" />
<input type="hidden" id="ajax-count" value="0" />
<input type="hidden" id="ajax-count-total" value="0" />
<input type="hidden" id="ajax-count-complete" value="0" />
<script type="text/javascript">
  $(document).ready(function() {
    overlay('overlay-loading');

    {# This will run productCount times #}
    {% for j in 0..i %}
      getSuggestedCategories('{{ j }}');
      modifyPrices('{{ j }}');
    {% endfor %}

    $('#activeItems').text($('#total-items').val());
  });

  function overlay(screen) {
    $('#ajax-count-complete').val(0);
    $('#ajax-count-complete-display').text(0);
    $('#ajax-count-total').val(0);
    $('#ajax-count-total-display').text(0);
    $('#loading-bar').css('width', '0%');
    $('#'+screen).modal('toggle');
  }

  function overlayHide() {
    $('.modal').modal('hide');
  }

  function modifyPrices(id) {
      var price_original  = parseFloat($('#price_original_'+id).val());
      var price_modified = '';
      var modify_percent = '';

      $.ajax({
          url: 'index.php?route=extension/openbay/ebay_profile/profileGet&user_token={{ user_token }}&ebay_profile_id='+$('#generic_profile_'+id).val(),
          type: 'GET',
          async: true,
          dataType: 'json',
          beforeSend: function() { addCount(); },
          success: function(data) {

              if (data.data.price_modify !== false && typeof data.data.price_modify !== 'undefined') {
                  modify_percent = 100 + parseFloat(data.data.price_modify);
                  modify_percent = parseFloat(modify_percent / 100);
                  price_modified = price_original * modify_percent;

                  $('#price_'+id).val(parseFloat(price_modified).toFixed(2));
              }

              removeCount();
          },
          failure: function() {
              removeCount();
          },
          error: function() {
              removeCount();
          }
      });
  }

  function addCount() {
    var count = parseInt($('#ajax-count').val()) + 1;
    $('#ajax-count').val(count);
    var count1 = parseInt($('#ajax-count-total').val())+1;
    $('#ajax-count-total').val(count1);
    $('#ajax-count-total-display').text(count1);
  }

  function removeCount() {
      var count = parseInt($('#ajax-count').val())-1;
      $('#ajax-count').val(count);
      var count1 = parseInt($('#ajax-count-complete').val())+1;
      $('#ajax-count-complete').val(count1);
      $('#ajax-count-complete-display').text(count1);

      var modifier = 0;
      var current = 0;
      var total = $('#ajax-count-total').val();

      modifier = 100 / total;
      current = parseFloat(modifier * count1);

      $('#loading-bar').css('width', current+"%");

      if (count == 0) {
          overlayHide();
      }
  }

  function removeBox(id) {
      $('#p_row_'+id).fadeOut('medium');

      setTimeout(function() {
          $('#p_row_'+id).remove();
      }, 1000);

      $('#total-items').val($('#total-items').val()-1);

      if ($('.listingBox').length == 1) {
          window.location = "index.php?route=marketplace/openbay/items&user_token={{ user_token }}";
      } else {
          $('#activeItems').text($('#total-items').val());
      }
  }

  function useManualCategory(id) {
      loadCategories(1, true, id);
      $('#cSelections_'+id).show();
  }

  function getSuggestedCategories(id) {
      var qry = $('#title_'+id).val();

      $.ajax({
          url: 'index.php?route=extension/openbay/ebay/getSuggestedCategories&user_token={{ user_token }}&qry='+qry,
          type: 'GET',
          async: true,
          dataType: 'json',
          beforeSend: function() { $('#loadingSuggestedCat_'+id).show(); addCount(); },
          success: function(data) {
              var html_inj = '';

              if (data.error == false && data.data) {
                var i = 1;

                $.each(data.data, function(key,val) {
                    if (val.percent != 0) {
                      html_inj += '<div class="row form-group">';
                        html_inj += '<div class="col-sm-1 text-right">';
                          html_inj += '<input type="radio" id="suggested_category_'+id+'" name="suggested_'+id+'" value="'+val.id+'" onchange="categorySuggestedChange('+val.id+','+id+')"';
                          if (i == 1) {
                              html_inj += ' checked="checked"';
                              categorySuggestedChange(val.id, id);
                          }
                          html_inj += '/>';
                        html_inj += '</div>';
                        html_inj += '<div class="col-sm-11">';
                          html_inj += '('+val.percent+'% match) '+val.name;
                        html_inj += '</div>';
                      html_inj += '</div>';
                    }
                    i++;
                });

                html_inj += '<div class="row form-group">';
                  html_inj += '<div class="col-sm-1 text-right"><input type="radio" id="manual_use_category_'+id+'" name="suggested_'+id+'" value="" onchange="useManualCategory('+id+')" /></div>';
                  html_inj += '<div class="col-sm-11">{{ text_category_choose }}</div>';
                html_inj += '</div>';
              } else {
                html_inj += '<div class="row form-group">';
                  html_inj += '<div class="col-sm-1 text-right"><input type="radio" id="manual_use_category_'+id+'" name="suggested_'+id+'" value="" onchange="useManualCategory('+id+')" /></div>';
                  html_inj += '<div class="col-sm-11">{{ text_category_choose }}</div>';
                html_inj += '</div>';
                useManualCategory(id);
              }
              $('#suggestedCat_'+id).empty().html(html_inj);
              $('#loadingSuggestedCat_'+id).hide();
              removeCount();
          },
          failure: function() {
              $('#loadingSuggestedCat_'+id).hide();
              removeCount();
          },
          error: function() {
              $('#loadingSuggestedCat_'+id).hide();
              removeCount();
          }
      });
  }

  function loadCategories(level, skip, id) {
      var parent = '';

      if (level == 1) {
          parent = ''
      } else {
          var prevLevel = level - 1;
          parent = $('#catsSelect'+prevLevel+'_'+id).val();
      }

      var countI = level;

      while(countI <= 6) {
          $('#catsSelect'+countI+'_'+id).hide().empty();
          countI++;
      }

      $.ajax({
          url: 'index.php?route=extension/openbay/ebay/getCategories&user_token={{ user_token }}&parent='+parent,
          type: 'GET',
          dataType: 'json',
          beforeSend: function() {
              $('#imageLoading_'+id).show();
          },
          success: function(data) {
              if (data.items != null) {
                  $('#catsSelect'+level+'_'+id).empty();
                  $('#catsSelect'+level+'_'+id).append('<option value="">-- SELECT --</option>');
                  $.each(data.cats, function(key, val) {
                      if (val.CategoryID != parent) {
                          $('#catsSelect'+level+'_'+id).append('<option value="'+val.CategoryID+'">'+val.CategoryName+'</option>');
                      }
                  });

                  if (skip != true) {
                      $('#finalCat_'+id).val('');
                  }

                  $('#catsSelect'+level+'_'+id).show();
              } else {
                  if (data.error) {

                  } else {
                      $('#finalCat_'+id).val($('#catsSelect'+prevLevel+'_'+id).val());
                      getCategoryFeatures($('#catsSelect'+prevLevel+'_'+id).val(), id);
                  }
              }
              $('#imageLoading_'+id).hide();
          }
      });
  }

  function getCategoryFeatures(cat, id) {
      itemFeatures(cat, id);
      $('#editCatalog_'+id).show();

      $('#durationLoading_'+id).show();
      $('#durationContainer_'+id).hide();

      $('#conditionLoading_'+id).show();
      $('#conditionContainer_'+id).hide();

    $('#product_identifier_container_'+id).hide();
    $('.product_identifier_required_'+id).val('0');

      $.ajax({
          url: 'index.php?route=extension/openbay/ebay/getCategoryFeatures&user_token={{ user_token }}&category='+cat,
          type: 'GET',
          dataType: 'json',
          beforeSend: function() { addCount(); },
          success: function(data) {
              if (data.error == false) {
                var html_inj = '';

                listingDuration(data.data.durations, id);

                if (data.data.conditions) {
                  $.each(data.data.conditions, function(key, val) {
                      html_inj += '<option value='+val.id+'>'+val.name+'</option>';
                  });

                  if (html_inj == '') {
                    $('#conditionRow_'+id).empty();
                    $('#conditionContainer_'+id).hide();
                    $('#conditionLoading_'+id).hide();
                  } else {
                    $('#conditionRow_'+id).empty().html(html_inj);
                    $('#conditionContainer_'+id).show();
                    $('#conditionLoading_'+id).hide();
                  }
                }

                if (data.data.ean_identifier_requirement != '') {
                  $('#product_identifier_container_'+id).show();
                  $('#product_identifier_ean_container_'+id).show();

                  if (data.data.ean_identifier_requirement == 'Required') {
                    $('#identifier_ean_required_'+id).val(1);
                  }
                }

                if (data.data.isbn_identifier_requirement != '') {
                  $('#product_identifier_container_'+id).show();
                  $('#product_identifier_isbn_container_'+id).show();

                  if (data.data.isbn_identifier_requirement == 'Required') {
                    $('#identifier_isbn_required_'+id).val(1);
                  }
                }

                if (data.data.upc_identifier_requirement != '') {
                  $('#product_identifier_container_'+id).show();
                  $('#product_identifier_upc_container_'+id).show();

                  if (data.data.upc_identifier_requirement == 'Required') {
                    $('#identifier_upc_required_'+id).val(1);
                  }
                }
              } else {
                  alert(data.msg);
              }
              removeCount();
          },
          failure: function() {
              removeCount();
          },
          error: function() {
              removeCount();
          }
      });
  }

  function itemFeatures(category_id, id) {
    $('#editFeature_'+id).hide();

    var product_id = $('#product-id-' + id).val();

      $.ajax({
          url: 'index.php?route=extension/openbay/ebay/getEbayCategorySpecifics&user_token={{ user_token }}&category_id=' + category_id + '&product_id=' + product_id,
          type: 'GET',
          dataType: 'json',
          beforeSend: function() { addCount(); },
          success: function(data) {
              if (data.error == false) {
                  $('#feature-data-' + id).empty();

                  var html_inj = '';
                  var html_inj2 = '';
                  var show_other = 0;
                  var show_other_value = '';

                  if (data.data) {
                    html_inj = '';
                    html_inj += '<div class="container-fluid">';
                      html_inj += '<div class="panel panel-default">';
                        html_inj += '<div class="panel-body">';
                          html_inj += '<div class="well">';
                            html_inj += '<div class="row">';
                              $.each(data.data, function(option_specific_key, option_specific_value) {
                                html_inj2 = '';
                                html_inj += '<div class="row form-group">';
                                  html_inj += '<label class="col-sm-2 control-label">'+option_specific_value.name+'</label>';
                                  html_inj += '<div class="col-sm-10">';

                                  if (("options" in option_specific_value) && (option_specific_value.validation.max_values == 1)) {
                                    // matched_value_key in option_specific_value
                                    if ("matched_value_key" in option_specific_value) {
                                      $.each(option_specific_value.options, function(option_key, option) {
                                        if (option_specific_value.matched_value_key == option_key) {
                                          html_inj2 += '<option value="' + option + '" selected>' + option + '</option>';
                                        } else {
                                          html_inj2 += '<option value="' + option + '">' + option + '</option>';
                                        }
                                      });
                                    } else {
                                      html_inj2 += '<option disabled selected>{{ text_select }}</option>';

                                      $.each(option_specific_value.options, function(option_key, option) {
                                        html_inj2 += '<option value="' + option + '">' + option + '</option>';
                                      });
                                    }

                                    show_other = false;
                                    show_other_value = '';

                                    if (option_specific_value.validation.selection_mode == 'FreeText') {
                                      if (option_specific_value.unmatched_value != '') {
                                        html_inj2 += '<option value="Other" selected>{{ text_other }}</option>';
                                        show_other = true;
                                        show_other_value = option_specific_value.unmatched_value;
                                      } else {
                                        html_inj2 += '<option value="Other">{{ text_other }}</option>';
                                      }
                                    }

                                    html_inj += '<div class="row">';
                                      html_inj += '<div class="col-sm-7">';
                                        html_inj += '<select name="feat[' + option_specific_value.name + ']" class="openbay_data_' + id + ' form-control" id="spec_sel_' + id + '" onchange="toggleSpecOther(' + id + ');">' + html_inj2 + '</select>';
                                      html_inj += '</div>';

                                      if (show_other == true) {
                                        html_inj += '<div class="col-sm-5" id="spec_' + id + '_other">';
                                      } else {
                                        html_inj += '<div class="col-sm-5" id="spec_' + id + '_other" style="display:none;">';
                                      }
                                      html_inj += '<input placeholder="{{ text_other }}" type="text" name="featother[' + option_specific_value.name + ']" class="form-control openbay_data_' + id + '" value="' + show_other_value + '"/>';
                                      html_inj += '</div>';
                                    html_inj += '</div>';
                                  } else if (("options" in option_specific_value) && (option_specific_value.validation.max_values > 1)) {
                                    html_inj += '<div class="row">';
                                    $.each(option_specific_value.options, function(option_key, option) {
                                      html_inj += '<div class="col-sm-2">';
                                        html_inj += '<label class="checkbox-inline">';
                                          html_inj += '<input type="checkbox" name="feat[' + option_specific_value.name + '][]" value="' + option + '" class="openbay_data_' + id + '" /> ' + option;
                                        html_inj += '</label>';
                                      html_inj += '</div>';
                                    });
                                    html_inj += '</div>';
                                  } else {
                                    html_inj += '<div class="row">';
                                      html_inj += '<div class="col-sm-7">';
                                        html_inj += '<input type="text" name="feat[' + option_specific_value.name + ']" class="openbay_data_' + id + ' form-control col-sm-6" value="' + option_specific_value.unmatched_value + '"  />';
                                      html_inj += '</div>';
                                    html_inj += '</div>';
                                  }

                                  html_inj += '</div>';
                                html_inj += '</div>';
                              });
                            html_inj += '</div>';
                          html_inj += '</div>';
                        html_inj += '</div>';
                      html_inj += '</div>';
                    html_inj += '</div>';
                    $('#feature-data-'+id).append(html_inj);
                  } else {
                    $('#feature-data-'+id).text('None');
                  }
              } else {
                  alert(data.msg);
              }

              $('#editFeature_'+id).show();

              removeCount();
          },
          failure: function() {
              removeCount();
          },
          error: function() {
              removeCount();
          }
      });
  }

  function toggleSpecOther(id) {
    var selectVal = $('#spec_sel_'+id).val();
    if (selectVal == 'Other') {
      $('#spec_'+id+'_other').show();
    } else {
      $('#spec_'+id+'_other').hide();
    }
  }

  function searchEbayCatalog(id) {
    var qry = $('#catalog_search_'+id).val();
    var cat = $('#finalCat_'+id).val();
    var html = '';
    $('#catalog-results-'+id).empty().hide();

      if (qry == '') {
        $('#catalog_search_'+id).before('<div class="alert alert-danger" id="catalog_search_'+id+'_error"><i class="fa fa-exclamation-circle"></i> {{ text_search_text }}</div>');
      } else {
        $.ajax({
            url: 'index.php?route=extension/openbay/ebay/searchEbayCatalog&user_token={{ user_token }}',
            type: 'POST',
            dataType: 'json',
            data: { category_id: cat, page: 1, search: qry },
            beforeSend: function() {
              $('#catalog_search_'+id+'_error').remove();
              $('#button-catalog-search-'+id).empty().html('<i class="fa fa-cog fa-lg fa-spin"></i>').attr('disabled','disabled');
            },
            success: function(data) {
                    if (data.error == false) {
                      if (data.results > 0) {
                        data.products = $.makeArray(data.products);

                        $.each(data.products, function(key, val) {
                          html = '';
                          html += '<div class="well">';
                          html += '<div class="row">';
                          html += '<div class="col-sm-1">';
                          html += '<input type="radio" class="openbay_data_'+id+'" name="catalog_epid_'+id+'" value="'+val.productIdentifier.ePID+'" />';
                          html += '</div>';
                          html += '<div class="col-sm-2 text-center">';
                          if (typeof(val.stockPhotoURL) != "undefined" && val.stockPhotoURL !== null) {
                            html += '<img class="img-thumbnail" src="'+val.stockPhotoURL.thumbnail.value+'"/>';
                          } else {
                            html += '<span class="img-thumbnail"><i class="fa fa-camera fa-5x"></i></span>';
                          }
                          html += '</div>';
                          html += '<div class="col-sm-9">';
                          html += '<p>'+val.productDetails.value.text.value+'</p>';
                          html += '</div>';
                          html += '</div>';
                          html += '</div>';

                          $('#catalog-results-'+id).append(html).show();
                        });
                      } else {
                        $('#catalog-results-'+id).append('<div class="alert alert-warning"><i class="fa fa-warning"></i> {{ text_catalog_no_products }}</div>').show();
                      }
                    } else {
                      $('#catalog-results-'+id).append('<div class="alert alert-danger alert-dismissible"><i class="fa fa-exclamation-circle"></i> '+data.error_message+'</div>').show();
                    }
            },
            complete: function() {
              $('#button-catalog-search-'+id).empty().html('<i class="fa fa-lg fa-search"></i> {{ text_search }}').removeAttr('disabled');
            },
            failure: function() {
                $('#catalog-results-'+id).append('{{ text_search_failed }}');
            },
            error: function() {
                $('#catalog-results-'+id).append('{{ text_search_failed }}');
            }
        });
      }
  }

  function listingDuration(data, id) {
    var lang            = new Array();
    var listingDefault  = '{{ default.defaults.listing_duration }}';

    lang["Days_1"]      = '1 Day';
    lang["Days_3"]      = '3 Days';
    lang["Days_5"]      = '5 Days';
    lang["Days_7"]      = '7 Days';
    lang["Days_10"]     = '10 Days';
    lang["Days_30"]     = '30 Days';
    lang["GTC"]         = 'GTC';

    html_inj        = '';
    $.each(data, function(key, val) {
        html_inj += '<option value="'+val+'"';
        if (val == listingDefault) { html_inj += ' selected="selected"';}
        html_inj += '>'+lang[val]+'</option>';
    });

    $('#durationRow_'+id).empty().html(html_inj);
    $('#durationLoading_'+id).hide();
    $('#durationContainer_'+id).show();
  }

  function categorySuggestedChange(val, id) {
      $('#cSelections_'+id).hide();
      loadCategories(1, true, id);
      $('#finalCat_'+id).val(val);
      getCategoryFeatures(val, id);
  }

  $('#button-verify').bind('click', function() {
      var id = '';
      var name = '';
      var processedData = '';

      overlay('overlay-loading');

      $('#button-verify').hide();
      $('#button-edit').show();
      $('#button-submit').show();

      $.each($('.product_id'), function(i) {
          id = $(this).val();
          name = $('#title_'+$(this).val()).val();

          $('#product_messages_'+id).html('<div class="alert alert-info"><i class="fa fa-cog fa-lg fa-spin"></i> {{ text_loading }}</div>').show();
          $('.product_content_'+id).hide();
          $('#product_title_'+id).text(name).show();

          $('#catalog_epid_'+id).val($("input[type='radio'][name='catalog_epid_"+id+"']:checked").val());

          processedData = $(".openbay_data_"+id).serialize();

          $.ajax({
              url: 'index.php?route=extension/openbay/ebay/verifyBulk&user_token={{ user_token }}&i='+id,
              type: 'POST',
              dataType: 'json',
              data: processedData,
              beforeSend: function() { addCount(); },
              success: function(data) {
                var html = '';
                if (data.ack != 'Failure') {
                  var fee_total = '';
                  var currency = '';

                  $('#p_row_buttons_'+data.i).prepend('<a class="btn btn-primary button-preview" target="_BLANK" href="'+data.preview+'">{{ text_preview }}</a>');

                  if (data.errors) {
                    $.each(data.errors, function(k,v) {
                      html += '<div class="alert alert-warning"><i class="fa fa-warning"></i> '+v+'</div>';
                    });
                  }

                  $.each(data.fees, function(key, val) {
                    if (val.Fee != 0.0 && val.Name != 'ListingFee') {
                      fee_total = fee_total + parseFloat(val.Fee);
                    }
                    currency = val.Cur;
                  });

                  html += '<div class="alert alert-info"><i class="fa fa-info-circle"></i> {{ text_total_fee }} '+currency+' '+parseFloat(fee_total).toFixed(2)+'</div>';
                } else {
                    $.each(data.errors, function(k,v) {
                        html += '<div class="alert alert-danger alert-dismissible"><i class="fa fa-exclamation-circle"></i> '+v+'</div>';
                    });
                }
                $('#product_messages_'+data.i).html(html);
                removeCount();
              },
              failure: function() {
                  removeCount();
                  alert('{{ text_error_reverify }}');
              },
              error: function() {
                  removeCount();
                  alert('{{ text_error_reverify }}');
              }
          });
      });
  });

  $('#button-edit').bind('click', function() {
    var id = '';
    var name = '';

    $('#button-verify').show();
    $('#button-edit').hide();
    $('#button-submit').hide();
    $('.button-preview').remove();
    $('.button-listing-view').remove();

    $.each($('.product_id'), function(i) {
      id = $(this).val();
      name = $('#title_'+$(this).val()).val();
      $('#product_messages_'+$(this).val()).empty().hide();
      $('.product_content_'+$(this).val()).show();
      $('#product_title_'+$(this).val()).text(name).hide();
    });
  });

  $('#button-submit').bind('click', function() {
      var confirm_box = confirm('{{ text_ajax_confirm_listing }}');
      if (confirm_box) {
          var id = '';
          var name = '';
          var processedData = '';

          overlay('overlay-loading');

          $('#button-verify').hide();
          $('#button-edit').hide();
          $('#button-submit').hide();
          $('.button-preview').remove();

          $.each($('.product_id'), function(i) {
              id = $(this).val();
              name = $('#title_'+$(this).val()).val();

              $('.product_content_'+$(this).val()).hide();
              $('#product_title_'+$(this).val()).text(name).show();

              $.ajax({
                url: 'index.php?route=extension/openbay/ebay/listItemBulk&user_token={{ user_token }}&i='+id,
                type: 'POST',
                dataType: 'json',
                data: $(".openbay_data_"+id).serialize(),
                beforeSend: function() { addCount(); },
                success: function(data) {
                  var html = '';
                  if (data.ack != 'Failure') {
                    if (data.errors) {
                      $.each(data.errors, function(k,v) {
                        html += '<div class="alert alert-warning"><i class="fa fa-warning"></i> '+v+'</div>';
                      });
                    }

                    $('#p_row_buttons_'+data.i).prepend('<a class="btn btn-primary button-listing-view" href="{{ listing_link }}'+data.itemid+'" target="_BLANK">{{ button_view }}</a>');

                    html += '<div class="alert alert-success alert-dismissible"><i class="fa fa-check-circle"></i> {{ text_listed }}'+data.itemid+'</div>';
                  } else {
                    $.each(data.errors, function(k,v) {
                      html += '<div class="alert alert-danger alert-dismissible"><i class="fa fa-exclamation-circle"></i> '+v+'</div>';
                    });
                  }
                  $('#product_messages_'+data.i).html(html).show();
                  removeCount();
                },
                failure: function() {
                    removeCount();
                },
                error: function() {
                    removeCount();
                }
              });
          });
      }
  });

  function identifierNotRequired(id) {
    var not_required_text = "{{ setting.product_details.product_identifier_unavailable_text }}";

    if ($('#identifier_not_required_' + id + ':checked').length == 1) {
      if ($('#identifier_ean_required_' + id).val() == 1) {
        $('#identifier_ean_' + id).val(not_required_text);
      }
      if ($('#identifier_isbn_required_' + id).val() == 1) {
        $('#identifier_isbn_' + id).val(not_required_text);
      }
      if ($('#identifier_upc_required_' + id).val() == 1) {
        $('#identifier_upc_' + id).val(not_required_text);
      }
    } else {
      if ($('#identifier_ean_required_' + id).val() == 1) {
        $('#identifier_ean_' + id).val($('#identifier_ean_original_' + id).val());
      }
      if ($('#identifier_isbn_required_' + id).val() == 1) {
        $('#identifier_isbn_' + id).val($('#identifier_isbn_original_' + id).val());
      }
      if ($('#identifier_upc_required_' + id).val() == 1) {
        $('#identifier_upc_' + id).val($('#identifier_upc_original_' + id).val());
      }
    }
  }

  function showFeatures(id) {
    overlay('overlay-feature-'+id);
  }

  function showCatalog(id) {
    overlay('overlay-catalog-'+id);
  }

  function showProfiles(id) {
    overlay('overlay-profile-'+id);
  }

  function showCategory(id) {
    overlay('overlay-category-'+id);
  }

  function genericProfileChange(id) {
      modifyPrices(id);
  }
</script>
{{ footer }}
