{{ header }}{{ column_left }}
<div id="content">
  <div class="page-header">
    <div class="container-fluid">
      <div class="float-end">
        <button type="button" data-bs-toggle="tooltip" title="{{ button_filter }}" onclick="$('#filter-affiliate').toggleClass('d-none');" class="btn btn-light d-md-none"><i class="fa-solid fa-filter"></i></button>
        <button type="button" id="button-calculate" data-bs-toggle="tooltip" title="{{ button_calculate }}" class="btn btn-warning"><i class="fa-solid fa-sync"></i></button>
        <button type="submit" form="form-affiliate" formaction="{{ csv }}" id="button-csv" data-bs-toggle="tooltip" title="{{ button_csv }}" class="btn btn-info"><i class="fa-solid fa-file-csv"></i></button>
        <button type="submit" form="form-affiliate" formaction="{{ complete }}" data-oc-toggle="ajax" id="button-complete" data-bs-toggle="tooltip" title="{{ button_complete }}" class="btn btn-success"><i class="fa-solid fa-credit-card"></i></button>
        <a href="{{ add }}" data-bs-toggle="tooltip" title="{{ button_add }}" class="btn btn-primary"><i class="fa-solid fa-plus"></i></a>
        <button type="submit" form="form-affiliate" formaction="{{ delete }}" data-bs-toggle="tooltip" title="{{ button_delete }}" onclick="return confirm('{{ text_confirm }}');" class="btn btn-danger"><i class="fa-regular fa-trash-can"></i></button>
      </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="row">
      <div id="filter-affiliate" class="col-lg-3 col-md-12 order-lg-last d-none d-lg-block mb-3">
        <div class="card">
          <div class="card-header"><i class="fa-solid fa-filter"></i> {{ text_filter }}</div>
          <div class="card-body">
            <form id="form-filter">
              <div class="mb-3">
                <label class="form-label">{{ entry_customer }}</label> <input type="text" name="filter_customer" value="{{ filter_customer }}" placeholder="{{ entry_customer }}" id="input-customer" data-oc-target="autocomplete-customer" class="form-control" autocomplete="off"/>
                <ul id="autocomplete-customer" class="dropdown-menu"></ul>
              </div>
              <div class="mb-3">
                <label for="input-tracking" class="form-label">{{ entry_tracking }}</label> <input type="text" name="filter_tracking" value="{{ filter_tracking }}" placeholder="{{ entry_tracking }}" id="input-tracking" class="form-control"/>
              </div>
              <div class="mb-3">
                <label for="input-payment-method" class="form-label">{{ entry_payment_method }}</label> <select name="filter_payment_method" id="input-payment-method" class="form-select">
                  <option value=""></option>
                  {% for payment_method in payment_methods %}
                    <option value="{{ payment_method.value }}"{% if filter_payment_method == payment_method.value %} selected{% endif %}>{{ payment_method.text }}</option>
                  {% endfor %}
                </select>
              </div>
              <div class="mb-3">
                <label for="input-commission" class="form-label">{{ entry_commission }}</label> <input type="text" name="filter_commission" value="{{ filter_commission }}" placeholder="{{ entry_commission }}" id="input-commission" class="form-control"/>
              </div>
              <div class="mb-3">
                <label for="input-date-from" class="form-label">{{ entry_date_from }}</label>
                <input type="date" name="filter_date_from" value="{{ filter_date_from }}" placeholder="{{ entry_date_from }}" id="input-date-from" class="form-control"/>
              </div>
              <div class="mb-3">
                <label for="input-date-to" class="form-label">{{ entry_date_to }}</label>
                <input type="date" name="filter_date_to" value="{{ filter_date_to }}" placeholder="{{ entry_date_to }}" id="input-date-to" class="form-control"/>
              </div>
              <div class="mb-3">
                <label for="input-status" class="form-label">{{ entry_status }}</label> <select name="filter_status" id="input-status" class="form-select">
                  <option value=""></option>
                  <option value="1"{% if filter_status == '1' %} selected{% endif %}>{{ text_enabled }}</option>
                  <option value="0"{% if filter_status == '0' %} selected{% endif %}>{{ text_disabled }}</option>
                </select>
              </div>
              <div class="mb-3">
                <label for="input-limit" class="form-label">{{ entry_limit }}</label> <select name="limit" id="input-limit" class="form-select">
                  {% for limits in limits %}
                    <option value="{{ limits.value }}"{% if limits.value == limit %} selected{% endif %}>{{ limits.text }}</option>
                  {% endfor %}
                </select>
              </div>
              <div class="text-end">
                <button type="button" id="button-filter" class="btn btn-light"><i class="fa-solid fa-filter"></i> {{ button_filter }}</button>
                <button type="reset" data-bs-toggle="tooltip" title="{{ button_reset }}" class="btn btn-outline-secondary"><i class="fa-solid fa-filter-circle-xmark"></i></button>
              </div>
            </form>
          </div>
        </div>
      </div>
      <div class="col-lg-9 col-md-12">
        <div class="card">
          <div class="card-header"><i class="fa-solid fa-list"></i> {{ text_list }}</div>
          <div id="affiliate" class="card-body">{{ list }}</div>
        </div>
      </div>
    </div>
  </div>
</div>
<script type="text/javascript"><!--
$('#affiliate').on('click', 'thead a, .pagination a', function(e) {
    e.preventDefault();

    $('#affiliate').load(this.href);
});

$('#button-filter').on('click', function() {
    url = '';

    var filter_customer = $('#input-customer').val();

    if (filter_customer) {
        url += '&filter_customer=' + encodeURIComponent(filter_customer);
    }

    var filter_tracking = $('#input-tracking').val();

    if (filter_tracking) {
        url += '&filter_tracking=' + encodeURIComponent(filter_tracking);
    }

    var filter_payment_method = $('#input-payment-method').val();

    if (filter_payment_method) {
        url += '&filter_payment_method=' + filter_payment_method;
    }

    var filter_commission = $('#input-commission').val();

    if (filter_commission) {
        url += '&filter_commission=' + filter_commission;
    }

    var filter_date_from = $('#input-date-from').val();

    if (filter_date_from) {
        url += '&filter_date_from=' + encodeURIComponent(filter_date_from);
    }

    var filter_date_to = $('#input-date-to').val();

    if (filter_date_to) {
        url += '&filter_date_to=' + encodeURIComponent(filter_date_to);
    }

    var filter_status = $('#input-status').val();

    if (filter_status !== '') {
        url += '&filter_status=' + filter_status;
    }

    var limit = $('#input-limit').val();

    if (limit) {
        url += '&limit=' + limit;
    }

    window.history.pushState({}, null, 'index.php?route=marketing/affiliate&user_token={{ user_token }}' + url);

    $('#affiliate').load('index.php?route=marketing/affiliate.list&user_token={{ user_token }}' + url);
});

$('#input-customer').autocomplete({
    'source': function(request, response) {
        $.ajax({
            url: 'index.php?route=customer/customer.autocomplete&user_token={{ user_token }}&filter_affiliate=1&filter_name=' + encodeURIComponent(request),
            dataType: 'json',
            success: function(json) {
                response($.map(json, function(item) {
                    return {
                        label: item['name'],
                        value: item['customer_id']
                    }
                }));
            }
        });
    },
    'select': function(item) {
        $('#input-customer').val(decodeHTMLEntities(item['label']));
    }
});

$('#button-calculate').on('click', function(e) {
    e.preventDefault();

    var element = this;

    $.ajax({
        url: 'index.php?route=marketing/affiliate.calculate&user_token={{ user_token }}',
        dataType: 'json',
        beforeSend: function() {
            $(element).button('loading');
        },
        complete: function() {
            $(element).button('reset');
        },
        success: function(json) {
            console.log(json);

            if (json['error']) {
                $('#alert').prepend('<div class="alert alert-danger alert-dismissible"><i class="fa-solid fa-circle-exclamation"></i> ' + json['error'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');
            }

            if (json['success']) {
                $('#alert').prepend('<div class="alert alert-success alert-dismissible"><i class="fa-solid fa-check-circle"></i> ' + json['success'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');

                url = '';

                var filter_customer = $('#input-customer').val();

                if (filter_customer) {
                    url += '&filter_customer=' + encodeURIComponent(filter_customer);
                }

                var filter_tracking = $('#input-tracking').val();

                if (filter_tracking) {
                    url += '&filter_tracking=' + encodeURIComponent(filter_tracking);
                }

                var filter_payment_method = $('#input-payment-method').val();

                if (filter_payment_method) {
                    url += '&filter_payment_method=' + filter_payment_method;
                }

                var filter_commission = $('#input-commission').val();

                if (filter_commission) {
                    url += '&filter_commission=' + filter_commission;
                }

                var filter_date_from = $('#input-date-from').val();

                if (filter_date_from) {
                    url += '&filter_date_from=' + encodeURIComponent(filter_date_from);
                }

                var filter_date_to = $('#input-date-to').val();

                if (filter_date_to) {
                    url += '&filter_date_to=' + encodeURIComponent(filter_date_to);
                }

                var filter_status = $('#input-status').val();

                if (filter_status !== '') {
                    url += '&filter_status=' + filter_status;
                }

                var limit = $('#input-limit').val();

                if (limit) {
                    url += '&limit=' + limit;
                }

                $('#affiliate').load('index.php?route=marketing/affiliate.list&user_token={{ user_token }}' + url);
            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });
});
//--></script>
{{ footer }}
