<div class="container-fluid">
    <div>
        <h3>Amazon S3 & FTP Settings</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">Amazon S3 & FTP Settings</li>
            </ol>
        </nav>
    </div>
	<div class="alert alert-warning">
        <i class="fa fa-fw fa-exclamation-triangle"></i> <strong>Important:</strong> You can't enable two storages at the same time, if you enable FTP, amazon s3 will be automatically disabled, same for amazon s3.
      </div>
    <div class="row">
        <div class="col-lg-6 col-md-6 ">
            <div class="card">
                <div class="card-body">
                    <h6 class="card-title">Configure Amazon S3</h6>
                    <div class="s3-settings-alert"></div>
                    <form class="s3-settings" method="POST">
                        <label for="amazone_s3">Amazon S3 Storage</label>
                        <div class="form-group">
                            <div class="form-check form-check-inline">
                                <input class="form-check-input" type="radio" name="s3_upload" id="amazone_s3-enabled" value="on" <?php echo ($music->config->s3_upload == 'on') ? 'checked': '';?>>
                                <label class="form-check-label" for="amazone_s3-enabled">Enabled</label>
                            </div>
                            <div class="form-check form-check-inline">
                                <input class="form-check-input" type="radio" name="s3_upload" id="amazone_s3-disabled" value="off" <?php echo ($music->config->s3_upload == 'off') ? 'checked': '';?>>
                                <label class="form-check-label" for="amazone_s3-disabled" class="m-l-20">Disabled</label>
                            </div>
                        </div>
                        <div class="form-group form-float">
                            <div class="form-line">
                                <label class="form-label">Amazon Bucket Name</label>
                                <input type="text" id="bucket_name" name="s3_bucket_name" class="form-control" value="{{CONFIG s3_bucket_name}}">
                                
                            </div>
                        </div>
                        <div class="form-group form-float">
                            <div class="form-line">
                                <label class="form-label">Amazon S3 Key</label>
                                <input type="text" id="amazone_s3_key" name="amazone_s3_key" class="form-control" value="{{CONFIG amazone_s3_key}}">
                                
                            </div>
                        </div>
                        <div class="form-group form-float">
                            <div class="form-line">
                                <label class="form-label">Amazon S3 Secret Key</label>
                                <input type="text" id="amazone_s3_s_key" name="amazone_s3_s_key" class="form-control" value="{{CONFIG amazone_s3_s_key}}">
                                
                            </div>
                        </div>
                        <label class="form-check-label" for="region">Amazon S3 bucket Region</label>
                        <select class="form-control show-tick" id="region" name="region">
                          <option value="us-east-1" <?php echo ($music->config->region == 'us-east-1')   ? ' selected' : '';?> >US East (N. Virginia)</option>
		                  <option value="us-west-2" <?php echo ($music->config->region == 'us-west-2')   ? ' selected' : '';?> >US West (Oregon)</option>
		                  <option value="ap-northeast-2" <?php echo ($music->config->region == 'ap-northeast-2')   ? ' selected' : '';?> >Asia Pacific (Seoul)</option>
                          <option value="ap-south-1" <?php echo ($music->config->region == 'ap-south-1')   ? ' selected' : '';?> >Asia Pacific (Mumbai)</option>
		                  <option value="ap-southeast-1" <?php echo ($music->config->region == 'ap-southeast-1')   ? ' selected' : '';?> >Asia Pacific (Singapore)</option>
		                  <option value="ap-southeast-2" <?php echo ($music->config->region == 'ap-southeast-2')   ? ' selected' : '';?> >Asia Pacific (Sydney)</option>
		                  <option value="ap-northeast-1" <?php echo ($music->config->region == 'ap-northeast-1')   ? ' selected' : '';?> >Asia Pacific (Tokyo)</option>
		                  <option value="eu-central-1" <?php echo ($music->config->region == 'eu-central-1')   ? ' selected' : '';?> >EU (Frankfurt)</option>
		                  <option value="eu-west-1" <?php echo ($music->config->region == 'eu-west-1')   ? ' selected' : '';?> >EU (Ireland)</option>
                        </select>
                        <div class="clearfix"></div>
                        <br><br>
                        <p class="help-block ">1. Before enabling Amazon S3, make sure you upload the whole "upload/" folder to your bucket.</p>
		                <p class="help-block ">2. Before disabling Amazon S3, make sure you download the whole "upload/" folder to your server.</p>
		                <p class="help-block ">3. We recommend to upload the folder and files via <a href="http://s3tools.org/s3cmd">S3cmd</a>.</p>
		                <p class="help-block ">4. If your site is still brand new, you can escape the upload step, but make sure to click on "Test Connection".</p>
                        <button type="submit" class="btn btn-primary m-t-15 waves-effect">Save</button>
                        <button type="button" class="btn btn-warning m-t-15 waves-effect test-con" onclick="test_s3();">Test Connection</button>
                        <input type="hidden" name="hash_id" value="<?php echo createMainSession();?>">
                    </form>
                </div>
            </div>
        </div>
        <div class="col-lg-6 col-md-6 ">
            <div class="card">
                <div class="card-body">
                    <h6 class="card-title">FTP Settings</h6>
                    <small>You can upload files directly from your server to another FTP server and load them from there.</small><br>
                    <small>Impotant: This may slow down your site's upload/delete speed, make sure to use fast FTP server.</small>
                    <div class="ftp-settings-alert"></div>
                    <form class="ftp-settings" method="POST">
                        <label for="ftp_upload">FTP Storage</label>
                        <div class="form-group">
                            <div class="form-check form-check-inline">
                                <input class="form-check-input" type="radio" name="ftp_upload" id="ftp_upload-enabled" value="on" <?php echo ($music->config->ftp_upload == 'on') ? 'checked': '';?>>
                                <label class="form-check-label" for="ftp_upload-enabled">Enabled</label>
                            </div>
                            <div class="form-check form-check-inline">
                                <input class="form-check-input" type="radio" name="ftp_upload" id="ftp_upload-disabled" value="off" <?php echo ($music->config->ftp_upload == 'off') ? 'checked': '';?>>
                                <label class="form-check-label" for="ftp_upload-disabled" class="m-l-20">Disabled</label>
                            </div>
                        </div>
                        <div class="form-group form-float">
                            <div class="form-line">
                                <label class="form-label">FTP Hostname</label>
                                <input type="text" id="ftp_host" name="ftp_host" class="form-control" value="{{CONFIG ftp_host}}">
                                
                            </div>
                        </div>
                        <div class="form-group form-float">
                            <div class="form-line">
                                <label class="form-label">FTP Username</label>
                                <input type="text" id="ftp_username" name="ftp_username" class="form-control" value="{{CONFIG ftp_username}}">
                                
                            </div>
                        </div>
                        <div class="form-group form-float">
                            <div class="form-line">
                                <label class="form-label">FTP Password</label>
                                <input type="text" id="ftp_password" name="ftp_password" class="form-control" value="{{CONFIG ftp_password}}">
                                
                            </div>
                        </div>
                        <div class="form-group form-float">
                            <div class="form-line">
                                <label class="form-label">FTP Port</label>
                                <input type="text" id="ftp_port" name="ftp_port" class="form-control" value="{{CONFIG ftp_port}}">
                                
                            </div>
                        </div>

                        <div class="form-group form-float">
                            <div class="form-line">
                                <label class="form-label">FTP Path</label>
                                <input type="text" id="ftp_path" name="ftp_path" class="form-control" value="{{CONFIG ftp_path}}">
                                
                            </div>
                        </div>

                        <div class="form-group form-float">
                            <div class="form-line">
                                <label class="form-label">FTP Endpoint (IP or domain where the FTP server is pointed to) example: wowonderstorage.com, files will be loaded from here.</label>
                                <input type="text" id="ftp_endpoint" name="ftp_endpoint" class="form-control" value="{{CONFIG ftp_endpoint}}">
                                
                            </div>
                        </div>
                        <div class="clearfix"></div>
                        <p class="help-block ">1. Before enabling FTP, make sure you upload the whole "upload/" folder to your FTP server.</p>
                        <p class="help-block ">2. Before disabling FTP, make sure you download the whole "upload/" folder to your server.</p>
                        <p class="help-block ">3. If your site is still brand new, you can escape the upload step, but make sure to click on "Test Connection".</p>
                        <button type="submit" class="btn btn-primary m-t-15 waves-effect">Save</button>
                        <button type="button" class="btn btn-warning m-t-15 waves-effect" onclick="TestFTP()">Test FTP Connection</button>
                        <input type="hidden" name="hash_id" value="<?php echo createMainSession();?>">
                    </form>
                </div>
            </div>
        </div>
        <div class="clearfix"></div>
    </div>
    <!-- #END# Vertical Layout -->
<script>

function TestFTP() {
    $('form.ftp-settings').find('.btn-warning').text('Please wait..');
    $.get('{{CONFIG ajax_url}}/ap/test_ftp', function (data) {
        if (data.status == 200) {
            $('.ftp-settings-alert').html('<div class="alert alert-success">Connection established!</div>');
            setTimeout(function () {
                $('.ftp-settings-alert').empty();
            }, 2000);
        } else if (data.status == 400) {
            $('.ftp-settings-alert').html('<div class="alert alert-danger">'+data.message+'</div>');
            setTimeout(function () {
                $('.ftp-settings-alert').empty();
            }, 2000);
        }
        else{
            $('.ftp-settings-alert').html('<div class="alert alert-danger">Error while connecting to ftp, please check your details</div>');
            setTimeout(function () {
                $('.ftp-settings-alert').empty();
            }, 2000);
        }
        $('form.ftp-settings').find('.btn-warning').text('Test FTP Connection');
    });
}

function test_s3() {
    $('.test-con').text('Please wait..');
    $.get('{{CONFIG ajax_url}}/ap/testS3',function (data) {
        if (data.status == 200) {
            $('.s3-settings-alert').html('<div class="alert alert-success"><i class="fa fa-check"></i> Connection successfully established!</div>');
            setTimeout(function () {
                $('.s3-settings-alert').empty();
            }, 2000);
        } 

    else if (data.status == 300) {
        $('.s3-settings-alert').html('<div class="alert alert-danger">Bucket doesn\'t exists!</div>');
            setTimeout(function () {
                $('.s3-settings-alert').empty();
            }, 2000);
        } 

    else if (data.status == 500) {
        $('.s3-settings-alert').html('<div class="alert alert-danger">Your s3 account doesn\'t have any buckets, please create one.</div>');
            setTimeout(function () {
                $('.s3-settings-alert').empty();
            }, 2000);
        } 

    else if (data.status == 400) {
        $('.s3-settings-alert').html('<div class="alert alert-danger">'+data.message+'</div>');
            setTimeout(function () {
                $('.s3-settings-alert').empty();
            }, 2000);
        } 

    else  {
        $('.s3-settings-alert').html('<div class="alert alert-danger">Error while connecting to amazone, please check your details</div>');
            setTimeout(function () {
                $('.s3-settings-alert').empty();
            }, 2000);
        }
        $('.test-con').text('Test Connection');
    });
}
$(function() {
    var form_s3_settings = $('form.s3-settings');
    form_s3_settings.ajaxForm({
        url: '{{CONFIG ajax_url}}/ap/save-settings',
        beforeSend: function() {
            form_s3_settings.find('.btn-primary').text('Please wait..');
        },
        success: function(data) {
            if (data.status == 200) {
            	form_s3_settings.find('.btn-primary').text('Save');
            	$('.s3-settings-alert').html('<div class="alert alert-success"><i class="fa fa-check"></i> Settings updated successfully</div>');
            	setTimeout(function () {
            		$('.s3-settings-alert').empty();
            	}, 2000);
            }
        }
    });

    var form_ftp_settings = $('form.ftp-settings');
    form_ftp_settings.ajaxForm({
        url: '{{CONFIG ajax_url}}/ap/save-settings',
        beforeSend: function() {
            form_ftp_settings.find('.btn-primary').text('Please wait..');
        },
        success: function(data) {
            if (data.status == 200) {
                form_ftp_settings.find('.btn-primary').text('Save');
                $('.ftp-settings-alert').html('<div class="alert alert-success"><i class="fa fa-check"></i> Settings updated successfully</div>');
                setTimeout(function () {
                    $('.ftp-settings-alert').empty();
                }, 2000);
            }
        }
    });

});

</script>