<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>应用插件管理</title>
        <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" />
        <link rel="stylesheet" href="/app/admin/admin/css/reset.css" />
    </head>
    <body class="pear-container">

        <div class="layui-card">
            <div class="layui-card-body">
                <table id="data-table" lay-filter="data-table"></table>
            </div>
        </div>

        <script type="text/html" id="install">
            {{# if(!d.installed){ }}
            <button class="pear-btn pear-btn-xs pear-btn-primary" onclick="install('{{ d.name }}','{{ d.version }}')">
                安装
            </button>
            {{# }else{ }}
                {{# if(d.installed !== d.version){ }}
                <button class="pear-btn pear-btn-xs pear-btn-success" onclick="install('{{ d.name }}','{{ d.version }}')">
                    升级
                </button>
                {{# } }}
                {{# if(d.name !== "admin"){ }}
                <button class="pear-btn pear-btn-xs pear-btn-danger" onclick="uninstall('{{ d.name }}','{{ d.installed }}')">
                    卸载
                </button>
                {{# } }}
            {{# } }}
        </script>

        <script src="/app/admin/component/layui/layui.js"></script>
        <script src="/app/admin/component/pear/pear.js"></script>
        <script src="/app/admin/admin/js/auth.js"></script>
        <script>

            const SELECT_API = "/app/admin/plugin/list";
            const AUTH_URL = "/app/admin/plugin/login";
            const SCHEMA_API = "/app/admin/plugin/schema";

            layui.use(["table", "form","common", "popup"], function() {
                let table = layui.table;
                let form = layui.form;
                let $ = layui.$;
                let common = layui.common;

                $.ajax({
                    url: SCHEMA_API,
                    dataType: "json",
                    success: function (e) {
                        let cols = e.data;
                        layui.each(cols, function (k, v) {
                            if (v.field === "installed") {
                                cols[k].templet = "#install";
                            }
                        })
                        function render()
                        {
                            table.render({
                                elem: "#data-table",
                                url: SELECT_API,
                                page: true,
                                cols: [cols],
                                skin: "line",
                                size: "lg",
                                autoSort: false,
                                defaultToolbar: [{
                                    title: "刷新",
                                    layEvent: "refresh",
                                    icon: "layui-icon-refresh",
                                }, "filter", "print", "exports"]
                            });
                        }
                        render();
                    }
                });

                window.install = function(name, version) {
                    let loading = layer.load();
                    $.ajax({
                        url: "/app/admin/plugin/install",
                        type: "POST",
                        dataType: "json",
                        data: {name, version},
                        success: function (res) {
                            if (res.code) {
                                return layui.popup.failure(res.msg);
                            }
                            // 需要登录官网
                            if (res.data && res.data.code === 401) {
                                layer.open({
                                    type: 2,
                                    title: "登录 workerman.net 官网",
                                    shade: 0.1,
                                    area: [common.isModile()?"100%":"500px", common.isModile()?"100%":"450px"],
                                    content: AUTH_URL
                                });
                                return;
                            }
                            return layui.popup.success("安装成功", function () {
                                parent.location.reload();
                            });
                        },
                        complete: function () {
                            layer.close(loading);
                        }
                    });
                }

                window.uninstall = function(name, version) {
                    layer.confirm("确定卸载?", {
                        icon: 3,
                        title: "提示"
                    }, function(index) {
                        layer.close(index);
                        let loading = layer.load();
                        $.ajax({
                            url: "/app/admin/plugin/uninstall",
                            type: "POST",
                            dataType: "json",
                            data: {name, version},
                            success: function (res) {
                                if (res.code) {
                                    return layui.popup.failure(res.msg);
                                }
                                return layui.popup.success("卸载成功", function () {
                                    parent.location.reload();
                                });
                            },
                            complete: function () {
                                layer.close(loading);
                            }
                        });
                    });
                }
            })


        </script>
    </body>
</html>
