<?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('mgt_custom_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) {
    $defaultValue = $ourPrice *$qty;
    $additionOptionPrice = $block->getAdditionPriceByIds($paramsAdditionOptions,$defaultValue);
    // $additionOptionPriceDiscount = $additionOptionPrice - (($additionOptionPrice * $defaultPreviewDiscount) / 100);
}
//  echo $additionOptionPrice;
$shippingDailyId = (int)$block->getRequest()->getParam('txt_daily_shipping',0);
$dailyPrice = 0;
$dailyDays = 0;
$ourPriceDiscount = 0;
$shippingDaily = $block->getDailyShippingById($shippingDailyId);
if($shippingDaily != null && $shippingDaily->getId()) {
    $dailyPrice = $shippingDaily->getData('amount');
    $dailyDays = $shippingDaily->getData('delivery_days');
}
?>
<?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_title')=="Processing & Finishing" && $qty <4) ? 0 : $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;
                 }
//                 echo $i . ' ' . $previewDiscounts->getSize().'<br/>';
                /* else if($i == $previewDiscounts->getSize() - 1) {
                     $priceDiscount = ($ourPrice * $shippingDaily ) / 100;
                 }*/
            ?>

            <div class="mgt-project-price-option">
                <div class="mgt-big-group-col-title">
                    <?php //echo $i == $previewDiscounts->getSize() - 1 && $dailyDays > 0 ? __('Delivery time: %1 days plus shipping',$dailyDays) : __($previewDiscount->getData('discount_title')) ?>
                    <?php if($i == $previewDiscounts->getSize() - 1 && $dailyDays > 0) : ?>
                    <?php echo __('Delivery time:') ?> <span><?php echo $dailyDays; ?> </span><?php echo __('days plus shipping') ?>
                    <?php else : ?>
                        <?php echo  __($previewDiscount->getData('discount_title')); ?>
                    <?php endif; ?>
                </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
                        $additionOptionPrice = $additionOptionPrice + $previewDiscount->getData('discount_price');

                        ?>
                        <span class="mgt-big-col-discount-price">
                             <?php echo $block->convertNumberToPrice($additionOptionPrice) ?>
                        </span>
                    <?php elseif($i == $previewDiscounts->getSize() - 1 && $dailyPrice > 0) : ?>
                        <?php
                        $ourShippingPrice = ($ourPrice * $dailyPrice * $qty) / 100;
//                        $shippingPrice = $previewDiscount->getData('discount_price') + ($ourPrice * $dailyPrice ) / 100;
                         $shippingPrice = $previewDiscount->getData('discount_price') + $ourShippingPrice;
                        //$priceDiscount = $shippingPrice - (($defaultPreviewDiscount * $shippingPrice) / 100);
                        $priceDiscount = $shippingPrice;
                        $finalPriceDiscount = $priceDiscount;
                        if(($totalPrice+$finalPriceDiscount)< 50)
                        {
                            $shippingPrice = $shippingPrice + 4;
                            $priceDiscount = $shippingPrice;
                            $finalPriceDiscount = $priceDiscount;
                        }
                        ?>
                        <span class="mgt-big-col-discount-price">
                            <?php echo  $block->convertNumberToPrice($shippingPrice); ?>
                        </span>
                    <?php else : ?>
                        <span class="mgt-big-col-discount-price">
                            <?php echo  $block->convertNumberToPrice(($previewDiscount->getData('discount_title')=="Processing & Finishing" && $qty <4) ? 0 : $previewDiscount->getData('discount_price')); ?>
                        </span>
                    <?php endif; ?>
                    <span class="mgt-big-col-discount-price">
                        -<?php if($i == $previewDiscounts->getSize() - 1 && $dailyPrice > 0){ echo "0"; } else { 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; ?>
                                <?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 $totalPrice += $finalPriceDiscount; ?>
            <?php $i++; ?>

        <?php endforeach; ?>
        <input type="hidden" name="txt_preview_our_price" value="<?php echo $ourPriceDiscount; ?>">
    <?php else : ?>
        <?php foreach ($previewDiscounts as $previewDiscount) : ?>
            <?php $priceDiscount =  ($previewDiscount->getData('discount_title')=="Processing & Finishing" && $qty <4) ? 0 : $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; }
            if($i == $previewDiscounts->getSize() - 1) {
                $finalPriceDiscount = $previewDiscount->getData('discount_price') + ($ourPrice * $dailyPrice * $qty) / 100;
                if(($totalPrice+$finalPriceDiscount)< 50)
                {
                    $priceDiscount = $priceDiscount + 4;
                    $finalPriceDiscount = $finalPriceDiscount + 4;
                }
            }
            ?>
            <div class="mgt-project-price-option">
                <div class="mgt-normally-coll-title">
                    <?php //echo $i == $previewDiscounts->getSize() - 1 && $dailyDays > 0 ? __('Delivery time: %1 days plus shipping',$dailyDays) : __($previewDiscount->getData('discount_title')) ?>
                    <?php if($i == $previewDiscounts->getSize() - 1 && $dailyDays > 0) : ?>
                        <?php echo __('Delivery time:') ?> <span><?php echo $dailyDays; ?> </span><?php echo __('days plus shipping') ?>
                    <?php else : ?>
                        <?php echo  __($previewDiscount->getData('discount_title')); ?>
                    <?php endif; ?>
                </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
                            $additionOptionPrice = $additionOptionPrice + $previewDiscount->getData('discount_price');
                            $finalPriceDiscount = $additionOptionPrice;
                             ?>
                        <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 $totalPrice += $finalPriceDiscount; ?>
            <?php $i++; ?>
        <?php endforeach; ?>
        <input type="hidden" name="txt_preview_our_price" value="<?php echo $ourPrice; ?>">
    <?php endif; ?>
    <?php $taxAmount = ($totalPrice * $tax) / 100; ?>
    <?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">
            <?php echo __('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">
            <span><strong><?php echo __('Price') ?></strong></span><span><?php echo __(' (net)') ?></span>
        </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">
            19.00 % <?php echo __('VAT') ?>
        </div>
        <div class="mgt-project-price-option-col-button">
            <?php echo $block->convertNumberToPrice($taxAmount) ?>
        </div>
    </div>
    <div class="mgt-project-price-option preview-total-price-content">
        <div class="preview-total-price-text">
            <b><?php echo __('Total price') ?></b>
        </div>
        <div class="mgt-project-price-option-col-button" id="preview-total-price-value">
            <?php echo $block->convertNumberToPrice($priceIncudeTax) ?>
        </div>
    </div>
<input type="hidden" name="final_price_discount_pro" value="<?php echo $priceIncudeTax; ?>">
<input type="hidden" name="custom_our_price" value="<?php echo $ourPriceDiscount; ?>">
<?php endif; ?>

<?php foreach ($previewDiscounts as $previewDiscount) : ?>
    <input type="hidden" name="ict_discounts_price[<?php echo $previewDiscount->getId(); ?>]" value="<?php echo $previewDiscount->getData('discount_price'); ?>">
<?php endforeach; ?>

