<?php
$code                  = custom_design('get');
$error                 = "";

$final_array['HEADER'] = (
    (isset($code[0]) && trim(strlen($code[0])) > 0) ? $code[0] : ''
);

$final_array['FOOTER'] = (
    (isset($code[1]) && trim(strlen($code[1])) > 0) ? $code[1] : ''
);

$final_array['STYLE']  = (
    (isset($code[2]) && trim(strlen($code[2])) > 0) ? $code[2] : ''
);
?>
<script src="<?php echo LoadAdminLink('vendors/codemirror-5.30.0/lib/codemirror.js'); ?>"></script>
<script src="<?php echo LoadAdminLink('vendors/codemirror-5.30.0/mode/css/css.js'); ?>"></script>
<script src="<?php echo LoadAdminLink('vendors/codemirror-5.30.0/mode/javascript/javascript.js'); ?>"></script>
<link rel="stylesheet" href="<?php echo LoadAdminLink('vendors/codemirror-5.30.0/lib/codemirror.css'); ?>">
<div class="container-fluid">
    <div>
        <h3>Custom JS / CSS</h3>
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb">
                <li class="breadcrumb-item">
                    <a href="#">Home</a>
                </li>
                <li class="breadcrumb-item">
                    <a href="#">Design</a>
                </li>
                <li class="breadcrumb-item active" aria-current="page">Custom JS / CSS</li>
            </ol>
        </nav>
    </div>
    <!-- Vertical Layout -->
    <div class="row">
        <div class="col-lg-12 col-md-12">
            <div class="card">
                <div class="card-body">
                    <h6 class="card-title">Custom JS / CSS</h6>
                    <div class="ads-settings-alert"></div>
                    <form class="ads-settings" method="POST">
                        <div class="form-group">
                        	<label class="form-label">Header Custom JavaScript</small></label>
                            <textarea name="cheader" id="cheader-js" class="form-control"><?php echo $final_array['HEADER']?></textarea>
                        </div>
                       <div class="form-group">
                        	<label class="form-label">Footer Custom JavaScript</small></label>
                            <textarea name="cfooter" id="fheader-js" class="form-control"><?php echo $final_array['FOOTER']?></textarea>
                        </div>
                        <div class="form-group">
                        	<label class="form-label">Header CSS Style</small></label>
                            <textarea name="css" id="custom-css" class="form-control"><?php echo $final_array['STYLE']?></textarea>
                        </div>
                        <button type="submit" class="btn btn-primary m-t-15 waves-effect">Save</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() {
    var form_ads_settings = $('form.ads-settings');
    var readOnlyCodeMirror = CodeMirror.fromTextArea(document.getElementById('cheader-js'), {
         mode: "javascript",
         theme: "default",
         lineNumbers: true,
         readOnly: false
      });

      var readOnlyCodeMirror = CodeMirror.fromTextArea(document.getElementById('fheader-js'), {
         mode: "javascript",
         theme: "default",
         lineNumbers: true,
         readOnly: false
      });

      var readOnlyCodeMirror = CodeMirror.fromTextArea(document.getElementById('custom-css'), {
         mode: "css",
         theme: "default",
         lineNumbers: true,
         readOnly: false
      });   
    form_ads_settings.ajaxForm({
        url: '{{CONFIG ajax_url}}/ap/save-custom-design-settings',
        beforeSend: function() {
            form_ads_settings.find('.waves-effect').text('Please wait..');
        },
        success: function(data) {
        	if (data.status == 200) {
                $('.ads-settings-alert').html('<div class="alert alert-success"><i class="fa fa-check"></i> Your changes have been saved successfully</div>');
                setTimeout(function () {
                    $('.ads-settings-alert').empty();
                }, 2000);
	           }
	           else if (data.status == 400) {
                $('.ads-settings-alert').html('<div class="alert alert-danger">Please check your details!</div>');
                setTimeout(function () {
                    $('.ads-settings-alert').empty();
                }, 2000);
	           }
	           else if (data.status == 500) {
                   $('.ads-settings-alert').html('<div class="alert alert-danger">Can`t save changes. themes/{{CONFIG theme}}/custom directory is not writable!</div>');
                    setTimeout(function () {
                        $('.ads-settings-alert').empty();
                    }, 2000);
	           }
	           form_ads_settings.find('.waves-effect').text('Save');
              $("html, body").animate({ scrollTop: 0 }, "slow");
        }
    });
});
</script>