<div class="container-fluid">
    <div>
        <h3>E-mail & SMS Setup</h3>
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb">
                <li class="breadcrumb-item">
                    <a href="#">Home</a>
                </li>
                <li class="breadcrumb-item">
                    <a href="#">Settings</a>
                </li>
                <li class="breadcrumb-item active" aria-current="page">E-mail & SMS Setup</li>
            </ol>
        </nav>
    </div>
    <div class="alert alert-info">
       <i class="fa fa-fw fa-exclamation-triangle"></i> <strong>Info: </strong> For more information on how to setup SMTP or SMS, please visit our <a href="https://docs.deepsoundscript.com/#idocs_smtp" target="_blank">Documentation</a> page.
   </div>
    <!-- Vertical Layout -->
    <div class="row">
        <div class="col-lg-6 col-md-6 ">
            <div class="card">
                <div class="card-body">
                    <h6 class="card-title">E-mail Configuration</h6>
                    <div class="email-settings-alert"></div>
                    <form class="email-settings" method="POST">
                        <label for="smtp_or_mail">Server Type</label>
                        <br><small class="admin-info">Select which E-mail server you want to use, Server Mail function is not recommended.</small>
                        <div class="form-group">
                            <select class="form-control show-tick" id="smtp_or_mail" name="smtp_or_mail">
                              <option value="smtp" <?php if($music->config->smtp_or_mail == 'smtp'){ echo "selected";} ?>>SMTP Server</option>
                              <option value="mail" <?php if($music->config->smtp_or_mail == 'mail'){ echo "selected";} ?>>Server Mail (Default)</option>
                            </select>
                        </div>
                        <hr>
                        <div class="form-group form-float">
                            <div class="form-line">
                                <label class="form-label">SMTP Host</label>
                                <input type="text" name="smtp_host" class="form-control" value="<?php echo ($music->config->smtp_host);?>">
                                <small class="admin-info">Your SMTP account host name, can be IP, domain or subdomain.</small>
                            </div>
                        </div>
                        <div class="clearfix"></div>
                        <hr>
                        <div class="form-group form-float">
                            <div class="form-line">
                                <label class="form-label">SMTP Username</label>
                                <input type="text" name="smtp_username" class="form-control" value="<?php echo ($music->config->smtp_username);?>">
                                <small class="admin-info">Your SMTP account username.</small>
                            </div>
                        </div>
                        <div class="clearfix"></div>
                        <hr>
                        <div class="form-group form-float">
                            <div class="form-line">
                                <label class="form-label">SMTP Password</label>
                                <input type="text" name="smtp_password" class="form-control" value="<?php echo ($music->config->smtp_password);?>">
                                <small class="admin-info">Your SMTP account password.</small>
                            </div>
                        </div>
                        <div class="clearfix"></div>
                        <hr>
                        <div class="form-group form-float">
                            <div class="form-line">
                                <label class="form-label">SMTP Port</label>
                                <input type="text" name="smtp_port" class="form-control" value="<?php echo ($music->config->smtp_port);?>">
                                <small class="admin-info">Which port does your SMTP server use? most used 587 for TLS, and 465 for SSL encryption.</small>
                            </div>
                        </div>
                        <div class="clearfix"></div>
                        <hr>
                        <label for="smtp_encryption">SMTP Encryption</label>

                        <div class="form-group">
                            <select class="form-control show-tick" id="smtp_encryption" name="smtp_encryption">
                              <option value="tls" <?php if($music->config->smtp_encryption == 'tls'){ echo "selected";} ?>>TLS (Not secured)</option>
                              <option value="ssl" <?php if($music->config->smtp_encryption == 'ssl'){ echo "selected";} ?>>SSL (Secured)</option>
                            </select>
                            <small class="admin-info">Which encryption method does your SMTP server use?</small>
                        </div>

                        <input type="hidden" name="hash_id" value="<?php echo createMainSession();?>">
                        <button type="button" class="btn btn-warning m-t-15 waves-effect" onclick="Wo_TestMessage();">Test E-mail Server</button>
                    </form>
                </div>
            </div>
        </div>
        <div class="col-lg-6 col-md-6">
            <div class="card">
                <div class="card-body">
                    <h6 class="card-title">SMS Configuration</h6>
                    <div class="sms-settings-alert"></div>
                    <form class="sms-settings" method="POST">
                        <div class="alert alert-success">To start sending SMS, you have to create an account and buy credits in <a href="http://www.twilio.com">Twilio</a>.</div>
                        <div class="form-group form-float">
                            <div class="form-line">
                                <label class="form-label">Twilio account_sid</label>
                                <input type="text" name="sms_twilio_username" class="form-control" value="<?php echo ($music->config->sms_twilio_username);?>">
                                <small class="admin-info"></small>
                            </div>
                        </div>
                        <div class="clearfix"></div>
                        <hr>
                        <div class="form-group form-float">
                            <div class="form-line">
                                <label class="form-label">Twilio auth_token</label>
                                <input type="text" name="sms_twilio_password" class="form-control" value="<?php echo ($music->config->sms_twilio_password);?>">
                                <small class="admin-info"></small>
                            </div>
                        </div>
                        <div class="clearfix"></div>
                        <hr>
                        <div class="form-group form-float">
                            <div class="form-line">
                                <label class="form-label">Twilio Phone number</label>
                                <input type="text" name="sms_t_phone_number" class="form-control" value="<?php echo ($music->config->sms_t_phone_number);?>">
                                <small class="admin-info"></small>
                            </div>
                        </div>
                        <div class="clearfix"></div>
                        <hr>
                        <div class="form-group form-float">
                            <div class="form-line">
                                <label class="form-label">Your Phone number <small>e.g (+9053..)</small></label>
                                <input type="text" name="sms_phone_number" class="form-control" value="<?php echo ($music->config->sms_phone_number);?>">
                                <small class="admin-info"></small>
                            </div>
                        </div>
                        <div class="clearfix"></div>
                        <p class="help-block">After clicking "Test Server", a test message will be sent to your phone</p>
                        <input type="hidden" name="hash_id" value="<?php echo createMainSession();?>">
                        <button type="button" class="btn btn-warning m-t-15 waves-effect" onclick="TestSMSMessage();">Test SMS Server</button>
                    </form>
                </div>
            </div>
        </div>
        <div class="clearfix"></div>
    </div>
    <!-- #END# Vertical Layout -->
<script>
    function TestSMSMessage() {
        $('form.sms-settings').find('.btn-warning').text('Please wait..');
        $.get('{{CONFIG ajax_url}}/ap/test_sms_message', function (data) {
            if (data.status == 200) {
                $('.sms-settings-alert').html('<div class="alert alert-success"><i class="fa fa-check"></i> Message sent!</div>');
                setTimeout(function () {
                    $('.sms-settings-alert').empty();
                }, 2000);
            } else {
                 $('.sms-settings-alert').html('<div class="alert alert-danger">Message failed to sent, error: ' + data.error+'</div>');
                setTimeout(function () {
                    $('.sms-settings-alert').empty();
                }, 2000);
            }
            $('form.sms-settings').find('.btn-warning').text('Test E-mail Server');
        });
    }
    function Wo_TestMessage() {
        $('form.email-settings').find('.btn-warning').text('Please wait..');
        $.get('{{CONFIG ajax_url}}/ap/test_message', function (data) {
            if (data.status == 200) {
                $('.email-settings-alert').html('<div class="alert alert-success"><i class="fa fa-check"></i> Message sent!</div>');
                setTimeout(function () {
                    $('.email-settings-alert').empty();
                }, 2000);
            } else {
                $('.email-settings-alert').html('<div class="alert alert-danger">Message failed to sent, Please test your SMTP details on https://www.smtper.net to make sure your SMTP server are working fine, If it works their and not in your website, please contact us for help and support.</div>');
                // setTimeout(function () {
                //     $('.email-settings-alert').empty();
                // }, 2000);
            }
            $('form.email-settings').find('.btn-warning').text('Test E-mail Server');
        });
    }

$(function() {
    $('.switcher input[type=checkbox]').click(function () {
        var configName = $(this).attr('name');
        var hash_id = $('input[name=hash_id]').val();
        var objData = {};
        if ($(this).is(":checked") === true) {
            objData[configName] = $(this).val();
        }
        else{
            if ($('input[name='+configName+']')[0]) {
                objData[configName] = $($('input[name='+configName+']')[0]).val();
            }
        }
        objData['hash_id'] = hash_id;
        $.post('{{CONFIG ajax_url}}/ap/save-settings', objData);
    });

    var setTimeOutColor = setTimeout(function (){});
    $('select').on('change', function() {
         clearTimeout(setTimeOutColor);
        var thisElement = $(this);
        var configName = thisElement.attr('name');
        var hash_id = $('input[name=hash_id]').val();
        var objData = {};
        objData[configName] = thisElement.val();
        objData['hash_id'] = hash_id;
        thisElement.addClass('warning');
        $.post("{{CONFIG ajax_url}}/ap/save-settings", objData, function (data) {
            if (data.status == 200) {
                thisElement.removeClass('warning');
                thisElement.addClass('success');
            } else {
                thisElement.addClass('error');
            }
            var setTimeOutColor = setTimeout(function () {
                thisElement.removeClass('success');
                thisElement.removeClass('warning');
                thisElement.removeClass('error');
            }, 2000);
        });
    });
    $('input[type=color]').on('change', delay(function() {
            clearTimeout(setTimeOutColor);
            var thisElement = $(this);
            var configName = thisElement.attr('name');
            var hash_id = $('input[name=hash_id]').val();
            var objData = {};
            objData[configName] = this.value;
            objData['hash_id'] = hash_id;
            thisElement.addClass('warning');
            $.post("{{CONFIG ajax_url}}/ap/save-settings", objData, function (data) {
                if (data.status == 200) {
                    thisElement.removeClass('warning');
                    thisElement.addClass('success');
                } else {
                    thisElement.addClass('error');
                }
                var setTimeOutColor = setTimeout(function () {
                    thisElement.removeClass('success');
                    thisElement.removeClass('warning');
                    thisElement.removeClass('error');
                }, 2000);
                //thisElement.focus();
            });
    }, 500));
    $('input[type=text], input[type=number], input[type=color] , textarea').on('keyup', delay(function() {
            clearTimeout(setTimeOutColor);
            var thisElement = $(this);
            var configName = thisElement.attr('name');
            var hash_id = $('input[name=hash_id]').val();
            var objData = {};
            objData[configName] = this.value;
            objData['hash_id'] = hash_id;
            thisElement.addClass('warning');
            $.post("{{CONFIG ajax_url}}/ap/save-settings", objData, function (data) {
                if (data.status == 200) {
                    thisElement.removeClass('warning');
                    thisElement.addClass('success');
                } else {
                    thisElement.addClass('error');
                }
                var setTimeOutColor = setTimeout(function () {
                    thisElement.removeClass('success');
                    thisElement.removeClass('warning');
                    thisElement.removeClass('error');
                }, 2000);
                //thisElement.focus();
            });
    }, 500));
});
</script>
