layui.extend({
    common    :   'js/common',
    dropdown    :   'layui/lay/modules/dropdown'
}).use(['jquery','table','layer','form','common'], function () {
    var $ = layui.jquery;
    var table = layui.table;
    //  弹窗插件
    var layer = layui.layer;
    var common = layui.common;
    //  引入表单
    var form = layui.form;

    var pageDate = {
        id  :   $('#id').val()
    };


    //表格对象
    var layuiTable = common.layuiTable.render(table,{
        elem: '#table_id'    //页面table的id
        , url: '/statistics/member/getAuthDeviceByMemberId?id=' + pageDate.id
        ,method: 'get'
        , cellMinWidth: 80 //全局定义常规单元格的最小宽度，
        , cols: [[
            { field: 'deviceAlias', title: '设备别名',align:'center' }
            ,{ field: 'deviceName', title: '设备名称',align:'center' }
            , { field: 'serialNumber', title: '设备序列号',align:'center' }
            , { field: 'residentialName', title: '所属小区',align:'center'}
            , { field: 'installPath', title: '安装位置',align:'center'}
            , { field: 'enable', title: '当前启用状态',templet:'#enable',align:'center'}
            , { title: '操作',align: 'center', toolbar: '#table-useradmin-webuser',width:180}
        ]]
        //  开启分页功能
        , page: true
    });

    table.on('tool(table_event)', function(obj){
        var data = obj.data;
        switch (obj.event) {
            case 'enable1' :
                layer.confirm('是否确认启用？',{icon: 7, title:'提示'}, function(index) {
                    $.ajax({
                        url: '/estate/memberDevice/enable',
                        type: 'POST',
                        data: {
                            id: data.id,
                            enable: 1
                        },
                        success: function (result) {
                            layer.msg('启用成功')
                        },
                        error: function () {
                            obj.othis.click();
                            layer.msg('启用失败')
                        }
                    });
                    queryTableFn();
                });
                break;
            case 'enable2' :
                layer.confirm('是否确认禁用？',{icon: 7, title:'提示'}, function(index) {
                    $.ajax({
                        url: '/estate/memberDevice/enable',
                        type: 'POST',
                        data: {
                            id: data.id,
                            enable: 2
                        },
                        success: function (result) {
                            layer.msg('禁用成功')
                        },
                        error: function () {
                            obj.othis.click();
                            layer.msg('禁用失败')
                        }
                    });
                    queryTableFn();
                });
                break;
            default :
                break;
        }
    });


    //  重新加载表格
    var queryTableFn = function(){
        //重新加载表格
        layuiTable.reload({
            where:{
            }
            //重新从第1页开始
            ,page: {
                curr: 1
            }
        });
    };

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

    //监听重置查询
    form.on('submit(reset)', function(){
        // 刷新select标签
        form.render('select');
        queryTableFn();
    });

});

