var ExcelData;

var excel;
layui.config({
    base: '/layuiadmin/' //静态资源所在路径
}).extend({
    myexcel: 'layui/lay/modules/excel',
    index: 'lib/index' //主入口模块
}).use(['index', 'table', 'excel', 'laydate'], function () {
    var $ = layui.$
        , form = layui.form
        , table = layui.table;

    //- 筛选条件-日期
    var laydate = layui.laydate;
    var layCreateDate = laydate.render({
        elem: '#CreateDate',
        type: 'date',
        range: true,
        trigger: 'click',
        change: function (value, date, endDate) {
            var op = true;
            if (date.year == endDate.year && endDate.month - date.month <= 2) {
                if (endDate.month - date.month == 2 && endDate.date > date.date) {
                    op = false;
                    layCreateDate.hint('日期范围请不要超过2个月');
                    setTimeout(function () {
                        $(".laydate-btns-confirm").addClass("laydate-disabled");
                    }, 1);
                }
            } else {
                op = false;
                layCreateDate.hint('日期范围请不要超过2个月');
                setTimeout(function () {
                    $(".laydate-btns-confirm").addClass("laydate-disabled");
                }, 1);
            }
            if (op) {
                $('#CreateDate').val(value);
            }
        }
    });


    //excel导入
    excel = layui.excel;
    $('#ExcelFile').change(function (e) {
        var files = e.target.files;
        excel.importExcel(files, {}, function (data) {
            ExcelData = data[0].sheet1;
        });
    });


    //列表数据
    table.render({
        elem: '#LAY-list-manage'
        , url: '/Admin/Users/BatchSeeUserInfoDo' //模拟接口
        , cols: [[
            { type: 'checkbox', fixed: 'left' }
            , { field: 'BrandName', width: 150, title: '机具类型', sort: true }
            , { field: 'BindingTime', width: 150, title: '绑定时间', sort: true }
            , { field: 'ActivationTime', width: 150, title: '激活时间', sort: true }
            , { field: 'MerchantName', width: 150, title: '商户姓名', sort: true }
            , { field: 'PosSn', width: 150, title: '机具Sn', sort: true }
            , { field: 'MerNo', width: 150, title: '商户编号', sort: true }
            , { field: 'userInfo', width: 150, title: '创客信息', sort: true }
            // , { field: 'MakerCode', width: 105, title: '创客编号', sort: true }
            // , { field: 'RealName', width: 105, title: '创客姓名', sort: true }
            , { field: 'Mobile', width: 150, title: '创客手机号', sort: true }
            , { field: 'Balance', width: 150, title: '创客账户余额', sort: true }
            , { field: 'PosFee', width: 150, title: '服务费', sort: true }
            , { field: 'TopMakerCode', width: 160, title: '顶级创客编号', sort: true }
            , { field: 'TopRealName', width: 160, title: '顶级创客名称', sort: true }
            , { field: 'SecondMakerCode', width: 160, title: '次顶级创客编号', sort: true }
            , { field: 'SecondRealName', width: 160, title: '次顶级创客姓名', sort: true }
            , { field: 'ThirdMakerCode', width: 160, title: '次次顶级创客编号', sort: true }
            , { field: 'ThirdRealName', width: 160, title: '次次顶级创客姓名', sort: true }
        ]]
        , where: {
        }
        , page: true
        , limit: 30
        , height: 'full-' + String($('.layui-card-header').height() + 130)
        , text: '对不起，加载出现异常！'
        , done: function (res, curr, count) {
            $(".layui-none").text("无数据");
        }
    });

    //监听工具条
    table.on('tool(LAY-list-manage)', function (obj) {
        var data = obj.data;

    });


    //监听搜索
    form.on('submit(LAY-list-front-search)', function (data) {
        var field = data.field;

        //执行重载
        table.reload('LAY-list-manage', {
            where: field
        });
    });
    form.on('submit(LAY-list-front-searchall)', function (data) {
        table.reload('LAY-list-manage', {
            where: {
                UserId: UserId
            }
        });
    });

    //事件
    var active = {
        batchdel: function () {
            var checkStatus = table.checkStatus('LAY-list-manage')
                , data = checkStatus.data; //得到选中的数据
            if (data.length < 1) {
                parent.layer.msg("请选择要删除的项");
            } else {
                var ids = "";
                $.each(data, function (index, value) {
                    ids += data[index].Id + ",";
                });
                ids = ids.substring(0, ids.length - 1);
                var index = layer.confirm('确定要删除吗？删除后不能恢复！', function (index) {
                    $.ajax({
                        type: "POST",
                        // url: "/Admin/LeaderReserveRecord/Delete?r=" + Math.random(1),
                        data: "Id=" + ids,
                        dataType: "text",
                        success: function (data) {
                            layer.close(index);
                            if (data == "success") {
                                table.reload('LAY-list-manage');
                            } else {
                                layer.msg(data);
                            }
                        }
                    });
                });
            }
        }
        , add: function () {
            var perContent = layer.open({
                type: 2
                , title: '盟主管理-添加'
                , content: 'Add'
                , maxmin: true
                , area: ['500px', '450px']
                , btn: ['确定', '取消']
                , yes: function (index, layero) {
                    var iframeWindow = window['layui-layer-iframe' + index]
                        , submitID = 'LAY-list-front-submit'
                        , submit = layero.find('iframe').contents().find('#' + submitID);

                    setTimeout(function () {
                        layero.find('iframe').contents().find('.layui-tab-item').each(function (i) {
                            var errObj = $(this).find('.layui-form-danger');
                            if (errObj.length > 0) {
                                iframeWindow.element.tabChange('mytabbar', String(i + 1));
                                submit.click();
                            }
                        });
                    }, 300);
                    //监听提交
                    iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                        var field = data.field; //获取提交的字段
                        var userdata = "";
                        for (var prop in field) {
                            userdata += prop + "=" + encodeURIComponent(field[prop]) + "&";
                        }
                        //提交 Ajax 成功后，静态更新表格中的数据
                        //$.ajax({});

                        $.ajax({
                            type: "POST",
                            // url: "/Admin/LeaderReserveRecord/Add?r=" + Math.random(1),
                            data: userdata,
                            dataType: "text",
                            success: function (data) {
                                layer.close(index); //关闭弹层
                                if (data == "success") {
                                    table.reload('LAY-list-manage'); //数据刷新
                                } else {
                                    layer.msg(data);
                                }
                            }
                        });
                    });

                    submit.trigger('click');
                }
            });
            layer.full(perContent);
        }
        , ImportData: function () {
            ExcelKind = 1;
            layer.open({
                type: 1,
                title: '导入',
                maxmin: false,
                area: ['460px', '280px'],
                content: $('#excelForm'),
                cancel: function () {
                }
            });
            $("#excelTemp").html('<a href="/excelfile/模板文件.xlsx">点击下载模板文件</a>');
        }
        , ExportExcel: function () {
            var userdata = '';
            $(".layuiadmin-card-header-auto input").each(function (i) {
                userdata += $(this).attr('name') + '=' + encodeURIComponent($(this).val()) + '&';
            });
            $(".layuiadmin-card-header-auto select").each(function (i) {
                userdata += $(this).attr('name') + '=' + encodeURIComponent($(this).val()) + '&';
            });
            var index = layer.confirm('确定导出吗？', function (index) {
                var indexs = layer.load(1, {
                    shade: [0.5, '#000']
                });
                $.ajax({
                    type: "Post",
                    url: "/Admin/Users/ExportBatchSeeUserInfoExcelDo?r=" + Math.random(1),
                    data: userdata,
                    dataType: "text",
                    success: function (data) {
                        layer.close(index); //关闭弹层
                        if (data == "success") {
                            layer.close(indexs); //关闭弹层
                            layer.msg('导出成功，请到我的下载中点击下载文件', {
                                time: 1000
                            }, function () {
                                window.location.reload();
                            });
                        } else {
                            layer.close(indexs); //关闭弹层
                            layer.msg(data);
                        }
                    }
                });
            });
        }
    };

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