var ExcelData;
function ConfirmImport() {
    $.ajax({
        type: "POST",
        url: "/Admin/UserAccountRecord/Import?r=" + Math.random(1),
        data: "ExcelData=" + encodeURIComponent(JSON.stringify(ExcelData)),
        dataType: "text",
        success: function (data) {
            if (data == "success") {
                layer.msg("导入成功", { time: 2000 }, function () {
                    window.location.reload();
                });
            } else {
                layer.msg(data);
            }
        }
    });
}

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.on('edit(LAY-list-manage)', function (obj) {
        var value = obj.value //得到修改后的值
            , data = obj.data //得到所在行所有键值
            , field = obj.field; //得到字段
        if (field == "Sort") {
            $.ajax({
                type: "POST",
                url: "/Admin/UserAccountRecord/Sort?r=" + Math.random(1),
                data: "Id=" + data.Id + "&Sort=" + value,
                dataType: "text",
                success: function (data) {
                }
            });
        }
    });

    //列表数据
    table.render({
        elem: '#LAY-list-manage'
        , url: '/Admin/UserAccountRecord/IndexData' //模拟接口
        , cols: [[
            { type: 'checkbox', fixed: 'left' }
            , { field: 'MakerCode', width: 105, title: '创客编号', sort: true }
            , { field: 'RealName', width: 105, title: '创客姓名', sort: true }
            // ,{field:'TopMakerCode', width: 200, title:'顶级创客编号', sort: true}
            // ,{field:'TopRealName', width: 200, title:'顶级创客名称', sort: true}
            , { field: 'ProductTypeName', width: 105, title: '产品类型', sort: true }
            // ,{field:'TransRecordNo', width: 200, title:'交易流水编号', sort: true}
            , { field: 'ChangeTypeName', width: 110, title: '交易类型', sort: true }
            , { field: 'ChangeAmount', width: 110, title: '交易金额', sort: true }
            , { field: 'Remark', width: 100, title: '备注', sort: true }
            , { field: 'BeforeTotalAmount', width: 130, title: '交易前总金额', sort: true }
            , { field: 'AfterTotalAmount', width: 130, title: '交易后总金额', sort: true }
            , { field: 'BeforeFreezeAmount', width: 150, title: '交易前冻结金额', sort: true }
            , { field: 'AfterFreezeAmount', width: 150, title: '交易后冻结金额', sort: true }
            , { field: 'BeforeBalanceAmount', width: 120, title: '交易前余额', sort: true }
            , { field: 'AfterBalanceAmount', width: 120, title: '交易后余额', sort: true }
            , { field: 'CreateDate', width: 110, title: '交易时间', sort: true }
            , { field: 'PosSn', width: 110, title: '机具Sn', sort: true }
        ]]
        , where: {
            UserId: UserId
        }
        , page: true
        , limit: 30
        , height: 'full-' + String($('.layui-card-header').height() + 130)
        , text: '对不起，加载出现异常！'
        , done: function (res, curr, count) {
            // $("#ChangeTypeAmount0").text(res.other.ChangeTypeAmount0);
            // $("#ChangeTypeAmount1").text(res.other.ChangeTypeAmount1);
            // $("#ChangeTypeAmount2").text(res.other.ChangeTypeAmount2);
            // $("#ChangeTypeAmount3").text(res.other.ChangeTypeAmount3);
            // $("#ChangeTypeAmount4").text(res.other.ChangeTypeAmount4);
            // $("#ChangeTypeAmount5").text(res.other.ChangeTypeAmount5);
            // $("#ChangeTypeAmount6").text(res.other.ChangeTypeAmount6);
            // $("#ChangeTypeAmount61").text(res.other.ChangeTypeAmount61);
            // $("#ChangeTypeAmount62").text(res.other.ChangeTypeAmount62);
            // $("#ChangeTypeAmount63").text(res.other.ChangeTypeAmount63);
            // $("#ChangeTypeAmount64").text(res.other.ChangeTypeAmount64);
            // $("#ChangeTypeAmount50").text(res.other.ChangeTypeAmount50);
            // $("#ChangeTypeAmount111").text(res.other.ChangeTypeAmount111);
            // $("#ChangeTypeAmount112").text(res.other.ChangeTypeAmount112);
            // $("#ChangeTypeAmount60").text(res.other.ChangeTypeAmount60);
            // $("#ChangeTypeAmount31").text(res.other.ChangeTypeAmount31);
            // $("#ChangeTypeAmount124").text(res.other.ChangeTypeAmount124);
            // $("#ChangeTypeAmount125").text(res.other.ChangeTypeAmount125);
            // $("#ChangeTypeAmount31s").text(res.other.ChangeTypeAmount31s);

            $("#ChangeTypeAmount0").text(res.other.ChangeTypeAmount0); //激活奖励
            $("#ChangeTypeAmount1").text(res.other.ChangeTypeAmount1); //交易分润
            $("#ChangeTypeAmount2").text(res.other.ChangeTypeAmount2); //提现申请
            $("#ChangeTypeAmount3").text(res.other.ChangeTypeAmount3); //提现代付
            $("#ChangeTypeAmount4").text(res.other.ChangeTypeAmount4); //提现手续费
            $("#ChangeTypeAmount5").text(res.other.ChangeTypeAmount5); //提现税点扣除
            $("#ChangeTypeAmount6").text(res.other.ChangeTypeAmount6); //提现失败，解冻
            $("#ChangeTypeAmount12").text(res.other.ChangeTypeAmount12); //交易奖励
            $("#ChangeTypeAmount20").text(res.other.ChangeTypeAmount20); //余额支付
            $("#ChangeTypeAmount31").text(res.other.ChangeTypeAmount31); //活动奖励
            $("#ChangeTypeAmount50").text(res.other.ChangeTypeAmount50); //开机奖励
            $("#ChangeTypeAmount60").text(res.other.ChangeTypeAmount60); //流量卡分佣
            $("#ChangeTypeAmount61").text(res.other.ChangeTypeAmount61); //人工冻结
            $("#ChangeTypeAmount62").text(res.other.ChangeTypeAmount62); //人工解冻
            $("#ChangeTypeAmount63").text(res.other.ChangeTypeAmount63); //系统扣减(人工扣减)
            $("#ChangeTypeAmount64").text(res.other.ChangeTypeAmount64); //系统奖励(人工增加)
            $("#ChangeTypeAmount111").text(res.other.ChangeTypeAmount111); //分润补贴
            $("#ChangeTypeAmount112").text(res.other.ChangeTypeAmount112); //推荐奖励
            $("#ChangeTypeAmount114").text(res.other.ChangeTypeAmount114); //私董会分红
            $("#ChangeTypeAmount115").text(res.other.ChangeTypeAmount115); //分仓奖励
            $("#ChangeTypeAmount116").text(res.other.ChangeTypeAmount116); //大盟主奖励
            $("#ChangeTypeAmount119").text(res.other.ChangeTypeAmount119); //分仓临时额度退还
            $("#ChangeTypeAmount121").text(res.other.ChangeTypeAmount121); //运营中心奖励
            $("#ChangeTypeAmount122").text(res.other.ChangeTypeAmount122); //推荐大盟主奖励
            $("#ChangeTypeAmount123").text(res.other.ChangeTypeAmount123); //推荐小盟主奖励
            $("#ChangeTypeAmount118And120").text(res.other.ChangeTypeAmount118And120); //购机奖励(118盟主奖励(购机奖励) 120运营中心(购机奖励))
            $("#ChangeTypeAmount124").text(res.other.ChangeTypeAmount124); //扣机具货款
            $("#ChangeTypeAmount125").text(res.other.ChangeTypeAmount125); //软件服务费
            $("#ChangeTypeAmount126").text(res.other.ChangeTypeAmount126); //机具货款退还
            $("#ChangeTypeAmount127").text(res.other.ChangeTypeAmount127); //运营中心分仓奖励
            $("#ChangeTypeAmount202").text(res.other.ChangeTypeAmount202); //创客预扣款
            $("#ChangeTypeAmount30").text(res.other.ChangeTypeAmount30); //红包奖励
            $(".layui-none").text("无数据");
        }
    });

    //监听工具条
    table.on('tool(LAY-list-manage)', function (obj) {
        var data = obj.data;
        if (obj.event === 'del') {
            var index = layer.confirm('确定要删除吗？删除后不能恢复！', function (index) {
                $.ajax({
                    type: "POST",
                    url: "/Admin/UserAccountRecord/Delete?r=" + Math.random(1),
                    data: "Id=" + data.Id,
                    dataType: "text",
                    success: function (data) {
                        if (data == "success") {
                            obj.del();
                            layer.close(index);
                        } else {
                            parent.layer.msg(data);
                        }
                    }
                });
            });
        } else if (obj.event === 'edit') {
            var tr = $(obj.tr);
            var perContent = layer.open({
                type: 2
                , title: '创客账户变动记录-编辑'
                , content: 'Edit?Id=' + data.Id + ''
                , 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/UserAccountRecord/Edit?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');
                }
                , success: function (layero, index) {

                }
            });
            layer.full(perContent);
        }
    });


    //监听搜索
    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/UserAccountRecord/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/UserAccountRecord/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 () {
            layer.open({
                type: 1,
                title: '导入',
                maxmin: false,
                area: ['460px', '180px'],
                content: $('#excelForm'),
                cancel: function () {
                }
            });
        }
        , 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()) + '&';
            });
            // $.ajax({
            //     type: "GET",
            //     url: "/Admin/UserAccountRecord/ExportExcel?r=" + Math.random(1),
            //     data: userdata,
            //     dataType: "json",
            //     success: function (data) {
            //         data.Obj.unshift(data.Fields);
            //         excel.exportExcel(data.Obj, data.Info, 'xlsx');
            //     }
            // });
            var index = layer.confirm('确定导出吗？', function (index) {
                var indexs = layer.load(1, {
                    shade: [0.5, '#000']
                });
                $.ajax({
                    type: "POST",
                    url: "/Admin/UserAccountRecord/QuickExportExcelDo?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);
                        }
                    }
                });
            });
        }
        , Open: 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/UserAccountRecord/Open?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);
                            }
                        }
                    });
                });
            }
        }
        , Close: 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/UserAccountRecord/Close?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);
                            }
                        }
                    });
                });
            }
        }
    };

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