<!-- Nav tabs -->
<div class="col-lg-12 page-title"><h1 class="text-uppercase">{{ Lang_translate( title , 'menu') }}</h1></div>
<br/>


<br/><br/>
{% if message %}
    <div id="container-feedback" class="container-feedback">
        <span class="ico-sprite fd_info"><span>{{ Lang_translate('_DETAILS', 'standard') }}</span></span> {{ message }}
    </div>
{% endif %}

<div class="col-lg-12">
    <div class="row nav nav-pills">
         <div class="form-group">
                <a style='float:right;' class="btn btn-success" href='index.php?r=adm/mailconfig/insert'>{{ Lang_translate('_INSERT','standard') }}</a>
            </div>
       
        </div>
    </div>
</div>
    <table class="table">
    <thead>
        <tr>
        <th scope="col">#</th>
        <th scope="col">{{ Lang_translate('_NAME', 'standard') }}</th>
        <th scope="col">{{ Lang_translate('_DEFAULT', 'standard') }}</th>
        <th scope="col">{{ Lang_translate('_ACTIVE', 'standard') }}</th>
        <th scope="col">{{ Lang_translate('_ACTIONS', 'standard') }}</th>
        </tr>
    </thead>
    <tbody>
    {% for setting in settings %}
        {% include 'row.html.twig' %}
    {% endfor %}
    </tbody>
    </table>

    <script>

    $(document).ready(function() {

        $('.defaultChoice').on('click', function(e) {

            e.preventDefault();
            var thisobj = $(this);
            
             $.ajax({

                    'url' : 'ajax.adm_server.php?r=adm/mailconfig/setSystem',
                    'type' : 'GET',
                    'data' : {
                        'id' : thisobj.val()
                    },
                    'success' : function(data) {              
                        $('.defaultChoice').prop('checked', false);
                        $('.btn-delete-confirm').show();
                        thisobj.prop('checked', true);
                        thisobj.parents('tr').find('a.btn-delete-confirm').hide();
                    
                    },
                    'error' : function(request,error)
                    {
                        alert("Request: "+JSON.stringify(request));
                    }
                });
        });

        $('.activeChoice').on('click', function(e) {
           
            var thisobj = $(this);
            
             $.ajax({

                    'url' : 'ajax.adm_server.php?r=adm/mailconfig/setActive',
                    'type' : 'GET',
                    'data' : {
                        'id' : thisobj.val()
                    },
                    'success' : function(data) {              
                      
                      
                    },
                    'error' : function(request,error)
                    {
                        alert("Request: "+JSON.stringify(request));
                    }
                });
        });

        $('.btn-test').on('click', function(e) {
            
            e.preventDefault();
            var href = $(this).attr('href');
            const modal = new window.frontend.modules.Modal('mail-test','#admcontainer');
            var btnLabel = "{{Lang_translate('_TEST', 'mailconfig')}}";
            modal.Content = `<form style="width:60%;height:50%;margin: 20% auto;">
                                <input type="email" class="form-control textfield" name="test_mail" value="" />
                                <a style ="margin:2% auto; display:block; width:50%" class="btn btn-info" id="testMail" href="`+href+`">` + btnLabel + `</a>
                                <div class="alert" role="alert">
 
                                </div>
                            </form>`;
            modal.Title = "{{Lang_translate('_TEST', 'standard')}}";
            modal.Open();
            
           
        });

        $('body').on("click","#testMail", function(e){
            e.preventDefault();
            var href = $(this).attr('href');
            var recipient = $(this).prev('input').val();
            var message = $(this).next();
            message.removeClass('alert-success').removeClass('alert-danger').empty();
             $.ajax({
                    'url' : href + '&recipient=' + recipient,
                    'type' : 'GET',
                    'success' : function(data) {
                        try {
                            var response = JSON.parse(data);
                            if (response.result === true) {
                                message.addClass('alert-success').html("Invio TEST avvenuto con successo");
                            } else {
                                message.addClass('alert-danger').html("Invio TEST fallito");
                            }
                        } catch (e) {
                            alert('Check console log for debug messages.')
                            console.log(data);
                        }                        
                    },
                    'error' : function(request,error)
                    {
                        //alert("Request: "+JSON.stringify(request));
                    }
                });
        })
    });
    </script>

