{% extends "../main.html" %}
{% block title %}基础设置{% end %}
{% block content %}
<style>
    #init-modal .modal-title {
        font-size: 1rem;
        color: #000;
    }

    #init-modal .modal-body {
        font-size: 12px;
        color: #4c4c4c;
        padding-bottom: 0;
    }

    #init-modal .modal-content {
        border-radius: 0;
    }

    #init-modal .loading {
        left: 140px;
        position: absolute;
        top: 36px;
    }

    #init-modal .modal-footer button {
        border-radius: 0;
        font-size: 12px;
        float: left;
    }

    #init-modal .modal-footer {
        padding-top: 0;
    }

    #accordion {
        font-size: 12px;
    }
</style>
<div>
    <div class="page-hero page-container " id="page-hero">
        {% module xsrf_form_html() %}
        <div class="padding d-flex" style="padding-bottom:0;">
            <div class="page-title">
                <h2 class="text-md text-highlight">设置</h2>
                <small class="text-muted">基础设置选项</small>
            </div>
            <div class="flex"></div>
        </div>
    </div>
    <div class="page-content page-container" id="page-content">
        <div class="padding">
            {% if not initialed %}
            <div class="alert bg-secondary mb-5 py-4" role="alert">
                <div class="d-flex">
                    <i data-feather="alert-octagon" width="32" height="32"></i>
                    <div class="px-3">
                        <h5 class="alert-heading">请立即初始化！</h5>
                        <p>建立及优化网站需要一些必要的数据文件，我们已为您定制好初始流程，不需要额外操作，请点击下方初始化按钮开始这一切！</p>
                        <p>这一过程将与远程服务端进行通信。</p>
                        <button class="inits btn btn-white">初始化
                            <i data-feather="arrow-right"></i>
                        </button>
                    </div>
                </div>
            </div>
            {% end %}
            <div id="accordion">
                <div class="card">
                    <div class="d-flex align-items-center px-4 py-3 b-t">
                        <i data-feather="refresh-cw"></i>
                        <div class="px-3">
                            <div>更新</div>
                            <div class="text-muted">是否开启静默更新模式 - 暂未开放修改</div>
                        </div>
                        <div class="flex"></div>
                        <span>
                                            <label class="ui-switch ui-switch-md">
                                            <input disabled type="checkbox" checked>
                                            <i></i>
                                            </label>
                                        </span>
                    </div>
                    <div class="d-flex align-items-center px-4 py-3 b-t">
                        <i data-feather="copy"></i>
                        <div class="px-3">
                            <div>伪原创</div>
                            <div class="text-muted">是否开启全局伪原创 - 暂未开放修改</div>
                        </div>
                        <div class="flex"></div>
                        <span>
                                            <label class="ui-switch ui-switch-md">
                                            <input disabled type="checkbox" checked>
                                            <i></i>
                                            </label>
                                        </span>
                    </div>
                    {% module xsrf_form_html() %}
                    <div class="d-flex align-items-center px-4 py-3 b-t pointer" data-toggle="collapse"
                         data-parent="#accordion" data-target="#c_1">
                        <i data-feather="settings"></i>
                        <div class="px-3">
                            <div>缓存管理</div>
                        </div>
                        <div class="flex"></div>
                        <div>
                            <i data-feather="chevron-right"></i>
                        </div>
                    </div>
                    <div class="collapse" id="c_1">
                        <form role="form" style="margin-left: 50px">
                            <div class="form-group">
                                <label>一级缓存</label>
                                <div class="d-flex align-items-center px-4 py-1">
                                    <div class="px-3">
                                        <div>是否开启nginx缓存</div>
                                    </div>
                                    <div class="flex"></div>
                                    <span>
                    <label class="ui-switch ui-switch-md">
                    <input type="checkbox" id="open_nginx_cache" {% if open_nginx_cache=="1" %}checked{% end %}>
                    <i></i>
                    </label>
                    </span>
                                </div>
                                <div class="d-flex align-items-center px-4 py-1">
                                    <div class="px-3">
                                        <div>是否清空nginx缓存</div>
                                    </div>
                                    <div class="flex"></div>
                                    <span id="clear_nginx_cache"><a><i
                                            data-feather="rotate-cw"></i>&nbsp;清空缓存</a></span>
                                </div>
                            </div>
                            <div class="form-group">
                                <label>二级缓存</label>
                                <div class="d-flex align-items-center px-4 py-1">
                                    <div class="px-3">
                                        <div>是否开启ssdb缓存</div>
                                    </div>
                                    <div class="flex"></div>
                                    <span>
                    <label class="ui-switch ui-switch-md">
                    <input type="checkbox" id="open_ssdb_cache" {% if open_ssdb_cache=="1" %}checked{% end %}>
                    <i></i>
                    </label>
                    </span>
                                </div>
                                <div class="d-flex align-items-center px-4 py-1">
                                    <div class="px-3">
                                        <div>是否清空ssdb缓存</div>
                                    </div>
                                    <div class="flex"></div>
                                    <span id="clear_ssdb_cache"><a><i data-feather="rotate-cw"></i>&nbsp;清空缓存</a></span>
                                </div>
                            </div>
                                                 <div class="form-group">
                                <label>三级缓存</label>
                                <div class="d-flex align-items-center px-4 py-1">
                                    <div class="px-3">
                                        <div>重载Redis缓存</div>
                                    </div>
                                    <div class="flex"></div>
                                    <span id="update-redis" onclick="update_redis()"><a><i data-feather="rotate-cw"></i>&nbsp;重载缓存</a></span>
                                </div>
                            </div>
                        </form>
                    </div>
                    <div id="reinit" class="d-flex align-items-center px-4 py-3 b-t pointer">
                        <i data-feather="alert-triangle"></i>
                        <div class="px-3">
                            <div>重新初始化</div>
                        </div>
                        <div class="flex"></div>
                        <div>
                            <i data-feather="chevron-right"></i>
                        </div>
                    </div>
                    <div id="clear_log" class="d-flex align-items-center px-4 py-3 b-t pointer">
                        <i data-feather="rotate-cw"></i>
                        <div class="px-3">
                            <div>清空日志</div>
                        </div>
                        <div class="flex"></div>
                        <div>
                            <i data-feather="chevron-right"></i>
                        </div>
                    </div>
                    <div id="init_sitemap" class="d-flex align-items-center px-4 py-3 b-t pointer">
                        <i data-feather="rotate-cw"></i>
                        <div class="px-3">
                            <div>初始化网站地图文件</div>
                        </div>
                        <div class="flex"></div>
                        <div>
                            <i data-feather="chevron-right"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<!-- .init modal -->
<div id="init-modal" class="modal fade" data-backdrop="static">
    <div class="modal-dialog ">
        <div class="modal-content box-shadow">
            <div class="modal-header ">
                <div class="modal-title text-md">初始化</div>
            </div>
            <div class="modal-body">
                <div class="p-4 text-center">
                    <div class="loading"></div>
                    <p>正在初始化中请不要关闭页面</p>
                </div>
            </div>
            <div class="modal-footer">
                <button disabled id="init-button" type="button" class="btn btn-primary" data-dismiss="modal">确认</button>
            </div>
        </div>
        <!-- /.modal-content -->
    </div>
</div>
<div id="footer" class="page-footer">
    <div style="text-align: center">
        <span class="text-sm text-muted flex">Swan Beta</span>
        <span class="text-sm text-muted">Version {{ l_c_version }}</span>
    </div>
</div>
<!-- / .init modal -->
{% include "../other/confirm.html" %}
{% end %}
{% block js %}
<script src="/static/libs/notie/dist/notie.min.js"></script>
<script>
    function submit_init(data) {
        $.ajax({
            url: "/initkeywords",
            type: "POST",
            dataType: "json",
            data: data,
            timeout: 1000000,
            success: function (res) {
                if (res.statu == 1) {
                    // 成功
                    $("#init-button").attr("disabled", false);
                    $(".modal-body .text-center").html('<span><i class="mx-2 text-success" data-feather="check-circle"></i> 全部初始化已完成!</span>');
                    feather.replace({'width': 21, 'height': 21, 'stroke-width': 1})
                } else {
                    // 失败
                    $("#init-modal").modal("hide");
                    notie.alert({type: 1, text: res.msg, position: 'top'});

                }
            },
            error: function (res) {
                // 网络类型错误
                $("#init-modal").modal("hide");
                notie.alert({type: 3, text: '<b>网络错误</b>', position: 'top'});
            }

        });
    }

    function submit_init_sitemap(data) {
        $.ajax({
            url: "/initsitemap",
            type: "POST",
            dataType: "json",
            data: data,
            timeout: 1000000,
            success: function (res) {
                if (res.statu == 1) {
                    // 成功
                    $("#init-button").attr("disabled", false);
                    $(".modal-body .text-center").html('<span><i class="mx-2 text-success" data-feather="check-circle"></i>'+res.msg+'</span>');
                    feather.replace({'width': 21, 'height': 21, 'stroke-width': 1})
                } else {
                    // 失败
                    $("#init-modal").modal("hide");
                    notie.alert({type: 1, text: res.msg, position: 'top'});

                }
            },
            error: function (res) {
                // 网络类型错误
                $("#init-modal").modal("hide");
                notie.alert({type: 3, text: '<b>网络错误</b>', position: 'top'});
            }

        });
    }

    $(function () {
        $(".inits").click(function () {
            var data = {
                _xsrf: $("input[name='_xsrf']").val(),
            };
            $("#init-modal").modal("show");
            submit_init(data)
        });

        $("#init-button").click(function () {
            $("#init-modal").modal("hide");
            window.location.reload()
        });

        $("#reinit").click(function () {
            $("#modal-confirm .modal-title span").text('重新初始化');
            $("#modal-confirm .modal-body").html("<div><p>您确定要重新初始化数据吗？</p><p>初始化包括：</p>栏目、关键词等基础数据。</div>");
            $("#modal-confirm").modal('show');
            url = "";
            types = "init"
        });

        $("#clear_nginx_cache").click(function () {
            $("#modal-confirm .modal-title span").text('清空nginx缓存');
            $("#modal-confirm .modal-body").html("<div><p>您确定要释放清空所有缓存吗？</p><p>清空包括：</p>删除本地所有缓存文件</div>");
            $("#modal-confirm").modal('show');
            url = "clear/nginxcache"
        });

        $("#clear_ssdb_cache").click(function () {
            $("#modal-confirm .modal-title span").text('清空ssdb缓存');
            $("#modal-confirm .modal-body").html("<div><p>您确定要释放清空所有缓存吗？</p><p>清空包括：</p>删除本地所有缓存文件</div>");
            $("#modal-confirm").modal('show');
            url = "/clear/ssdbcache"
        });

        $("#clear_log").click(function () {
            $("#modal-confirm .modal-title span").text('清空日志');
            $("#modal-confirm .modal-body").html("<div><p>您确定要清空所有访问日志吗？</p><p>这将清除本地所有访问日志！</p></div>")
            $("#modal-confirm").modal('show');
            url = "/accesslog"
        });
        $("#init_sitemap").click(function () {
            var data = {
                _xsrf: $("input[name='_xsrf']").val(),
            };
            $("#init-modal").modal("show");
            submit_init_sitemap(data)
        });
        $("#open_nginx_cache").change(function () {
            var is_checked = $("#open_nginx_cache").is(":checked");
            if (is_checked) {
                var cache = 1;
            } else {
                var cache = 0;
            }
            data = {
                _xsrf: $("[name='_xsrf']").val(),
                cache: cache,
            };
            $.ajax({
                url: "/update/nginxcache",
                type: "POST",
                dataType: "json",
                data: data,
                timeout: 1000000,
                success: function (res) {
                    if (res.statu == 1) {
                        notie.alert({type: 1, text: '<b>' + res.msg + '</b>', position: 'top'});
                    } else {
                        notie.alert({type: 0, text: '<b>' + res.msg + '</b>', position: 'top'});
                    }
                },
                error: function (res) {
                    notie.alert({type: 3, text: '<b>网络错误</b>', position: 'top'});
                }
            })
        });
        $("#open_ssdb_cache").change(function () {
            var is_checked = $("#open_ssdb_cache").is(":checked");
            if (is_checked) {
                var cache = 1;
            } else {
                var cache = 0;
            }
            data = {
                _xsrf: $("[name='_xsrf']").val(),
                cache: cache,
            };
            $.ajax({
                url: "/update/ssdbcache",
                type: "POST",
                dataType: "json",
                data: data,
                timeout: 1000000,
                success: function (res) {
                    if (res.statu == 1) {
                        notie.alert({type: 1, text: '<b>' + res.msg + '</b>', position: 'top'});
                    } else {
                        notie.alert({type: 0, text: '<b>' + res.msg + '</b>', position: 'top'});
                    }
                },
                error: function (res) {
                    notie.alert({type: 3, text: '<b>网络错误</b>', position: 'top'});
                }
            })
        });

        $("#confirm-button").click(function () {
            // 提交到后端删除角色
            if (url == "/accesslog") {
                $.ajax({
                    url: url,
                    type: "DELETE",
                    dataType: "json",
                    data: data,
                    timeout: 1000000,
                    success: function (res) {
                        if (res.statu == 1) {
                            // 成功
                            //重载表格
                            notie.alert({type: 1, text: '<b>' + res.msg + '</b>', position: 'top'});
                        } else {
                            // 失败
                            notie.alert({type: 3, text: '<b>' + res.msg + '</b>', position: 'top'});
                        }
                    },
                    error: function (res) {
                        // 网络类型错误
                        notie.alert({type: 3, text: '<b>网络错误</b>', position: 'top'});
                    }
                });
                return
            }
            if (url) {
                $.ajax({
                    url: url,
                    type: "POST",
                    dataType: "json",
                    data: data,
                    timeout: 1000000,
                    success: function (res) {
                        if (res.statu == 1) {
                            // 成功
                            //重载表格
                            notie.alert({type: 1, text: '<b>' + res.msg + '</b>', position: 'top'});
                        } else {
                            // 失败
                            notie.alert({type: 3, text: '<b>' + res.msg + '</b>', position: 'top'});
                        }
                    },
                    error: function (res) {
                        // 网络类型错误
                        notie.alert({type: 3, text: '<b>网络错误</b>', position: 'top'});
                    }
                });
            } else {
                if (types == "init") {
                    $("#init-modal").modal("show");
                    var data = {
                        _xsrf: $("input[name='_xsrf']").val(),
                    };
                    submit_init(data)
                }
            }
        });
    });

    var url = "";
    var data = "";
    var type = "";

    let update_redis = function () {
        $.ajax({
            url: "/update/redis",
            type: "POST",
            dataType: "json",
            data: {
                _xsrf: $("[name='_xsrf']").val(),
            },
            timeout: 1000000,
            success: function (res) {
                if (res.statu == 1) {
                    notie.alert({type: 1, text: '<b>' + res.msg + '</b>', position: 'top'});
                } else {
                    notie.alert({type: 0, text: '<b>' + res.msg + '</b>', position: 'top'});
                }
            },
            error: function (res) {
                notie.alert({type: 3, text: '<b>网络错误</b>', position: 'top'});
            }
        })
    }
</script>
{% end %}