{include file="public/head"}
<div class="layui-fluid layui-rg-bg">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
            <ul class="layui-tab-title">
            <li class="layui-this">本地插件列表</li>
            <div class="layui-inline" style="float:right;">
                <div class="layui-input-inline"><a class="layui-btn layui-btn-primary layui-btn-sm refresh"><i class="layui-icon layui-icon-refresh-3"></i></a></div>
            </div>
            </ul>
            <div class="layui-tab-content">
                <div class="layui-tab-item layui-show">
                    <table id="data_table" class="layui-table" lay-filter="data_table"></table>
                </div>
            </div>
        </div>
    </div>
</div>
{include file="public/foot"}
<script type="text/html" id="status">
    {{#  if(!d.isinstall){ }}
    <input type="checkbox" lay-skin="switch" disabled>
    {{#  } else { }}
    <input type="checkbox" name="status" lay-skin="switch" value="{{d.id}}" addon="{{d.name}}" lay-filter="status" {{ d.status == 1 ? 'checked' : '' }}>
    {{#  } }}
</script>
<script type="text/html" id="action">
    {{#  if(!d.isinstall){ }}
    <div class="layui-input-inline"><a class="layui-btn layui-btn-xs" lay-event="install"><i class='layui-icon layui-icon-add-1'></i>{:lang('install')}</a></div>
    <div class="layui-input-inline"><a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delete"><i class='layui-icon layui-icon-delete'></i>{:lang("del")}</a></div>
    {{#  } else { }}
        {{# if(d.is_admin){ }}
        <div class="layui-input-inline"><a class="layui-btn layui-btn-xs" lay-event="manager"><i class='layui-icon layui-icon-tabs'></i>管理</a></div>
        {{# } }}
        {{# if(d.setting){ }}
        <div class="layui-input-inline"><a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="setting"><i class='layui-icon layui-icon-set'></i>设置</a></div>
        {{# } }}
    <div class="layui-input-inline"><a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="uninstall"><i class='layui-icon layui-icon-delete'></i>{:lang('uninstall')}</a></div>
    {{#  } }}
</script>
<script type="text/html" id="help">
<a class="layui-btn layui-btn-xs" lay-event="help"><i class='layui-icon layui-icon-survey'></i>说明</a>
</script>
<script type="text/javascript">
    layui.use(['table','form','element','code'],function(){
        var table = layui.table, form = layui.form, $ = layui.jquery; //日期控件
        var layer = layui.layer;
        loading = layer.load(1, {shade: [0.1, '#fff']});
        table.render({
            elem: '#data_table',
            url: '{:url("index")}',
            method: 'POST',
            cols: [[
            {type:'numbers'},
            {field:'title', title:'插件名称'},
            {field:'name', title:'插件目录',width: 150},
            {width: 90, align: 'center', title:'说明', templet: '#help'},
            {field:'author', title:'作者'},
            {field:'description', title:'描述'},
            {field:'version',width: 100, align: 'center', title:'版本号'},
            {field:'isinstall', title:'安装',width:100,align:'center',templet:function(d){
                if(d.isinstall) {
                    return '<font color="#009688">已安装</font>';
                } else {
                    return '<font color="#FF5722">未安装</font>';
                }
            }},
            {field:'status',width: 80, align: 'center', title:'启用', templet: '#status'},
            {toolbar: '#action',width: 200, align: 'center', title: '{:lang("act")}', fixed: 'right'}
            ]],
            limit: {:config('app.admin.pagelimit')},
            page: true,
            done: function (res) {
                $('.countnum').text('记录数：' + res.count + '条');
                layer.close(loading);
            },
        });
        //点击事件
        table.on('tool()', function(obj) {
            var data = obj.data;
            var event = obj.event;
            if(event === 'install' || event == 'uninstall' || event== 'delete'){
                var actname = event == 'install' ? '安装' : (event == 'delete'? '删除' : '卸载');
                layer.confirm('确认'+actname+'插件('+ data.name +')吗？',{title:actname},function(){
                    table_ajax(data,'{:url("'+event+'")}',1);
                })
                return false;
            } else if(event == 'manager') {
                var url = '/addons/'+data.name+'.admin/index';
                openlayer(url,data.title+'管理','m');
            } else if(event == 'setting') {
                var url = '{:url("config")}?addon_name='+data.name;
                openlayer(url,data.title+'配置','xs');
            } else if(event == 'help') {
                var help = data.help == undefined ? '无' : data.help;
                var hooks = data.hooks;
                layer.open({
                   type:0,
                   maxmin: false,
                   area: ['50%', '50%'], //宽高
                   title: false,
                   content: "<h3>插件钩子</h3><p style='color:#999'>"+(hooks?hooks:"无")+"</p><h3>模板中使用方法</h3><p style='color:#999'>{\:hook('钩子名称', '参数')}</p><h3>PHP业务中使用方法</h3><p style='color:#999'>hook('钩子名称', '参数')</p><h3>详细帮助</h3><p style='color:#999'>"+help+"</p>"
               });
            }
        });
        //监听开关操作
        form.on('switch(status)', function(obj){
            var data = {};
            data[obj.elem.name] = obj.elem.checked ? 1 : 0;
            data['name'] = $(this).attr('addon');
            table_ajax(data,"{:url('setstatus')}",2);
        });
    })
</script>