<? global $client, $lang_id;?>
<div class="basket-summary-container box">
  <h1 class="box-title"><?= ucfirst( \Shared\Helpers\Helpers::lang( 'podsumowanie-zamowienia' ) );?>:</h1>
  <div id="basket-summary" class="content">
    <? if ( is_array( $this -> basket ) and count( $this -> basket ) ):?>
      <? foreach ( $this -> basket as $position_hash => $position ):?>
        <? $product = (new \Domain\Product\ProductRepository($GLOBALS['mdb']))->findCached( $position[ 'product-id' ], $this -> lang_id );?>
        <div class="basket-product">
          <div class="image">
            <? if ( file_exists( substr( $product[ 'images' ][ 0 ][ 'src' ], 1 ) ) ):?>
              <img class="main-img" src="<?= $product[ 'images' ][ 0 ][ 'src' ];?>" alt="">
            <? else:?>

            <? endif;?>
          </div>
          <div class="details">
            <?
            $product[ 'language' ][ 'seo_link' ] ? $url = '/' . $product[ 'language' ][ 'seo_link' ] : $url = '/p-' . $product[ 'id' ] . '-' . \Shared\Helpers\Helpers::seo( $product[ 'language' ][ 'name' ] );

            if ( \Shared\Helpers\Helpers::get_session( 'current-lang' ) != ( new \Domain\Languages\LanguagesRepository( $GLOBALS['mdb'] ) )->defaultLanguage() and $url != '#' )
              $url = '/' . \Shared\Helpers\Helpers::get_session( 'current-lang' ) . $url;
            ?>
            <div class="name">
              <a href="<?= $url;?>"><?= $product[ 'language' ][ 'name' ];?></a>
            </div>
            <? if ( is_array( $position[ 'attributes' ] ) and count( $position[ 'attributes' ] ) ):?>
              <? foreach ($position['attributes'] as $row) : ?>
              <div class="description">
                <?
                if ($row)
                {
                  $row = explode('-', $row);
                  $attributeRepo = new \Domain\Attribute\AttributeRepository($GLOBALS['mdb']);
                  $attribute = $attributeRepo->frontAttributeDetails((int)$row[0], $this->lang_id);
                  echo '<span>' . $attribute['language']['name'] . ':</span> ';
                  $value = $attributeRepo->frontValueDetails((int)$row[1], $this->lang_id);
                  echo $value['language']['name'];
                }
                ?>
              </div>
              <? endforeach; ?>
              <hr>
            <? endif;?>
            <?= \Shared\Tpl\Tpl::view( 'shop-basket/_partials/product-custom-fields', [
              'custom_fields' => $position['custom_fields']
            ] ); ?>
            <div class="product-message">
              <?= $position['message'] != '' ? '<strong>Wiadomość:</strong> ' . $position['message'] : '';?>
            </div>
          </div>
          <div class="prices">
            <div class="price">
              <?
                $price_product = \Domain\Basket\BasketCalculator::calculateBasketProductPrice((float)$product['price_brutto_promo'], (float)$product['price_brutto'], $this -> coupon, $position);

                if ($price_product['price_new'])
                  echo \Shared\Helpers\Helpers::decimal($price_product['price_new']) . ' zł';

                if ($price_product['price_new'] and $price_product['price_new'] < $price_product['price'])
                  echo '<u>' . \Shared\Helpers\Helpers::decimal($price_product['price']) . ' zł</u>';

                if ($price_product['price_new'] and $price_product['price_new'] < $price_product['price'])
                  $discount += \Shared\Helpers\Helpers::normalize_decimal(($price_product['price'] - $price_product['price_new']) * $position['quantity']);
              ?>
              <span>x <?= $position[ 'quantity' ];?> <?= \Shared\Helpers\Helpers::lang( 'szt' );?>.</span>
              <?= \Shared\Helpers\Helpers::decimal( \Shared\Helpers\Helpers::normalize_decimal( $price_product['price_new'] * $position[ 'quantity' ] ) );?> zł
            </div>
          </div>
        </div>
        <? $summary += \Shared\Helpers\Helpers::normalize_decimal( $price_product['price'] * $position[ 'quantity' ] );?>
        <?
          if ( $begin_checkout_items )
            $begin_checkout_items .= ',';

          $begin_checkout_items .= '{';
          $begin_checkout_items .= '"id": "' . $product['id'] . '",';
          $begin_checkout_items .= '"name": "' . $product['language']['name'] . '",';
          $begin_checkout_items .= '"price": ' . \Shared\Helpers\Helpers::normalize_decimal( $price_product['price_new'] ) . ',';
          $begin_checkout_items .= '"quantity": ' . $position['quantity'];
          $begin_checkout_items .= '}';
        ?>
      <? endforeach;?>
      <div class="hr"></div>
      <div class="basket-summary">
        Wartość koszyka:
        <span>
          <?= \Shared\Helpers\Helpers::decimal( $summary );?> zł
        </span>
      </div>
      <? if ( $discount ):?>
        <div class="basket-summary">
          Rabat:
          <span class="text-danger">-<?= \Shared\Helpers\Helpers::decimal( $discount ); ?> zł</span>
        </div>
      <? endif;?>
      <div class="basket-summary">
        <?= $this -> transport[ 'name_visible' ];?>:
        <span>
          <?= $summary >= $this -> settings[ 'free_delivery' ] ? '0,00' : \Shared\Helpers\Helpers::decimal( $this -> transport[ 'cost' ] );?> zł
        </span>
      </div>
      <div class="basket-summary big">
          Razem:
          <?
            if ( $discount )
              $summary -= $discount;
          ?>
          <span id="order-summary"><?= $summary >= $this -> settings[ 'free_delivery' ] ? \Shared\Helpers\Helpers::decimal( $summary ) : \Shared\Helpers\Helpers::decimal( $summary + $this -> transport[ 'cost' ] );?> zł</span>
      </div>
      <div class="basket-summary">
        <?= $this -> payment_method[ 'name' ];?>
      </div>
      <? if ( $this -> basket_message ):?>
        <div class="basket-summary">
          Wiadomość:
          <span><?= $this -> basket_message;?></span>
        </div>
      <? endif;?>
      <div class="hr"></div>
    <? else:?>
      <div class="alert alert-danger">Brak produktów w koszyku</div>
    <? endif;?>
  </div>
  <? if ( is_array( $this -> basket ) and count( $this -> basket ) ):?>
    <div class="basket-options">
      <div class="content">
        <? if ( !$this -> client ):?>
          <div class="left">
            <?=
            \front\Views\ShopClient::loginForm( [
                'long' => true
            ] );
            ?>
          </div>
          <div class="right">
            <?= \Shared\Tpl\Tpl::view( 'shop-basket/address-form', [
                'transport_method' => $this -> transport
              ] );?>
          </div>
        <? else:?>
          <?= \Shared\Tpl\Tpl::view( 'shop-basket/address-form', [
              'client' => $this -> client,
              'addresses' => $this -> addresses,
              'transport_method' => $this -> transport
          ] );?>
        <? endif;?>
      </div>
    <a href="/koszyk" class="btn btn-dark"><?= \Shared\Helpers\Helpers::lang( 'wroc-do-koszyka' );?></span></a>
    </div>
  <? endif;?>
</div>
<script class="footer" type="text/javascript">
  document.getElementById('order-send').addEventListener('click', function() {
    var form = document.getElementById('form-order'); // Zastąp 'form-id' rzeczywistym ID Twojego formularza
    if (form.checkValidity()) {
      this.classList.add('loading-button');
      this.disabled = true;
      form.submit();
    } else {
      // Opcjonalnie: wywołaj funkcję reportValidity(), aby wyświetlić komunikaty o błędach formularza
      form.reportValidity();
    }
  });
  <? if ( $this -> settings['google_tag_manager_id'] ):?>
    dataLayer.push({
      event: "begin_checkout",
      ecommerce: {
        currency: "PLN",
        value: <?= \Shared\Helpers\Helpers::normalize_decimal( \Domain\Basket\BasketCalculator::summaryPrice( $this -> basket, $this -> coupon ) );?>,
        items: [
          <?= $begin_checkout_items;?>
        ]
      }
    });
  <? endif;?>
</script>
