<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
    <title>小程序列表</title>
    </title>
    <!-- 引入 WeUI -->
    <link rel="stylesheet" href="https://res.wx.qq.com/open/libs/weui/1.1.2/weui.min.css"/>
    <link rel="stylesheet" href="https://cdn.bootcss.com/jquery-weui/1.2.0/css/jquery-weui.css"/>
    <style type="text/css">
        body, html {
            height: 100%;
            background-color: #f4f4f4;
            -webkit-tap-highlight-color: transparent;
        }

        footer {
            text-align: center;
            font-size: 14px;
            padding: 20px;
        }

        .weui-loadmore_line .weui-loadmore__tips {
            background-color: #f4f4f4;
        }

        footer a {
            color: #999;
            text-decoration: none;
        }

    </style>
</head>

<body ontouchstart="">
<div class="weui-cells__title weui-cell">
    小程序列表
</div>
<div class="weui-cells plugin-lists"></div>


<div class="weui-loadmore weui-loadmore_line loadmore_plugin_list">
    <span class="weui-loadmore__tips"></span>
</div>

<input type='hidden' id='init_url' url-data="/plugin/pluginList"/>
<input type='hidden' id='del_plugin' plugin-id=""/>
<script type="text/javascript" src="https://cdn.bootcss.com/jquery/2.2.4/jquery.js"></script>
<script type="text/javascript" src="https://res.wx.qq.com/open/libs/weuijs/1.1.3/weui.min.js"></script>
<script type="text/javascript" src="https://cdn.bootcss.com/jquery-weui/1.2.0/js/jquery-weui.js"></script>
<script type="text/javascript">

    $(function () {
        showList();
    });

    function getOsType() {
        var u = navigator.userAgent;
        if (u.indexOf('Android') > -1 || u.indexOf('Linux') > -1) {
            return 'Android';
        } else if (u.indexOf('iPhone') > -1) {
            return 'IOS';
        } else {
            return 'PC';
        }
    }

    function reqUrl(reqUri, params, callbackName) {
        reqUri = "/akaxin" + reqUri;
        var type = getOsType();
        if (type == 'Android') {
            Android.requestPost(reqUri, params, callbackName);
        } else if (type == 'IOS') {
            ios_requestPost(reqUri, params, callbackName);
        } else {
            $.toast('暂时不支持该设备');
        }
    }

    function reqHtml(reqUri, params) {
        reqUri = "/akaxin" + reqUri;
        var type = getOsType();
        if (type == 'Android') {
            Android.requestPage(reqUri, params)
        } else if (type == 'IOS') {
            ios_requestPage(reqUri, params);
        } else {
            $.toast('暂时不支持该设备');
        }
    }

    function reqImageUpload(callback) {
        var type = getOsType();
        if (type == 'Android') {
            Android.imageUpload(callback);
        } else if (type == 'IOS') {
            ios_imageUpload(callback);
        } else {
            $.toast('暂时不支持该设备');
        }
    }

    function reqImageDownload(imageid, callback) {
        var type = getOsType();
        if (type == 'Android') {
            Android.imageDownload(imageid, callback);
        } else if (type == 'IOS') {
            ios_imageDownload(imageid, callback);
        } else {
            $.toast('暂时不支持该设备');
        }
    }

    function toast(msg) {
        var type = getOsType();
        if (type == 'Android') {
            Android.showToast(msg);
        } else if (type == 'IOS') {
            $.toast(msg);
        } else {
            $.toast('暂时不支持该设备');
        }
    }

    $(document).on('click', '.update-plugin', function () {
        var reqUri = $(this).attr('url-data');
        var pluginId = $(this).attr('plugin-id');
        var params = {
            'plugin_id': pluginId,
        };
        reqHtml(reqUri, JSON.stringify(params));
    });

    $(document).on("click", ".delete-plugin", function () {
        var pluginId = $(this).attr('plugin-id');
        var reqUri = $(this).attr('url-data');
        $('#del_plugin').attr('plugin-id', pluginId);
        $.confirm({
            title: '删除插件',
            text: '插件删除后，将不能使用！',
            onOK: function () {
                var params = {
                    'plugin_id': pluginId,
                };
                reqUrl(reqUri, JSON.stringify(params), "delPlugin");
                delPlugin('success');
                $.closeModal();
            },
            onCancel: function () {
                console.log('cancle');
                toast('取消操作');
                $.closeModal();
            }
        });
    });

    function delPlugin(result) {
        if (result == 'success') {
            var pluginId = $('#del_plugin').attr('plugin-id');
            $('#' + pluginId).remove();
            $.toast('删除成功');
        } else {
            $.toast('删除失败', 'forbidden');
        }
    }

    var page = 0;
    var loading = false;

    function showList() {
        if (loading) {
            $('.loadmore_plugin_list').addClass('weui-loadmore_line');
            $('.loadmore_plugin_list span').html('暂无数据');
            $('.loadmore_plugin_list i').remove();
            return
        }
        page = page + 1;
        var reqUri = $('#init_url').attr('url-data');
        var params = {
            "page": page.toString()
        };
        reqUrl(reqUri, JSON.stringify(params), 'addpluginToList');
        loading = true;
    }

    $(document.body).infinite().on("infinite", function () {
        showList();
    });

    function addpluginToList(results) {
        var JsonData = eval("(" + results + ")");
        var data = JsonData.pluginData;
        var html = '';
        $.each(data, function (index, plugin) {
            var html = '<a class="weui-cell weui-cell_access  update-plugin" url-data="/plugin/editPage" plugin-id="' + plugin.id + '"  href="javascript:;" >  <div class="weui-cell__bd"> <p>' + plugin.name + '</p> </div> <div class="weui-cell__ft"></div> </a>';
            $('.plugin-lists').append(html);
        });
        loading = JsonData.loading;
        if (loading == true) {
            $(".weui-loadmore__tips").html("到底啦");
        } else {
            $(".weui-loadmore__tips").html("加载更多");
        }
    }
</script>
</body>

</html>