<div class="mini-box">
  <h1 class="box-title"><?= ucfirst( \Shared\Helpers\Helpers::lang( 'zawartosc-koszyka' ) ); ?>:</h1>
  <div id="basket" class="content">
    <? if ( is_array( $this -> basket ) and count($this->basket)) : ?>
      <? foreach ($this->basket as $position_hash => $position) : ?>
        <?
        unset( $price );
        unset( $price_new );
        $product = (new \Domain\Product\ProductRepository($GLOBALS['mdb']))->findCached( (int)$position['product-id'], $this -> lang_id );

        $permutation = null;
        if ( isset( $position['parent_id'] ) and (int)$position['parent_id'] and isset( $position['product-id'] ) )
          $permutation = (new \Domain\Product\ProductRepository($GLOBALS['mdb']))->getProductPermutationHash( (int)$position['product-id'] );

        if ( !$permutation and isset( $position['attributes'] ) and is_array( $position['attributes'] ) and count( $position['attributes'] ) )
          $permutation = implode( '|', $position['attributes'] );

        $quantity_options = (new \Domain\Product\ProductRepository($GLOBALS['mdb']))->getProductPermutationQuantityOptions(
          (int)( $position['parent_id'] ? $position['parent_id'] : $position['product-id'] ),
          $permutation
        );

        $max_quantity = (int)$quantity_options['quantity'];
        if ( !$max_quantity and (int)$quantity_options['stock_0_buy'] )
          $max_quantity = 999;
        ?>
        <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">
            <?php
            $url = \front\Views\ShopProduct::productUrl( $product );

            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 '<div>' . $attribute['language']['name'] . ':</div>';
                    $value = $attributeRepo->frontValueDetails((int)$row[1], $this->lang_id);
                    echo '<div>' . $value['language']['name'] . '</div>';
                  }
                  ?>
                </div>
              <? endforeach; ?>
              <hr>
            <? endif; ?>
            <?= \Shared\Tpl\Tpl::view( 'shop-basket/_partials/product-custom-fields', [
              'custom_fields' => $position['custom_fields']
            ] ); ?>
            <? if ( $product['additional_message'] ):?>
              <div class="basket-product-message">
                <textarea name="product-message" class="form-control" position="<?= $position_hash;?>" placeholder="Miejsce na dodatkową wiadomość"><?= htmlspecialchars( $position['message'] );?></textarea>
              </div>
            <? endif;?>
          </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']);
              ?>
            </div>
          </div>
          <div class="buttons">
            <a href="#" class="btn btn-default btn-minus" product-hash="<?= $position_hash; ?>">
              <i class="fa fa-minus"></i>
            </a>
            <input type="text" name="quantity" id="quantity" class="int-format form-control" min="1" max="<?= $max_quantity; ?>" value="<?= $position['quantity']; ?>" product-hash="<?= $position_hash; ?>">
            <a href="#" class="btn btn-default btn-plus" product-hash="<?= $position_hash; ?>">
              <i class="fa fa-plus"></i>
            </a>
            <a href="#" class="btn btn-danger btn-delete" product-hash="<?= $position_hash; ?>">
              <i class="fa fa-trash"></i>
            </a>
          </div>
        </div>
        <? $summary += $price_product['price'] * $position['quantity'];?>
      <? endforeach; ?>
      <div class="basket-summary">
        Wartość koszyka: <span class="price"><?= \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>
        <div class="basket-summary">
          Wartość koszyka po rabacie:
          <span class="text-danger"><?= \Shared\Helpers\Helpers::decimal($summary - $discount); ?> zł</span>
        </div>
      <? endif; ?>
      <div class="basket-bottom">
        <div class="basket-message">
          <textarea name="basket-message" class="form-control" id="basket-message" placeholder="Dodatkowe informacje..."><?= htmlspecialchars($this->basket_message); ?></textarea>
        </div>
        <div class="coupon">
          <?= \Shared\Tpl\Tpl::view( 'shop-coupon/form', [
                      'coupon' => \Shared\Helpers\Helpers::get_session('coupon')
                  ] );
          ?>
        </div>
      </div>
    <? else : ?>
      <div class="alert alert-danger">Brak produktów w koszyku</div>
    <? endif; ?>
  </div>
</div>
