<?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"><?= $escaper->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><?= $escaper->escapeHtml(__('Important:')) ?></strong>
</p>
<div class="wtai-notice-container">
    <ul>
        <li><?= $escaper->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 */ __('Please make sure you’ve reviewed all generated texts 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": <?= $escaper->escapeHtmlAttr(Store::DEFAULT_STORE_ID) ?>
            }}'>
            <fieldset class="wtai-fieldset wtai-checkboxes">
                <div class="wtai-fieldset-title"><?= $escaper->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="<?= $escaper->escapeHtmlAttr(Store::DEFAULT_STORE_ID) ?>" 
                            name="stores[]"
                            id="wtaiAllStoreView"
                            data-value="<?= $escaper->escapeHtmlAttr(Store::DEFAULT_STORE_ID) ?>"
                            data-bind="checked: selectedStores"/>
                        <label class="wtai-label admin__field-label wtai-strong" for="wtaiAllStoreView">
                            <?= $escaper->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">
                            <?= $escaper->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">
                                    <?= $escaper->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="<?= $escaper->escapeHtmlAttr($view['value']) ?>" 
                                                id="wtaiStore<?= $escaper->escapeHtmlAttr($view['value']) ?>" 
                                                name="stores[]"
                                                data-value="<?= $escaper->escapeHtmlAttr($view['value']) ?>"
                                                data-bind="checked: selectedStores"
                                            />
                                            <label class="wtai-label admin__field-label"
                                                for="wtaiStore<?= $escaper->escapeHtmlAttr($view['value']) ?>">
                                                <?= $escaper->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>
