<?php
/**
 * Maho
 *
 * @category    design
 * @package     default_default
 * @copyright   Copyright (c) 2006-2020 Magento, Inc. (https://magento.com)
 * @copyright   Copyright (c) 2021-2022 The OpenMage Contributors (https://openmage.org)
 * @copyright   Copyright (c) 2024 Maho (https://mahocommerce.com)
 * @license     https://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
?>
<style type="text/css" >
    ul { list-style-type:none; padding:0; margin:0; }
    li { margin-left:0; border:1px solid #ccc; margin:2px; padding:2px 2px 2px 2px; font:normal 12px sans-serif; }
    img { margin-right:5px; }
</style>
<script type="text/javascript">
    var FORM_KEY = "<?= $this->getFormKey() ?>";
</script>
    <ul>
        <li>
        <?php if ($this->getProfile()->getId()):?>
            <img src="<?= $this->getSkinUrl('images/note_msg_icon.gif') ?>" class="v-middle" style="margin-right:5px"/>
            <?= $this->__("Starting profile execution, please wait...") ?>
            </li>
            <li style="background-color:#FFD;">
            <img src="<?= $this->getSkinUrl('images/fam_bullet_error.gif') ?>" class="v-middle" style="margin-right:5px"/>
            <?= $this->__("Warning: Please do not close the window during importing/exporting data") ?>
        <?php else: ?>
            <img src="<?= $this->getSkinUrl('images/error_msg_icon.gif') ?>" class="v-middle" style="margin-right:5px"/>
            <?= $this->__("No profile loaded...") ?>
        <?php endif ?>
        </li>
    </ul>

    <?php if ($this->getProfile()->getId()):?>
    <ul id="profileRows">
        <?php foreach ($this->getExceptions() as $exceptionInfo):?>
            <li style="<?= $exceptionInfo['style'] ?>">
                <img src="<?= $exceptionInfo['src'] ?>" class="v-middle"/>
                <?= $exceptionInfo['message'] ?>
                <?php if ($exceptionInfo['position']):?>
                    <small>(<?= $exceptionInfo['position'] ?>)</small>
                <?php endif ?>
            </li>
        <?php endforeach ?>
        <?php if($this->getProfile()->getEntityType() == 'product' && $this->getProfile()->getDirection() == 'import'):?>
            <li id="liBeforeFinish" style="background-color:#FFD; display:none;">
                <img src="<?= $this->getSkinUrl('images/fam_bullet_error.gif') ?>" class="v-middle" style="margin-right:5px"/>
                <?= $this->__("Please wait while the indexes are being refreshed.") ?>
                <img id="before-finish-wait-img" src="<?= $this->getSkinUrl('images/loading.svg') ?>" class="v-middle" style="margin-right:5px"/>
            </li>
        <?php endif ?>
            <li id="liFinished" style="display:none;">
            <img src="<?= $this->getSkinUrl('images/note_msg_icon.gif') ?>" class="v-middle" style="margin-right:5px"/>
            <?= $this->__("Finished profile execution.") ?>
            </li>
            </ul>
            <?php if ($batchId = $this->getBatchModel()->getId()):?>
                <?php if ($this->getBatchModelHasAdapter()):?>
                <script type="text/javascript">
                    var countOfStartedProfiles = 0;
                    var countOfUpdated = 0;
                    var countOfError = 0;
                    var importData = [];
                    var totalRecords = <?= $this->getBatchItemsCount() ?>;
                    var config= <?= $this->getBatchConfigJson() ?>;
                </script>
                <script type="text/javascript">
                    function addImportData(data) {
                        importData.push(data);
                    }

                    function execImportData() {
                        if (importData.length == 0) {
                            $("updatedRows_img").src = config.styles.message.icon;
                            $("updatedRows").style.backgroundColor = config.styles.message.bg;
                            Element.insert($("liFinished"), {before: config.tpl.evaluate({
                                style: "background-color:"+config.styles.message.bg,
                                image: config.styles.message.icon,
                                text: config.tplSccTxt.evaluate({updated:(countOfUpdated-countOfError)}),
                                id: "updatedFinish"
                            })});

                            if ($("liBeforeFinish")) {
                                Element.insert($("liFinished"), {before: $("liBeforeFinish")});
                                $("liBeforeFinish").show();
                            }

                            new Ajax.Request("<?= $this->getUrl('*/*/batchFinish', ['id' => $batchId]) ?>", {
                                method: "post",
                                parameters: {form_key: FORM_KEY},
                                onComplete: function(transport) {
                                    if (transport.responseText.isJSON()) {
                                        var response = transport.responseText.evalJSON();
                                        if (response.error) {
                                            Element.insert($("liFinished"), {before: config.tpl.evaluate({
                                                style: "background-color:"+config.styles.error.bg,
                                                image: config.styles.error.icon,
                                                text: response.error.escapeHTML(),
                                                id: "error-finish"
                                            })});
                                        }
                                    }

                                    if ($("before-finish-wait-img")) {
                                        $("before-finish-wait-img").hide();
                                    }

                                    $('liFinished').show();
                                }
                            });
                        } else {
                            sendImportData(importData.shift());
                        }
                    }

                    function sendImportData(data) {
                        if (!config.tpl) {
                            config.tpl = new Template(config.template);
                            config.tplTxt = new Template(config.text);
                            config.tplSccTxt = new Template(config.successText);
                        }
                        if (!$("updatedRows")) {
                            Element.insert($("liFinished"), {before: config.tpl.evaluate({
                                style: "background-color: #FFD;",
                                image: config.styles.loader,
                                text: config.tplTxt.evaluate({updated:countOfUpdated, percent:getPercent()}),
                                id: "updatedRows"
                            })});
                        }
                        countOfStartedProfiles++;
                        if (!data.form_key) {
                            data.form_key = FORM_KEY;
                        }

                        new Ajax.Request("<?= $this->getUrl('*/*/batchRun') ?>", {
                          method: "post",
                          parameters: data,
                          onSuccess: function(transport) {
                            countOfStartedProfiles --;
                            countOfUpdated += data["rows[]"].length;
                            if (transport.responseText.isJSON()) {
                                addProfileRow(transport.responseText.evalJSON());
                            } else {
                                Element.insert($("updatedRows"), {before: config.tpl.evaluate({
                                    style: "background-color:"+config.styles.error.bg,
                                    image: config.styles.error.icon,
                                    text: transport.responseText.escapeHTML(),
                                    id: "error-" + countOfStartedProfiles
                                })});
                                countOfError += data["rows[]"].length;
                            }
                            execImportData();
                          }
                        });
                    }

                    function getPercent() {
                        return Math.ceil((countOfUpdated/totalRecords)*1000)/10;
                    }

                    function addProfileRow(data) {
                        if (data.errors.length > 0) {
                            for (var i=0, length=data.errors.length; i<length; i++) {
                                Element.insert($("updatedRows"), {before: config.tpl.evaluate({
                                    style: "background-color:"+config.styles.error.bg,
                                    image: config.styles.error.icon,
                                    text: data.errors[i],
                                    id: "id-" + (countOfUpdated + i + 1)
                                })});
                                countOfError ++;
                            }
                        }
                        $("updatedRows_status").update(config.tplTxt.evaluate({updated:countOfUpdated, percent:getPercent()}));
                    }
                </script>
                <?php $importData = $this->getImportData();?>
                <script type="text/javascript">
                    <?php foreach ($importData as  $importValue):?>
                    addImportData(<?= $this->jsonEncode($importValue) ?>);
                    <?php endforeach ?>
                    execImportData();
                </script>
                <?php endif ?>
            <?php endif ?>
            <?php if ($this->getShowFinished()):?>
                <script type="text/javascript">$('liFinished').show();</script>
            <?php endif ?>
   <?php endif ?>
