<?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 */
$mediaUrl = $block->getMediaUrl(). 'advancepreview/icons/';
$_product = $block->getPreviewProductData();
$productId = $_product->getId();
$previewModels = $block->getPreviewByProductId($_product->getId());
$namePreviewModel = '';
$texture =  '';
$light = 0;
$zoom = 0;
$textureWidth = 0;
$textureHeight = 0;
$folderPreview = '';
if($previewModels && $previewModels->getSize() > 0) {
    $namePreviewModel = $previewModels->getFirstItem()->getData('file_preview');
    $texture = $previewModels->getFirstItem()->getData('texture');
    $light = $previewModels->getFirstItem()->getData('preview_light');
    $zoom = $previewModels->getFirstItem()->getData('preview_zoom');
    $textureWidth = $previewModels->getFirstItem()->getData('texture_width');
    $textureHeight = $previewModels->getFirstItem()->getData('texture_height');
    $folderPreview = $previewModels->getFirstItem()->getData('folder_preview');
    if($folderPreview != '') {
        $folderPreview = 'model-'.$folderPreview;
    }
}
// pattert file for download and upload

$samplePdfFile = $block->getSamplePdfFile($_product->getId());
$sampleAiFile = $block->getSampleAiFile($_product->getId());
$isUpdateDesignOrder = (int)$block->getRequest()->getParam('re_up',0);
$reupItemId = 0;
$reupItem = null;
$customerUploadDesign = '';
$orderId = 0;
// just preview
$previewOrderDesignId = (int)$block->getRequest()->getParam('preview_design',0);
$previewDesignOrder = 0;
if($isUpdateDesignOrder == 0 && $previewOrderDesignId > 0) {
    $previewDesignOrder = 1;
    $isUpdateDesignOrder = $previewOrderDesignId;
}
if($isUpdateDesignOrder > 0) {
    $reupItem = $block->getOrderItem($isUpdateDesignOrder);
    if($reupItem && $reupItem->getItemId() > 0) {
        $reupItemId = $reupItem->getItemId();
        $previewOrderItem = $block->getPreviewItemByItemId($reupItemId);
        $requestOption = $reupItem->getProductOptionByCode('info_buyRequest');;
        $folderPreview = 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';
        $customerUploadDesign = isset($requestOption['customer_upload_design']) ? trim($requestOption['customer_upload_design']) : '';
        $orderId = $reupItem->getOrderId();
        if(trim($previewOrderItem->getData('preview_model')) != '') {
            $folderPreview = $previewOrderItem->getData('preview_model');
        }

    }
}
$iframeUrl = $block->getUrl('advance_preview/newPreview/index');
$is3D = 0;
if(($_product->getData('pcto_is_3d') == 1 || $_product->getData('pcto_cust_design') == 1) && $namePreviewModel != '' && $folderPreview != '') {
    $is3D = 1;
}
$pcto_cust_design = 0;
if($_product->getData('pcto_cust_design') == 1) {
    $pcto_cust_design = 1;
}
?>

<input type="hidden" id="txt-is-3d" value="<?php echo $is3D ?>">
<input type="hidden" id="txt-is-pcto-cust-design" value="<?php echo $pcto_cust_design ?>">
<!-- Template code  -->
<?php if($namePreviewModel != '' && ($_product->getData('pcto_is_3d') == 1 || $_product->getData('pcto_cust_design') == 1) && $folderPreview != '') : ?>
    <?php $previewParam = "name_preview/{$namePreviewModel}/model_preview/{$folderPreview}/product_id/{$productId}"; ?>
    <input type="hidden" id="txt-default-preview-name" value="<?php echo $namePreviewModel; ?>">
    <input type="hidden" id="txt-default-iframe-url" value="<?php echo $iframeUrl; ?>">
    <input type="hidden" id="txt-preview-sample-pdf-file" value="<?php echo $samplePdfFile; ?>">
    <input type="hidden" id="txt-preview-sample-ai-file" value="<?php echo $sampleAiFile; ?>">
    <input type="hidden" id="txt-design-item-order-view-url" value="<?php echo $block->getUrl('sales/order/view/order_id/'.$orderId); ?>">
    <!--Upload custom design loading-->
    <div id="preview-upload-custom-design" style="display: none">
        <img src="<?php echo $block->getMediaUrl().'advancepreview/icons/loading_3d.gif' ?>" alt="loading" />
    </div>
    <?php
        if ($_product->getData('pcto_is_3d') == 1) { ?>
            <iframe id="iframe-preview-pro" src="<?php echo $iframeUrl.$previewParam; ?>" title="HIhi" style="width: 100%; height: 500px;"></iframe>
    <?php } else{?>
        <iframe id="iframe-preview-pro" src="<?php echo $iframeUrl.$previewParam; ?>" title="HIhi" style="display: none"></iframe>
    <?php }?>
    
    <div class="mgt-project-preview-actions" <?php if($previewDesignOrder) { echo 'style="display: none"'; } ?>>
        <div class="mgt-project-preview-action-left">
            <div id="btn-prview-standrad-design" <?php if($reupItemId > 0) { ?> style="display: none" <?php } ?>>
                <div class="primary actions-primary">
                    <button type="submit" class="action submit primary"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 576 512"><!--! Font Awesome Pro 6.0.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) Copyright 2022 Fonticons, Inc. --><path d="M224 263.3C224.2 233.3 238.4 205.2 262.4 187.2L499.1 9.605C517.7-4.353 543.6-2.965 560.7 12.9C577.7 28.76 580.8 54.54 568.2 74.07L406.5 324.1C391.3 347.7 366.6 363.2 339.3 367.1L224 263.3zM320 400C320 461.9 269.9 512 208 512H64C46.33 512 32 497.7 32 480C32 462.3 46.33 448 64 448H68.81C86.44 448 98.4 429.1 96.59 411.6C96.2 407.8 96 403.9 96 400C96 339.6 143.9 290.3 203.7 288.1L319.8 392.5C319.9 394.1 320 397.5 320 400V400z"/></svg><span><?php echo __('Standard Design') ?></span></button>
                </div>
            </div>
            <div id="btn-prview-own-design" <?php if($reupItemId > 0) { ?> style="display: none" <?php } ?>>
                <div class="primary actions-primary">
                    <button type="submit" class="action submit primary unselected"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Pro 6.0.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) Copyright 2022 Fonticons, Inc. --><path d="M492.7 42.75C517.7 67.74 517.7 108.3 492.7 133.3L436.3 189.7L322.3 75.72L378.7 19.32C403.7-5.678 444.3-5.678 469.3 19.32L492.7 42.75zM44.89 353.2L299.7 98.34L413.7 212.3L158.8 467.1C152.1 473.8 143.8 478.7 134.6 481.4L30.59 511.1C22.21 513.5 13.19 511.1 7.03 504.1C.8669 498.8-1.47 489.8 .9242 481.4L30.65 377.4C33.26 368.2 38.16 359.9 44.89 353.2zM249.4 103.4L103.4 249.4L16 161.9C-2.745 143.2-2.745 112.8 16 94.06L94.06 16C112.8-2.745 143.2-2.745 161.9 16L181.7 35.76C181.4 36.05 181 36.36 180.7 36.69L116.7 100.7C110.4 106.9 110.4 117.1 116.7 123.3C122.9 129.6 133.1 129.6 139.3 123.3L203.3 59.31C203.6 58.99 203.1 58.65 204.2 58.3L249.4 103.4zM453.7 307.8C453.4 308 453 308.4 452.7 308.7L388.7 372.7C382.4 378.9 382.4 389.1 388.7 395.3C394.9 401.6 405.1 401.6 411.3 395.3L475.3 331.3C475.6 330.1 475.1 330.6 476.2 330.3L496 350.1C514.7 368.8 514.7 399.2 496 417.9L417.9 496C399.2 514.7 368.8 514.7 350.1 496L262.6 408.6L408.6 262.6L453.7 307.8z"/></svg><span><?php echo __('Your design') ?></span></button>
                </div>
            </div>
        </div>
        <div class="mgt-project-preview-action-middle" <?php if($reupItemId <= 0) { ?> style="display: none" <?php } ?>>
            <div class="mgt-project-preview-action-middle-one-icon">
                <div class="primary actions-primary">
                    <button type="button" class="action submit primary" id="btn-own-design-pdf-download"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 384 512"><!--! Font Awesome Pro 6.0.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) Copyright 2022 Fonticons, Inc. --><path d="M184 208c0-4.406-3.594-8-8-8S168 203.6 168 208c0 2.062 .2969 23.31 9.141 50.25C179.1 249.6 184 226.2 184 208zM256 0v128h128L256 0zM80 422.4c0 9.656 10.47 11.97 14.38 6.375C99.27 421.9 108.8 408 120.1 388.6c-14.22 7.969-27.25 17.31-38.02 28.31C80.75 418.3 80 420.3 80 422.4zM224 128L224 0H48C21.49 0 0 21.49 0 48v416C0 490.5 21.49 512 48 512h288c26.51 0 48-21.49 48-48V160h-127.1C238.3 160 224 145.7 224 128zM292 312c24.26 0 44 19.74 44 44c0 24.67-18.94 44-43.13 44c-5.994 0-11.81-.9531-17.22-2.805c-20.06-6.758-38.38-15.96-54.55-27.39c-23.88 5.109-45.46 11.52-64.31 19.1c-14.43 26.31-27.63 46.15-36.37 58.41C112.1 457.8 100.8 464 87.94 464C65.92 464 48 446.1 48 424.1c0-11.92 3.74-21.82 11.18-29.51c16.18-16.52 37.37-30.99 63.02-43.05c11.75-22.83 21.94-46.04 30.33-69.14C136.2 242.4 136 208.4 136 208c0-22.05 17.95-40 40-40c22.06 0 40 17.95 40 40c0 24.1-7.227 55.75-8.938 62.63c-1.006 3.273-2.035 6.516-3.082 9.723c7.83 14.46 17.7 27.21 29.44 38.05C263.1 313.4 284.3 312.1 287.6 312H292zM156.5 354.6c17.98-6.5 36.13-11.44 52.92-15.19c-12.42-12.06-22.17-25.12-29.8-38.16C172.3 320.6 164.4 338.5 156.5 354.6zM292.9 368C299 368 304 363 304 356.9C304 349.4 298.6 344 292 344H288c-.3438 .0313-16.83 .9687-40.95 4.75c11.27 7 24.12 13.19 38.84 18.12C288 367.6 290.5 368 292.9 368z"/></svg><span><?php echo __('Download PDF') ?></span></button>
                </div>

                <div class="primary actions-primary">
                    <button type="submit" class="action submit primary" id="btn-own-design-ai-download"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Pro 6.0.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) Copyright 2022 Fonticons, Inc. --><path d="M512 255.1C512 256.9 511.1 257.8 511.1 258.7C511.6 295.2 478.4 319.1 441.9 319.1H344C317.5 319.1 296 341.5 296 368C296 371.4 296.4 374.7 297 377.9C299.2 388.1 303.5 397.1 307.9 407.8C313.9 421.6 320 435.3 320 449.8C320 481.7 298.4 510.5 266.6 511.8C263.1 511.9 259.5 512 256 512C114.6 512 0 397.4 0 256C0 114.6 114.6 0 256 0C397.4 0 512 114.6 512 256V255.1zM96 255.1C78.33 255.1 64 270.3 64 287.1C64 305.7 78.33 319.1 96 319.1C113.7 319.1 128 305.7 128 287.1C128 270.3 113.7 255.1 96 255.1zM128 191.1C145.7 191.1 160 177.7 160 159.1C160 142.3 145.7 127.1 128 127.1C110.3 127.1 96 142.3 96 159.1C96 177.7 110.3 191.1 128 191.1zM256 63.1C238.3 63.1 224 78.33 224 95.1C224 113.7 238.3 127.1 256 127.1C273.7 127.1 288 113.7 288 95.1C288 78.33 273.7 63.1 256 63.1zM384 191.1C401.7 191.1 416 177.7 416 159.1C416 142.3 401.7 127.1 384 127.1C366.3 127.1 352 142.3 352 159.1C352 177.7 366.3 191.1 384 191.1z"/></svg><span><?php echo __('Download AI') ?></span></button>
                </div>

            </div>
        </div>
        <div class="mgt-project-preview-action-right"  <?php if($reupItemId <= 0) { ?> style="display: none" <?php } ?>>
            <div class="primary actions-primary">
                <!-- <button type="submit" class="action submit primary"><span>Upload your design</span></button> -->
                <?php if($reupItemId > 0 && $reupItem != null) : ?>
                    <div class="yourdesign-upload-btn-wrapper">
                        <form action="/" method="post" id="form-upload-custom-design" enctype="multipart/form-data" >
                            <button class="action primary" id="btn-txt-order-item-re-up-design"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path d="M105.4 182.6c12.5 12.49 32.76 12.5 45.25 .001L224 109.3V352c0 17.67 14.33 32 32 32c17.67 0 32-14.33 32-32V109.3l73.38 73.38c12.49 12.49 32.75 12.49 45.25-.001c12.49-12.49 12.49-32.75 0-45.25l-128-128C272.4 3.125 264.2 0 256 0S239.6 3.125 233.4 9.375L105.4 137.4C92.88 149.9 92.88 170.1 105.4 182.6zM480 352h-160c0 35.35-28.65 64-64 64s-64-28.65-64-64H32c-17.67 0-32 14.33-32 32v96c0 17.67 14.33 32 32 32h448c17.67 0 32-14.33 32-32v-96C512 366.3 497.7 352 480 352zM432 456c-13.2 0-24-10.8-24-24c0-13.2 10.8-24 24-24s24 10.8 24 24C456 445.2 445.2 456 432 456z"/></svg><span><?php echo __('Upload your design') ?></span></button>
                            <input type="hidden" name="product_id" value="<?php echo $_product->getId(); ?>">
                            <input type="hidden" name="texture" value="<?php echo $texture; ?>">
                            <input type="hidden" name="texture_width" value="<?php echo $textureWidth; ?>">
                            <input type="hidden" name="texture_height" value="<?php echo $textureHeight; ?>">
                            <input type="hidden" name="own_folder" value="<?php echo $folderPreview; ?>">
                            <input type="hidden" name="original_file" value="<?php echo $customerUploadDesign; ?>">
                            <input type="hidden" name="zoom" value="<?php echo $zoom; ?>">
                            <input type="hidden" name="light" value="<?php echo $light ?>">
                            <input type="hidden" name="name_preview" value="<?php echo $namePreviewModel ?>">
                            <input id="txt-order-item-re-up-design" type="file" name="custom_preview" value="" accept=".pdf, .ai">
                            <input type="hidden" name="item_id" value="<?php echo $reupItemId; ?>">
                            <input type="hidden" name="is_reup_product" value="1">
                        </form>
                    </div>
                <?php else : ?>
                    <div class="yourdesign-upload-btn-wrapper">
                        <form action="/" method="post" id="form-upload-custom-design" enctype="multipart/form-data" >
                            <button class="action primary"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Pro 6.0.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) Copyright 2022 Fonticons, Inc. --><path d="M105.4 182.6c12.5 12.49 32.76 12.5 45.25 .001L224 109.3V352c0 17.67 14.33 32 32 32c17.67 0 32-14.33 32-32V109.3l73.38 73.38c12.49 12.49 32.75 12.49 45.25-.001c12.49-12.49 12.49-32.75 0-45.25l-128-128C272.4 3.125 264.2 0 256 0S239.6 3.125 233.4 9.375L105.4 137.4C92.88 149.9 92.88 170.1 105.4 182.6zM480 352h-160c0 35.35-28.65 64-64 64s-64-28.65-64-64H32c-17.67 0-32 14.33-32 32v96c0 17.67 14.33 32 32 32h448c17.67 0 32-14.33 32-32v-96C512 366.3 497.7 352 480 352zM432 456c-13.2 0-24-10.8-24-24c0-13.2 10.8-24 24-24s24 10.8 24 24C456 445.2 445.2 456 432 456z"/></svg><span><?php echo __('Upload your design') ?></span></button>
                            <input type="hidden" name="product_id" value="<?php echo $_product->getId(); ?>">
                            <input type="hidden" name="texture" value="<?php echo $texture; ?>">
                            <input type="hidden" name="texture_width" value="<?php echo $textureWidth; ?>">
                            <input type="hidden" name="texture_height" value="<?php echo $textureHeight; ?>">
                            <input id="txt-preview-upload-custom-design" type="file" name="custom_preview" accept=".pdf, .ai" />
                        </form>
                    </div>
                <?php endif; ?>
                    <?php // Download button custom design for non 3d product start
                        if ($_product->getData('pcto_cust_design') == 1) { ?>
                            <div id="cust-download-image" style="display: none; text-align: center; width: 200px;">
                                <a id="cust-download-image-link" href='' download><?php echo __('Download the file') ?></a>
                            </div>                            
                    <?php } // Download button custom design for non 3d product end ?>
            </div>
        </div>
        <div id="preview-standard-list-designs" <?php if($reupItemId > 0) { ?> style="display: none" <?php } ?>>
            <?php foreach ($previewModels as $previewModel) : ?>
                <div class="primary actions-primary" style="">
                    <?php
                   /* $texture = $previewModel->getData('texture');
                    $light = $previewModel->getData('preview_light');
                    $zoom = $previewModel->getData('preview_zoom');
                    $textureWidth = $previewModel->getData('texture_width');
                    $textureHeight = $previewModel->getData('texture_height');*/
                    $namePreviewModelItem = $previewModel->getData('file_preview');
                    $folderPreviewItem = $previewModel->getData('folder_preview');

                    if($folderPreviewItem != '') {
                        $folderPreviewItem = 'model-'.$folderPreviewItem;
                    }
                    $previewParamItem = $iframeUrl."model_preview/{$folderPreviewItem}/product_id/{$productId}/name_preview/{$namePreviewModelItem}";
                    $active =$previewModel->getData('is_default') == 1 ? '1' : '0';
                    ?>
                    <img
                        class="img-preview-standard-model-item"
                        src="<?php echo $block->getMediaUrl(). 'advancepreview/custom_previews/product_id_'.$_product->getId().'/model-thum-'.$previewModel->getData('folder_preview').'/'.$previewModel->getData('thumbnail') ?>"
                        full-preview-model="<?php echo $previewParamItem ?>"
                        preview-model="<?php echo $folderPreviewItem ?>"
                        is-loadder="<?php echo $active ?>"
                    />
                </div>
            <?php endforeach; ?>
        </div>
    </div>


<?php endif; ?>