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

<?php
    $templateSettings = $block->getTemplateSettings();
    $entityType = $block->getEntityType();
    $field = $block->getField();
    $templates = [];//$templateSettings->getCurrentTemplateSettings($field);
?>

<div class="admin__field wtai-select-template"
    data-bind="scope: 'wtai-select-template<?= /* @noEscape */ $field ?>'"
    id="wtai-select-template<?= /* @noEscape */ $field ?>"
>
    <label class="admin__field-label" for="wtai-template-<?= /* @noEscape */ $field ?>">
        <span><?= /* @noEscape */ __('Template') ?></span>
    </label>
    <div class="admin__field-control">

        <select name="wtai-edit-template-<?= /* @noEscape */ $field ?>"
            id="wtai-edit-template-<?= /* @noEscape */ $field ?>"
            class="admin__control-select wtai-select-template-edit"
            data-bind="
                options: templateOptions,
                optionsText: function(item) { 
                    var displayName = item.name.length > 60 ? item.name.substring(0, 60) + '...' : item.name;
                    return displayName + '\u00A0\u00A0'; 
                },
                optionsValue: 'id',
                value: selectedTemplateValue['<?= /* @noEscape */ $field ?>'],
                optionsCaption: '<?= /* @noEscape */ __('No template') ?>',
                event: { 
                    change: function(data, event) { 
                        onTemplateChange('<?= /* @noEscape */ $field ?>', event);
                    }
                },
                optionsAfterRender: function(option, item) {
                    if (item && item.name) {
                        option.title = item.name;
                    }
                }
            ">
        </select>
    </div>
    <div class="admin__field-tooltip wtai-small">
        <a class="admin__field-tooltip-action" href="#" data-toggle="dropdown"></a>
        <div class="admin__field-tooltip-content" data-target="dropdown">
            <div class="wtai-content-wrapper">
                <?= /* @noEscape */ sprintf(
                    __('Don’t see any templates?'
                        . ' You can create and assign them in <a href="%s" target="_blank">platform.writetext.ai</a>. '
                        . 'Assigned templates will appear here.'),
                                          'https://platform.writetext.ai/webshops/'
                ) ?>
            </div>
        </div>
    </div>
</div>
<script type="text/x-magento-init">
    {
        "#wtai-select-template<?= /* @noEscape */ $field ?>": {
            "Magento_Ui/js/core/app": {
                "components": {
                    "wtai-select-template<?= /* @noEscape */ $field ?>": {
                        "component": "WriteTextAI_WriteTextAI/js/edit/select-template",
                        "field": "<?= /* @noEscape */ $field ?>",
                        "entityType": "<?= /* @noEscape */ $entityType ?>",
                        "saveUrl": "<?= /* @noEscape */ $block->getUrl('wtai/settings/savetemplateselected') ?>",
                        "initialTemplates": <?= /* @noEscape */ json_encode($templates) ?>
                    }
                }
            }
        }
    }
</script>