<?php
    /**
     * @var \yii\web\View                    $this
     * @var \artbox\order\models\Basket      $basket
     * @var \artbox\catalog\models\Variant[] $variants
     * @var \artbox\catalog\models\Product[] $topItems
     */
    use artbox\core\helpers\ImageHelper;
    use yii\bootstrap\Html;
    
    $formatter = \Yii::$app->formatter;
    $sum = 0;
    $sumDiscount = 0;
    foreach ($variants as $variant) {
        $count = $basket->getItem($variant->id)[ 'count' ];
        $sum += $variant->price * $count;
        if (!empty($variant->price_old)) {
            $sumDiscount += ( $variant->price_old - $variant->price ) * $count;
        }
    }
?>
<div id="content">
  <div class="container">
    <div class="row">
      <div id="basket" class="basket-container clearfix">
          <?php
              if (!empty($variants)) {
                  ?>
                <div class="col-md-9 clearfix">
                  <h1><?php echo \Yii::t('app', 'Ваша корзина'); ?></h1>
                  
                  <div class="box">
                    
                    <div class="table-responsive">
                      <table class="table">
                        <thead>
                          <tr>
                            <th colspan="2"><?php echo \Yii::t('app', 'Продукт'); ?></th>
                            <th><?php echo \Yii::t('app', 'Количество'); ?></th>
                            <th><?php echo \Yii::t('app', 'Цена за единицу'); ?></th>
                            <th><?php echo \Yii::t('app', 'Скидка'); ?></th>
                            <th colspan="2"><?php echo \Yii::t('app', 'Всего'); ?></th>
                          </tr>
                        </thead>
                        <tbody>
                            <?php
                                foreach ($variants as $variant) {
                                    $count = $basket->getItem($variant->id)[ 'count' ];
                                    ?>
                                  <tr data-id="<?php echo $variant->id; ?>" class="product-row-basket">
                                    <td>
                                        <?php
                                            echo Html::a(
                                                ImageHelper::set($variant->product->image->getPath())
                                                           ->fillResize(50, 50)
                                                           ->renderImage(
                                                               [
                                                                   'alt'   => $variant->product->lang->title,
                                                                   'title' => $variant->product->lang->title,
                                                               ]
                                                           ),
                                                [
                                                    '/product/view',
                                                    'id' => $variant->product->id,
                                                ],
                                                [
                                                    'target' => '_blank',
                                                ]
                                            );
                                        ?>
                                    </td>
                                    <td>
                                        <?php
                                            echo Html::a(
                                                $variant->product->lang->title,
                                                [
                                                    '/product/view',
                                                    'id' => $variant->product->id,
                                                ],
                                                [
                                                    'target' => '_blank',
                                                ]
                                            );
                                        ?>
                                    </td>
                                    <td>
                                        <?php
                                            echo Html::input(
                                                'number',
                                                '',
                                                $count,
                                                [
                                                    'class' => 'form-control increase-product-basket',
                                                ]
                                            );
                                        ?>
                                    </td>
                                    <td>
                                        <?php
                                            echo $formatter->asDecimal($variant->price ? : 0, 2);
                                        ?>
                                    </td>
                                    <td>
                                        <?php
                                            if (!empty($variant->price_old)) {
                                                echo $formatter->asDecimal($variant->price_old - $variant->price, 2);
                                            } else {
                                                echo $formatter->asDecimal(0, 2);
                                            }
                                        ?>
                                    </td>
                                    <td>
                                        <?php
                                            echo $formatter->asDecimal(
                                                ( $variant->price ? : 0 ) * $count,
                                                2
                                            );
                                        ?>
                                    </td>
                                    <td><a href="#" class="remove-product-cart"><i class="fa fa-trash-o"></i></a>
                                    </td>
                                  </tr>
                                    <?php
                                }
                            ?>
                        </tbody>
                        <tfoot>
                          <tr>
                            <th colspan="5"><?php echo \Yii::t('app', 'Всего'); ?></th>
                            <th colspan="2"><?php echo $formatter->asDecimal($sum, 2); ?></th>
                          </tr>
                        </tfoot>
                      </table>
                    
                    </div>
                    <!-- /.table-responsive -->
                    
                    <div class="box-footer">
                      <div class="pull-left">
                          <?php
                              echo Html::a(
                                  Html::icon('chevron-left', [ 'prefix' => 'fa fa-' ]) . \Yii::t(
                                      'app',
                                      ' Продолжить покупки'
                                  ),
                                  [ 'site/index' ],
                                  [
                                      'class' => 'btn btn-default',
                                  ]
                              )
                          ?>
                      </div>
                      <div class="pull-right">
                          <?php
                              echo Html::a(
                                  \Yii::t(
                                      'app',
                                      ' Оформить заказ '
                                  ) . Html::icon('chevron-right', [ 'prefix' => 'fa fa-' ]),
                                  [ 'checkout/info' ],
                                  [
                                      'class' => 'btn btn-success',
                                  ]
                              )
                          ?>
                      </div>
                    </div>
                  
                  </div>
                  <!-- /.box -->
                
                </div>
                <!-- /.col-md-9 -->
                
                <div class="col-md-3">
                  <div class="box" id="order-summary">
                    <div class="box-header">
                      <h3>Итоговый счет</h3>
                    </div>
                    <p class="text-muted small"><?php echo \Yii::t(
                            'app',
                            'Стоимость доставки рассчитывается в зависимости от адреса и способа доставки'
                        ); ?></p>
                    
                    <div class="table-responsive">
                      <table class="table">
                        <tbody>
                          <tr>
                            <td><?php echo \Yii::t('app', 'Всего за товары'); ?></td>
                            <th><?php echo $formatter->asDecimal($sum, 2); ?></th>
                          </tr>
                          <tr>
                            <td><?php echo \Yii::t('app', 'Сумма скидки'); ?></td>
                            <th><?php echo $formatter->asDecimal($sumDiscount, 2); ?></th>
                          </tr>
                          <tr class="total">
                            <td><?php echo \Yii::t('app', 'Итого к оплате'); ?></td>
                            <th><?php echo $formatter->asDecimal($sum, 2); ?></th>
                          </tr>
                        </tbody>
                      </table>
                    </div>
                  </div>
                </div>
                <!-- /.col-md-3 -->
                  <?php
              } else {
                  echo Html::tag('p', \Yii::t('app', 'Ваша корзина пуста!'));
              }
          ?>
      </div>
        <?php
            if (!empty($topItems)) {
                ?>
              <div class="heading text-center">
                <h2><?php echo \Yii::t('app', 'Смотрите также'); ?></h2>
              </div>
              <div class="product-carousel">
                <div class="homepage owl-carousel">
                    <?php
                        $newItemsArrays = array_chunk($topItems, 4);
                        foreach ($newItemsArrays as $newItemsArray) {
                            ?>
                          <div class="products">
                              <?php
                                  foreach ($newItemsArray as $product) {
                                      echo $this->render(
                                          '@frontend/views/site/_slider_product',
                                          [
                                              'product' => $product,
                                          ]
                                      );
                                  }
                              ?>
                          </div>
                            <?php
                        }
                    ?>
                </div>
              </div>
                <?php
            }
        ?>
    </div>
  </div>
</div>