{include file="public/head"}
<div class="layui-fluid layui-rg-bg">
    <div class="layui-card">
        <div class="layui-card-header">
            <div class="layui-inline">
                <div class="layui-input-inline"><button class="layui-btn layui-btn-primary layui-btn-sm layui-btn-disabled countnum"></button></div>
            </div>
            <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 class="layui-input-inline"><a class="layui-btn layui-btn-sm" onclick="openlayer('{:url("add")}','{:lang('add')}','xs')">{:lang('add')}</a></div>
            </div>
        </div>
        <div class="layui-card-body">
        {if $admininfo.isboss}
            <div class="layui-form">
                <div class="layui-inline">
                    <div class="layui-form-item">
                        <label class="layui-form-label">角色选择：</label>
                        <div class="layui-input-inline">
                            <select lay-filter="groupid">
                            <option value="">选择角色</option>
                            {foreach :app\admin\model\Admingroup::select() as $v}
                            <option value="{$v.id}">{$v.title}</option>
                            {/foreach}
                            </select>
                        </div>
                    </div>
                </div>
            </div>
        {/if}
            <table id="data_table" class="layui-table" lay-filter="data_table"></table>
            {:pagedebug()}
        </div>
    </div>
</div>
{include file="public/foot"}
<script type="text/html" id="action">
    <div class="layui-input-inline"><a class="layui-btn layui-btn-xs" lay-event="edit"><i class='layui-icon layui-icon-edit'></i>{:lang('edit')}</a></div>
    {{# if(d.id !== '{$admininfo.id}'){ }}
    <div class="layui-input-inline"><a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del"><i class='layui-icon layui-icon-delete'></i>{:lang('del')}</a></div>
    {{# } }}
</script>
<script type="text/html" id="status">
    {{# if(d.id !== '{$admininfo.id}'){ }}
    <input type="checkbox" name="status" value="{{d.id}}" nickname="{{d.nickname}}" username="{{d.username}}" admingroup_id="{{d.admingroup_id}}" lay-skin="switch" lay-filter="status" {{ d.status == 1 ? 'checked' : '' }}>
    {{# } else { }}
    <font color='#FF5722'>当前</font>
    {{# } }}
</script>
<script type="text/javascript">
    layui.use(['table','form','element'],function(){
        var table = layui.table, form = layui.form, $ = layui.jquery;
        loading = layer.load(1, {shade: [0.1, '#fff']});
        table.render({
            elem: '#data_table',
            url: '{:url("index")}',
            method: 'POST',
            cols: [[
            {field:'id',title:'ID',align:'center',width:60},
            {title:'用户名【昵称】',templet:function(d){
                return d.username + '【'+ d.nickname +'】';
            },minWidth:150},
            {title:'角色', templet: function(d){
                return d.admingroup.title;
            }},
            {title:'上次登陆',templet:function(d){
                return d.logindata.lsttime;
            },width: 160},
            {field:'create_time', title:'创建日期',width: 160},
            {align: 'center', title:'次数',templet:function(d){
                return d.logindata.count;
            },width:80},
            {title:'操作站点',templet:function(d){
                if(!d.sitename) {
                    return '<font color="#FF5722">所有站点</font>';
                } else {
                    return d.sitename;
                }
                
            }},
            {field: 'status',width: 80, align: 'center', title: '{:lang("state")}', templet: '#status'},
            {toolbar: '#action', width: 140, 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('rowDouble(data_table)', function(obj){
            var url = url = "{:url('edit')}?id="+obj.data.id;
            openlayer(url,'{:lang("edit")}','xs');
        });
        //点击事件
        table.on('tool(data_table)', function(obj) {
            var data = obj.data;
            var event = obj.event;
            if(event === 'del'){
                layer.confirm('确认{:lang("del")}管理员('+ data.username +')吗？',{title:'{:lang("del")}'},function(){
                    table_ajax(data,'{:url("del")}',10);
                })
                return false;
            }else if(event === 'edit'){
                var url = "{:url('edit')}?id="+data.id;
                openlayer(url,'修改资料','xs');
            }
        });
        // 监听下拉
        form.on('select(groupid)', function(data){
            var groupid = data.value;
            table.reload('data_table',{
                page:{curr:1},
                where:{groupid:groupid}
            });
        });
        //监听开关操作
        form.on('switch(status)', function(obj){
            var data = {};
            data[obj.elem.name] = obj.elem.checked ? 1 : 0;
            data['id'] = obj.value;
            data['list_switch'] = true;
            data['username'] = $(obj.elem).attr('username');
            data['nickname'] = $(obj.elem).attr('nickname');
            table_ajax(data,'{:url("edit")}',11);
        });
    })
</script>