<?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<?= $field ?>'" id="wtai-select-template<?= $field ?>">
    <label class="admin__field-label" for="wtai-template-<?= $field ?>">
        <span><?= __('Template') ?></span>
    </label>
    <div class="admin__field-control">

        <select name="wtai-edit-template-<?= $field ?>" id="wtai-edit-template-<?= $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['<?= $field ?>'],
                optionsCaption: '<?= __('No template') ?>',
                event: { 
                    change: function(data, event) { 
                        onTemplateChange('<?= $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<?= $field ?>": {
            "Magento_Ui/js/core/app": {
                "components": {
                    "wtai-select-template<?= $field ?>": {
                        "component": "WriteTextAI_WriteTextAI/js/edit/select-template",
                        "field": "<?= $field ?>",
                        "entityType": "<?= $entityType ?>",
                        "saveUrl": "<?= $block->getUrl('wtai/settings/savetemplateselected') ?>",
                        "initialTemplates": <?= json_encode($templates) ?>
                    }
                }
            }
        }
    }
</script>