layui.extend({
    common    :   'js/common'
}).use(['jquery','table','common','util','laydate','form'], function () {
    var $ = layui.jquery;
    var table = layui.table;
    var common=layui.common;
    var util=layui.util;
    var laydate=layui.laydate;
    var  form=layui.form;
    common.layuiTable.render(table,{
         elem: '#table-id'
        , url: '/statistics/sysOperationLog/showSysOperationLogList' //模拟接口
        ,method:'post'
        , cellMinWidth: 80 //全局定义常规单元格的最小宽度，
        , cols: [[
             { field: 'operatorAccountName', title: '操作人',align:'center'}
            , { field: 'operationEvent', title: '操作事件',templet:'#operationEvent',align:'center'}
            , { field: 'oldObj', title: '操作对象',align:'center',width:100 }
            , { field: 'newObj', title: '新对象',align:'center',width:100 }
            , { field: 'menu', title: '操作菜单',align:'center'}
            , { field: 'operatorIp', title: 'ip',align:'center'}
            , { field: 'operationDate',align:'center', title: '操作时间',templet:function(d) {
                        return util.toDateString(d.operationDate);
                }}
            , { field: 'message',align:'center', title: '日志信息',templet:function(d) {
                    if(!d.message){
                        //如果为空，系统自动填充日志内容
                        var event=d.operationEvent;
                        var tableName=d.tableName;
                        var tableId=d.tableId;
                        var batchOperation=false;
                        if(tableId){
                            var index=tableId.indexOf(",");
                            batchOperation=index>0?true:false;
                        }
                        //事件
                        var message="";
                        var m1="",m2="";
                        if(tableName) {
                            m1="表("+tableName+")";
                        }
                        if(batchOperation){
                            m1 = m1+"批量";
                        }
                        if(tableId) {
                            m2="id("+tableId+")";
                        }
                        if(event== 0){
                            if(tableName){
                                message=m1+"新增一条"+m2+"记录";
                            }else{
                                message=m1+"新增一条"+m2+"记录";
                            }
                        }else if(event== 1){
                            message = m1+"删除"+m2+"记录";
                        }else if(event== 2){
                                message = "修改" + m2 + "记录";
                        }else if(event== 3){
                            message="登录系统";
                        }else if(event== 4){
                            message="退出系统";
                        }
                        return message;
                    }
                    return d.message;
                }}
            ,{ title: '操作',align: 'center', toolbar: '#table-useradmin-webuser',width:180 }
        ]]
       , id: 'table-id'
        , page: true
    });

    var startOperationDate,endOperationDate;
    var queryTableFn=function(){
        var operatorAccountName = $('input[name="operatorAccountName"]').val();
        var menu = $('input[name="menu"]').val();
        var operationEvent= $('select[name="operationEvent"] :selected').val();
        //执行重载
        table.reload('table-id', {
            page: {
                curr: 1 //重新从第 1 页开始
            }
            ,where: {
                operatorAccountName: operatorAccountName,
                menu: menu,
                operationEvent: operationEvent,
                startOperationDate:startOperationDate,
                endOperationDate:endOperationDate
            }
        });
    }

    //日期时间范围选择
    laydate.render({
        elem: '#laydate-range-datetime',
        format:'yyyy-MM-dd'
        ,range:true //或 range: '~' 来自定义分割字符
        ,done: function(value, date, endDate){
            if(value){
                var dates=value.split(" - ");
                startOperationDate=dates[0];
                endOperationDate=dates[1];
            }else{
                startOperationDate="";
                endOperationDate="";
            }
        }
    });

    //监听搜索
    form.on('submit(query)', function(){
        queryTableFn();
    });

    //监听重置查询
    form.on('submit(reset)', function(){
        //清空条件
        $('input[name="operatorAccountName"]').val("");
        $('input[name="menu"]').val("");
        $('select[name="operationEvent"]').val("");
        startOperationDate="";
        endOperationDate="";
        $("#laydate-range-datetime").val("");
        // 刷新select标签
        form.render('select');
        queryTableFn();
    });

    table.on('tool(table-id)', function(obj){
        var data = obj.data; //获得当前行数据
        switch (obj.event) {
            case 'detail'://详情
                common.openDefaultIframe({
                    title: '详情',
                    content:  '/statistics/sysOperationLog/detail/'+data.id
                });
                break;
            default:
                break;
        };
    });
});

