<?php
/******************************************************************************
 *   ______                                                                   *
 *  /       \                                                                 *
 * /$$$$$$  | ________  __    __   ______    ______                           *
 * $$ |__$$ |/        |/  |  /  | /      \  /      \                          *
 * $$    $$ |$$$$$$$$/ $$ |  $$ |/$$$$$$  |/$$$$$$  |                         *
 * $$$$$$$$ |  /  $$/  $$ |  $$ |$$ |  $$/ $$    $$ |                         *
 * $$ |  $$ | /$$$$/__ $$ \__$$ |$$ |      $$$$$$$$/                          *
 * $$ |  $$ |/$$      |$$    $$/ $$ |      $$       |                         *
 * $$/   $$/ $$$$$$$$/  $$$$$$/  $$/        $$$$$$$/                          *
 *                                                                            *
 * @PROJECT    : 3D Preview Extension [Magetop.com]                           *
 * @AUTHOR     : Azure - Developer                                            *
 * @COPYRIGHT  : © 2019  Magetop.com                                          *
 * @LINK       : https://Magetop.com                                          *
 * @CREATED    :  10/01/2022                                                  *
 ******************************************************************************/
/** @var \Magetop\Advancepreview\Block\CustomProduct $block */
$customerGroup = $block->getCustomerGroup();
$productId = $block->getRequest()->getParam('product',0);
$previewDiscounts = $block->getPreviewDiscounts($productId);
$qty = $block->getRequest()->getParam('qty',1);
$tierPrices = $block->getTierPriceByProductId($productId);
$ourPrice = $block->getTierPriceItemByQty($tierPrices, $qty);
$tax = 19;
$defaultPreviewDiscount = $block->getCustomPreviewDiscount($productId);
// $block->getProductTax($productId);
$paramsAdditionOptions = $block->getRequest()->getParam('preview_additional_options',[]);
//print_r($paramsAdditionOptions);
$additionOptionPrice = 0;
$designType = $block->getRequest()->getParam('txt_custom_preview_design_type',0);
if($designType == 1) {
    $additionOptionPrice = $block->getAdditionPriceByIds($paramsAdditionOptions);
}
//  echo $additionOptionPrice;
?>
<?php if($previewDiscounts && $previewDiscounts->getSize() && $previewDiscounts->getSize() > 0) : ?>
    <?php $i = 0; ?>
    <?php $totalPrice = 0; ?>
    <?php if($customerGroup > 1) : ?>
        <?php foreach ($previewDiscounts as $previewDiscount) : ?>
            <?php $priceDiscount = $previewDiscount->getData('discount_price') - (($defaultPreviewDiscount * $previewDiscount->getData('discount_price')) / 100); ?>
            <?php $finalPriceDiscount = $priceDiscount ?>
            <?php
                 if($i == 0) {
                     $ourPriceDiscount =  $ourPrice - (($defaultPreviewDiscount * $ourPrice) / 100);
                     $finalPriceDiscount = $ourPriceDiscount * $qty;
                 };
            ?>
            <?php $totalPrice += $finalPriceDiscount; ?>
            <div class="mgt-project-price-option">
                <div class="mgt-big-group-col-title">
                    <?php echo __($previewDiscount->getData('discount_title')) ?>
                </div>
                <div class="mgt-big-group-col-price">
                    <?php if($i == 0) : ?>
                        <span class="mgt-big-col-discount-price">
                            <?php echo $block->convertNumberToPrice($ourPrice); ?>
                        </span>
                    <?php elseif($designType == 1 && $i == 1) : ?>
                        <?php 
                            if($additionOptionPrice > 0) {
                                $additionOptionPrice = $additionOptionPrice + $previewDiscount->getData('discount_price');
                               
                            }
                             ?>
                        <span class="mgt-big-col-discount-price">
                             <?php echo $block->convertNumberToPrice($additionOptionPrice) ?>
                        </span>  
                    <?php else : ?>
                        <span class="mgt-big-col-discount-price">
                            <?php echo  $block->convertNumberToPrice($previewDiscount->getData('discount_price')); ?>
                        </span>
                    <?php endif; ?>
                    <span class="mgt-big-col-discount-price">
                        -<?php echo $defaultPreviewDiscount ?>%
                    </span>
                    <span class="mgt-big-col-discount-price">
                        <?php if($i == 0) : ?>
                         <?php echo $block->convertNumberToPrice($ourPriceDiscount); ?>
                        
                         <?php elseif($i == 1 && $designType == 1) : ?>
                                <?php $additionOptionPriceDiscount = $additionOptionPrice - (($additionOptionPrice * $defaultPreviewDiscount) / 100) ?>
                                <?php  $finalPriceDiscount = $additionOptionPriceDiscount * $qty; ?>
                                <?php echo $block->convertNumberToPrice($additionOptionPriceDiscount); ?>
                              
                        <?php else : ?>
                            <?php echo $block->convertNumberToPrice($priceDiscount); ?>
                        <?php endif; ?>
                    </span>
                    <span class="mgt-big-col-discount-price">
                        <?php echo $block->convertNumberToPrice($finalPriceDiscount) ?>
                    </span>
                </div>
            </div>
            <?php $i++; ?>

        <?php endforeach; ?>
    <?php else : ?>
        <?php foreach ($previewDiscounts as $previewDiscount) : ?>
            <?php $priceDiscount =  $previewDiscount->getData('discount_price') ?>
            <?php //if($customerGroup > 1) { $priceDiscount = $previewDiscount->getData('discount_price') - (($previewDiscount->getData('discount_amount') * $previewDiscount->getData('discount_price')) / 100); } ?>
            <?php $finalPriceDiscount = $priceDiscount ?>
            <?php if($i == 0) { $finalPriceDiscount = $ourPrice * $qty; }; ?>
            <?php $totalPrice += $finalPriceDiscount; ?>
            <div class="mgt-project-price-option">
                <div class="mgt-normally-coll-title">
                    <?php echo __($previewDiscount->getData('discount_title')) ?>
                </div>
                <div class="mgt-normally-coll-price">
                    <?php if($i == 0) : ?>
                        <span class="mgt-normally-col-our-price">
                             <?php echo $block->convertNumberToPrice($ourPrice) ?>
                        </span>
                    <?php elseif($designType == 1 && $i == 1) : ?>
                        <?php 
                            if($additionOptionPrice > 0) {
                                $additionOptionPrice = $additionOptionPrice + $previewDiscount->getData('discount_price');
                               
                            }
                            $finalPriceDiscount = $additionOptionPrice * $qty;
                             ?>
                        <span class="mgt-normally-col-our-price">
                             <?php echo $block->convertNumberToPrice($additionOptionPrice) ?>
                        </span>
                    <?php endif ?>
                    <span class="mgt-normally-col-last">
                        <?php echo $block->convertNumberToPrice($finalPriceDiscount) ?>
                    </span>
                </div>
            </div>
            <?php $i++; ?>
        <?php endforeach; ?>
    <?php endif; ?>
    <?php $priceIncudeTax = $totalPrice + ($totalPrice * $tax) / 100; ?>
    <div class="mgt-project-price-option" style="display: none;">
        <div class="mgt-project-price-option-col-button mgt-project-price-option-col-button-first">
            Versand
        </div>
        <div class="mgt-project-price-option-col-button">
            <?php echo $block->convertNumberToPrice(0) ?>
        </div>
    </div>
    <div class="mgt-project-price-option">
        <div class="mgt-project-price-option-col-button mgt-project-price-option-col-button-first">
            <b>Preis</b> (netto)
        </div>
        <div class="mgt-project-price-option-col-button">
            <?php echo $block->convertNumberToPrice($totalPrice) ?>
        </div>
    </div>
    <div class="mgt-project-price-option mgt-project-price-option-last">
        <div class="mgt-project-price-option-col-button mgt-project-price-option-col-button-first">
            <b>19.00 % MwSt.</b> (netto)
        </div>
        <div class="mgt-project-price-option-col-button">
            <?php echo $block->convertNumberToPrice($priceIncudeTax) ?>
        </div>
    </div>
<input type="hidden" name="final_price_discount_pro" value="<?php echo $priceIncudeTax; ?>">
<?php endif; ?>

