<!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.Order"></h2>
              </div>
              <div class="col-auto ms-auto d-print-none">
                <div class="d-flex">
                  <div class="me-3">
                    <div id="FinalPriceShop"></div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
        <!-- Page body -->
        <div class="page-body">
          <div class="container-xl">
            <div class="row row-cards row-deck">
              <div class="space-y" id="inventory-container">
              </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>
    <div class="modal modal-blur fade" id="newArticleModal" tabindex="-1" role="dialog" aria-hidden="true">
      <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" data-translate="Shop.Modal.ItemDetails"></h5>
            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
          </div>
          <div class="modal-body">
            <div class="mb-3">
              <label class="form-label" data-translate="Shop.Modal.ItemDescription"></label>
              <span id="itemDescription"></span>
            </div>
            <div class="mb-3">
              <label class="form-label" data-translate="Shop.Modal.ChooseSize"></label>
              <input id="itemId" style="display: none;"></input>
              <input id="totalPrice" style="display: none;"></input>
              <select id="itemSize" class="form-select">
                <option value="stock_xs">XS</option>
                <option value="stock_s">S</option>
                <option value="stock_m">M</option>
                <option value="stock_l">L</option>
                <option value="stock_xl">XL</option>
                <option value="stock_xxl">XXL</option>
              </select>
            </div>
            <div class="mb-3">
              <label class="form-label" data-translate="Shop.Modal.ChooseAmount"></label>
              <input type="number" id="itemAmount" class="form-control" min="1" value="1">
            </div>
            <div class="mb-3">
              <label class="form-label" data-translate="Shop.Modal.Price"></label>
              <p id="itemPrice"></p>
              <p id="itemPriceTotal"></p>
            </div>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-primary" id="addToCartButton" data-translate="Shop.Modal.Button.Order"></button>
            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal" data-translate="Shop.Modal.Button.Cancel"></button>
          </div>
        </div>
      </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 %>/dist/libs/nouislider/dist/nouislider.min.js?1692870487" defer></script>
    <script src="<%= domain %>/appjs/layout.js"></script>
    <script>
      const deduplicateAndSum = () => {
        const result = [];
        const items = JSON.parse(localStorage.getItem('cart'));

        items.forEach(item => {
            const existingItem = result.find(
                existing => existing.id === item.id && existing.size === item.size
            );

            if (existingItem) {
                existingItem.amount = parseInt(existingItem.amount) + parseInt(item.amount);
                existingItem.totalPrice = parseFloat(existingItem.totalPrice) + parseFloat(item.totalPrice);
            } else {
                // Clone the item to avoid modifying the original array
                result.push({ ...item });
            }
        });
        return result;
      };

      const getUniqueIds = (items) => {
        
        const ids = new Set();

        items.forEach(item => {
            ids.add(item.id);
        });

        return Array.from(ids);
      }

      function renderCards(items, inventoryMap) {
        console.log(items, inventoryMap)
      const container = document.getElementById('inventory-container');
      container.innerHTML = ''; // Clear existing cards

      let finalPrice = 0;

      items.forEach(item => {
        const dbItem = inventoryMap[item.id];
        const totalPriceItem = Number(dbItem.price) * Number(item.amount)
        finalPrice += Number(totalPriceItem);
        const card = document.createElement('div');
        card.className = 'card col-md-12';
        card.innerHTML = `
          <div class="row g-0">
            <div class="col-auto">
              <div class="card-body">
                <div class="avatar avatar-md" style="background-image: url(${dbItem.image})"></div>
              </div>
            </div>
            <div class="col">
              <div class="card-body ps-0">
                <div class="row">
                  <div class="col">
                    <h3 class="mb-0"><a>${dbItem.article}</a></h3>
                  </div>
                </div>
                <div class="row">
                  <div class="col-md">
                    <div class="mt-3 list-inline list-inline-dots mb-0 text-secondary d-sm-block d-none">
                      <div class="list-inline-item">
                        <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-caret-right">
                          <path stroke="none" d="M0 0h24v24H0z" fill="none"/>
                          <path d="M10 18l6 -6l-6 -6v12"/>
                        </svg>
                        ${i18next.t(`Admin.Page.Inventory.Modal.AddNew.ArticleType_${dbItem.article_type}`)}
                      </div>
                      <div class="list-inline-item">
                        ${dbItem.gender === "Male" ? 
                          '<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-man"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M10 16v5"/><path d="M14 16v5"/><path d="M9 9h6l-1 7h-4z"/><path d="M5 11c1.333 -1.333 2.667 -2 4 -2"/><path d="M19 11c-1.333 -1.333 -2.667 -2 -4 -2"/><path d="M12 4m-2 0a2 2 0 1 0 4 0a2 2 0 1 0 -4 0"/></svg>' : 
                          '<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-woman"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M10 16v5"/><path d="M14 16v5"/><path d="M8 16h8l-2 -7h-4z"/><path d="M5 11c1.667 -1.333 3.333 -2 5 -2"/><path d="M19 11c-1.667 -1.333 3.333 -2 5 -2"/><path d="M12 4m-2 0a2 2 0 1 0 4 0a2 2 0 1 0 -4 0"/></svg>'
                        }
                        ${i18next.t(`Admin.Page.Inventory.Modal.AddNew.Gender_${dbItem.gender}`)}
                      </div>
                      <div class="list-inline-item">
                        <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>
                        ${i18next.t('Shop.Price', {count: dbItem.price})}
                      </div>
                    </div>
                  </div>
                  <div class="col-md-auto">
                    <div class="mt-3 badges">
                      <span class="badge bg-primary">${i18next.t(`Admin.Page.Inventory.Modal.AddNew.${item.size}`)}</span>
                      <span class="badge bg-success">${i18next.t('Shop.Toal_Items', {count: Number(item.amount)})}</span>
                      <span class="badge bg-info">${i18next.t('Shop.Total_Price', {count: totalPriceItem})}</span>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        `;
        container.appendChild(card);
      });

      document.getElementById('FinalPriceShop').innerHTML = i18next.t('Shop.Total_Price', {count: finalPrice});
    }

      const fetchInventory = async (order_data) => {
          const response = await fetch('/api/v1/inventory/list', {
            method: 'POST',
            headers: {
              'Content-Type': 'application/json',
              'Authorization': 'Bearer ' + localStorage.getItem('token')
            },
            body: JSON.stringify({ ids: getUniqueIds(order_data) })
          });

          if (!response.ok) {
            console.error('Failed to fetch inventory data');
            return;
          }

          const inventory = await response.json();
          const inventoryMap = inventory.reduce((map, item) => {
            map[item.id] = item;
            return map;
          }, {});

          renderCards(order_data, inventoryMap);
        }

      const fetchOrder = async () => {
        const response = await fetch(`/api/v1/shop/myorder/<%= params.uuid %>`, {
          method: 'GET',
          headers: {
            'Content-Type': 'application/json',
            'Authorization': `Bearer ${localStorage.getItem('token')}`
          }
        });

        if (!response.ok) {
          console.error('Failed to fetch inventory data');
          return;
        }

        const json_response = await response.json();
        fetchInventory(JSON.parse(json_response[0].order_data));
      }

    document.addEventListener('DOMContentLoaded', fetchOrder);
    </script>
  </body>
</html>