//config的设置是全局的
layui.config({
    base: '/static/js/extend/' //假设这是你存放拓展模块的根目录
}).extend({ //设定模块别名
    common: 'common' //如果 mymod.js 是在根目录，也可以不用设定别名
});

layui.use(['form','table','laydate','upload','common'], function() {
        var form = layui.form;
        var common = layui.common;
        var table = layui.table;
        var $ = layui.jquery,upload = layui.upload;
        var laydate = layui.laydate;
        var tableInfo=table.render({
            elem: '#tableList'
            ,url:'/sysExhibition/list'
            ,id: 'id'
            ,method:"post"
            ,contentType:"application/json;charset=UTF-8"
            ,loading:true
            ,cols: [[
                {field:'id', title: 'ID', sort: true,checkbox:true ,fixed: 'left'}
                ,{field:'id', title: '编号'}
                ,{field:'name', title: '图片标题'}
                ,{field:'imageUrl', title: '图片',templet:function(d){
                    return '<img onclick="showImg(\''+d.imageUrl+'\',\''+d.name+'\')" src='+d.imageUrl+'>';
                }}
                ,{field:'state', title: '状态', sort: true,templet:function (d) {
                        if (d.state==1){
                            return '<input type="checkbox" value="'+d.id+'" name="state" checked="checked" lay-skin="switch"  lay-filter="switchTest" lay-text="启用|禁用">';
                        }else{
                            return '<input type="checkbox" value="'+d.id+'" name="state" lay-skin="switch"  lay-filter="switchTest" lay-text="启用|禁用">';
                        }
                    }}
            ]]
            ,page: true
        });

    //普通图片上传
    var uploadInst = upload.render({
        elem: '#test1'
        ,auto: true
        ,url: '/sys/pushFile'
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                $('#demo1').attr('src', result); //图片链接（base64）
            });
        }
        ,done: function(res){
            form.val('fromData', {
                "imageUrl": res.body
            })

            //如果上传失败
            if(res.code !='01'){
                return layer.msg('上传失败');
            }
        }
    });

    //监听锁定操作
    form.on('switch(switchTest)', function(obj){
        common.ajaxGetRequest("/sysExhibition/updateByState",{id:this.value,state:obj.elem.checked==false?0:1})
    });

    //日期时间选择器
    laydate.render({
        elem: '#startTime'
        ,type: 'datetime'
    });
    //日期时间选择器
    laydate.render({
        elem: '#endTime'
        ,type: 'datetime'
    });


    //监听提交
    form.on('submit(formSubmit)', function(data){
        delete data.field.file
        common.ajaxPostRequest("/sysExhibition/insertOrUpdata",data.field,"id");
    });

    //打开 新增or 修改窗口
    $(document).on('click', '#openSaveOrUpdate', function() {
        common.layuiOpen("addDiv","60%","80%")
    });
});


function showImg(url,title) {
    layer.photos({
        photos: { "data": [{alt:title,"src": url}] }
    });
}