<script src="{{CONFIG theme_url}}/js/tinymce/js/tinymce/tinymce.min.js"></script>
<?php 
if (empty($_GET['type']) || !in_array($_GET['type'], array('terms_of_use_page','privacy_policy_page','about_page','refund_terms_page'))) {
    header("Location: " . PT_Link('admincp'));
    exit();
}
$page = $db->where('lang_key',PT_Secure($_GET['type']))->getOne(T_LANGS);
?>
<div class="container-fluid">
    <div>
        <h3>Edit Terms Pages</h3>
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb">
                <li class="breadcrumb-item">
                    <a href="#">Home</a>
                </li>
                <li class="breadcrumb-item">
                    <a href="#">Pages</a>
                </li>
                <li class="breadcrumb-item active" aria-current="page">Edit Terms Pages</li>
            </ol>
        </nav>
    </div>
    <!-- Vertical Layout -->
    <div class="row">
        <div class="col-lg-8 col-md-8">
            <div class="card">
                <div class="card-body">
                    <h6 class="card-title">Edit Term Pages</h6>
                    <div class="add-settings-alert"></div>
                    <form class="add-settings" method="POST">
                        <?php 
                        foreach ($page as $key => $value) {
                            if (!in_array($key, array('id','lang_key','type'))  ) { ?>
                        <div class="form-group form-float">
                            <div class="form-line">
                                <label class="form-label"><?php echo(ucfirst($key)) ?> (HTML Allowed)</label>
                                <textarea name="<?php echo($key); ?>" id="<?php echo($key); ?>" class="form-control" cols="30" rows="10"><?php echo $value;?></textarea>
                            </div>
                        </div>
                        <br>
                        <?php } }  ?>
                        <input type="hidden" name="lang_key" value="<?php echo $page->lang_key;?>">
                        <button type="submit" class="btn btn-primary m-t-15 waves-effect">Save</button>
                    </form>
                </div>
            </div>
        </div>
        <div class="clearfix"></div>
    </div>
    <!-- #END# Vertical Layout -->
<script>

$(function() {
    <?php 
    foreach ($page as $key => $value) {
        if (!in_array($key, array('id','lang_key','type'))  ) { ?>
    tinymce.init({
          selector: '#<?php echo($key); ?>',
          height: 270,
          entity_encoding : "raw",
          paste_data_images: true,
          image_advtab: true,
          toolbar1: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image",
          toolbar2: "print preview media | forecolor backcolor",
          plugins: [
              "advlist autolink lists link image charmap print preview hr anchor pagebreak",
              "searchreplace wordcount visualblocks visualchars code fullscreen",
              "insertdatetime media nonbreaking save table contextmenu directionality",
              "template paste textcolor colorpicker textpattern"
            ],
    });
    <?php } }  ?>
    var form_add_settings = $('form.add-settings');
    form_add_settings.ajaxForm({
        url: '{{LINK aj/ap/save-terms}}',
        beforeSend: function() {
            form_add_settings.find('.waves-effect').text('Please wait..');
        },
        beforeSubmit : function(arr, $form, options){
            for (var i = 0; i < arr.length; i++) {
                if (arr[i].name != "hash_id" && arr[i].name != "lang_key") {
                    arr[i].value = btoa(unescape(encodeURIComponent($("#"+arr[i].name).value=tinymce.editors[$("#"+arr[i].name).attr('id')].getContent())));
                }
            }
        },
        success: function(data) {
            if (data.status == 200) {
                $("html, body").animate({ scrollTop: 0 }, "slow");
                $('.add-settings-alert').html('<div class="alert alert-success"><i class="fa fa-check"></i> Pages saved successfully</div>');
                location.reload()
            } else if (data.status == 400) {
              $('.add-settings-alert').html('<div class="alert alert-danger">'+data.message+'</div>');
                setTimeout(function () {
                    $('.add-settings-alert').empty();
                }, 2000);
            }
            form_add_settings.find('.waves-effect').text('Save');
        }
    });
});
</script>