layui.use(['laypage', 'layer', 'table', 'element', 'form', 'laydate'], function () {

    var laypage = layui.laypage //分页
        , layer = layui.layer //弹层
        , table = layui.table //表格
        , element = layui.element //元素操作
        , form = layui.form,
        laydate = layui.laydate;


    laydate.render({
        elem: '#date1'
    });
    laydate.render({
        elem: '#date2'
    });
    //第一个实例
    var tableIns = table.render({
        elem: '#userlist'
        , url: '/usersignup/homeform' //数据接口
        , where: {token: csrfToken}
        , method: 'post'
        , id: 'tablerReload'
        , page: true //开启分页
        ,height:600
        , limit: 20
        , text: '数据加载中'
        , cols: [[ //表头
            // {field: 'id', title: 'ID', width:80, sort: true, fixed: 'left',align:'center'},
            {type: 'numbers', fixed: "left", title: '序号'},
            {type: "checkbox", width: 50},
            {
                field: 'equipment',
                title: '设备',
                width: 110,
                align: 'center',
                templet: "#equipment",
                templet: function (d) {
                    if (d.equipment == 'PC') {
                        return '<div class="layui-table-cell" style="color:red;">' + d.equipment + '</div>'
                    } else {
                        return '<div class="layui-table-cell" style="color:#009688;">' + d.equipment + '</div>'
                    }
                }
            }
            , {field: 'ip', title: 'ip', width: 150, align: 'center'}
            , {field: 'source', title: '来源', align: 'center'}
            // ,{field: 'name', title: '姓名',width:100,align:'center'}
            , {field: 'mobile', title: '手机号码', width: 150, align: 'center'}
            , {
                field: 'city_name', title: '区域', width: 150, align: 'center', templet: function (d) {
                    if (d.city_name != null) {
                        return '<div class="layui-table-cell" style="color:#c8601f;font-weight:600">' + d.city_name + '</div>'
                    } else {
                        return '<div class="layui-table-cell" style="font-weight:600">-----</div>'
                    }
                }
            }
            , {
                field: 'house_name', title: '归属', width: 200, align: 'center', templet: function (d) {
                    if (d.house_name != null) {
                        return '<div class="layui-table-cell" style="font-weight:600">' + d.house_name + '</div>'
                    } else {
                        return '<div class="layui-table-cell" style="font-weight:600">-----</div>'
                    }
                }
            }
            , {field: 'state', title: '发送状态', width: 150, align: 'center'}
            , {field: 'send_email', title: '发送邮箱', width: 150, align: 'center'}
            , {field: 'create_at', title: '日期', width: 150, align: 'center'}
            // ,{field: 'city', title: '用户区域',width:80,align:'center'}
            // ,{field: 'send_email', title: '发送邮箱',width:180,align:'center'}
            // ,{field: 'state', title: '发送状态',width:100,align:'center',templet:"#state",templet:function(d){
            //         if(d.state == '成功'){
            //             return '<div class="layui-table-cell" style="color:#00960b;font-weight:600">'+d.state+'</div>'
            //         }else {
            //             return '<div class="layui-table-cell" style="color:red;font-weight:600">'+d.state+'</div>'
            //         }
            //     }}

            , {fixed: 'right', title: '操作', width: 150, align: 'center', toolbar: '#signupBar'}

        ]]
    });

    // tableIns.reload('tablerReload');        //重新加载

    //列表操作
    table.on('tool(userlist)', function (obj) {
        var layEvent = obj.event,
            data = obj.data;
        if (layEvent === 'edit') {
            layer.open({
                type: 2,
                title: '用户详情',
                area: ['600px', '500px'],
                anim: 2,
                content: '/usersignup/userdetails?id=' + data.id,
                end: function () {
                    table.reload('tablerReload');
                }//iframe的url,no代表不显示滚动条
            });
        } else if (layEvent === 'del') {
            layer.confirm('您确认要删除吗?', {btn: ['确认', '取消']}, function () {
                console.log(data.id)
                $.post('/usersignup/signupdel', {id: data.id}, function (data) {
                    layer.msg(data.msg);
                    // parent.layui.
                    table.reload('tablerReload');

                })
            })
        }

    });

    var $ = layui.$, active = {
        addData: function () { //获取选中数据
            layer.open({
                type: 2,
                title: '添加账号',
                closeBtn: 1, //不显示关闭按钮
                shade: [0],
                area: ['600px', '500px'],
                anim: 2,
                content: ['/user/adduser', 'no'], //iframe的url,no代表不显示滚动条
            });
        },
        sendEmail: function () {
            var checkStatus = table.checkStatus('tablerReload');
            var index = layer.msg('数据提交中,请稍候', {icon: 16, time: false, shade: 0.8});      //数据提交提示
            if (typeof  checkStatus.data[0] == 'object') {
                layer.confirm('确定要发送吗?', {btn: ['确认', '取消']}, function () {
                    var formdat = new FormData();
                    $.each(checkStatus.data, function (key, val) {
                        formdat.append('hid[]', val.id);
                    })
                    $.ajax({
                        cache: true,
                        type: "post",
                        url: "/usersignup/emailsend",
                        data: formdat,  // 你的formid
                        // async : false,          //async 默认为true异步,修改为false为同步
                        contentType: false,   //jax 中 contentType 设置为 false 是为了避免 JQuery 对其操作,从而失去分界符,而使服务器不能正常解析文件
                        processData: false,   //当设置为true的时候,jquery ajax 提交的时候不会序列化 data,而是直接使用data
                        error: function (request) {
                            layer.close(index);         //关闭数据提交信息
                            layer.alert("网络超时");
                        },
                        success: function (data) {
                            if (data.code == 200) {
                                layer.msg(data.msg);
                                reloadTable(reloadId);
                            } else {
                                layer.msg(data.msg);
                            }
                        }
                    });

                })
                return false;
            }
            layer.msg('请选择需要重新发送的报名信息', {icon: 5});
            return false;
        },
        reload: function () {
            var form = pfgs_serialize($('form'));

            var a = compareTime(form.start_time, form.end_time);
            if (a === false) {
                layer.msg('开始时间不能大于结束时间');
                return;
            }
            table.reload('tablerReload', {
                where: {
                    equipment: form.equipment,
                    start_time: form.start_time,
                    end_time: form.end_time,
                    source_id: form.source,
                    state: form.state
                },
                page: {      //重载分页
                    curr: 1
                }
            })
        }
    };

    function compareTime(a, b) {
        var arr = a.split("-"); //log [2016,04,06]
        var start = new Date(arr[0], (arr[1] - 1), arr[2]);
        var starts = start.getTime(); //输出时间戳进行对比
        var arrs = b.split("-");
        var end = new Date(arrs[0], (arrs[1] - 1), arrs[2]);
        var ends = end.getTime();
        if (starts > ends) return false;
    }

    $('.demoTable .layui-btn').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });


});