<?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"><?=
    $escaper->escapeHtml(__('Select the text types you want to transfer to Magento'
        . ' and the correct store views that you want to transfer to.'))?>
    <br><br>
    <?= $escaper->escapeHtml(__('Transferring your product text to Magento will publish it live on the website.'
        . ' If the updates did not reflect in your page,')) ?>
        <br>
        <?= $escaper->escapeHtml(__('you may need to flush the cache or run reindexing.'
        . ' Please also make sure that your product is enabled in the frontend.')) ?>
    <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="wtaiBulkTransferForm"
    class="wtai-bulk-generate-form"
    data-bind="scope: 'wtaiGridTransferPrompt'"
    data-mage-init='{"WriteTextAI_WriteTextAI/js/grid/massactions/transfer-prompt": {
        "saveUrl": "<?= $escaper->escapeHtmlAttr($block->getUrl('wtai/settings/save')) ?>",
        "selectedFields": <?= $escaper->escapeHtmlAttr(json_encode($block->getFieldsSelected())) ?>,
        "isCronRunning": <?= /* @noEscape */ json_encode((bool) $isCronRunning) ?>
    }}'
>
    <div class="wtai-form-fields">
        <div class="wtai-col">
            <fieldset class="wtai-fieldset wtai-checkboxes">
                <div class="wtai-fieldset-title"><?= $escaper->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">
                        <?= $escaper->escapeHtml(__('Select all')) ?>
                    </label>
                </div>
                <?php foreach ($fields as $key => $field): ?>
                <div class="wtai-field">
                    <input type="checkbox"
                        class="admin__control-checkbox" 
                        value="<?= $escaper->escapeHtmlAttr($field['value']) ?>" 
                        id="wtaiField<?= $escaper->escapeHtmlAttr($key) ?>" 
                        name="fields[]"
                        data-bind="checked: selectedFields"
                    />
                    <label class="wtai-label admin__field-label" for="wtaiField<?= $escaper->escapeHtmlAttr($key) ?>">
                        <?= $escaper->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">
                        <?= $escaper->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"><?= $escaper->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="<?= $escaper->escapeHtmlAttr(Store::DEFAULT_STORE_ID) ?>" 
                            name="stores[]"
                            id="wtaiAllStoreView"
                            data-bind="checked: selectedStores"
                            data-value="<?= $escaper->escapeHtmlAttr(Store::DEFAULT_STORE_ID) ?>"
                        />
                        <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 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-bind="checked: selectedStores"
                                                data-value="<?= $escaper->escapeHtmlAttr($view['value']) ?>"
                                            />
                                            <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($) {
    $('#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>
