<?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\Grid\TransferPrompt $block
 */

$fields = $block->getFields();
$stores = $block->getStores();
$isCronRunning = $block->isCronRunning();
?>

<p class="wtai-subtitle"><?=
    $block->escapeHtml(__('Select the text types you want to transfer to Magento'
        . ' and the correct store views that you want to transfer to.'))?>
    <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="wtaiBulkTransferForm"
    class="wtai-bulk-generate-form"
    data-bind="scope: 'wtaiGridTransferPrompt'"
    data-mage-init='{"WriteTextAI_WriteTextAI/js/grid/massactions/transfer-prompt": {
        "saveUrl": "<?= $block->escapeHtmlAttr($block->getUrl('wtai/settings/save')) ?>",
        "selectedFields": <?= $block->escapeHtmlAttr(json_encode($block->getFieldsSelected())) ?>,
        "isCronRunning": <?= json_encode((bool)$isCronRunning) ?>
    }}'
>
    <div class="wtai-form-fields">
        <div class="wtai-col">
            <fieldset class="wtai-fieldset wtai-checkboxes">
                <div class="wtai-fieldset-title"><?= $block->escapeHtml(__('Transfer')) ?></div>
                <div class="wtai-field">
                    <input type="checkbox"
                        class="admin__control-checkbox wtai-checkbox-select-all"
                        value="all"
                        id="wtaiFieldsAll"
                        data-bind="checked: selectAllPromptFields, event: {change: selectAllPromptFieldsChanged}"
                    />
                    <label class="wtai-label admin__field-label wtai-strong" for="wtaiFieldsAll">
                        <?= $block->escapeHtml(__('Select all')) ?>
                    </label>
                </div>
                <?php foreach ($fields as $key => $field): ?>
                <div class="wtai-field">
                    <input type="checkbox"
                        class="admin__control-checkbox" 
                        value="<?= $block->escapeHtmlAttr($field['value']) ?>" 
                        id="wtaiField<?= $block->escapeHtmlAttr($key) ?>" 
                        name="fields[]"
                        data-bind="checked: selectedFields"
                    />
                    <label class="wtai-label admin__field-label" for="wtaiField<?= $block->escapeHtmlAttr($key) ?>">
                        <?= $block->escapeHtml($field['label']) ?>
                    </label>
                </div>
                <?php endforeach ?>
                <div class="wtai-field">
                    <input type="checkbox"
                        class="admin__control-checkbox"
                        value="alt_text"
                        id="wtaiFieldAltText"
                        name="fields[]"
                        data-bind="checked: selectedFields"
                    />
                    <label class="wtai-label admin__field-label" for="wtaiFieldAltText">
                        <?= $block->escapeHtml(__("Image alt text")) ?>
                    </label>
                    <!-- <?= $block->getChildBlock('wtai.pro.badge.transfer') ? $block->getChildBlock('wtai.pro.badge.transfer')->toHtml() : '' ?> -->
                </div>
                <div class="wtai-error-message"></div>
            </fieldset>
        </div>
        <div class="wtai-col wtai-stores wtai-transfer-prompt">
            <fieldset class="wtai-fieldset wtai-checkboxes">
                <div class="wtai-fieldset-title"><?= $block->escapeHtml(__('Stores')) ?></div>
                <div class="wtai-field 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-bind="checked: selectedStores"
                            data-value="<?= $block->escapeHtmlAttr(Store::DEFAULT_STORE_ID) ?>"
                        />
                        <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 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-bind="checked: selectedStores"
                                                data-value="<?= $block->escapeHtmlAttr($view['value']) ?>"
                                            />
                                            <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($) {
    $('#wtaiBulkTransferForm').mage('validation', {
        "rules": {
            "fields[]": {
                "required": true
            },
            "stores[]": {
                "required": true
            }
        },
        errorPlacement: function (error, element) {
            var parent = element.closest('.wtai-fieldset');
            $(parent).find('.wtai-error-message').html(error);
        }
    });
    $("#wtaiBulkTransferForm").trigger('contentUpdated');
});
</script>
