{extends file="admin-layout.tpl"}

{block name="no-return-functions"}
    {$admin_current_location = 'configuration'}
{/block}

{block name="page-title"}{intl l='Thelia Mailing System'}{/block}

{block name="check-resource"}admin.configuration.mailing-system{/block}
{block name="check-access"}view{/block}

{block name="main-content"}
<div class="mailing-system">

    <div id="wrapper" class="container">

        <ul class="breadcrumb">
            <li><a href="{url path='/admin/home'}">{intl l="Home"}</a></li>
            <li><a href="{url path='/admin/configuration'}">{intl l="Configuration"}</a></li>
            <li><a href="{url path='/admin/configuration/mailingSystem'}">{intl l="Mailing system"}</a></li>
        </ul>

        {hook name="mailing-system.top" location="mailing_system_top" }

        <div class="row">
            <div class="col-md-12">
                <div class="general-block-decorator">

                    <div class="title title-without-tabs">{intl l="Configuration mailing system"}</div>

                    {form name="thelia.admin.mailing-system.update"}

                    <form method="POST" action="{url path="/admin/configuration/mailingSystem/save"}" {form_enctype} >

                        <div class="row inner-toolbar">
                            <div class="col-md-12 inner-actions clearfix">
                                <button type="submit" name="save_mode" value="stay" class="btn btn-default btn-success pull-right" title="{intl l="Save SMTP configuration"}">{intl l="Save"} <span class="glyphicon glyphicon-ok"></span></button>
                            </div>
                        </div>

                        {form_hidden_fields}

                        {if $form_error}<div class="alert alert-danger">{$form_error_message}</div>{/if}

                        {render_form_field field='enabled'}


                        <div class="row">
                            <div class="col-sm-6">
                                {render_form_field field='host'}
                            </div>

                            <div class="col-sm-6">
                                {render_form_field field='port'}
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-sm-6">
                                {render_form_field field='username'}
                            </div>
                            <div class="col-sm-6">
                                {render_form_field field='password'}
                            </div>
                        </div>


                        <div class="row">
                            <div class="col-sm-6">
                                {render_form_field field='encryption'}
                            </div>

                            <div class="col-sm-6">
                                {render_form_field field='authmode'}
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-sm-6">
                                {render_form_field field='timeout'}
                            </div>

                            <div class="col-sm-6">
                                {render_form_field field='sourceip'}
                            </div>
                        </div>

                        <div class="row inner-toolbar">
                            <div class="col-md-12 inner-actions clearfix">
                                <button type="submit" name="save_mode" value="stay" class="btn btn-default btn-success pull-right" title="{intl l="Save SMTP configuration"}">{intl l="Save"} <span class="glyphicon glyphicon-ok"></span></button>
                            </div>
                        </div>
                    </form>
                    {/form}
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-md-12">
                <div class="general-block-decorator">
                    <div class="title title-without-tabs">{intl l="Test your configuration"}</div>

                    <form id="test-configuration" class="form-inline" onsubmit="return false;" role="form">
                        <div class="form-group">
                            <label class="sr-only" for="test-email">{intl l="Your email"}</label>
                            <input type="text" class="form-control" id="test-email" name="test-email" value="{config key="store_email"}">
                        </div>
                        <button type="button" id="test-send" class="btn btn-primary">{intl l="Send"}</button>
                        <div>&nbsp;</div>
                        <div id="test-result"></div>
                    </form>
                </div>
            </div>
        </div>

        {hook name="mailing-system.bottom" location="mailing_system_bottom" }

    </div>
</div>
{/block}

{block name="javascript-initialization"}
    {javascripts file='assets/js/bootstrap-switch/bootstrap-switch.js'}
        <script src="{$asset_url}"></script>
    {/javascripts}

<script type="text/javascript">

jQuery(function($) {
    $('#enable-smtp').on('switch-change', function (e, data) {
        $('#smtp_enabled').val(data.value ? 1 : 0);
    });

    $('#test-send').on("click", function(e){
        e.preventDefault();

        $('body').append('<div class="modal-backdrop fade in" id="loading-event"><div class="loading"></div></div>');

        $.ajax({
            url: '{url path="/admin/configuration/mailingSystem/test"}',
            data: {
                email: $('#test-email').val()
            },
            type: "GET",
            dataType: "json"
        }).done(function(data){
            var messages = '',
                    i = 0
            $("#loading-event").remove();
            if (data.success){
                $("#test-result").removeClass("alert alert-danger").html(data.message);
            } else {
                $("#test-result").addClass("alert alert-danger").html(data.message);
            }
        })
        .fail(function(jqXHR, textStatus, errorThrown){
            $("#loading-event").remove();
            $("#test-result").html(textStatus).addClass("alert alert-danger");
        });

    });

});

</script>
{/block}

{block name="javascript-last-call"}
    {hook name="mailing-system.js" location="mainling-system-js" }
{/block}
