<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover"/>
    <meta http-equiv="X-UA-Compatible" content="ie=edge"/>
    <title>VAG - Dienstkleidung</title>
    <!-- CSS files -->
    <link href="<%= domain %>/dist/css/tabler.min.css?1684106062" rel="stylesheet"/>
    <link href="<%= domain %>/dist/css/tabler-flags.min.css?1684106062" rel="stylesheet"/>
    <link href="<%= domain %>/dist/css/tabler-payments.min.css?1684106062" rel="stylesheet"/>
    <link href="<%= domain %>/dist/css/tabler-vendors.min.css?1684106062" rel="stylesheet"/>
    <style>
      @import url('https://rsms.me/inter/inter.css');
      :root {
      	--tblr-font-sans-serif: 'Inter Var', -apple-system, BlinkMacSystemFont, San Francisco, Segoe UI, Roboto, Helvetica Neue, sans-serif;
      }
      body {
      	font-feature-settings: "cv03", "cv04", "cv11";
      }
    </style>
  </head>
  <body >
    <script src="/appjs/format.js"></script>
    <div class="page">
      <!-- Navbar -->
      <header class="navbar navbar-expand-md d-print-none" >
        <div class="container-xl">
          <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbar-menu" aria-controls="navbar-menu" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
          </button>
          <h1 class="navbar-brand navbar-brand-autodark d-none-navbar-horizontal pe-0 pe-md-3">
            <a href="/dashboard">
              <img src="<%= domain %>/dist/logo-transparent.png" width="110" height="32" alt="EBG" class="navbar-brand-image">
              <div data-translate="Dashboard.Header.Pagename">
            </a>
          </h1>
          <div class="navbar-nav flex-row order-md-last">
            <div class="nav-item d-none d-md-flex me-3">
              <div class="btn-list">
                <!-- Download SVG icon from http://tabler-icons.io/i/heart -->
                <svg  xmlns="http://www.w3.org/2000/svg"  width="24"  height="24"  viewBox="0 0 24 24"  fill="none"  stroke="currentColor"  stroke-width="2"  stroke-linecap="round"  stroke-linejoin="round"  class="icon icon-tabler icons-tabler-outline icon-tabler-coins"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M9 14c0 1.657 2.686 3 6 3s6 -1.343 6 -3s-2.686 -3 -6 -3s-6 1.343 -6 3z" /><path d="M9 14v4c0 1.656 2.686 3 6 3s6 -1.344 6 -3v-4" /><path d="M3 6c0 1.072 1.144 2.062 3 2.598s4.144 .536 6 0c1.856 -.536 3 -1.526 3 -2.598c0 -1.072 -1.144 -2.062 -3 -2.598s-4.144 -.536 -6 0c-1.856 .536 -3 1.526 -3 2.598z" /><path d="M3 6v10c0 .888 .772 1.45 2 2" /><path d="M3 11c0 .888 .772 1.45 2 2" /></svg>
                <div id="UserCreditamount"></div>
              </div>
            </div>
            <div class="d-none d-md-flex">
              <a onclick="toggleColor('dark')" class="nav-link px-0 hide-theme-dark" style="cursor: pointer;" data-translate-title="Dashboard.Header.Buttons.ToggleColor.Dark" title="" data-bs-toggle="tooltip"
		   data-bs-placement="bottom">
                <!-- Download SVG icon from http://tabler-icons.io/i/moon -->
                <svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M12 3c.132 0 .263 0 .393 0a7.5 7.5 0 0 0 7.92 12.446a9 9 0 1 1 -8.313 -12.454z" /></svg>
              </a>
              <a onclick="toggleColor('light')" class="nav-link px-0 hide-theme-light" style="cursor: pointer;" data-translate-title="Dashboard.Header.Buttons.ToggleColor.Light" title="" data-bs-toggle="tooltip"
		   data-bs-placement="bottom">
                <!-- Download SVG icon from http://tabler-icons.io/i/sun -->
                <svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M12 12m-4 0a4 4 0 1 0 8 0a4 4 0 1 0 -8 0" /><path d="M3 12h1m8 -9v1m8 8h1m-9 8v1m-6.4 -15.4l.7 .7m12.1 -.7l-.7 .7m0 11.4l.7 .7m-12.1 -.7l-.7 .7" /></svg>
              </a>
              <div class="nav-item dropdown d-none d-md-flex me-3">
                <a data-translate-title="Dashboard.Header.Buttons.ShoppingCart" class="nav-link px-0" data-bs-toggle="dropdown" tabindex="-1" aria-label="Show notifications">
                  <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="icon icon-tabler icons-tabler-outline icon-tabler-shopping-cart">
                    <path stroke="none" d="M0 0h24v24H0z" fill="none"/>
                    <path d="M6 19m-2 0a2 2 0 1 0 4 0a2 2 0 1 0 -4 0"/>
                    <path d="M17 19m-2 0a2 2 0 1 0 4 0a2 2 0 1 0 -4 0"/>
                    <path d="M17 17h-11v-14h-2"/>
                    <path d="M6 5l14 1l-1 7h-13"/>
                  </svg>
                  
                </a>
                <div class="dropdown-menu dropdown-menu-arrow dropdown-menu-end" style="width: 400px;">
                  <div class="card">
                    <div class="card-header">
                      <h3 class="card-title" data-translate="Dashboard.Header.Buttons.ShoppingCart"></h3>
                    </div>
                    <div class="list-group list-group-flush list-group-hoverable">
                      <div class="list-group-item">
                        <div class="row align-items-center">
                          <a href="/cart" class="col text-truncate">
                            <span class="text-body d-block" id="CartHeaderElementAmount"></span>
                            <div class="d-block text-muted text-truncate mt-n1">
                              <span id="CartHeaderElementCost"></span>
                            </div>
                          </a>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </div>              
            </div>
            <div class="nav-item dropdown">
              <a href="#" class="nav-link d-flex lh-1 text-reset p-0" data-bs-toggle="dropdown" aria-label="Open user menu">
                <span class="avatar avatar-sm" id="Dashboard.Profile.AvatarIcon" style="background-image: url()"></span>
                <div class="d-none d-xl-block ps-2">
                  <div id="Dashboard.Profile.Username"></div>
                  <div class="mt-1 small text-muted" id="Dashboard.Profile.User_Group"></div>
                </div>
              </a>
              <div class="dropdown-menu dropdown-menu-end dropdown-menu-arrow" id="Dashboard.ProfileDropdown">
              </div>
            </div>
          </div>
        </div>
      </header>
      <header class="navbar-expand-md">
        <div class="collapse navbar-collapse" id="navbar-menu">
          <div class="navbar">
            <div class="container-xl">
              <ul class="navbar-nav" id="Dashboard.Navbar.Elements">
              </ul>
            </div>
          </div>
        </div>
      </header>
      <div class="page-wrapper">
        <!-- Page header -->
        <div class="page-header d-print-none">
          <div class="container-xl">
            <div class="row g-2 align-items-center">
              <div class="col">
                <h2 class="page-title" data-translate="Admin.Headlines.Orders"></h2>
              </div>
              <!-- Page title actions -->
              <div class="col-auto ms-auto d-print-none">
                <div class="d-flex">
                  <div class="me-3">
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
        <!-- Page body -->
        <div class="page-body">
          <div class="container-xl">
            <div class="card">
              <div class="card-body">
                <div id="table-default" class="table-responsive">
                  <table class="table">
                    <thead>
                      <tr>
                        <th><button class="table-sort" data-sort="sort-uuid" data-translate="Admin.Page.Orders.Table.UUID"></button></th>
                        <th><button class="table-sort" data-sort="sort-user_id" data-translate="Admin.Page.Orders.Table.User_id"></button></th>
                        <th><button class="table-sort" data-sort="sort-price" data-translate="Admin.Page.Orders.Table.Price"></button></th>
                        <th><button class="table-sort" data-sort="sort-pickup_time" data-translate="Admin.Page.Orders.Table.Pickup_time"></button></th>
                        <th><button class="table-sort" data-sort="sort-status" data-translate="Admin.Page.Orders.Table.Status"></button></th>
                      </tr>
                    </thead>
                    <tbody class="table-tbody">
                    </tbody>
                  </table>
                </div>
                <div id="paginationContainer"></div>
              </div>
            </div>
          </div>
        </div>
        <footer class="footer footer-transparent d-print-none">
          <div class="container-xl">
            <div class="row text-center align-items-center flex-row-reverse">
              <div class="col-lg-auto ms-lg-auto">
                <ul class="list-inline list-inline-dots mb-0">
                  <li class="list-inline-item"><a href="<%= domain %>/legal/legal#imprint" data-translate="Dashboard.Footer.Links.Imprint" class="link-secondary" rel="noopener"></a></li>
                  <li class="list-inline-item"><a href="<%= domain %>/legal/legal#privacy" data-translate="Dashboard.Footer.Links.Privacy" class="link-secondary"></a></li>
                  <li class="list-inline-item"><a href="<%= domain %>/legal/guidelines" data-translate="Dashboard.Footer.Links.Guidelines" class="link-secondary" rel="noopener"></a></li>
                </ul>
              </div>
              <div class="col-12 col-lg-auto mt-3 mt-lg-0">
                <ul class="list-inline list-inline-dots mb-0">
                  <li class="list-inline-item">
                    Copyright &copy; <%= new Date().getFullYear() %>
                    <a href="https://ebg.pw" class="link-secondary">EBG</a>.
                    All rights reserved.
                  </li>
                </ul>
              </div>
            </div>
          </div>
        </footer>
      </div>
    </div>
    <!-- Libs JS -->
    <!-- Tabler Core -->
    <script src="<%= domain %>/appjs/permission.js"></script>
    <script src="<%= domain %>/appjs/translate.js"></script>
    <script src="<%= domain %>/dist/js/tabler.min.js?1684106062" defer></script>
    <script src="<%= domain %>/dist/js/demo.min.js?1684106062" defer></script>
    <script src="<%= domain %>/dist/js/i18next.min.js?1684106062"></script>
    <script src="<%= domain %>/appjs/layout.js"></script>
    <script>
      const rowsPerPage = 10;
      let currentPage = 1;
      let sortColumn = 'pickup_time';
      let sortOrder = 'asc';

      const loadTable = async (in_currentPage) => {
        currentPage = in_currentPage ?? currentPage; // Update currentPage
        const search = "";
        const response = await fetch(`/api/v1/shop/myorders?page=${currentPage}&limit=${rowsPerPage}&sort=${sortColumn}&order=${sortOrder}&search=${search}`, {
          method: 'GET',
          headers: {
            'Content-Type': 'application/json',
            'Authorization': 'Bearer ' + localStorage.getItem('token')
          }
        })

        if(response.status === 200) {
          const data = await response.json()
          const tbody = document.querySelector('.table-tbody')
          tbody.innerHTML = ''
          data.table.forEach(order => {
            const tr = document.createElement('tr')
            // Make Credits editable, set user id as data attribute and give class to make it editable
            tr.innerHTML = `
              <td class="sort-uuid"><a href="/orders/${order.uuid}">${order.uuid}</a></td>
              <td class="sort-user_id">${order.user_id}</td>
              <td class="sort-price">${order.price}</td>
              <td class="sort-pickup_time">${order.pickup_time}</td>
              <td class="sort-status">${order.status}</td>
            `
            tbody.appendChild(tr)
          })

          return data;
        } else {
          alert('Error fetching table data')
          return {};
        }
      }
      document.addEventListener('DOMContentLoaded', async () => {
        await generatePagination(currentPage, rowsPerPage, loadTable)

        document.querySelectorAll('.table-sort').forEach(header => {
              header.addEventListener('click', () => {
                  const newSortColumn = header.getAttribute('data-sort').replace('sort-', '');
                  if (sortColumn === newSortColumn) {
                      sortOrder = sortOrder === 'asc' ? 'desc' : 'asc';
                  } else {
                      sortColumn = newSortColumn;
                      sortOrder = 'asc';
                  }
                  loadTable();
              });
            });
      })
    </script>
  </body>
</html>