<?php
/**
 * @author 1902 Software
 * @copyright Copyright © 2023 1902 Software (https://1902software.com/magento/)
 * @package WriteTextAI_WriteTextAI
 */
?>

<?php
/**
 * @var \WriteTextAI\WriteTextAI\Block\Adminhtml\Grid\ProgressBar $block
 */

$user = $block->getUser();
$statuses = [];//$block->getStatuses();
$transferStatuses = $block->getTransferStatuses();
$cronStatusViewModel = $block->getCronStatusViewModel();
$isCronRunning = $cronStatusViewModel->getIsCronRunning();
?>

<div data-bind="scope: 'wtaiProgressBar'">
    <div class="wtai-progress-bar-container wtai-no-display" data-bind="css: {
        'wtai-saw-more': opened() || allProductsFailed(),
        'wtai-saw-less': !opened() && !allProductsFailed(),
        'wtai-no-display': statuses().length < 1
    }">
        <div class="wtai-message-container" data-bind="css: { 'wtai-all-done': allDone }">
            <div class="wtai-header-container" data-bind="css: { 'wtai-has-dismiss-action': showDismissAll }">
                <div class="wtai-header wtai-completed" data-bind="visible: allDone() && !allProductsFailed()">
                    <span><?= $block->escapeHtml(__('Bulk actions completed')) ?></span>
                </div>
                <div class="wtai-header" data-bind="hidden: allDone() && !allProductsFailed(), css: {
                    'wtai-error': timedOut() || allProductsFailed(),
                    'wtai-ongoing': !timedOut() && !allProductsFailed()
                }">
                    <span data-bind="visible: !timedOut() && !allProductsFailed()">
                        <?= $block->escapeHtml(__('Bulk actions ongoing')) ?>
                    </span>
                    <span data-bind="visible: allProductsFailed()">
                        <?= $block->escapeHtml(__('Bulk actions failed')) ?>
                    </span>
                </div>
                <button class="wtai-view wtai-dismiss" data-bind="
                    visible: showDismissAll,
                    click: dismissMessages
                ">
                    <span><?= $block->escapeHtml(__('Dismiss completed processes')) ?></span>
                </button>
            </div>
            <div class="wtai-user-status-container">
                <!-- ko foreach: { data: statuses(), as: 'item' } -->
                <div class="wtai-user-status" data-bind="attr: { 'data-id': item.id }, css: {
                    'wtai-completed': item.status === 'Completed',
                    'wtai-cancelling': $parent.isCancelling(item),
                    'wtai-cancelled': !$parent.isCancelling(item) && item.status === 'Cancelled',
                    'wtai-error': item.status === 'TimedOut' ||
                        (item.failed > 0 && (item.status === 'Completed' || item.status === 'Failed')) ||
                        item.failed === item.total,
                }">
                    <div class="wtai-content">
                        <span class="wtai-label" data-bind="
                            html: $parent.getMessage(item),
                            css: {'wtai-error': 
                                item.status === 'TimedOut' ||
                                (item.failed > 0 && item.failed === item.total)
                            }
                        "></span>
                        <!-- ko if: item.status === 'TimedOut' -->
                        <a class="wtai-retry" data-bind="
                            text: $parent.retryLabel,
                            click: function(item) { $parent.retry(item.id) }
                        "></a>
                        <!-- /ko -->
                        <!-- ko ifnot: item.status === 'TimedOut' -->
                        <span class="wtai-summary-container">
                            <span class="wtai-count" data-bind="text: $parent.getSummaryMessage(item)"></span>
                            <span class="wtai-failed-count" data-bind="text: $parent.getFailedCount(item)"></span>
                        </span>
                        <!-- /ko -->
                        <!-- ko if: 
                            item.status === 'Running'
                            && item.transfer === true
                            && item.user === $parent.user().email
                            && item.total === 1
                        -->
                        <div class="wtai-transfer-additional-comment">
                            <span class="wtai-red">
                                <?= /* @noEscape */__('Keep this tab open and active to'
                                    . ' ensure uninterrupted transfer.')
                            ?></span><br>
                            <span><?=('The process will pause if the tab is closed or goes idle,'
                                . ' and resume automatically when reopened or reactivated.') ?>
                            </span>
                        </div>
                        <!-- /ko -->
                        <!-- ko if: 
                            item.status === 'Preparing'
                        -->
                        <div class="wtai-transfer-additional-comment">
                            <span><?= /* @noEscape */__('The process is starting and may'
                                . ' take a few minutes before you see activity.')
?></span>
                            <span class="wtai-red"><?=(' Please keep this page open and avoid reloading,'
                                . ' closing, switching tabs, or minimizing until preparation is finished.') ?>
                            </span>
                        </div>
                        <!-- /ko -->
                        <div class="wtai-user" data-bind="text: item.user"></div>
                        <progress class="wtai-progress-bar"
                            max="100"
                            data-bind="
                                attr: { 'value': item.percent }
                            "
                        ></progress>
                    </div>
                    <div class="wtai-action">
                        <!-- ko if: 
                            (item.status === 'Running' || item.status === 'TimedOut' || item.status === 'Pending')
                            && item.user === $parent.user().email
                        -->
                        <button class="wtai-cancel" data-bind="
                            disable: !(
                                item.id !== $parent.koTempId &&
                                item.id !== $parent.tempId &&
                                item.id !== $parent.transferTempId && (
                                    item.status === 'TimedOut' ||
                                    item.status === 'Pending' || (
                                        item.status === 'Running' &&
                                        item.queuedIds.length >= 1
                                    )
                                )
                            ),
                            click: function(item) { $parent.cancelBulk(item.id) }
                        ">
                            <!-- ko if: item.total === 1 && item.transfer === true -->
                            <span><?= $block->escapeHtml(__('Ok')) ?></span>
                            <!-- /ko -->
                            <!-- ko if: item.total > 1 -->
                            <span><?= $block->escapeHtml(__('Cancel')) ?></span>
                            <!-- /ko -->
                        </button>
                        <!-- /ko -->
                        <!-- ko if: !$parent.checkingIsSingleRequest() && (
                                item.status === 'Completed' || (
                                !$parent.isCancelling(item) && item.status === 'Cancelled'
                            ) || (item.status === 'Failed')
                        ) -->
                        <button class="wtai-view" data-bind="click: function(item) { $parent.dismissMessage(item) }">
                            <span><?= $block->escapeHtml(__('OK')) ?></span>
                        </button>
                        <!-- /ko -->
                    </div>
                </div>
                <!-- /ko -->
            </div>
            <div class="wtai-comment" data-bind="visible: showGenerateComment">
                <span><?=
                    /* @noEscape */ __('<b>Important:</b> Depending on the type of bulk action,'
                        . ' some processes can continue in the background, while others require'
                        . ' you to keep this tab open until completion. If instructed to keep the'
                        . ' tab open, please do so; otherwise, you can safely navigate away.'); ?></span>
                <br/><br/>
                <span><?=
                    $block->escapeHtml(__('While process(es) are running, product editing'
                        . ' for affected items is temporarily disabled. Cancellation may be'
                        . ' limited during initial processing or once an item is already in progress.')); ?></span>
            </div>
            <div class="wtai-comment" data-bind="visible: showKOComment">
                <span><?=
                    /* @noEscape */__('<b>Important</b>: Depending on the type of bulk action,'
                        . ' some processes can continue in the background, while others require'
                        . ' you to keep this tab open until completion. If instructed to keep'
                        . ' the tab open, please do so; otherwise, you can safely navigate away.'); ?>
                </span>
                <br/><br/>
                <span><?=
                    /* @noEscape */ sprintf(
                        __('While process(es) are running, product editing for affected items is'
                            . ' temporarily disabled. Cancellation may be limited during initial'
                            . ' processing or once an item is already in progress. Keyword analysis'
                            . ' may take 3-10 minutes per product'
                            . ' — <a href="%s" target="_blank">learn why it takes time ⟶</a>'),
                        'https://writetext.ai/why-keyword-analysis-takes-time'
                    ) ?>
                </span>
            </div>
            <button class="wtai-see-more" data-bind="
                click: seeMore
            ">
                <span><?= $block->escapeHtml(__('View')) ?></span>
            </button>
            <button class="wtai-see-less" data-bind="
                click: seeLess,
                css: { 'wtai-all-products-failed': allProductsFailed }
            ">
                <span><?= $block->escapeHtml(__('Hide')) ?></span>
            </button>
        </div>
    </div>
</div>
<script type="text/x-magento-init">
    {
        "*": {
            "Magento_Ui/js/core/app": {
                "components": {
                    "wtaiProgressBar": {
                        "component": "WriteTextAI_WriteTextAI/js/grid/popups/progressbar",
                        "user": {
                            "id": <?= $block->escapeJs($user['id']) ?>,
                            "name": "<?= $block->escapeJs($user['name']) ?>",
                            "email": "<?= $block->escapeJs($user['email']) ?>"
                        },
                        "cancelUrl": "<?= $block->escapeJs($block->getUrl('wtai/grid/generate_cancel')) ?>",
                        "transferCancelUrl": "<?= $block->escapeJs(
                            $block->getUrl('wtai/grid/transfer_cancel')
                        ) ?>",
                        "retryUrl": "<?= $block->escapeJs($block->getUrl('wtai/grid/generate_retry')) ?>",
                        "statuses": <?= /* @noEscape */ json_encode($statuses) ?>,
                        "dismissUrl": "<?= $block->escapeJs($block->getUrl('wtai/grid/dismissMessage')) ?>",
                        "transferStatuses": <?= /* @noEscape */ json_encode($transferStatuses) ?>,
                        "isCronRunning": <?= /* @noEscape */ json_encode($isCronRunning) ?>
                    }
                }
            }
        }
    }
</script>
