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

<?php
use Magento\Store\Model\Store;

/**
 * @var WriteTextAI\WriteTextAI\Block\Adminhtml\Edit\TransferSettings $block
 */

$stores = $block->getStores();
?>

<p class="wtai-subtitle"><?= $block->escapeHtml(__(
    'Select the store(s) you want to transfer to in Magento.'
    . ' Any media or directive you have inserted in your current Magento text'
    . ' will be overwritten when you transfer from WriteText.ai.'
)) ?>
    <br><br>
    <strong><?= $block->escapeHtml(__('Important:')) ?></strong>
</p>
<div class="wtai-notice-container">
    <ul>
        <li><?= $block->escapeHtml(__('Magento text fields have character limits. If the generated'
            . ' text is too long, some parts may be cut off during transfer.')) ?></li>
        <li><?= /* @noEscape */ __('Once you click Transfer, the process cannot be canceled or'
            . ' undone. Please make sure you’ve reviewed all generated texts<br/> before proceeding.'
            . ' Any changes will overwrite existing content in Magento.') ?></li>
    </ul>
</div>
<form method="post" id="wtaiEditTransferForm" class="wtai-bulk-generate-form">
    <div class="wtai-form-fields">
        <div class="wtai-col wtai-stores wtai-transfer-prompt" data-bind="scope: 'wtaiEditTransferPrompt'"
            data-mage-init='{"WriteTextAI_WriteTextAI/js/edit/transfer-prompt": {
                "defaultStoreId": <?= $block->escapeHtmlAttr(Store::DEFAULT_STORE_ID) ?>
            }}'>
            <fieldset class="wtai-fieldset wtai-checkboxes">
                <div class="wtai-fieldset-title"><?= $block->escapeHtml(__('Stores')) ?></div>
                <div class="wtai-field wtai-all">
                    <div class="wtai-field-content">
                        <input type="checkbox"
                            class="admin__control-checkbox wtai-checkbox-select-all"
                            value="<?= $block->escapeHtmlAttr(Store::DEFAULT_STORE_ID) ?>" 
                            name="stores[]"
                            id="wtaiAllStoreView"
                            data-value="<?= $block->escapeHtmlAttr(Store::DEFAULT_STORE_ID) ?>"
                            data-bind="checked: selectedStores"/>
                        <label class="wtai-label admin__field-label wtai-strong" for="wtaiAllStoreView">
                            <?= $block->escapeHtml(__('All store views')) ?>
                        </label>
                    </div>
                </div>
                <div class="wtai-fields">
                <?php foreach ($stores as $website): ?>
                    <div class="wtai-field wtai-website">
                        <div class="wtai-field-content">
                            <?= $block->escapeHtml($website['label']) ?>
                        </div>
                    </div>
                    <div class="wtai-fields">
                        <?php foreach ($website['value'] as $store): ?>
                            <div class="wtai-field wtai-store">
                                <div class="wtai-field-content">
                                    <?= $block->escapeHtml($store['label']) ?>
                                </div>
                            </div>
                            <div class="wtai-fields">
                                <?php foreach ($store['value'] as $view): ?>
                                    <div class="wtai-field wtai-view">
                                        <div class="wtai-field-content">
                                            <input type="checkbox" class="admin__control-checkbox" 
                                                value="<?= $block->escapeHtmlAttr($view['value']) ?>" 
                                                id="wtaiStore<?= $block->escapeHtmlAttr($view['value']) ?>" 
                                                name="stores[]"
                                                data-value="<?= $block->escapeHtmlAttr($view['value']) ?>"
                                                data-bind="checked: selectedStores"
                                            />
                                            <label class="wtai-label admin__field-label"
                                                for="wtaiStore<?= $block->escapeHtmlAttr($view['value']) ?>">
                                                <?= $block->escapeHtml($view['label']) ?>
                                            </label>
                                        </div>
                                    </div>
                                <?php endforeach ?>
                            </div>
                        <?php endforeach ?>
                    </div>
                <?php endforeach ?>
                </div>
                <div class="wtai-error-message"></div>
            </fieldset>
        </div>
    </div>
</form>
<script>
require([
    "jquery",
    "mage/mage",
    "mage/validation"
], function($) {
    $('#wtaiEditTransferForm').mage('validation', {
        "rules": {
            "stores[]": {
                "required": true
            }
        },
        errorPlacement: function (error, element) {
            var parent = element.closest('.wtai-fieldset');
            $(parent).find('.wtai-error-message').html(error);
        }
    });
    $("#wtaiEditTransferForm").trigger('contentUpdated');
});
</script>
