<?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) {
    $additionOptionPrice = $block->getAdditionPriceByIds($paramsAdditionOptions);
}
//  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');
}
$designServiceRequest = [];

//kien
$product_discount = \Magento\Framework\App\ObjectManager::getInstance()->create('Magento\Catalog\Model\Product')->load($productId)->getProductDiscount()/100;
?>
<?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;
                 }
//                 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 + $ourPrice * $product_discount); ?>
                        </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 + $additionOptionPrice * $product_discount) ?>
                        </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);
                        $finalPriceDiscount = $priceDiscount;
                        ?>
                        <span class="mgt-big-col-discount-price">
                            <?php echo  $block->convertNumberToPrice($shippingPrice + $shippingPrice * $product_discount); ?>
                        </span>
                    <?php else : ?>
                        <span class="mgt-big-col-discount-price">
                            <?php echo  $block->convertNumberToPrice($previewDiscount->getData('discount_price') + $previewDiscount->getData('discount_price') * $product_discount); ?>
                        </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 + $ourPriceDiscount * $product_discount); ?>

                         <?php elseif($i == 1 && $designType == 1) : ?>
                                <?php $additionOptionPriceDiscount = $additionOptionPrice - (($additionOptionPrice * $defaultPreviewDiscount) / 100) ?>
                                <?php  $finalPriceDiscount = $additionOptionPriceDiscount; ?>
                                <?php echo $block->convertNumberToPrice($additionOptionPriceDiscount + $additionOptionPriceDiscount * $product_discount); ?>

                        <?php else : ?>
                            <?php echo $block->convertNumberToPrice($priceDiscount + $priceDiscount * $product_discount); ?>
                        <?php endif; ?>
                    </span>
                    <span class="mgt-big-col-discount-price">
                        <?php echo $block->convertNumberToPrice($finalPriceDiscount + $finalPriceDiscount * $product_discount) ?>
                    </span>
                    <?php
                    $discountTitle = $previewDiscount->getData('discount_title');
                    $discountTitle = str_replace(' ','_',$discountTitle);
                    $designServiceRequest[$discountTitle] = $finalPriceDiscount;
                    ?>
                </div>
            </div>
            <?php $totalPrice += $finalPriceDiscount; ?>
            <?php $i++; ?>

        <?php endforeach; ?>
        <input type="hidden" name="txt_preview_our_price" value="<?php echo $ourPriceDiscount + $ourPriceDiscount * $product_discount; ?>">
    <?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; }
            if($i == $previewDiscounts->getSize() - 1) {
                $finalPriceDiscount = $previewDiscount->getData('discount_price') + ($ourPrice * $dailyPrice * $qty) / 100;
            }
            ?>
            <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 + $ourPrice * $product_discount) ?>
                        </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 + $additionOptionPrice * $product_discount) ?>
                        </span>
                    <?php endif ?>
                    <span class="mgt-normally-col-last">
                        <?php echo $block->convertNumberToPrice($finalPriceDiscount + $finalPriceDiscount * $product_discount) ?>
                    </span>
                </div>
            </div>
            <?php $totalPrice += $finalPriceDiscount; ?>
            <?php $i++; ?>
        <?php endforeach; ?>
        <input type="hidden" name="txt_preview_our_price" value="<?php echo $ourPrice + $ourPrice * $product_discount; ?>">
    <?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 + $totalPrice * $product_discount) ?>
        </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 + $taxAmount * $product_discount) ?>
        </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 + $priceIncudeTax * $product_discount) ?>
        </div>
    </div>
<input type="hidden" name="final_price_discount_pro" value="<?php echo $priceIncudeTax + $priceIncudeTax * $product_discount; ?>">
<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; ?>

<input type="hidden" id="txt-preview-list-discount-price" name="txt_preview_list_discount_price" value="<?php echo htmlentities(json_encode($designServiceRequest)); ?>">
