{% extends base %}

{% block init_vars_block %}
    {% init show_right = False %}
{% end %}

{% block head %}
    <script type="text/javascript" src="{{_server_home}}/_static/lib/utf.js/utf-v1.0.js"></script>
{% end %}

{% block body %}

{% set encode_name = xutils.get_argument_str("tab", "BASE64") %}

<div class="card">
    {% set title = encode_name %}
    {% include "common/title/base_title.html" %}
</div>

{% include tools/encode_tab.html %}

<div class="card">
    <div class="row">
        <textarea id="input" class="col-md-12" rows=10 placeholder="请输入内容"></textarea>
    </div>
    
    <div class="col-md-12 top-offset-1">
        <button class="btn" onclick="base64Encode()">编码</button>
        <button class="btn" onclick="base64Decode()">解码</button>
    </div>

</div>

<div class="card">
    <div class="row">
        <textarea id="output" class="col-md-12" rows=10 placeholder="编解码结果"></textarea>
    </div>
</div>

<script>
function base64Encode() {
    var input = $("#input").val();
    var inputText = $("#input").val();
    var type = xnote.getUrlParam("type", "base64");
    var params = {type:type, input: inputText};
    xnote.http.post("/api/encode", params, function (resp) {
        if (resp.success) {
            $("#output").val(resp.data);
        } else {
            $("#output").val("");
            xnote.toast(resp.message);
        }
    });
    // var bytes = utf16to8($("#input").val());
    // $("#output").val(btoa(bytes));
}

function base64Decode() {
    var input = $("#input").val();
    var inputText = $("#input").val();
    var type = xnote.getUrlParam("type", "base64");
    var params = {type:type, input: inputText};
    xnote.http.post("/api/decode", params, function (resp) {
        if (resp.success) {
            $("#output").val(resp.data);
        } else {
            $("#output").val("");
            xnote.toast(resp.message);
        }
    });
}
</script>
{% end %}