<?php
    use artbox\catalog\models\OptionGroup;
    use artbox\catalog\models\Product;
    use artbox\catalog\models\Variant;
    use artbox\core\components\SeoComponent;
    use artbox\core\helpers\ImageHelper;
    use frontend\models\Order;
    use yii\bootstrap\ActiveForm;
    use yii\bootstrap\Html;
    use yii\helpers\ArrayHelper;
    use yii\helpers\Url;
    use yii\web\View;
    
    /**
     * @var View         $this
     * @var Product      $model
     * @var Variant      $variant
     * @var SeoComponent $seo
     * @var array        $groups
     * @var Product[]    $similar
     */
    $seo = \Yii::$app->get('seo');
    if (!empty($model->category)) {
        if (!empty($model->category->parent)) {
            $this->params[ 'breadcrumbs' ][] = [
                'label' => $model->category->parent->lang->title,
                'url'   => [
                    '/category/view',
                    'category' => $model->category->parent->lang->alias->value,
                ],
            ];
        }
        $this->params[ 'breadcrumbs' ][] = [
            'label' => $model->category->lang->title,
            'url'   => [
                '/category/view',
                'category' => $model->category->lang->alias->value,
            ],
        ];
    }
    $this->params[ 'breadcrumbs' ][] = $seo->title;
    $images = $model->images;
    if (!empty($model->image)) {
        array_unshift($images, $model->image);
    }
?>
<div id="content">
  <div class="container">
    <div class="row">
      <div class="col-md-12">
        
        <div class="row" id="productMain">
          <div class="col-sm-6">
            <div id="mainImage">
                <?php
                    if (!empty($images)) {
                        echo ImageHelper::set($images[ 0 ]->getPath())
                                        ->fillResize(555, 555)
                                        ->renderImage(
                                            [
                                                'class' => 'img-responsive',
                                                'alt'   => $model->lang->title,
                                                'title' => $model->lang->title,
                                            ]
                                        );
                    } else {
                        echo ImageHelper::set('@frontend/web/img/no-image.png')
                                        ->fillResize(555, 555)
                                        ->renderImage(
                                            [
                                                'class' => 'img-responsive',
                                                'alt'   => $model->lang->title,
                                                'title' => $model->lang->title,
                                            ]
                                        );
                    }
                ?>
            </div>
              
              <?php
                  if ($model->is('akcia')) {
                      ?>
                    <div class="ribbon sale">
                      <div class="theribbon">АКЦИЯ</div>
                      <div class="ribbon-background"></div>
                    </div>
                    <!-- /.ribbon -->
                      <?php
                  }
                  if ($model->is('new')) {
                      ?>
                    <div class="ribbon new">
                      <div class="theribbon">НОВОЕ</div>
                      <div class="ribbon-background"></div>
                    </div>
                    <!-- /.ribbon -->
                      <?php
                  }
                  if ($model->is('top')) {
                      ?>
                    <div class="ribbon top">
                      <div class="theribbon">ТОП</div>
                      <div class="ribbon-background"></div>
                    </div>
                    <!-- /.ribbon -->
                      <?php
                  }
              ?>
            
            <div class="row" id="thumbs">
                <?php
                    if (!empty($images)) {
                        foreach ($images as $image) {
                            echo Html::tag(
                                'div',
                                Html::a(
                                    ImageHelper::set($image->getPath())
                                               ->fillResize(70, 60)
                                               ->renderImage(
                                                   [
                                                       'class' => 'img-responsive',
                                                       'alt'   => $model->lang->title,
                                                       'title' => $model->lang->title,
                                                   ]
                                               ),
                                    ImageHelper::set($image->getPath())
                                               ->fillResize(555, 555)
                                               ->render(),
                                    [
                                        'class' => 'thumb',
                                    ]
                                ),
                                [
                                    'class' => 'col-xs-2',
                                ]
                            );
                        }
                    } else {
                        echo Html::tag(
                            'div',
                            Html::a(
                                ImageHelper::set('@frontend/web/img/no-image.png')
                                           ->fillResize(70, 60)
                                           ->renderImage(
                                               [
                                                   'class' => 'img-responsive',
                                                   'alt'   => $model->lang->title,
                                                   'title' => $model->lang->title,
                                               ]
                                           ),
                                ImageHelper::set('@frontend/web/img/no-image.png')
                                           ->fillResize(555, 555)
                                           ->render(),
                                [
                                    'class' => 'thumb',
                                ]
                            ),
                            [
                                'class' => 'col-xs-2',
                            ]
                        );
                    }
                ?>
            </div>
              <?php
                  if (!empty($model->video)) {
                      ?>
                    <div class="product-video">
                      <div class="h3">
                          <?php echo \Yii::t('app', 'Видеообзор продукта'); ?>
                      </div>
                      <div class="video-box">
                          <?php echo $model->video; ?>
                      </div>
                    </div>
                      <?php
                  }
              ?>
          </div>
          <div class="col-sm-6">
            <div class="box">
              <h1><?php echo $model->lang->title; ?></h1>
              <p class="no-margin"><?php echo $variant->sku; ?></p>
              <p class="price">
                <span class="price-title">Цена:</span><?php echo $variant->price ? : 0; ?> грн&ensp;
                  <?php
                      if ($variant->canBuy()) {
                          echo Html::a(
                              Html::tag(
                                  'i',
                                  '',
                                  [
                                      'class' => 'fa fa-shopping-cart',
                                  ]
                              ) . \Yii::t('app', 'Добавить в корзину'),
                              '#',
                              [
                                  'class'   => 'btn btn-success add-to-basket',
                                  'data-id' => $variant->id,
                              ]
                          );
                      } else {
                          echo Html::a(
                              \Yii::t('app', 'Нет в наличии'),
                              '#',
                              [
                                  'class'   => 'btn btn-info disabled',
                                  'data-id' => $variant->id,
                              ]
                          );
                      }
                  ?>
                  <?php
                      if (\Yii::$app->user->isGuest) {
                          echo Html::button(
                              Html::tag('i', '', [ 'class' => 'fa fa-heart-o' ]),
                              [
                                  'title' => 'Добавить в избранное',
                                  'data'  => [
                                      'toggle'    => 'tooltip',
                                      'placement' => 'top',
                                  ],
                                  'class' => 'btn btn-default pull-right disabled',
                              ]
                          );
                      } elseif (in_array($variant->id, \Yii::$app->user->identity->wishlist)) {
                          echo Html::button(
                              Html::tag('i', '', [ 'class' => 'fa fa-heart' ]),
                              [
                                  'title' => 'Убрать из избранного',
                                  'data'  => [
                                      'toggle'    => 'tooltip',
                                      'placement' => 'top',
                                      'product'   => $model->id,
                                      'variant'   => $variant->id,
                                      'user'      => \Yii::$app->user->identity->getId(),
                                      'url'       => Url::to([ 'product/wishlist-rm' ]),
                                  ],
                                  'class' => 'wishlist-rm btn btn-success pull-right',
                              ]
                          );
                      } else {
                          echo Html::button(
                              Html::tag('i', '', [ 'class' => 'fa fa-heart-o' ]),
                              [
                                  'title' => 'Добавить в избранное',
                                  'data'  => [
                                      'toggle'    => 'tooltip',
                                      'placement' => 'top',
                                      'product'   => $model->id,
                                      'variant'   => $variant->id,
                                      'user'      => \Yii::$app->user->identity->getId(),
                                      'url'       => Url::to([ 'product/wishlist-add' ]),
                                  ],
                                  'class' => 'wishlist-add btn btn-success pull-right',
                              ]
                          );
                      }
                  ?>
              </p>
              <hr>
                <?php
                    if ($variant->canBuy()) {
                        echo Html::a(
                            Html::icon(
                                'phone',
                                [
                                    'prefix' => 'fa fa-',
                                ]
                            ) . \Yii::t('app', 'Купить в один клик'),
                            '#',
                            [
                                'data'  => [
                                    'toggle' => 'modal',
                                    'target' => '#oneclick-modal',
                                ],
                                'class' => 'btn btn-template-main',
                            ]
                        );
                    }
                ?>
                <button type="submit" class="btn btn-template-main"><i class="fa fa-tags"></i> Купить в кредит</button>
                <button type="submit" class="btn btn-template-main"><i class="fa fa-tags"></i> Купить в рассрочку</button>
            </div>
            <div class="box" id="details">
              <h2><?php echo \Yii::t('app', 'Описание товара'); ?></h2>
                <?php echo $model->lang->description ? : \Yii::t('app', 'Нет описания'); ?>
            </div>
            
            <div class="box" id="details">
              <h2><?php echo \Yii::t('app', 'Характеристики'); ?></h2>
                <?php
                    if (!empty($groups)) {
                        ?>
                      <table class="table">
                        <tbody>
                            <?php
                                foreach ($groups as $group) {
                                    foreach ($group as $optionGroup) {
                                        /**
                                         * @var OptionGroup $optionGroup
                                         */
                                        echo Html::tag(
                                            'tr',
                                            Html::tag(
                                                'td',
                                                $optionGroup->lang->title,
                                                [ 'class' => 'td-title' ]
                                            ) . Html::tag(
                                                'td',
                                                implode(
                                                    ', ',
                                                    ArrayHelper::getColumn(
                                                        $optionGroup->currentOptions,
                                                        'lang.value'
                                                    )
                                                )
                                            )
                                        );
                                    }
                                }
                            ?>
                        </tbody>
                      </table>
                        <?php
                    } else {
                        echo \Yii::t('app', 'Нет характеристик');
                    }
                ?>
            </div>
          </div>
        
        </div>
          
          <?php
              if (!empty($similar)) {
                  ?>
                <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($similar, 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>
      <!-- /.col-md-9 -->
    </div>
    <!-- /.row -->
  </div>
  <!-- /.container -->
</div>
<!-- /#content -->
<div class="modal fade" id="oneclick-modal" tabindex="-1" role="dialog">
  <div class="modal-dialog modal-sm">
    
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title callback"><?php echo \Yii::t('app', 'Купить в один клик'); ?></h4>
      </div>
      <div class="modal-body">
          <?php
              $order = new Order(
                  [
                      'scenario'  => Order::SCENARIO_FAST,
                      'variantId' => $variant->id,
                      'returnUrl' => \Yii::$app->request->absoluteUrl,
                  ]
              );
              $form = ActiveForm::begin(
                  [
                      'action' => [ 'checkout/fast' ],
                      'id'     => 'fast-buy-form',
                  ]
              );
              echo $form->field($order, 'variantId')
                        ->label(false)
                        ->hiddenInput();
              echo $form->field($order, 'returnUrl')
                        ->label(false)
                        ->hiddenInput();
              echo $form->field($order, 'name')
                        ->label(false)
                        ->textInput(
                            [
                                'placeholder' => $order->getAttributeLabel('name'),
                            ]
                        );
              echo $form->field($order, 'phone')
                        ->label(false)
                        ->textInput(
                            [
                                'placeholder' => $order->getAttributeLabel('phone'),
                            ]
                        );
              echo Html::tag(
                  'p',
                  Html::submitButton(
                      Html::icon(
                          'shopping-cart',
                          [
                              'prefix' => 'fa fa-',
                          ]
                      ) . \Yii::t('app', ' Отправить'),
                      [
                          'class' => 'btn btn-template-main',
                      ]
                  ),
                  [
                      'class' => 'text-center',
                  ]
              );
              $form::end();
          ?>
      </div>
    </div>
  </div>
</div>
