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

<?php
/**
 * @var WriteTextAI\WriteTextAI\Block\Adminhtml\Setup\Index $block
 */

$tones = $block->getTones();
$styles = $block->getStyles();
$audience = $block->getAudience();
$attributes = $block->getAttributes();
$rules = $block->getRules();
$disallowedCombinations = $block->getDisallowedCombinations();
$templateSettings = $block->getTemplateSettings();

$descMin = $block->getDescriptionMin();
$descMax = $block->getDescriptionMax();
$excerptMin = $block->getExcerptMin();
$excerptMax = $block->getExcerptMax();
$catDescMin = $block->getCategoryDescriptionMin();
$catDescMax = $block->getCategoryDescriptionMax();
$autoTransferSchedule = $block->getAutoTransferSchedule();
?>

<form action="<?= $block->escapeUrl($block->getSaveUrl()) ?>"
    method="post"
    id="wtaiStep3Form"
    class="wtai-step3-form wtai-global-settings"
    data-bind="scope: 'wtaiGlobalSettings'">
    <input type="hidden" name="step" value="3" />
    <?= /* @noEscape */ $block->getBlockHtml('formkey') ?>
    <fieldset class="wtai-fieldset wtai-checkboxes">
        <div class="wtai-fieldset-title"><?= $block->escapeHtml(__('Set default tones')) ?></div>
        <div class="wtai-fieldset-row">
            <input type="checkbox" class="admin__control-checkbox wtai-checkbox-select-all" 
                value="all" 
                id="wtaiToneAll" 
                data-name="tones"
            />
            <label class="wtai-label admin__field-label wtai-strong" for="wtaiToneAll">
                <?= $block->escapeHtml(__('Select all')) ?>
            </label>
        </div>
        <?php if (is_array($tones) && count($tones) > 0): ?>
            <?php foreach ($tones as $key => $tone): ?>
                <div class="wtai-field-column">
                    <input type="checkbox" class="admin__control-checkbox" 
                        value="<?= $block->escapeHtmlAttr($tone['id']) ?>" 
                        id="wtaiTone<?= $block->escapeHtmlAttr($key) ?>" 
                        name="tones[]"
                        <?= /* @noEscape */ $tone['default'] ? 'checked' : '' ?>
                    />
                    <label class="wtai-label admin__field-label" for="wtaiTone<?= $block->escapeHtmlAttr($key) ?>">
                        <?= $block->escapeHtml($tone['name']) ?>
                    </label>
                    <div class="wtai-disabled-tooltip">
                        <div class="wtai-disabled-tooltip-content">
                            <span class="wtai-tooltip-text"></span>
                        </div>
                    </div>
                </div>
            <?php endforeach ?>
        <?php endif ?>
        <div class="wtai-error-message"></div>
    </fieldset>
    <fieldset class="wtai-fieldset">
        <div class="wtai-fieldset-title"><?= $block->escapeHtml(__('Set default style')) ?></div>
        <?php if (is_array($styles) && count($styles) > 0): ?>
            <?php foreach ($styles as $key => $style): ?>
                <div class="wtai-field-column">
                    <input type="radio" class="admin__control-radio" 
                        value="<?= $block->escapeHtmlAttr($style['id']) ?>" 
                        id="wtaiStyle<?= $block->escapeHtmlAttr($key) ?>"
                        name="styles"
                        <?= /* @noEscape */ $style['default'] ? 'checked' : '' ?> />
                    <label class="wtai-label admin__field-label" for="wtaiStyle<?= $block->escapeHtmlAttr($key) ?>">
                        <?= $block->escapeHtml($style['name']) ?>
                    </label>
                    <div class="wtai-disabled-tooltip">
                        <div class="wtai-disabled-tooltip-content">
                            <span class="wtai-tooltip-text"></span>
                        </div>
                    </div>
                </div>
            <?php endforeach ?>
        <?php endif ?>
        <div class="wtai-error-message"></div>
    </fieldset>
    <fieldset class="wtai-fieldset wtai-checkboxes">
        <div class="wtai-fieldset-title"><?= $block->escapeHtml(__('Set default audiences')) ?></div>
        <div class="wtai-fieldset-row">
            <input type="checkbox" class="admin__control-checkbox wtai-checkbox-select-all" 
                value="all" 
                id="wtaiAudienceAll" 
                data-name="audiences"
            />
            <label class="wtai-label admin__field-label wtai-strong" for="wtaiAudienceAll">
                <?= $block->escapeHtml(__('Select all')) ?>
            </label>
        </div>
        <?php if (is_array($audience) && count($audience) > 0): ?>
            <?php foreach ($audience as $key => $group): ?>
                <div class="wtai-field-column">
                    <input type="checkbox" class="admin__control-checkbox" 
                            value="<?= $block->escapeHtmlAttr($group['id']) ?>" 
                            id="wtaiAudience<?= $block->escapeHtmlAttr($key) ?>" 
                            name="audiences[]"
                            <?= /* @noEscape */ $group['default'] ? 'checked' : '' ?> />
                    <label class="wtai-label admin__field-label" for="wtaiAudience<?= $block->escapeHtmlAttr($key) ?>">
                        <?= $block->escapeHtml($group['name']) ?>
                    </label>
                </div>
            <?php endforeach ?>
        <?php endif ?>
        <div class="wtai-error-message"></div>
    </fieldset>
    <fieldset class="wtai-fieldset wtai-checkboxes">
        <div class="wtai-fieldset-title"><?= $block->escapeHtml(__('Set default product data')) ?></div>
        <p class="wtai-fieldset-content">
            <?= $block->escapeHtml(__(
                "Choose the default product data that will be considered in generating your".
                " product text. This includes Attributes, Thumbnail, and optional Product Research.".
                " Make sure your selected image accurately represents the product (not a placeholder" .
                " or generic shop image). While product data helps guide the AI, there is no".
                " guarantee that every detail will appear in the final text. Each product can use up to".
                " 50 product data fields at no extra cost, with 1 credit added per product for every 100".
                " additional fields. Empty or unused fields are ignored."
            )) ?>
        </p>
        <div class="wtai-fieldset-row">
            <input type="checkbox" class="admin__control-checkbox wtai-checkbox-select-all" 
                value="all" 
                id="wtaiAttributesAll" 
                data-name="attributes"
            />
            <label class="wtai-label admin__field-label wtai-strong" for="wtaiAttributesAll">
                <?= $block->escapeHtml(__('Select all')) ?>
            </label>
        </div>
        <?php if (is_array($attributes) && count($attributes) > 0): ?>
            <?php foreach ($attributes as $key => $attribute): ?>
                <div class="wtai-field-column">
                    <?php if ($attribute['value'] == 'thumbnail'): ?>
                        <input type="checkbox"
                            class="admin__control-checkbox"
                            value="<?= $block->escapeHtmlAttr($attribute['value']) ?>"
                            id="wtaiAttr<?= $block->escapeHtmlAttr($key) ?>"
                            name="attributes[]"
                            checked />
                        <label class="wtai-label admin__field-label" for="wtaiAttr<?= $block->escapeHtmlAttr($key) ?>">
                            <?= $block->escapeHtml($attribute['label']) ?>
                        </label>
                    <?php else: ?>
                        <input type="checkbox"
                            class="admin__control-checkbox"
                            value="<?= $block->escapeHtmlAttr($attribute['value']) ?>"
                            id="wtaiAttr<?= $block->escapeHtmlAttr($key) ?>"
                            name="attributes[]"
                            <?= /* @noEscape */ $attribute['default'] ? 'checked' : '' ?> />
                        <label class="wtai-label admin__field-label" for="wtaiAttr<?= $block->escapeHtmlAttr($key) ?>">
                            <?= $block->escapeHtml($attribute['label']) ?>
                        </label>
                    <?php endif; ?>
                </div>
            <?php endforeach ?>
        <?php endif ?>
        <div class="wtai-error-message"></div>
    </fieldset>
    <fieldset class="wtai-fieldset wtai-template-assigning">
        <div class="wtai-fieldset-title"><?= $block->escapeHtml(__('Assign templates')) ?></div>
        <p class="wtai-fieldset-content">
            <?= $block->escapeHtml(__(
                "(OPTIONAL) Assign or create templates for this store through"
                . " your WriteText.ai backend. Templates allow for better"
                . " configuration of your product and category text and ensure"
                . " consistent styling across your store."
            )) ?>
        </p>
        <ul class="wtai-templates-optional-lists">
            <li><?= /* @noEscape */ __('<strong>Optional step</strong>. You may skip'
                . ' this and continue generating text without templates') ?></li>
            <li><?= /* @noEscape */ __('<strong>If you create or assign a template</strong>,'
                . ' please note that you will not be redirected back to the setup wizard.'
                . ' You’ll need to manually return here to complete your setup.') ?></li>
            <li><?= /* @noEscape */ __('<strong>Highly recommended</strong>. Setting up templates provides'
                . ' better control and optimization for your product and category texts.') ?></li>
        </ul>
        <p><a href="https://platform.writetext.ai/webshops" target="_blank">Go to templates →</a></p>
    </fieldset>
    <fieldset class="wtai-fieldset wtai-description-length">
        <div class="wtai-fieldset-title">
            <?= $block->escapeHtml(__('Set default target length')) ?>
        </div>
        <?= $block->getChildHtml('wtai.custom.text.warning') ?>
        <div class="wtai-field-column">
            <div class="wtai-fieldset-title">
                <?= $block->escapeHtml(__('Product description')) ?>
            </div>
            <div class="wtai-fieldset-subtitle">
                <?= $block->escapeHtml(__('Target length (in words)')) ?>
            </div>
        </div>
        <div class="wtai-field-column">
            <div class="wtai-field admin__field row">
                <div class="wtai-control">
                    <label class="wtai-label"><?= $block->escapeHtml(__('Min')) ?></label>
                    <input type="number"
                        class="admin__control-text wtai-input-number"
                        name="description_min"
                        data-bind="
                            value: descMin,
                            event: {
                                blur: validateMinMax,
                                keypress: ensureNumerical
                            },
                            attr: {
                                min: minOutputWords,
                                max: descMax() - 1
                            }"/>
                </div>
                <div class="wtai-control">
                    <label class="wtai-label"><?= $block->escapeHtml(__('Max')) ?></label>
                    <input type="number"
                        class="admin__control-text wtai-input-number"
                        name="description_max"
                        data-bind="
                            value: descMax,
                            event: {
                                blur: validateMinMax,
                                keypress: ensureNumerical
                            },
                            attr: {
                                min: Number(descMin()) + 1,
                                max: maxOutputWords
                            }"/>
                </div>
            </div>
        </div>
    </fieldset>
    <fieldset class="wtai-fieldset wtai-description-length">
        <div class="wtai-field-column">
            <div class="wtai-fieldset-title">
                <?= $block->escapeHtml(__('Product short description')) ?>
            </div>
            <div class="wtai-fieldset-subtitle">
                <?= $block->escapeHtml(__('Target length (in words)')) ?>
            </div>
        </div>
        <div class="wtai-field-column">
            <div class="wtai-field admin__field row">
                <div class="wtai-control">
                    <label class="wtai-label"><?= $block->escapeHtml(__('Min')) ?></label>
                    <input type="number"
                        class="admin__control-text wtai-input-number"
                        name="excerpt_min"
                        data-bind="
                            value: excMin,
                            event: {
                                blur: validateMinMax,
                                keypress: ensureNumerical
                            },
                            attr: {
                                min: minOutputWords,
                                max: excMax() - 1
                            }"/>
                </div>
                <div class="wtai-control">
                    <label class="wtai-label"><?= $block->escapeHtml(__('Max')) ?></label>
                    <input type="number"
                        class="admin__control-text wtai-input-number"
                        name="excerpt_max"
                        data-bind="
                            value: excMax,
                            event: {
                                blur: validateMinMax,
                                keypress: ensureNumerical
                            },
                            attr: {
                                min: Number(excMin()) + 1,
                                max: maxOutputWords
                            }"/>
                </div>
            </div>
        </div>
    </fieldset>
    <fieldset class="wtai-fieldset wtai-description-length wtai-with-border">
        <div class="wtai-field-column">
            <div class="wtai-fieldset-title">
                <?= $block->escapeHtml(__('Category description')) ?>
            </div>
            <div class="wtai-fieldset-subtitle">
                <?= $block->escapeHtml(__('Target length (in words)')) ?>
            </div>
        </div>
        <div class="wtai-field-column">
            <div class="wtai-field admin__field row">
                <div class="wtai-control">
                    <label class="wtai-label"><?= $block->escapeHtml(__('Min')) ?></label>
                    <input type="number"
                        class="admin__control-text wtai-input-number"
                        name="category_description_min"
                        data-bind="
                            value: catDescMin,
                            event: {
                                blur: validateMinMax,
                                keypress: ensureNumerical
                            },
                            attr: {
                                min: minOutputWords,
                                max: catDescMax() - 1
                            }"/>
                </div>
                <div class="wtai-control">
                    <label class="wtai-label"><?= $block->escapeHtml(__('Max')) ?></label>
                    <input type="number"
                        class="admin__control-text wtai-input-number"
                        name="category_description_max"
                        data-bind="
                            value: catDescMax,
                            event: {
                                blur: validateMinMax,
                                keypress: ensureNumerical
                            },
                            attr: {
                                min: Number(catDescMin()) + 1,
                                max: maxOutputWords
                            }"/>
                </div>
            </div>
        </div>
    </fieldset>
    <?= /* @noEscape */ $block->getChildHtml('wtai.setup.keywords') ?>
    <?= /* @noEscape */ $block->getChildHtml('wtai.setup.autotransfer') ?>
    <div class="wtai-error-message"></div>
    <div class="wtai-actions">
        <button type="submit" id="wtaiFinishButton" class="wtai-button">
            <?= $block->escapeHtml(__('Finalize setup')) ?> <span class="wtai-button-icon">&rarr;</span>
        </button>                
    </div>
</form>
<script type="text/x-magento-init">
    {
        "*": {
            "Magento_Ui/js/core/app": {
                "components": {
                    "wtaiGlobalSettings": {
                        "component": "WriteTextAI_WriteTextAI/js/setup/global-settings",
                        "maxOutputWords":  <?= /* @noEscape */ $rules['maxOutputWords'] ?? '' ?>,
                        "minOutputWords":  <?= /* @noEscape */ $rules['minOutputWords'] ?? '' ?>,
                        "descMin": <?= /* @noEscape */ $descMin ?>,
                        "descMax": <?= /* @noEscape */ $descMax ?>,
                        "excMin": <?= /* @noEscape */ $excerptMin ?>,
                        "excMax": <?= /* @noEscape */ $excerptMax ?>,
                        "catDescMin": <?= /* @noEscape */ $catDescMin ?>,
                        "catDescMax": <?= /* @noEscape */ $catDescMax ?>,
                        "disallowedCombinations": <?= /* @noEscape */ json_encode($disallowedCombinations) ?>
                    }
                }
            }
        }
    }
</script>
