<?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
 */

$tooltip = $block->getTooltip();
$rules = $block->getRules();
$categoryFields = $block->getCategoryFields();
$productFields = $block->getProductFields();
$automaticTextOptimization = $block->getAutomaticTextOptimization();
$triggerActions = $block->getTriggerActions();
$defaultProductFields = $block->getDefaultProductTexts();
$defaultCategoryFields = $block->getDefaultCategoryTexts();
$productFieldsValues = array_column($productFields, 'value');
$categoryFieldsValues = array_column($categoryFields, 'value');
$defaultTriggerAction = $block->getDefaultTriggerAction();
$defaultAutomaticTextOptimization = $block->getDefaultAutomaticTextOptimization();
$defaultClusterCount = $block->getDefaultClusterCount();
$defaultClusterCountCategory = $block->getDefaultClusterCountCategory();
$whenPageIsRankingValue = $block->getTriggerActionPageIsRankingValue();
$afterNDaysValue = $block->getTriggerActionAfterNDaysValue();
$minTriggerActionNPosition = $rules['minTriggerActionNPosition'] ?? 2;
$maxTriggerActionNPosition = $rules['maxTriggerActionNPosition'] ?? 100;
$defaultTriggerActionNPosition = $block->getTriggerActionNPositionDefaultValue();
$minTriggerActionNDays = $rules['minTriggerActionNDays'] ?? 1;
$maxTriggerActionNDays = $rules['maxTriggerActionNDays'] ?? 365;
$defaultTriggerActionNDays = $block->getTriggerActionNDaysDefaultValue();
$isCronRunning = $block->isCronRunning();
$getCronJobDisabledAutoTransferMessage = $block->getCronJobDisabledAutoTransferMessage();
$hasProAccess = $block->getHasProAccess();
$isDisabledClass = $hasProAccess ? '' : 'wtai-disabled';
// phpcs:disable Magento2.Functions.DiscouragedFunction.DiscouragedWithAlternative
?>

<div data-bind="scope: 'wtaiSetupKeywords'" class="wtai-setup-keywords-settings">
    <div class="wtai-title">
        <?= $block->escapeHtml(__('Keyword optimization')) ?>
        <?php if (!$hasProAccess): ?>
            <span class="wtai-premium">
                <?= $block->escapeHtml(__('PRO')) ?>
            </span>
        <?php endif; ?>
    </div>
    <div class="<?= $block->escapeHtmlAttr($isDisabledClass) ?>">
        <span class="tooltip-toggle"></span>
        <span class="tooltip-content" data-bind="hidden: hasProAccess">
            <?= $block->escapeHtml(__('These settings are enabled for Pro accounts only.')) ?>
        </span>
        <div class="wtai-content-wrapper">
            <fieldset class="wtai-fieldset wtai-cluster-count">
                <div class="wtai-field-column">
                    <div class="wtai-fieldset-title">
                        <?= /* @noEscape */ __('No. of target keywords per product'
                            . ' <i>(cluster-based pipelines)</i>') ?>
                    </div>
                    <div class="wtai-field admin__field row">
                        <div class="wtai-control">
                            <input type="number"
                                class="admin__control-text wtai-input-number wtai-cluster-count"
                                name="cluster_count"
                                data-bind="
                                    value: clusterCount,
                                    event: {
                                        blur: validateMinMax,
                                        keypress: ensureNumerical
                                    },
                                    attr: {
                                        min: minClusterCount,
                                        max: maxClusterCount
                                    }
                                "
                            />
                        </div>
                    </div>
                </div>
                <div class="wtai-field-column">
                    <div class="wtai-fieldset-title">
                        <?= /* @noEscape */ __('No. of target keywords per'
                            . ' category <i>(cluster-based pipelines)</i>') ?>
                    </div>
                    <div class="wtai-field admin__field row">
                        <div class="wtai-control">
                            <input type="number"
                                class="admin__control-text wtai-input-number wtai-cluster-count"
                                name="cluster_count_category"
                                data-bind="
                                    value: clusterCountCategory,
                                    event: {
                                        blur: validateMinMax,
                                        keypress: ensureNumerical
                                    },
                                    attr: {
                                        min: minClusterCountCategory,
                                        max: maxClusterCountCategory
                                    }
                                "
                            />
                        </div>
                    </div>
                </div>
                <div class="wtai-error-message"></div>
            </fieldset>
            <fieldset class="wtai-fieldset wtai-trigger-action">
                <div class="wtai-fieldset-title"><?= $block->escapeHtml(__('Trigger')) ?></div>
                <div class="wtai-fieldset-subtitle">
                    <?= $block->escapeHtml(__('Monitoring for this trigger will only start'
                        . ' once you transfer the text optimized with the keyword.')) ?>
                </div>
                <div class="wtai-field-column wtai-full-width">
                    <input type="radio"
                        class="admin__control-radio"
                        name="trigger_action"
                        value="<?= $block->escapeHtmlAttr($whenPageIsRankingValue) ?>"
                        data-bind="checked: triggerAction, event: {change: validateTriggerAction}"
                        id="wtaiTriggerActionNPosition"
                        required="true"
                    />
                    <label class="wtai-label admin__field-label" for="wtaiTriggerActionNPosition">
                        <div class="wtai-label-with-input">
                            <span><?= $block->escapeHtml(__('When page is ranking within position 1 -')) ?></span>
                            <div class="wtai-control">
                                <input type="number"
                                    class="admin__control-text wtai-input-number wtai-trigger-action-n-position"
                                    name="trigger_action_n_position"
                                    data-bind="
                                        value: triggerActionNPosition,
                                        event: {
                                            blur: validateMinMax,
                                            keypress: ensureNumerical
                                        },
                                        attr: {
                                            min: minTriggerActionNPosition,
                                            max: maxTriggerActionNPosition
                                        }
                                    "
                                />
                            </div>
                            <span><?= $block->escapeHtml(__('in the SERP')) ?></span>
                        </div>
                    </label>
                </div>
                <div class="wtai-field-column wtai-full-width">
                    <input type="radio"
                        class="admin__control-radio"
                        name="trigger_action"
                        value="<?= $block->escapeHtmlAttr($afterNDaysValue) ?>"
                        data-bind="checked: triggerAction, event: {change: validateTriggerAction}"
                        id="wtaiTriggerActionAfterNDays"
                    />
                    <label class="wtai-label admin__field-label" for="wtaiTriggerActionAfterNDays">
                        <div class="wtai-label-with-input">
                            <span><?= $block->escapeHtml(__('After ')) ?></span>
                            <div class="wtai-control">
                                <input type="number"
                                    class="admin__control-text wtai-input-number wtai-trigger-action-after-n-days"
                                    name="trigger_action_n_days"
                                    data-bind="
                                        value: triggerActionAfterNDays,
                                        event: {
                                            blur: validateMinMax,
                                            keypress: ensureNumerical
                                        },
                                        attr: {
                                            min: minTriggerActionNDays,
                                            max: maxTriggerActionNDays
                                        }
                                    "
                                />
                            </div>
                            <span><?= $block->escapeHtml(__('day/s')) ?></span>
                        </div>
                    </label>
                </div>
                <div class="wtai-field-column wtai-full-width">
                    <input type="radio"
                        class="admin__control-radio"
                        name="trigger_action"
                        value="none"
                        data-bind="checked: triggerAction, event: {change: validateTriggerAction}"
                        id="wtaiTriggerActionNone"
                    />
                    <label class="wtai-label admin__field-label" for="wtaiTriggerActionNone">
                        <?= $block->escapeHtml(__('None')) ?>
                    </label>
                </div>
                <div class="wtai-error-message"></div>
            </fieldset>
            <fieldset class="wtai-fieldset wtai-ato" data-bind="
                disable: disableATO, css: { 'wtai-disabled': disableATO }
            ">
                <div class="wtai-fieldset-title">
                    <?= $block->escapeHtml(__('Automatic text optimization in the pipeline')) ?>
                </div>
                <div class="wtai-fieldset-subtitle">
                    <?= $block->escapeHtml(__('When trigger is met in the pipeline...')) ?>
                </div>
                <?php $atoCnt = 0;?>
                <?php foreach ($automaticTextOptimization as $ato): ?>
                <div class="wtai-field-column wtai-full-width">
                    <input type="radio"
                        class="admin__control-radio"
                        value="<?= $block->escapeHtmlAttr($ato['value']) ?>"
                        name="automatic_text_optimization"
                        data-bind="checked: automaticTextOptimization"
                        id="wtaiAutomaticTextOptimization<?= $block->escapeHtmlAttr($atoCnt) ?>"
                    />
                    <label class="wtai-label admin__field-label"
                        for="wtaiAutomaticTextOptimization<?= $block->escapeHtmlAttr($atoCnt) ?>"
                    >
                        <?= $block->escapeHtml($ato['label']) ?>
                        <?php if (!$isCronRunning && $ato['value'] === 'auto_rewrite_and_transfer'): ?>
                            <span class="wtai-auto-transfer-cron-not-available" 
                                title="<?= $block->escapeHtml($getCronJobDisabledAutoTransferMessage) ?>"></span>
                        <?php endif ?>
                    </label>
                    <?php $atoCnt++;?>
                </div>
                <?php endforeach; ?>
                <div class="wtai-error-message"></div>
            </fieldset>
            <div class="wtai-trigger-action-related-fields wtai-field-column">
                <fieldset class="wtai-fieldset" data-bind="
                    disable: disableTextTypes, css: { 'wtai-disabled': disableTextTypes }
                ">
                    <div class="wtai-fieldset-title"><?= $block->escapeHtml(__('Apply to product text types:')) ?></div>
                    <div class="wtai-fieldset-row">
                        <input type="checkbox"
                            class="admin__control-checkbox" 
                            data-bind="checked: allProductFieldsSelected"
                            id="wtaiAllProductFields"
                        />
                        <label class="wtai-label admin__field-label wtai-strong" for="wtaiAllProductFields">
                            <?= $block->escapeHtml(__('Select all')) ?>
                        </label>
                    </div>
                    <?php $productFieldsCnt = 0;?>
                    <?php foreach ($productFields as $productField): ?>
                        <div class="wtai-field-column">
                            <input type="checkbox"
                                class="admin__control-checkbox"
                                value="<?= $block->escapeHtmlAttr($productField['value']) ?>"
                                data-bind="checked: productFields"
                                name="product_texts[]"
                                id="wtaiProductFields<?= $block->escapeHtmlAttr($productFieldsCnt) ?>"
                            />
                            <label class="wtai-label admin__field-label"
                                for="wtaiProductFields<?= $block->escapeHtmlAttr($productFieldsCnt) ?>"
                            >
                                <?= $block->escapeHtml($productField['label']) ?>
                            </label>
                        </div>
                        <?php $productFieldsCnt++;?>
                    <?php endforeach; ?>
                    <div class="wtai-error-message"></div>
                </fieldset>
                <fieldset class="wtai-fieldset" data-bind="
                    disable: disableTextTypes, css: { 'wtai-disabled': disableTextTypes }
                ">
                    <div class="wtai-fieldset-title">
                        <?= $block->escapeHtml(__('Apply to category text types:')) ?>
                    </div>
                    <div class="wtai-fieldset-row">
                        <input type="checkbox"
                            class="admin__control-checkbox" 
                            data-bind="checked: allCategoryFieldsSelected"
                            id="wtaiAllCategoryFields"
                        />
                        <label class="wtai-label admin__field-label wtai-strong" for="wtaiAllCategoryFields">
                            <?= $block->escapeHtml(__('Select all')) ?>
                        </label>
                    </div>
                    <?php $categoryFieldsCnt = 0;?>
                    <?php foreach ($categoryFields as $categoryField): ?>
                        <div class="wtai-field-column">
                            <input type="checkbox"
                                class="admin__control-checkbox"
                                value="<?= $block->escapeHtmlAttr($categoryField['value']) ?>"
                                data-bind="checked: categoryFields"
                                name="category_texts[]"
                                id="wtaiCategoryFields<?= $block->escapeHtmlAttr($categoryFieldsCnt) ?>"
                            />
                            <label class="wtai-label admin__field-label"
                                for="wtaiCategoryFields<?= $block->escapeHtmlAttr($categoryFieldsCnt) ?>"
                            >
                                <?= $block->escapeHtml($categoryField['label']) ?>
                            </label>
                        </div>
                        <?php $categoryFieldsCnt++;?>
                    <?php endforeach; ?>
                    <div class="wtai-error-message"></div>
                </fieldset>
                <div class="wtai-disabled-tooltip">
                    <div class="wtai-disabled-tooltip-content">
                        <span>
                            <?= /* @noEscape */ $tooltip->getTooltip('wtai.trigger.action.none.selected') ?>
                        </span>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/x-magento-init">
    {
        "*": {
            "Magento_Ui/js/core/app": {
                "components": {
                    "wtaiSetupKeywords": {
                        "component": "WriteTextAI_WriteTextAI/js/setup/keywords",
                        "clusterCount": <?= /* @noEscape */ $defaultClusterCount ?>,
                        "minClusterCount": <?= /* @noEscape */ $rules['minClusterBasedPipelinesCount'] ?? 1?>,
                        "maxClusterCount": <?= /* @noEscape */ intval(($rules['maxKeywords'] ?? 6) - 1) ?>,
                        "clusterCountCategory": <?= /* @noEscape */ $defaultClusterCountCategory ?>,
                        "minClusterCountCategory": <?= /* @noEscape */
                            $rules['minClusterBasedPipelinesCountCategory'] ?? 1
                        ?>,
                        "maxClusterCountCategory": <?= /* @noEscape */ intval(($rules['maxKeywords'] ?? 6) - 1) ?>,
                        "triggerActionNPosition": <?= /* @noEscape */ $defaultTriggerActionNPosition ?>,
                        "minTriggerActionNPosition": <?= /* @noEscape */ $minTriggerActionNPosition ?>,
                        "maxTriggerActionNPosition": <?= /* @noEscape */ $maxTriggerActionNPosition ?>,
                        "triggerActionAfterNDays": <?= /* @noEscape */ $defaultTriggerActionNDays ?>,
                        "minTriggerActionNDays": <?= /* @noEscape */ $minTriggerActionNDays ?>,
                        "maxTriggerActionNDays": <?= /* @noEscape */ $maxTriggerActionNDays ?>,
                        "productFields": <?= /* @noEscape */ json_encode($defaultProductFields) ?>,
                        "categoryFields": <?= /* @noEscape */ json_encode($defaultCategoryFields) ?>,
                        "allProductFields": <?= /* @noEscape */ json_encode($productFieldsValues) ?>,
                        "allCategoryFields": <?= /* @noEscape */ json_encode($categoryFieldsValues) ?>,
                        "triggerAction": "<?= $block->escapeJs($defaultTriggerAction) ?>",
                        "automaticTextOptimization": "<?= $block->escapeJs($defaultAutomaticTextOptimization) ?>",
                        "hasProAccess": <?= /* @noEscape */ $hasProAccess ? 'true' : 'false' ?>
                    }
                }
            }
        }
    }
</script>
<?php
// phpcs:enable Magento2.Functions.DiscouragedFunction.DiscouragedWithAlternative
?>