<?php
/**
 * @author 1902 Software
 * @copyright Copyright © 2023 1902 Software (https://1902software.com/magento/)
 * @package WriteTextAI_WriteTextAI
 */
?>
<div class="wtai-history-log-modal" data-bind="scope: 'wtaiHistoryLog'">
    <div data-bind="blockLoader: isLoading">
        <div class="wtai-actions">
            <form name="history_log_filter"
                  class="wtai-history-log-filter-form"
                  action="<?= $block->escapeUrl($block->getUrl('wtai/edit/history')) ?>"
                  method="post">
                <input type="hidden" name="form_key" value="<?= $block->escapeHtmlAttr($block->getFormKey()) ?>"/>
                <input type="hidden" name="record_id" data-bind="value: recordId"/>
                <input type="hidden" name="history_type" data-bind="value: historyType"/>
                <input type="hidden" name="store_id" data-bind="value: storeId"/>
                <div class="wtai-control">
                    <input type="text"
                           class="admin__control-text wtai-calendar"
                           name="start_date"
                           placeholder="<?= $block->escapeHtmlAttr(__('Start date')) ?>"
                           data-bind="value: startDate"/>
                </div>
                <div class="wtai-control">
                    <input type="text"
                           class="admin__control-text wtai-calendar"
                           name="end_date"
                           placeholder="<?= $block->escapeHtmlAttr(__('End date')) ?>"
                           data-bind="value: endDate"/>
                </div>
                <div class="wtai-control">
                    <select name="user"
                        class="admin__control-select"
                        data-bind="
                            options: users,
                            optionsText: 'name',
                            optionsValue: 'id',
                            optionsCaption: userOptionCaption
                        ">
                        <option value=""><?= $block->escapeHtml(__('Filter by user')) ?></option>
                    </select>
                </div>
                <button class="action-basic"><?= $block->escapeHtml(__('Filter')) ?></button>
            </form>
        </div>
        <div class="wtai-accordion" data-bind="foreach: { data: Object.keys(historyEntries()), as: 'historyEntry' }">
            <div class="wtai-accordion-block">
                <div data-role="collapsible" class="admin__collapsible-block-wrapper">
                    <div data-role="trigger" class="admin__collapsible-title">
                        <h4 class="wtai-date" data-bind="text: $parent.historyEntries()[historyEntry].timestamp"></h4>
                        <p class="wtai-product-name" data-bind="
                            html: $parent.historyEntries()[historyEntry].recordName,
                            visible: !$parent.recordId(),
                            click: function() { $parent.showProduct(
                                $parent.historyEntries()[historyEntry].recordId,
                                $parent.historyEntries()[historyEntry].storeId
                            ); }
                        "></p>
                        <p class="wtai-editor" data-bind="text:
                            $parent.historyEntries()[historyEntry].actionDisplay
                        "></p>
                    </div>
                </div>
                <div data-role="content" data-bind="foreach: {
                        data: $parent.historyEntries()[historyEntry].textEntries,
                        as: 'textEntry'
                    }">
                    <div class="wtai-text-types" data-bind="foreach: {
                        data: Object.keys(textEntry),
                        as: 'textEntryData'
                    }">
                        <!-- ko foreach: textEntry[textEntryData] -->
                        <p class="wtai-text-type" data-bind="text: $parents[3].getTextTypeLabel(textEntryData)"></p>
                        <p class="wtai-text-content" data-bind="html: $data"></p>
                        <!-- /ko -->
                    </div>
                </div>
            </div>
        </div>
        <p class="wtai-no-result" data-bind="visible: historyEntries().length <= 0">
            <?= $block->escapeHtml(__('No result found.')) ?>
        </p>
        <div class="wtai-footer-actions" data-bind="visible: Object.keys(historyEntries()).length > 0">
            <button class="action-basic" data-bind="click: loadMore, visible: nextPageToken().length">
                <?= $block->escapeHtml(__('Load more')) ?>
            </button>
            <span class="wtai-no-more" data-bind="visible: isLastPage, i18n: 'No more to show.'"></span>
        </div>
    </div>
</div>
<script type="text/x-magento-init">
    {
        "*": {
            "Magento_Ui/js/block-loader":
                "<?= $block->escapeJs($block->getViewFileUrl('images/loader-1.gif')) ?>",
            "Magento_Ui/js/core/app": {
                "components": {
                    "wtaiHistoryLog": {
                        "component": "WriteTextAI_WriteTextAI/js/history-log",
                        "historyLogModalSelector": ".wtai-history-log-modal",
                        "formSelector": "form[name=history_log_filter]",
                        "accordionSelector": ".wtai-history-log-modal .wtai-accordion-block",
                        "historyType": "Product"
                    }
                }
            }
        }
    }
</script>
