var ExcelData, ExcelKind;
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 layActivationTime = laydate.render({
        elem: '#ActivationTime',
        type: 'date',
        range: true,
        trigger: 'click',
        change: function (value, date, endDate) {
            var op = true;
            if (date.year == endDate.year && endDate.month - date.month <= 1) {
                if (endDate.month - date.month == 1 && endDate.date > date.date) {
                    op = false;
                    layActivationTime.hint('日期范围请不要超过1个月');
                    setTimeout(function () {
                        $(".laydate-btns-confirm").addClass("laydate-disabled");
                    }, 1);
                }
            } else {
                op = false;
                layActivationTime.hint('日期范围请不要超过1个月');
                setTimeout(function () {
                    $(".laydate-btns-confirm").addClass("laydate-disabled");
                }, 1);
            }
            if (op) {
                $('#ActivationTime').val(value);
            }
        }
    });

    var layBindingTime = laydate.render({
        elem: '#BindingTime',
        type: 'date',
        range: true,
        trigger: 'click',
        change: function (value, date, endDate) {
            var op = true;
            if (date.year == endDate.year && endDate.month - date.month <= 1) {
                if (endDate.month - date.month == 1 && endDate.date > date.date) {
                    op = false;
                    layBindingTime.hint('日期范围请不要超过1个月');
                    setTimeout(function () {
                        $(".laydate-btns-confirm").addClass("laydate-disabled");
                    }, 1);
                }
            } else {
                op = false;
                layBindingTime.hint('日期范围请不要超过1个月');
                setTimeout(function () {
                    $(".laydate-btns-confirm").addClass("laydate-disabled");
                }, 1);
            }
            if (op) {
                $('#BindingTime').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/StoreHouse/Sort?r=" + Math.random(1),
                data: "Id=" + data.Id + "&Sort=" + value,
                dataType: "text",
                success: function (data) { }
            });
        }
    });

    //列表数据
    table.render({
        elem: '#LAY-list-manage'
        , url: '/Admin/SysTools/Team1InfoDo' //模拟接口
        , cols: [[
            { type: 'checkbox', fixed: 'left' }
            , { field: 'wname', width: 200, title: '伙伴名称', sort: true }
            , { field: 'Name', width: 200, title: '机具品牌', sort: true }
            , { field: 'PosSn', width: 200, title: '机具Sn', sort: true }
            , { field: 'merchantname', width: 200, title: '商户姓名', sort: true }
            , { field: 'SeoKeyword', width: 200, title: '服务费', sort: true }
            , { field: 'BindingTime', width: 200, title: '绑定时间', sort: true }
            , { field: 'ActivationTime', width: 200, title: '激活时间', sort: true }
            , { field: 'amount', width: 200, title: '返现金额', sort: true }
            , { field: 'maxdate', width: 200, title: '返现时间', sort: true }
            , { field: 'CreditRewardAmount', width: 200, title: '开机奖', sort: true }
            , { field: 'opdate', width: 200, title: '开机奖发放时间', sort: true }
            , { field: 'info', width: 200, 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("无数据");
        }
    });

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

    //监听工具条
    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,
            page: {
                curr: 1
            }
        });
    });
    form.on('submit(LAY-list-front-searchall)', function (data) {
        table.reload('LAY-list-manage', {
            where: null,
            page: {
                curr: 1
            }
        });
    });

    //事件
    var active = {
        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/SysTools/ExportTeam1InfoExcelDo?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) : '';
    });
});