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

<?php
/** @var \WriteTextAI\WriteTextAI\ViewModel\PipelineSettings $pipelineSettingsViewModel */
$parentBlock = $block->getParentBlock()->getParentBlock()->getParentBlock();
$pipelineSettingsViewModel = $parentBlock->getPipelineSettings();
$rules = $pipelineSettingsViewModel->getRules();
$minTriggerActionNPosition = $rules['minTriggerActionNPosition'] ?? 2;
$maxTriggerActionNPosition = $rules['maxTriggerActionNPosition'] ?? 100;
$minTriggerActionNDays = $rules['minTriggerActionNDays'] ?? 1;
$maxTriggerActionNDays = $rules['maxTriggerActionNDays'] ?? 365;
$entityType = $parentBlock->getEntityType();
$textFields = $pipelineSettingsViewModel->getTriggerSettingsTextFields($entityType);
$automaticTextOptimization = $pipelineSettingsViewModel->getAutomaticTextOptimization();
$whenPageIsRankingValue = 'when_page_is_ranking';
$afterNDaysValue = 'after_n_days';
$difficulty = $block->getParentBlock()->getDifficulty();
$difficultyLabel = $block->getParentBlock()->getDifficultyLabel();
?>

<div data-bind="attr: {
    class: 'wtai-trigger-settings wtai-dropdown-wrapper'
        + $pipeline.id
        + '<?= $escaper->escapeHtmlAttr($difficulty) ?>'
}">
    <button  data-toggle="dropdown" data-bind="attr: {
            class: 'wtai-dropdown-trigger wtai-dropdown-btn'
                + $pipeline.id
                + '<?= $escaper->escapeHtmlAttr($difficulty) ?>'
        },
        text: $parent.getTriggerDropdownLabel($pipeline.id, '<?= $escaper->escapeHtml($difficulty) ?>'),
        disable: $parent.triggerSettingsDisabled"
    >
            <?= $escaper->escapeHtml(__('When page is ranking')) ?>
    </button>
    <div
        data-target="dropdown"
        data-bind="attr: { class: 'wtai-dropdown-content wtai-hide-initially wtai-dropdown-content' +
            $pipeline.id + '<?= $escaper->escapeHtmlAttr($difficulty) ?>' },
            css: $parent.tsContainerClass"
    >
        <div class="wtai-dropdown-block">
            <div class="wtai-fields-container">
                <fieldset class="wtai-fieldset">
                    <div class="wtai-trigger-action-header">
                        <div class="wtai-fieldset-title"><?= $escaper->escapeHtml(__('Trigger action')) ?></div>
                    </div>
                    <div class="wtai-fieldset-subtitle">
                        <?= $escaper->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 trigger-action-input"
                            value="<?= $escaper->escapeHtmlAttr($whenPageIsRankingValue) ?>"
                            id="wtaiTriggerActionNPosition"
                            data-bind="attr: {
                                    'checked': $parent.isTriggerActionSelected(
                                        $pipeline.id,
                                        'WhenRanking',
                                        '<?= $escaper->escapeHtml($difficulty) ?>'
                                    ),
                                    id: 'wtaiTriggerActionNPosition' + $pipeline.id +
                                        '<?= $escaper->escapeHtml($difficulty) ?>',
                                    name: 'trigger_action' + $pipeline.id + '<?= $escaper->escapeHtml($difficulty) ?>'
                                }, click: function() {
                                    return $parent.selectTriggerAction();
                                }"
                        />
                        <label class="wtai-label admin__field-label" for="wtaiTriggerActionNPosition"
                            data-bind="
                                attr: {
                                    for: 'wtaiTriggerActionNPosition' + $pipeline.id +
                                    '<?= $escaper->escapeHtml($difficulty) ?>'
                                }
                            ">
                            <div class="wtai-label-with-input">
                                <span><?= $escaper->escapeHtml(__('When page is ranking within position 1 -')) ?></span>
                                <div class="wtai-control">
                                    <input type="number"
                                        class="admin__control-text wtai-input-number"
                                        name="trigger_action_n_position"
                                        data-bind="value:
                                            $parent.getTriggerActionNPosition($pipeline.id,
                                            '<?= $escaper->escapeHtml($difficulty) ?>'),
                                            attr: {
                                                'min': <?= $escaper->escapeHtml($minTriggerActionNPosition) ?>,
                                                'max': <?= $escaper->escapeHtml($maxTriggerActionNPosition) ?>
                                            },
                                            event: {
                                                keypress: $parent.ensureNumerical
                                            }
                                        "
                                    />
                                </div>
                                <span><?= $escaper->escapeHtml(__('in the SERP')) ?></span>
                            </div>
                        </label>
                    </div>
                    <div class="wtai-field-column wtai-full-width">
                        <input type="radio"
                            class="admin__control-radio trigger-action-input"
                            value="<?= $escaper->escapeHtmlAttr($afterNDaysValue) ?>"
                            data-bind="attr: {
                                    'checked': $parent.isTriggerActionSelected(
                                        $pipeline.id,
                                        'AfterSetDays',
                                        '<?= $escaper->escapeHtml($difficulty) ?>'
                                    ),
                                    id: 'wtaiTriggerActionAfterNDays' + $pipeline.id +
                                        '<?= $escaper->escapeHtml($difficulty) ?>',
                                    name: 'trigger_action' + $pipeline.id + '<?= $escaper->escapeHtml($difficulty) ?>'
                                }, click: function() {
                                    return $parent.selectTriggerAction();
                                }"
                            required
                        />
                        <label class="wtai-label admin__field-label"
                            data-bind="
                                attr: {
                                    for: 'wtaiTriggerActionAfterNDays' + $pipeline.id +
                                    '<?= $escaper->escapeHtml($difficulty) ?>'
                                }
                            ">
                            <div class="wtai-label-with-input">
                                <span><?= $escaper->escapeHtml(__('After ')) ?></span>
                                <div class="wtai-control">
                                    <input type="number"
                                        class="admin__control-text wtai-input-number"
                                        name="trigger_action_n_days"
                                        data-bind="value:
                                            $parent.getTriggerActionNDays($pipeline.id,
                                            '<?= $escaper->escapeHtml($difficulty) ?>'),
                                            attr: {
                                                'min': <?= $escaper->escapeHtml($minTriggerActionNDays) ?>,
                                                'max': <?= $escaper->escapeHtml($maxTriggerActionNDays) ?>
                                            },
                                            event: {
                                                keypress: $parent.ensureNumerical
                                            }
                                        "
                                    />
                                </div>
                                <span><?= $escaper->escapeHtml(__('day/s')) ?></span>
                            </div>
                        </label>
                    </div>
                    <div class="wtai-field-column wtai-full-width">
                        <input type="radio"
                            class="admin__control-radio trigger-action-input"
                            value="none"
                            data-bind="
                                attr: {
                                    'checked': $parent.isTriggerActionSelected(
                                        $pipeline.id,
                                        'None',
                                        '<?= $escaper->escapeHtml($difficulty) ?>'
                                    ),
                                    id: 'wtaiTriggerActionNone'
                                        + $pipeline.id
                                        + '<?= $escaper->escapeHtml($difficulty) ?>',
                                    name: 'trigger_action'
                                        + $pipeline.id
                                        + '<?= $escaper->escapeHtml($difficulty) ?>'
                                },
                                click: function() {
                                        return $parent.selectTriggerAction();
                                }
                            "
                            required
                        />
                        <label class="wtai-label admin__field-label"
                            data-bind="
                                attr: {
                                    for: 'wtaiTriggerActionNone'
                                        + $pipeline.id
                                        + '<?= $escaper->escapeHtml($difficulty) ?>'
                                }
                            ">
                            <?= $escaper->escapeHtml(__('None')) ?>
                        </label>
                    </div>
                    <div class="wtai-error-message"></div>
                </fieldset>
                <fieldset class="wtai-fieldset"
                    data-bind="disable: $parent.tsActiveTriggerAction() === 'none',
                        css: {'wtai-disabled': $parent.tsActiveTriggerAction() === 'none'}"
                >
                    <div class="wtai-fieldset-title wtai-ato-title">
                        <?= $escaper->escapeHtml(__('Automatic text optimization in the pipeline')) ?>
                    </div>
                    <div class="wtai-fieldset-subtitle">
                        <?= $escaper->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 wtai-auto-text-optimization-input"
                            value="<?= $escaper->escapeHtmlAttr($ato['value']) ?>"
                            name="automatic_text_optimization"
                            id="wtaiAutomaticTextOptimization<?= $escaper->escapeHtmlAttr($atoCnt) ?>"
                            data-bind="attr: {
                                    'checked': $parent.isAutomaticTextOptimizationSelected(
                                        $pipeline.id,
                                        '<?= $escaper->escapeHtml($ato['value']) ?>',
                                        '<?= $escaper->escapeHtml($difficulty) ?>'
                                    ),
                                    id: 'wtaiAutomaticTextOptimization' +
                                        $pipeline.id +
                                        '<?= $escaper->escapeHtml($ato['value']) ?>' +
                                        '<?= $escaper->escapeHtml($difficulty) ?>',
                                    name: 'automatic_text_optimization' + $pipeline.id +
                                        '<?= $escaper->escapeHtml($difficulty) ?>'
                                }, click: function() {
                                    return $parent.selectAutomaticTextOptimization();
                                }"
                            required
                        />
                        <label class="wtai-label admin__field-label"
                            data-bind="attr: { for: 'wtaiAutomaticTextOptimization' +
                                        $pipeline.id +
                                        '<?= $escaper->escapeHtml($ato['value']) ?>' +
                                        '<?= $escaper->escapeHtml($difficulty) ?>',}"
                        >
                            <?= $escaper->escapeHtml($ato['label']) ?>
                        </label>
                        <?php $atoCnt++;?>
                    </div>
                    <?php endforeach; ?>
                    <div class="wtai-error-message"></div>
                    <span class="tooltip-toggle"></span>
                    <span class="tooltip-content" data-bind="
                        visible: $parent.tsActiveTriggerAction() === 'none'
                    ">
                        <?= $escaper->escapeHtml(__('This section is only enabled if a trigger action is set.')) ?>
                    </span>
                </fieldset>
                <fieldset class="wtai-fieldset wtai-rewrite-fieldset"
                    data-bind="disable: $parent.disableRewriteFields(),
                        css: {'wtai-disabled': $parent.disableRewriteFields()}">
                    <div class="wtai-fieldset-title">
                        <?= $escaper->escapeHtml(__('Automatically generate with updated keywords')) ?>
                    </div>
                    <div class="wtai-fieldset-subtitle">
                        <?= $escaper->escapeHtml(__(
                            'Only text types previously generated in WriteText.ai will be updated; '
                            . 'otherwise it will be skipped.'
                        )) ?>
                    </div>
                    <div class="wtai-fieldset-row">
                        <input type="checkbox"
                            class="admin__control-checkbox"
                            data-bind="
                                checked: $parent.isAllTextFieldsSelected(
                                    $pipeline.id,
                                    '<?= $escaper->escapeHtml($difficulty) ?>'
                                ),
                                attr: {
                                    id: 'wtaiAllTextFields' + $pipeline.id + '<?= $escaper->escapeHtml($difficulty) ?>'
                                },
                                click: function(data, event) {
                                    return $parent.selectAllFields(
                                    data,
                                    event,
                                    $pipeline.id,
                                    '<?= $escaper->escapeHtml($difficulty) ?>'
                                );
                                
                            }"
                        />
                        <label class="wtai-label admin__field-label wtai-strong"
                            data-bind="attr: {
                                for: 'wtaiAllTextFields' + $pipeline.id + '<?= $escaper->escapeHtml($difficulty) ?>'
                            }"
                        >
                            <?= $escaper->escapeHtml(__('Select all')) ?>
                        </label>
                    </div>
                    <div class="wtai-field-columns">
                        <?php $textFieldsCnt = 0;?>
                        <?php foreach ($textFields as $textField): ?>
                            <div class="wtai-field-column">
                                <input type="checkbox"
                                    class="admin__control-checkbox"
                                    value="<?= $escaper->escapeHtmlAttr($textField['value']) ?>"
                                    data-bind="attr: {
                                        'checked': $parent.isTextFieldSelected(
                                            $pipeline.id,
                                            '<?= $escaper->escapeHtml($textField['value']) ?>',
                                            '<?= $escaper->escapeHtml($difficulty) ?>'
                                        ),
                                        id: 'wtaiTriggerTextFields' +
                                            $pipeline.id +
                                            '<?= $escaper->escapeHtml($textField['value']) ?>' +
                                            '<?= $escaper->escapeHtml($difficulty) ?>',
                                        name: 'text_fields_' + $pipeline.id + '<?= $escaper->escapeHtml($difficulty) ?>'
                                    }, click: function(data, event) {
                                        return $parent.selectTextField(
                                            data,
                                            event,
                                            $pipeline.id,
                                            '<?= $escaper->escapeHtml($difficulty) ?>'
                                        );
                                    }"
                                />
                                <label class="wtai-label admin__field-label"
                                    data-bind="attr: { for: 'wtaiTriggerTextFields' +
                                                $pipeline.id +
                                                '<?= $escaper->escapeHtml($textField['value']) ?>' +
                                                '<?= $escaper->escapeHtml($difficulty) ?>',}"
                                >
                                    <?= $escaper->escapeHtml($textField['label']) ?>
                                </label>
                            </div>
                            <?php $textFieldsCnt++;?>
                        <?php endforeach; ?>
                    </div>
                    <div class="wtai-error-message"></div>
                    <span class="tooltip-toggle"></span>
                    <span class="tooltip-content" data-bind="
                        visible: $parent.disableRewriteFields(),
                        css: {'wtai-disabled': $parent.disableRewriteFields()}
                    ">
                        <?= $escaper->escapeHtml(
                            __('This section is only enabled if any of the auto regenerate options is selected.')
                        ) ?>
                    </span>
                </fieldset>
            </div>
        </div>
        <div class="wtai-row">
            <div class="wtai-dropdown-actions">
                <button class="wtai-pipeline-setting-btn wtai-apply-settings"
                    data-bind="click: function() { 
                            $parent.applyTriggerAction(
                                $pipeline.id,
                                '<?= $escaper->escapeHtml($difficulty) ?>'
                            );
                        }
                    "><?= $escaper->escapeHtml(__('Apply')) ?></button>
                <div class="wtai-dropdown-action-container">
                    <button data-bind="attr: {
                        class: 'wtai-pipeline-setting-btn wtai-apply-more-settings wtai-dropdown-btn-action'
                            + $pipeline.id + '<?= $escaper->escapeHtmlAttr($difficulty) ?>'
                    }">
                    </button>
                    <ul data-bind="attr: {
                        class: 'wtai-dropdown-more-actions wtai-actions-content'
                            + $pipeline.id
                            + '<?= $escaper->escapeHtmlAttr($difficulty) ?>' 
                    }">
                        <li >
                            <a href="#" class="item wtai-dropdown-action"
                                data-bind="click: function() {
                                    $parent.applyTriggerActionToAll(
                                        $pipeline.id,
                                        '<?= $escaper->escapeHtml($difficulty) ?>'
                                    );
                                }">
                                <span><?= $escaper->escapeHtml(__('Apply to all')) ?></span>
                            </a>
                        </li>
                        <li>
                            <a href="#" class="item wtai-dropdown-action"
                                data-bind="click: function() {
                                    $parent.applyTriggerActionToPipeline(
                                        $pipeline.id,
                                        '<?= $escaper->escapeHtml($difficulty) ?>'
                                    );
                                }"
                            >
                                <span><?= $escaper->escapeHtml(__('Apply to pipeline')) ?></span>
                            </a>
                        </li>
                        <li>
                            <a href="#" class="item wtai-dropdown-action"
                                data-bind="click: function() {
                                    $parent.applyTriggerActionToSameDifficulty(
                                        $pipeline.id,
                                        '<?= $escaper->escapeHtml($difficulty) ?>'
                                    );
                                }"
                            >
                                <span><?= $escaper->escapeHtml(
                                    sprintf(__('Apply to row'), $difficultyLabel)
                                ) ?></span>
                            </a>
                        </li>
                    </ul>
                </div>
            </div>
        </div>
        <div class="wtai-edit-trigger-loading-mask"></div>
    </div>
</div>