{% extends 'Admin/layout.html.twig' %}
{% set themeLight = true %}

{% block stylesheets %}
<style type="text/css">
    #sms_campaign_pee_recipients .form-row-field-child {
        float: left;
        width: 50%;
    }

    #sms_campaign_pee_content {
        width: auto;
    }
</style>
{% endblock %}

{% block javascripts %}
<script type="text/javascript">
    (function($){
        var model_selector = $('select.load-model');
        
        // Add BTN after model selector
        model_selector.after('&nbsp;<a class="btn mt-2" href="{{ path("admin_sms_model_index") }}" target="_blank">{{ "action.manage.sms.model"|trans({}, "action") }}</a>');
        model_selector.after('&nbsp;<button class="btn btn-load-model mt-2" type="button">{{ "action.load.sms.model"|trans({}, "action") }}</button>');
        
        // Add Ajax call to get model
        function loadModel (id) {
            $.ajax({
                type: "POST",
                url: "{{ path('admin_sms_model_get_content') }}",
                data: {
                    'id': id
                },
                dataType: "json",
                cache: false
            })
            .done( function(data) {
                // Populate Textarea value with model content
                var target = model_selector.attr('data-target');
                $(target).val(data.content);

                countdownTextarea($(target));
            })
            .fail( function(jqXHR, textStatus, errorThrown) {
                console.log(jqXHR);
                console.log(textStatus);
                console.log(errorThrown);
                alert("Erreur lors de la modification !");
            });
        }

        $('body').on('click', 'button.btn-load-model', function (e) {
            e.preventDefault();
            loadModel(
                model_selector.val()
            );
            return false;
        });

        var content_textarea = $('#sms_campaign_pee_content');
        
        // Add BTN after model selector
        content_textarea.after('&nbsp;<button class="btn btn-update-content mt-2" type="button">{{ "action.load.sms.content"|trans({}, "action") }}</button>');
        
        // Add Ajax call to get content updated with keyword
        function loadContent (content) {
            if (content.length > 0) {
                $.ajax({
                    type: "POST",
                    url: "{{ path('admin_sms_campaign_pee_update_content', {'id':ope}) }}",
                    data: {
                        'content': content
                    },
                    dataType: "json",
                    cache: false
                })
                .done( function(data) {
                    // Populate Textarea value with model content
                    content_textarea.val(data.content);

                    countdownTextarea(content_textarea);
                })
                .fail( function(jqXHR, textStatus, errorThrown) {
                    console.log(jqXHR);
                    console.log(textStatus);
                    console.log(errorThrown);
                    alert("Erreur lors de la modification !");
                });
            }
        }

        $('body').on('click', 'button.btn-update-content', function (e) {
            e.preventDefault();
            loadContent(
                content_textarea.val()
            );
            return false;
        });
        
    })(jQuery);
</script>
{% endblock javascripts %}

{% block body %}
    <header class="header">
        <div class="header-wrapper">
            <h1 class="header-title">{{ 'form.sms.admin.modal.title'|trans({}, 'form') }}</h1>
            <nav class="header-actions">
                <ul class="btns">
                    <li>
                        <button class="btn btn-save" form="form-sms-campaign-add">
                            {{ 'action.send.sms'|transchoice(2, {}, 'action') }}
                        </button>
                    </li>
                </ul>
            </nav>
        </div>
    </header>

    {{ form(form, {'attr': {'id': 'form-sms-campaign-add'}}) }}

{% endblock body %}
