<div class="container-fluid">
    <div>
        <h3>File Upload Configuration</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">File Upload Configuration</li>
            </ol>
        </nav>
    </div>
    <!-- Vertical Layout -->
    <div class="row">
		<div class="col-lg-12 col-md-12">
            <h3>Storage &amp; CDN Configuration <br><hr></h3>
        </div>
        <div class="col-lg-6 col-md-6 ">
            <div class="card">
                <div class="card-body">
                    <h6 class="card-title">Amazon S3 Settings</h6>
                    <div class="general-settings-alert"></div>
                    <form class="general-settings" method="POST">
						<div>
							<div class="float-left">
								<label for="amazone_s3" class="main-label">Amazon S3 Storage</label>
								<br><small class="admin-info">Enable Amazon Storage to store your files in Amazon S3.</small>
							</div>
							<div class="form-group float-right switcher">
								<input type="hidden" name="amazone_s3" value="0">
								<input type="checkbox" name="amazone_s3" id="amazone_s3-enabled" value="1" <?php echo ($wo['config']['amazone_s3'] == '1') ? 'checked': '';?>>
								<label for="amazone_s3-enabled" class="check-trail"><span class="check-handler"></span></label>
							</div>
							<div class="clearfix"></div>
							<hr>
						</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="bucket_name" class="form-control" value="<?php echo $wo['config']['bucket_name']?>">
								<small class="admin-info">Your Amazon S3 <a href="https://docs.aws.amazon.com/AmazonS3/latest/userguide/creating-bucket.html" target="_blank">Bucket Name</a></small>
                            </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="<?php echo $wo['config']['amazone_s3_key']?>">
								<small class="admin-info">Your Amazon Key from <a href="https://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html" target="_blank">AWS credentials</a></small>
                            </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="<?php echo $wo['config']['amazone_s3_s_key']?>">
								<small class="admin-info">Your Amazon Secret from <a href="https://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html" target="_blank">AWS credentials</a></small>
                            </div>
                        </div>
                        <label for="region">Amazon S3 bucket Region</label>
                        <select class="form-control show-tick" id="region" name="region">
                          <option value="us-east-2" <?php echo ($wo['config']['region'] == 'us-east-2')   ? ' selected' : '';?> >US East (Ohio)</option>
		                  <option value="us-west-1" <?php echo ($wo['config']['region'] == 'us-east-1')   ? ' selected' : '';?> >US East (N. Virginia)</option>
                          <option value="us-west-1" <?php echo ($wo['config']['region'] == 'us-west-1')   ? ' selected' : '';?> >US West (N. California)</option>
                          <option value="us-west-2" <?php echo ($wo['config']['region'] == 'us-west-2')   ? ' selected' : '';?> >US West (Oregon)</option>
		                  <option value="ap-east-1" <?php echo ($wo['config']['region'] == 'ap-east-1')   ? ' selected' : '';?> >Asia Pacific (Hong Kong)</option>
                          <option value="ap-south-1" <?php echo ($wo['config']['region'] == 'ap-south-1')   ? ' selected' : '';?> >Asia Pacific (Mumbai)</option>
		                  <option value="ap-southeast-1" <?php echo ($wo['config']['region'] == 'ap-southeast-1')   ? ' selected' : '';?> >Asia Pacific (Singapore)</option>
		                  <option value="ap-southeast-2" <?php echo ($wo['config']['region'] == 'ap-southeast-2')   ? ' selected' : '';?> >Asia Pacific (Sydney)</option>
		                  <option value="ap-northeast-1" <?php echo ($wo['config']['region'] == 'ap-northeast-1')   ? ' selected' : '';?> >Asia Pacific (Tokyo)</option>

                            <option value="cn-north-1" <?php echo ($wo['config']['region'] == 'cn-north-1')   ? ' selected' : '';?> >China (Beijing)</option>
                            <option value="cn-northwest-1" <?php echo ($wo['config']['region'] == 'cn-northwest-1')   ? ' selected' : '';?> >China (Ningxia)</option>

                          <option value="ca-central-1" <?php echo ($wo['config']['region'] == 'ca-central-1')   ? ' selected' : '';?> >Canada (Central)</option>
		                  <option value="eu-central-1" <?php echo ($wo['config']['region'] == 'eu-central-1')   ? ' selected' : '';?> >EU (Frankfurt)</option>
		                  <option value="eu-west-1" <?php echo ($wo['config']['region'] == 'eu-west-1')   ? ' selected' : '';?> >EU (Ireland)</option>
                          <option value="eu-west-2" <?php echo ($wo['config']['region'] == 'eu-west-2')   ? ' selected' : '';?> >EU (London)</option>
                            <option value="eu-west-3" <?php echo ($wo['config']['region'] == 'eu-west-3')   ? ' selected' : '';?> >EU (Paris)</option>
                            <option value="eu-north-1" <?php echo ($wo['config']['region'] == 'eu-north-1')   ? ' selected' : '';?> >EU (Stockholm)</option>
                            <option value="me-south-1" <?php echo ($wo['config']['region'] == 'me-south-1')   ? ' selected' : '';?> >Middle East (Bahrain)</option>
                          <option value="sa-east-1" <?php echo ($wo['config']['region'] == 'sa-east-1')   ? ' selected' : '';?> >South America (São Paulo)</option>
                            <option value="us-gov-east-1" <?php echo ($wo['config']['region'] == 'us-gov-east-1')   ? ' selected' : '';?> >AWS GovCloud (US-East)</option>
                            <option value="us-gov-west-1" <?php echo ($wo['config']['region'] == 'us-gov-west-1')   ? ' selected' : '';?> >AWS GovCloud (US-West)</option>
                        </select>
						<small class="admin-info">Your Amazon's S3 Region</small>
                        <div class="clearfix"></div>
                        <br>
						<div class="alert alert-info">
                           Before enabling Amazon S3, make sure you upload the whole "upload/" folder to your bucket. <br><br>
                            Before disabling Amazon S3, make sure you download the whole "upload/" folder to your server.<br><br>
                            We recommend to upload the folder and files via <a href="http://s3tools.org/s3cmd">S3cmd</a>.<br><br>
                            If your site is still brand new, you can escape the upload step, but make sure to click on "Test Connection".<br>
                        </div>
                        <input type="hidden" name="hash_id" value="<?php echo Wo_CreateSession();?>">
                        <button type="button" class="btn btn-warning m-t-15 waves-effect" onclick="Wo_TestS3()">Test Connection</button>
                    </form>
                </div>
            </div>
        </div>
        <div class="clearfix"></div>
    </div>
    <!-- #END# Vertical Layout -->
<script>
function Wo_TestS3() {
	$('form.general-settings').find('.btn-warning').text('Please wait..');
	$.get(Wo_Ajax_Requests_File(), {f:'admin_setting', s: 'test_s3', p: 'amazon-settings'}, function (data) {
		if (data.status == 200) {
            $('.general-settings-alert').html('<div class="alert alert-success"><i class="fa fa-check"></i> Connection established!</div>');
            setTimeout(function () {
                $('.general-settings-alert').empty();
            }, 2000);
		} else if (data.status == 300) {
            $('.general-settings-alert').html("<div class='alert alert-danger'>Bucket doesn\'t exists</div>");
            setTimeout(function () {
                $('.general-settings-alert').empty();
            }, 2000);
		} else if (data.status == 500) {
            $('.general-settings-alert').html("<div class='alert alert-danger'>Your s3 account doesn\'t have any buckets, please create one.</div>");
            setTimeout(function () {
                $('.general-settings-alert').empty();
            }, 2000);
		} else if (data.status == 400) {
            $('.general-settings-alert').html('<div class="alert alert-danger">'+data.message+'</div>');
            setTimeout(function () {
                $('.general-settings-alert').empty();
            }, 2000);
		} else  {
            $('.general-settings-alert').html('<div class="alert alert-danger">Error while connecting to amazone, please check your details</div>');
            setTimeout(function () {
                $('.general-settings-alert').empty();
            }, 2000);
		}
		$('form.general-settings').find('.btn-warning').text('Test Connection');
	});
}

$(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(Wo_Ajax_Requests_File() + '?f=admin_setting&s=update_general_setting', 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] = this.value;
        objData['hash_id'] = hash_id;
        thisElement.addClass('warning');
        $.post(Wo_Ajax_Requests_File() + '?f=admin_setting&s=update_general_setting', 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=text], input[type=number], input[type=password], 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(Wo_Ajax_Requests_File() + '?f=admin_setting&s=update_general_setting', 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>