/**
 * App eCommerce customer all
 */

'use strict';

// Datatable (jquery)
$(function () {
  let borderColor, bodyBg, headingColor;

  if (isDarkStyle) {
    borderColor = config.colors_dark.borderColor;
    bodyBg = config.colors_dark.bodyBg;
    headingColor = config.colors_dark.headingColor;
  } else {
    borderColor = config.colors.borderColor;
    bodyBg = config.colors.bodyBg;
    headingColor = config.colors.headingColor;
  }

  // Variable declaration for table
  var dt_customer_table = $('.datatables-customers'),
    select2 = $('.select2'),
    customerView = baseUrl + 'app/ecommerce/customer/details/overview';
  if (select2.length) {
    var $this = select2;
    $this.wrap('<div class="position-relative"></div>').select2({
      placeholder: 'United States ',
      dropdownParent: $this.parent()
    });
  }

  // Dobijamo trenutni URL
  var currentURL = window.location.href;

  // Delimo URL po '/' karakterima
  var parts = currentURL.split('/');

  // Dobijamo poslednji deo URL-a koji je broj
  var lastPart = parts.pop();
  console.log(lastPart);
  // customers datatable
  if (dt_customer_table.length) {
    var dt_customer = dt_customer_table.DataTable({
      ajax: {
        url: '/api/qrcodes/lista/' + lastPart,
        dataSrc: ''
      },
      columns: [
        // columns according to JSON
        { data: 'qrcode' },
        { data: 'br_bodova' },
        { data: 'id_korisnik' },
        { data: 'created_at' },
        { data: 'updated_at' }
      ],
      order: [[2, 'desc']],
      language: {
        sLengthMenu: '_MENU_',
        search: '',
        emptyTable: 'Nema podatak',
        info: 'Prikazuje _START_ do _END_ od _TOTAL_ od ukupno rezultata',
        infoEmpty: 'Prikazuje 0 do 0 od 0 unosa',
        loadingRecords: 'Učitavanje...',
        searchPlaceholder: 'Pretraži',
        paginate: {
          first: 'Prvi',
          last: 'Posljednji',
          next: 'Naredna',
          previous: 'Prethodna'
        }
      },
      // For responsive popup
      responsive: {
        details: {
          display: $.fn.dataTable.Responsive.display.modal({
            header: function (row) {
              var data = row.data();
              return 'Details of ' + data['customer'];
            }
          }),
          type: 'column',
          renderer: function (api, rowIdx, columns) {
            var data = $.map(columns, function (col, i) {
              return col.title !== '' // ? Do not show row in modal popup if title is blank (for check box)
                ? '<tr data-dt-row="' +
                    col.rowIndex +
                    '" data-dt-column="' +
                    col.columnIndex +
                    '">' +
                    '<td>' +
                    col.title +
                    ':' +
                    '</td> ' +
                    '<td>' +
                    col.data +
                    '</td>' +
                    '</tr>'
                : '';
            }).join('');

            return data ? $('<table class="table"/><tbody />').append(data) : false;
          }
        }
      }
    });
    $('.dataTables_length').addClass('ms-n2 mt-0 mt-md-3 me-2');
    $('.dt-action-buttons').addClass('pt-0');
    $('.dataTables_filter').addClass('ms-n3');
    $('.dt-buttons').addClass('d-flex flex-wrap');
  }

  // Filter form control to default size
  // ? setTimeout used for multilingual table initialization
  setTimeout(() => {
    $('.dataTables_filter .form-control').removeClass('form-control-sm');
    $('.dataTables_length .form-select').removeClass('form-select-sm');
  }, 300);
});
