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


    //表格对象
    var layuiTable = common.layuiTable.render(table, {
        elem: '#table-id'    //页面table的id
        , url: '/statistics/member/showMemberList' //后台接口
        , method: 'post'
        , cellMinWidth: 80 //全局定义常规单元格的最小宽度，
        , cols: [[
            {field: 'name', title: '名字', align: 'center'}
            , {field: 'tel', title: '电话', align: 'center'}
            , {field: 'sex', title: '性别', templet: '#sex', align: 'center', sort: true}
            , {field: 'state', title: '认证状态', templet: '#state', align: 'center', sort: true}
            , {field: 'createDate', title: '注册时间', align: 'center', sort: true}
            , {field: 'isEnable', title: '状态', templet: '#entity_is_enable', width: 100, align: 'center', sort: true}
            , {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 'detail':
                common.openDefaultIframe({
                    title: '详情',
                    content: ''
                });
                break;
            case 'password':
                layer.open({
                    title: '修改账号密码',
                    type: 1,
                    icon: 3,
                    trigger: 'click',
                    area: ['400px', '200px'],
                    content: '' +
                        '   <div class="layui-card-body" style="margin-top:17px;margin-left: 20px">' +
                        '       <div class="layui-form">' +
                        '           <div class="layui-form-item">' +
                        '               <label class="layui-form-label"><span class="must">*</span><span>新密码:</span></label>' +
                        '               <div class="layui-inline">' +
                        '                   <div class="layui-input-inline">' +
                        '                       <input id="password" type="password" autocomplete="off" class="layui-input">' +
                        '                   </div>' +
                        '               </div>' +
                        '           </div>' +
                        '       </div>' +
                        '</div>',
                    btn: ['确定', '取消'],
                    yes: function (index) {
                        let password = $('#password').val();
                        if (password.length > 0) {
                            var data = {};
                            data.id = obj.data.id;
                            data.password = password;
                            $.ajax({
                                url: '/statistics/member/password',
                                data: data,
                                type: 'POST',
                                success: function () {
                                    layer.close(index);
                                    layer.msg('修改该账号密码成功');
                                }
                            });
                        } else {
                            layer.close(index);
                        }
                    },
                    btn2: function (index) {
                        layer.close(index);
                    }
                });
                break;
            case 'Tel':
                layer.open({
                    title: '更换绑定手机',
                    type: 1,
                    icon: 3,
                    trigger: 'click',
                    area: ['400px', '200px'],
                    content: '' +
                        '   <style type="text/css">' +
                        '       input::-webkit-outer-spin-button,' +
                        '       input::-webkit-inner-spin-button {-webkit-appearance: none;}' +
                        '       input[type="number"] {moz-appearance: textfield}' +
                        '   </style>' +
                        '   <div class="layui-card-body" style="margin-top:17px;margin-left: 20px">' +
                        '       <div class="layui-form">' +
                        '           <div class="layui-form-item">' +
                        '               <label class="layui-form-label"><span class="must">*</span><span>新手机号码:</span></label>' +
                        '               <div class="layui-inline">' +
                        '                   <div class="layui-input-inline">' +
                        '                       <input id="tel" type="number" autocomplete="off" class="layui-input">' +
                        '                   </div>' +
                        '               </div>' +
                        '           </div>' +
                        '       </div>' +
                        '</div>',
                    btn: ['确定', '取消'],
                    yes: function (index) {
                        let tel = $('#tel').val();
                        if (tel.length == 11) {
                            var data = {};
                            data.id = obj.data.id;
                            data.tel = tel;
                            $.ajax({
                                url: '/statistics/member/update',
                                data: data,
                                type: 'POST',
                                success: function () {
                                    layer.close(index);
                                    layer.msg('更换绑定手机成功');
                                }
                            });
                        } else if (tel.length == 0) {
                            layer.close(index);
                        } else {
                            layer.msg("请输入正确的手机号")
                        }
                    },
                    btn2: function (index) {
                        layer.close(index);
                    }
                });
                break;
            case 'room':
                common.openDefaultIframe({
                    title: '查看认证房屋',
                    content: '/statistics/member/room/' + data.id
                });
                break;
            case 'access':
                common.openDefaultIframe({
                    title: '管理门禁授权',
                    content:  '/statistics/member/access/' + data.id
                });
                break;
            default:
                break;
        }
    });

    //监听启用或者禁用
    form.on('switch(enable)', function (obj) {
        let enable = obj.elem.checked;
        let text = enable ? '启用' : '禁用';
        $.ajax({
            url: '/statistics/member/enable',
            type: 'POST',
            data: {
                id: this.value,
                enable: enable ? 1 : 2
            },
            /*contentType:"application/json",
            data    :   JSON.stringify({
                id: this.value,
                enable: enable ? 1 : 2
            }),*/
            success: function (result) {
                layer.msg(text + '成功')
            },
            error: function () {
                obj.othis.click();
                layer.msg(text + '失败')
            }
        });
    });

    //  重新加载表格
    var queryTableFn = function (obj) {
        var name = $('#selectedName').val();
        var tel = $('#selectedTel').val();
        var state = $('#selectedState').val();
        //重新加载表格
        layuiTable.reload({
            where: {
                name: name,
                tel: tel,
                state: state,
                //排序
                sortField: obj ? obj.field : '',
                sortOrder: obj ? obj.type : '',
            }
            //重新从第1页开始
            , page: {
                curr: 1
            },
            initSort: obj,
        });
    };

    //监听排序
    table.on('sort(table_event)', function (obj) {
        queryTableFn(obj)
    });

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

    //监听添加
    form.on('submit(add)', function(){
        common.openDefaultIframe({
            title: '添加APP会员',
            content:  '/statistics/member/add'
        });
    });

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

    //监听会员添加
    form.on('submit(add)', function () {
        layer.open({
            title: '添加会员',

            type: 1,
            icon: 3,
            trigger: 'click',
            area: ['400px', '400px'],
            content: '' +
                '<script src="http://pv.sohu.com/cityjson?ie=utf-8"></script>'+
            ' <script type="text/javascript">'+
            ' window.onload = function () {'+
                '    document.getElementById("ip").value = returnCitySN["cip"]'+
                ' }'+
                ' </script>'+

            '   <style type="text/css">' +
                '       input::-webkit-outer-spin-button,' +
                '       input::-webkit-inner-spin-button {-webkit-appearance: none;}' +
                '       input[type="number"] {moz-appearance: textfield}' +
                '   </style>' +
                '   <div class="layui-card-body" style="margin-top:17px;margin-left: 20px">' +
                '       <div class="layui-form">' +
                '           <div class="layui-form-item">' +
                '               <label class="layui-form-label"><span class="must">*</span><span>会员姓名:</span></label>' +
                '               <div class="layui-inline">' +
                '                   <div class="layui-input-inline">' +
                '                       <input id="name" type="text" autocomplete="off" class="layui-input">' +
                '                   </div>' +
                '               </div>' +
                '           </div>' +
                '           <div class="layui-form-item">' +
                '               <label class="layui-form-label"><span class="must">*</span><span>电话号码:</span></label>' +
                '               <div class="layui-inline">' +
                '                   <div class="layui-input-inline">' +
                '                       <input id="tel" type="number" autocomplete="off" class="layui-input">' +
                '                   </div>' +
                '               </div>' +
                '           </div>' +
                '           <div class="layui-form-item">' +
                '               <label class="layui-form-label"><span class="must">*</span><span>登录密码:</span></label>' +
                '               <div class="layui-inline">' +
                '                   <div class="layui-input-inline">' +
                '                       <input id="password" type="text" autocomplete="off" class="layui-input">' +
                '                   </div>' +
                '               </div>' +
                '           </div>' +

                '       </div>' +
                '</div>',
            btn: ['确定', '取消'],
            yes: function (index) {
                let tel = $('#tel').val();
                let name = $('#name').val();
                let password = $('#password').val();
                let createIp = $('#ip').val();
                if (tel.length == 11) {
                    var data = {};
                    data.name = name;
                    data.tel = tel;
                    data.password = password;
                    data.createIp=createIp;
                    $.ajax({
                        'url': '/statistics/member/memberRegister',
                        'data': data,
                        'type': 'POST',
                        'success': function (result) {
                            console.log(result.msg)
                            console.log(result.code)
                            //关闭弹窗层
                            layer.close(index);
                            // 父页面刷新
                           window.location.reload();
                        }
                    });
                } else if (tel.length == 0) {
                    layer.close(index);
                } else {
                    layer.msg("请输入正确的手机号")
                }
            },
            btn2: function (index) {
                layer.close(index);
            }
        });
    });

});

