<?php
/**@var \Magetop\Advancepreview\Block\Order\CustomOrderView $block */
$orderId = $block->getRequest()->getParam('order_id',0);
$imageHelper = $this->helper('Magento\Catalog\Helper\Image');
$order = $block->getData('order');
$items = $order->getAllItems();
$customerGroup = $block->getCustomerGroup();
$tax = 19;
$grandTotal = 0;
$customerId = $order->getCustomerId();
$statusLabels = [
    'new'=>'New',
    'process'=>'In progress',
    'shipping'=>'Ready for shipping',
    'shipped'=>'Shipped'
];
$statusCustomPrintLabels = [
    'new'=>'New',
    'downloading'=>'Checking design by Myfitmix',
    'design_failed'=>'Design failed',
    'design_approved'=>'Design approved',
    'customer_reject'=>'Customer reject',
    'design_full_approved'=>'Design fully approved',
    'process'=>'In progress',
    'shipping'=>'Ready for shipping/production',
    'shipped'=>'Shipped'
];
$statusCustomNoPrintLabels = [
    'new'=>'New',
    'downloading'=>'Checking design by Myfitmix',
    'design_failed'=>'Design failed',
    'design_approved'=>'Design approved',
    'customer_reject'=>'Customer reject',
    'design_full_approved'=>'Design fully approved',
    'process'=>'In progress',
    'shipping'=>'Ready for shipping/production',
    'shipped'=>'Shipped'
];
$item = $block->getData('item');
?>
<?php
/** @var  \Magento\Sales\Model\Order\Item $item*/
$productId = $item->getProduct()->getId();
$_product = $block->getProductById($productId);
$previewOrderItem = $block->getPreviewItemByItemId($item->getId());
$status = '';
if($previewOrderItem != null) {
    $status = $previewOrderItem->getData('status');
}
$requestOption = $block->getCustomItemDetail($item);
$tierPrice = $block->getTierPriceByProductId($_product);
$tierQtyPrice = $block->getTierPriceItemByQty($tierPrice, $item->getQtyOrdered());
/*$ourPrice = $block->getCustomTierOurPrice($_product,$tierQtyPrice, $customerId);
$ourPriceRow = $ourPrice * $item->getQtyOrdered();
$customDiscounts = $block->getPreviewDiscounts($productId);*/
$customDiscounts = isset($requestOption['txt_preview_list_discount_price']) ? $requestOption['txt_preview_list_discount_price'] : '';
if($customDiscounts != '') {
    $customDiscounts = (array)json_decode($customDiscounts);
}
else {
    $customDiscounts = [];
}
$ourPriceRow = isset($customDiscounts['our_price']) ? $customDiscounts['our_price'] : 0;
$ourPrice = $ourPriceRow / $item->getQtyOrdered();
$total = $ourPriceRow;
$designType = isset($requestOption['txt_custom_preview_design_type']) ? $requestOption['txt_custom_preview_design_type'] : 0;
$customerUploadDesign = isset($requestOption['customer_upload_design']) ? trim($requestOption['customer_upload_design']) : '';
$modelPreview = isset($requestOption['txt_preview_selected']) ? $requestOption['txt_preview_selected'] : 0;
$gernerateProjoectId = isset($requestOption['text_project_generate']) ? $requestOption['text_project_generate'] : 0;
$designOrderStatus = 'Ordered with standard design';
if(isset($requestOption['txt_is_request_sample']) && $requestOption['txt_is_request_sample'] == 1) {
    $designOrderStatus = 'Ordered for samples';
}
else if($designType == 1) {
    $designOrderStatus = 'Ordered with custom design';
    $customWithExtraCost = isset($requestOption['preview_additional_options']) ? $requestOption['preview_additional_options'] : [];
    if(count($customWithExtraCost)) {
        $designOrderStatus = 'Ordered with custom design extra costs';
    }

}
if($gernerateProjoectId > 0) {
    if($gernerateProjoectId > 1000) {
        $gernerateProjoectId = $gernerateProjoectId;
    }
    else if($gernerateProjoectId > 100) {
        $gernerateProjoectId = '0'.$gernerateProjoectId;
    }
    else if($gernerateProjoectId > 10) {
        $gernerateProjoectId = '00'.$gernerateProjoectId;
    }
    else {
        $gernerateProjoectId = '000'.$gernerateProjoectId;
    }
}
else {
    $gernerateProjoectId = '';
}
$projectName = isset($requestOption['custom_project_name']) ? $requestOption['custom_project_name'] : '';
/* Changed by Nalini*/
// if($gernerateProjoectId != '') {
//     $projectName = $gernerateProjoectId . ' - ' .$projectName;
// }
//            echo $modelPreview;
$previewModels = $block->getPreviewByProductId($productId);
/*$light = 0;
$namePreviewModel = '';
$zoom = 0;
$texture =  '';
$textureWidth = 0;
$textureHeight = 0;
if($previewModels && $previewModels->getSize() > 0) {
    $namePreviewModel = $previewModels->getFirstItem()->getData('file_preview');
    $light = $previewModels->getFirstItem()->getData('preview_light');
    $zoom = $previewModels->getFirstItem()->getData('preview_zoom');
    $texture = $previewModels->getFirstItem()->getData('texture');
    $zoom = $previewModels->getFirstItem()->getData('preview_zoom');
    $textureWidth = $previewModels->getFirstItem()->getData('texture_width');
    $textureHeight = $previewModels->getFirstItem()->getData('texture_height');
}*/
$modelPreview = isset($requestOption['txt_preview_selected']) ? $requestOption['txt_preview_selected'] : '';
$namePreviewModel = isset($requestOption['txt_preview_file_selected']) ? $requestOption['txt_preview_file_selected'] : '';
$light = isset($requestOption['txt_preview_light_selected']) ? (int)$requestOption['txt_preview_light_selected'] : 0;
$zoom = isset($requestOption['txt_preview_zoom_selected']) ? (int)$requestOption['txt_preview_zoom_selected'] : 0;
$textureWidth =  isset($requestOption['txt_preview_width_selected']) ? (int)$requestOption['txt_preview_width_selected'] : 0;
$textureHeight =  isset($requestOption['txt_preview_height_selected']) ? (int)$requestOption['txt_preview_height_selected'] : 0;
$texture =  isset($requestOption['txt_preview_texture_selected']) ? $requestOption['txt_preview_texture_selected'] : '0';
$is3D = false;
if($block->checkProductEnable3D($_product) && $modelPreview != '' && $namePreviewModel != '' && $light > 0 && $zoom > 0) {
    $is3D = true;
}
// custom shipping
$dailyId = isset($requestOption['txt_daily_shipping']) ? (int)$requestOption['txt_daily_shipping'] : 0;
$dailyShippigAmount = 0;
$dailyDays = 0;
if($dailyId > 0) {
    $dailyShippig = $block->getDailyShippingById($dailyId);
    $dailyShippigAmount = $dailyShippig != null && $dailyShippig->getData('amount') > 0 ? $dailyShippig->getData('amount') : 0;
    $dailyDays = $dailyShippig != null && $dailyShippig->getData('delivery_days') > 0 ? $dailyShippig->getData('delivery_days') : 0;
}
$paramAdditionalOptions = isset($requestOption['preview_additional_options']) ? $requestOption['preview_additional_options'] : [];
?>
<tr style="width:100%">
    <td style="width: 32%;">
        <span><p style="margin-bottom:0">&nbsp;</p></span>
        <?php
        $image = $imageHelper->init($_product, 'product_page_image_small')->keepAspectRatio(true)->resize(200, 200)->getUrl();
        ?>
        <img src="<?php echo $image;?>" alt="<?php echo $_product->getName() ?>" type="<?php echo $_product->getName() ?>">
    </td>
    <td style="width: 24%">
        <span><p style="margin-bottom:0">&nbsp;</p></span>
        <span class="pc-item-info"><?php echo $projectName ?></span><br/>
        <span class="pc-item-info"><a href="<?php echo $_product->getProductUrl() ?>"><?php echo $_product->getName(); ?></a> </span><br/>
        <span class="pc-item-info"><?php echo $_product->getSku(); ?></span><br/>
        <span class="pc-item-info"><?php echo __($designOrderStatus) ?></span><br/>
        <span class="pc-item-info"><?php echo $_product->getData('short_description'); ?></span><br/>
        <?php if(count($paramAdditionalOptions)) : ?>
            <?php $addionLables = $block->getPreviewAdditionalOptionLabels($paramAdditionalOptions); ?>
            <span class="pc-item-info"><?php echo __($addionLables); ?></span>
        <?php endif; ?>
    </td>
    <td style="width:32%">
        <span><p style="margin-bottom:0">&nbsp;</p></span>
        <span class="quantity"><?php echo (int)$item->getQtyOrdered() ?></span><br/>
        <span class="pc-item-info"><?php echo (int)$item->getQtyOrdered(); ?> x <?php echo $block->convertNumberToPrice($ourPrice); ?></span><br/>
        <?php $i = 0;
        $customOptions = [];
        ?>
        <?php foreach ($customDiscounts as $akt => $customDiscount) : ?>
      <?php foreach ($customDiscounts as $akt => $customDiscount) : ?>
            <?php if($i == 0) { $i++; continue; } ?>
        <?php foreach ($customDiscounts as $akt => $customDiscount) : ?>
            <?php if($i == 0) { $i++; continue; } ?>
            <?php
//            $priceDiscountItem = $customDiscount->getData('discount_price');
//            $discountTitle = $customDiscount->getData('discount_title');
            $txtDiscountTitle = str_replace('_',' ',$akt);
            $priceDiscountItem = $customDiscount;
            $priceAfterDiscont = $customDiscount;
            if($designType == 1 && $i == 1) {


//                $priceDiscountItem += $block->getAdditionalOptionPrice($paramAdditionalOptions, $productId);
            }
            else if($dailyShippigAmount > 0 && $i == count($customDiscounts) - 1) {
                // if shipping
//                $shippingCharge = (($tierQtyPrice * $dailyShippigAmount * $item->getQtyOrdered()) / 100);
//                $priceDiscountItem += $shippingCharge;
                $txtDiscountTitle = __('Delivery time: %1 days plus shipping',$dailyDays);
            }
//            $priceAfterDiscont = $block->getAddDiscountAdditionAllPrice($priceDiscountItem, $_product, $customerId);

            $total += $priceAfterDiscont;
            $customOptions[] = [
                'title'=>$txtDiscountTitle,
                'title'=>$txtDiscountTitle,

                'price'=>$priceAfterDiscont,
            ];
            $i++; ?>
        <?php endforeach; ?>
        <?php if(count($customOptions)) : ?>
            <?php foreach ($customOptions as $customOption) : ?>
                <span><?php echo __($customOption['title']); ?></span><br/>
            <?php endforeach; ?>
        <?php endif; ?>
        <?php $txtTax = number_format((float)$tax, 2, '.', ''); ?>
        <span class="pc-item-info"><?php echo __('19.00 % VAT', $tax) ?></span>
        <br/><br/>
        <span class="pc-item-info"><b><?php echo __('Total') ?></b></span>
    </td>
    <td style="text-align: right;">
        <span><p style="margin-bottom:0">&nbsp;</p></span>
        <span style="text-align: right;">PCS</span><br>
        <span style="text-align: right;"><?php echo $block->convertNumberToPrice($ourPriceRow) ?></span><br/>
        <?php if(count($customOptions)) : ?>
            <?php foreach ($customOptions as $customOption) : ?>
                <span style="text-align: right;"><?php echo $block->convertNumberToPrice($customOption['price']); ?></span><br/>
            <?php endforeach; ?>
        <?php endif; ?>
        <?php $taxTotal = (($total * $tax) / 100); // add tax ?>
        <?php $total = $total + $taxTotal; // add tax ?>
        <span style="text-align: right;"><?php echo $block->convertNumberToPrice($taxTotal) ?></span><br/><br/>
        <span style="text-align: right;"><b><?php echo $block->convertNumberToPrice($total) ?><b></span><br/>
    </td>
</tr>
