{{ header }}{{ column_left }}
<div id="content">
  <div class="page-header">
    <div class="container-fluid">
      <div class="pull-right"> <a href="{{ link_overview }}" 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 bulk_listing_status %}
      <div class="well">
        <div class="row">
          <div class="col-sm-12 text-right"><a class="btn btn-primary" id="button-search"><i class="fa fa-search"></i> {{ button_search }}</a> </div>
        </div>
      </div>
      <form id="bulk-list-form" class="form-horizontal">
        <table class="table table-bordered table-hover">
          <thead>
            <tr>
              <th class="text-center"><input type="checkbox" onclick="$('input[name*=\'product_ids[]\']').prop('checked', this.checked);"/></th>
              <th class="text-center">{{ column_image }}</th>
              <th class="text-left">{{ column_name }}</th>
              <th class="text-right">{{ column_model }}</th>
              <th class="text-right">{{ column_status }}</th>
              <th class="text-right">{{ column_matches }}</th>
              <th class="text-left">{{ column_result }}</th>
            </tr>
          </thead>
          <tbody>
            {% if products %}
            {% for product in products %}
            <tr>
              <td class="text-center"><input class="amazon-listing" type="checkbox" name="product_ids[]" value="{{ product.product_id }}"/></td>
              <td class="text-center"><img src="{{ product.image }}"/></td>
              <td class="text-left"><a href="{{ product.href }}" target="_blank">{{ product.name }}</a></td>
              <td class="text-right">{{ product.model }}</td>
              <td class="text-right">{{ product.search_status }}</td>
              <td class="text-right">{% if product.matches is not null %}{{ product.matches }}{% else %}-{% endif %}</td>
              <td class="text-left" id="result-{{ product.product_id }}">
                {% if product.matches is not empty and product.matches > 0 %}
                  {% set checked = false %}
                  {% if product.matches > 0 %}
                    <input class="amazon-listing" type="radio" name="products[{{ product.product_id }}]" value=""/>{{ text_dont_list }}<br/>
                    {% for search_results in product.search_results %}
                      {% if not checked %}
                        <input class="amazon-listing" checked="checked" type="radio" name="products[{{ product.product_id }}]" value="{{ search_result.asin }}"/>
                        {% set checked = true %}
                      {% else %}
                        <input class="amazon-listing" type="radio" name="products[{{ product.product_id }}]" value="{{ search_result.asin }}"/>
                      {% endif %}
                      <a target="_blank" href="{{ search_result.href }}">{{ search_result.title }}</a><br/>
                    {% endfor %}
                  {% else %}
                    <input class="amazon-listing" checked="checked" type="radio" name="products[{{ product.product_id }}]" value=""/> {{ text_dont_list }}<br/>
                  {% endif %}
                {% endif %}
              </td>
            </tr>
            {% endfor %}
            {% else %}
            <tr>
              <td colspan="7" class="text-center">{{ text_no_results }}</td>
            </tr>
            {% endif %}
          </tbody>
        </table>
      </form>
      <div class="row">
        <div class="col-sm-6 text-left">{{ pagination }}</div>
        <div class="col-sm-6 text-right">{{ results }}</div>
      </div>
      <div class="well" style="margin-top:10px;">
        <div class="row">
          <div class="col-sm-4">
            <div class="form-group">
              <label for="input-condition" class="control-label">{{ entry_condition }}</label>
              <select name="condition" class="form-control" id="input-condition">
                <option value=""></option>
                {% for value, name in conditions %}
                {% if value == default_condition %}
                <option selected="selected" value="{{ value }}">{{ name }}</option>
                {% else %}
                <option value="{{ value }}">{{ name }}</option>
                {% endif %}
                {% endfor %}
              </select>
            </div>
          </div>
          <div class="col-sm-4">
            <div class="form-group">
              <label for="input-condition-note" class="control-label">{{ entry_condition_note }}</label>
              <input type="text" name="condition_note" class="form-control" id="input-condition-note" />
            </div>
          </div>
          <div class="col-sm-4">
            <div class="form-group">
              <label for="input-start" class="control-label">{{ entry_start_selling }}</label>
              <div class="input-group date">
                <input type="text" class="form-control" id="input-start" data-date-format="YYYY-MM-DD" placeholder="{{ entry_start_selling }}" name="start_selling">
                <span class="input-group-btn">
                <button type="button" class="btn btn-primary"><i class="fa fa-calendar"></i></button>
                </span> </div>
            </div>
          </div>
        </div>
        <div class="row">
          <div class="col-sm-12">
            <div class="pull-right"> <a class="btn btn-primary" id="button-list"><i class="fa fa-plus-circle"></i> {{ button_list }}</a> </div>
          </div>
        </div>
      </div>
    {% else %}
      <div class="warning">{{ error_bulk_listing_permission }}</div>
    {% endif %}
  </div>
</div>
<script type="text/javascript"><!--
$(document).ready(function () {
  $('#product-form table thead input[type="checkbox"]').change(function () {
    var checkboxes = $('input[name="product_ids[]"]');
    if ($(this).is(':checked')) {
      checkboxes.attr('checked', 'checked');
    } else {
      checkboxes.removeAttr('checked');
    }
  });

  $('input[name="product_ids[]"]').change(function () {
    if (!$(this).is(':checked')) {
      $('#product-form table thead input[type="checkbox"]').removeAttr('checked');
    }
  });
});

$('#button-list').bind('click', function(e) {
  e.preventDefault();

  var request_data = $('input.amazon-listing:checked').serialize();

  if (request_data) {
    request_data += '&marketplace={{ filter_marketplace }}';
  }

  var condition = $('select[name="condition"]').val();
  var condition_note = $('input[name="condition_note"]').val();

  if (condition && condition_note) {
    request_data += '&condition=' + encodeURIComponent(condition) + '&condition_note=' + encodeURIComponent(condition_note);
  }

  var start_selling = $('input[name="start_selling"]').val();

  if (start_selling) {
    request_data += '&start_selling=' + encodeURIComponent(start_selling);
  }

  $.ajax({
    url: 'index.php?route=extension/openbay/amazonus/doBulkList&user_token={{ user_token }}',
    data: request_data,
    dataType: 'json',
    type: 'POST',
    success: function (json) {
      $('.warning, .success').remove();

      var html = '';

      if (json.status) {
        html = '<div class="success">' + json.message + '</div>';
        $('input.amazon-listing:checked[value!=""]').parent().parent().fadeOut(450);
      } else {
        html = '<div class="warning">' + json.message + '</div>';
      }

      $('.box').prepend(html);
    },
    error: function(xhr, ajaxOptions, thrownError) {
      if (xhr.status != 0) { alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText); }
    }
  });
});

$('#button-search').bind('click', function(e) {
  e.preventDefault();

  var request_data = $('input[name="product_ids[]"]:checked').serialize();

  if (request_data != '') {
    request_data += '&marketplace={{ filter_marketplace }}';

    $.ajax({
      url: 'index.php?route=extension/openbay/amazonus/dobulksearch&user_token={{ user_token }}',
      data: request_data,
      dataType: 'json',
      type: 'POST',
      beforeSend: function() {
        $('.alert-dismissible').remove();
      },
      success: function (json) {
        $.each(json, function (key, value) {
          var element = $('#result-' + key);
          if (value.error) {
            element.html('<div class="alert alert-danger alert-dismissible">' + value.error + '</span>');
          } else if (value.success) {
            element.html('<div class="alert alert-success alert-dismissible">' + value.success + '</span>');
          }

          $('input[name="product_ids[]"]').removeAttr('checked');
        });
      },
      error: function(xhr, ajaxOptions, thrownError) {
        if (xhr.status != 0) { alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText); }
      }
    });
  } else {
    $('#bulk-list-form').prepend('<div class="alert alert-danger alert-dismissible">{{ error_select_items }}</div>');
  }
});
//--></script>
{{ footer }}
