<?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
        + '<?= $block->escapeHtmlAttr($difficulty) ?>'
}">
    <button  data-toggle="dropdown" data-bind="attr: {
            class: 'wtai-dropdown-trigger wtai-dropdown-btn'
                + $pipeline.id
                + '<?= $block->escapeHtmlAttr($difficulty) ?>'
        },
        text: $parent.getTriggerDropdownLabel($pipeline.id, '<?= $block->escapeHtml($difficulty) ?>'),
        disable: $parent.triggerSettingsDisabled"
    >
            <?= $block->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 + '<?= $block->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"><?= $block->escapeHtml(__('Trigger action')) ?></div>
                    </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 trigger-action-input"
                            value="<?= $block->escapeHtmlAttr($whenPageIsRankingValue) ?>"
                            id="wtaiTriggerActionNPosition"
                            data-bind="attr: {
                                    'checked': $parent.isTriggerActionSelected(
                                        $pipeline.id,
                                        'WhenRanking',
                                        '<?= $block->escapeHtml($difficulty) ?>'
                                    ),
                                    id: 'wtaiTriggerActionNPosition' + $pipeline.id +
                                        '<?= $block->escapeHtml($difficulty) ?>',
                                    name: 'trigger_action' + $pipeline.id + '<?= $block->escapeHtml($difficulty) ?>'
                                }, click: function() {
                                    return $parent.selectTriggerAction();
                                }"
                        />
                        <label class="wtai-label admin__field-label" for="wtaiTriggerActionNPosition"
                            data-bind="
                                attr: {
                                    for: 'wtaiTriggerActionNPosition' + $pipeline.id +
                                    '<?= $block->escapeHtml($difficulty) ?>'
                                }
                            ">
                            <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"
                                        name="trigger_action_n_position"
                                        data-bind="value:
                                            $parent.getTriggerActionNPosition($pipeline.id,
                                            '<?= $block->escapeHtml($difficulty) ?>'),
                                            attr: {
                                                'min': <?= $block->escapeHtml($minTriggerActionNPosition) ?>,
                                                'max': <?= $block->escapeHtml($maxTriggerActionNPosition) ?>
                                            },
                                            event: {
                                                keypress: $parent.ensureNumerical
                                            }
                                        "
                                    />
                                </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 trigger-action-input"
                            value="<?= $block->escapeHtmlAttr($afterNDaysValue) ?>"
                            data-bind="attr: {
                                    'checked': $parent.isTriggerActionSelected(
                                        $pipeline.id,
                                        'AfterSetDays',
                                        '<?= $block->escapeHtml($difficulty) ?>'
                                    ),
                                    id: 'wtaiTriggerActionAfterNDays' + $pipeline.id +
                                        '<?= $block->escapeHtml($difficulty) ?>',
                                    name: 'trigger_action' + $pipeline.id + '<?= $block->escapeHtml($difficulty) ?>'
                                }, click: function() {
                                    return $parent.selectTriggerAction();
                                }"
                            required
                        />
                        <label class="wtai-label admin__field-label"
                            data-bind="
                                attr: {
                                    for: 'wtaiTriggerActionAfterNDays' + $pipeline.id +
                                    '<?= $block->escapeHtml($difficulty) ?>'
                                }
                            ">
                            <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"
                                        name="trigger_action_n_days"
                                        data-bind="value:
                                            $parent.getTriggerActionNDays($pipeline.id,
                                            '<?= $block->escapeHtml($difficulty) ?>'),
                                            attr: {
                                                'min': <?= $block->escapeHtml($minTriggerActionNDays) ?>,
                                                'max': <?= $block->escapeHtml($maxTriggerActionNDays) ?>
                                            },
                                            event: {
                                                keypress: $parent.ensureNumerical
                                            }
                                        "
                                    />
                                </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 trigger-action-input"
                            value="none"
                            data-bind="
                                attr: {
                                    'checked': $parent.isTriggerActionSelected(
                                        $pipeline.id,
                                        'None',
                                        '<?= $block->escapeHtml($difficulty) ?>'
                                    ),
                                    id: 'wtaiTriggerActionNone'
                                        + $pipeline.id
                                        + '<?= $block->escapeHtml($difficulty) ?>',
                                    name: 'trigger_action'
                                        + $pipeline.id
                                        + '<?= $block->escapeHtml($difficulty) ?>'
                                },
                                click: function() {
                                        return $parent.selectTriggerAction();
                                }
                            "
                            required
                        />
                        <label class="wtai-label admin__field-label"
                            data-bind="
                                attr: {
                                    for: 'wtaiTriggerActionNone'
                                        + $pipeline.id
                                        + '<?= $block->escapeHtml($difficulty) ?>'
                                }
                            ">
                            <?= $block->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">
                        <?= $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 wtai-auto-text-optimization-input"
                            value="<?= $block->escapeHtmlAttr($ato['value']) ?>"
                            name="automatic_text_optimization"
                            id="wtaiAutomaticTextOptimization<?= $block->escapeHtmlAttr($atoCnt) ?>"
                            data-bind="attr: {
                                    'checked': $parent.isAutomaticTextOptimizationSelected(
                                        $pipeline.id,
                                        '<?= $block->escapeHtml($ato['value']) ?>',
                                        '<?= $block->escapeHtml($difficulty) ?>'
                                    ),
                                    id: 'wtaiAutomaticTextOptimization' +
                                        $pipeline.id +
                                        '<?= $block->escapeHtml($ato['value']) ?>' +
                                        '<?= $block->escapeHtml($difficulty) ?>',
                                    name: 'automatic_text_optimization' + $pipeline.id +
                                        '<?= $block->escapeHtml($difficulty) ?>'
                                }, click: function() {
                                    return $parent.selectAutomaticTextOptimization();
                                }"
                            required
                        />
                        <label class="wtai-label admin__field-label"
                            data-bind="attr: { for: 'wtaiAutomaticTextOptimization' +
                                        $pipeline.id +
                                        '<?= $block->escapeHtml($ato['value']) ?>' +
                                        '<?= $block->escapeHtml($difficulty) ?>',}"
                        >
                            <?= $block->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'
                    ">
                        <?= $block->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">
                        <?= $block->escapeHtml(__('Automatically generate with updated keywords')) ?>
                    </div>
                    <div class="wtai-fieldset-subtitle">
                        <?= $block->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,
                                    '<?= $block->escapeHtml($difficulty) ?>'
                                ),
                                attr: {
                                    id: 'wtaiAllTextFields' + $pipeline.id + '<?= $block->escapeHtml($difficulty) ?>'
                                },
                                click: function(data, event) {
                                    return $parent.selectAllFields(
                                    data,
                                    event,
                                    $pipeline.id,
                                    '<?= $block->escapeHtml($difficulty) ?>'
                                );
                                
                            }"
                        />
                        <label class="wtai-label admin__field-label wtai-strong"
                            data-bind="attr: {
                                for: 'wtaiAllTextFields' + $pipeline.id + '<?= $block->escapeHtml($difficulty) ?>'
                            }"
                        >
                            <?= $block->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="<?= $block->escapeHtmlAttr($textField['value']) ?>"
                                    data-bind="attr: {
                                        'checked': $parent.isTextFieldSelected(
                                            $pipeline.id,
                                            '<?= $block->escapeHtml($textField['value']) ?>',
                                            '<?= $block->escapeHtml($difficulty) ?>'
                                        ),
                                        id: 'wtaiTriggerTextFields' +
                                            $pipeline.id +
                                            '<?= $block->escapeHtml($textField['value']) ?>' +
                                            '<?= $block->escapeHtml($difficulty) ?>',
                                        name: 'text_fields_' + $pipeline.id + '<?= $block->escapeHtml($difficulty) ?>'
                                    }, click: function(data, event) {
                                        return $parent.selectTextField(
                                            data,
                                            event,
                                            $pipeline.id,
                                            '<?= $block->escapeHtml($difficulty) ?>'
                                        );
                                    }"
                                />
                                <label class="wtai-label admin__field-label"
                                    data-bind="attr: { for: 'wtaiTriggerTextFields' +
                                                $pipeline.id +
                                                '<?= $block->escapeHtml($textField['value']) ?>' +
                                                '<?= $block->escapeHtml($difficulty) ?>',}"
                                >
                                    <?= $block->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()}
                    ">
                        <?= $block->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,
                                '<?= $block->escapeHtml($difficulty) ?>'
                            );
                        }
                    "><?= $block->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 + '<?= $block->escapeHtmlAttr($difficulty) ?>'
                    }">
                    </button>
                    <ul data-bind="attr: {
                        class: 'wtai-dropdown-more-actions wtai-actions-content'
                            + $pipeline.id
                            + '<?= $block->escapeHtmlAttr($difficulty) ?>' 
                    }">
                        <li >
                            <a href="#" class="item wtai-dropdown-action"
                                data-bind="click: function() {
                                    $parent.applyTriggerActionToAll(
                                        $pipeline.id,
                                        '<?= $block->escapeHtml($difficulty) ?>'
                                    );
                                }">
                                <span><?= $block->escapeHtml(__('Apply to all')) ?></span>
                            </a>
                        </li>
                        <li>
                            <a href="#" class="item wtai-dropdown-action"
                                data-bind="click: function() {
                                    $parent.applyTriggerActionToPipeline(
                                        $pipeline.id,
                                        '<?= $block->escapeHtml($difficulty) ?>'
                                    );
                                }"
                            >
                                <span><?= $block->escapeHtml(__('Apply to pipeline')) ?></span>
                            </a>
                        </li>
                        <li>
                            <a href="#" class="item wtai-dropdown-action"
                                data-bind="click: function() {
                                    $parent.applyTriggerActionToSameDifficulty(
                                        $pipeline.id,
                                        '<?= $block->escapeHtml($difficulty) ?>'
                                    );
                                }"
                            >
                                <span><?= $block->escapeHtml(
                                    sprintf(__('Apply to row'), $difficultyLabel)
                                ) ?></span>
                            </a>
                        </li>
                    </ul>
                </div>
            </div>
        </div>
        <div class="wtai-edit-trigger-loading-mask"></div>
    </div>
</div>