layui.use(['laytpl', 'table', 'jquery', 'form', 'miniTab', 'xmSelect',"laydate"], function () {
    let $ = layui.jquery,
        form = layui.form,
        table = layui.table,
        layer = layui.layer,
        xmSelect = layui.xmSelect,
        miniTab = layui.miniTab,
        laydate  = layui.laydate ;

    /**** index begin ***/
    //index页面
    if ($('.location-index-page').length > 0) {
        miniTab.listen();
        var exportDataTitle=
            [
                "消费者昵称","核销编码","扣款签到券名称",
                "所属商家名称","商家所属平台商",
                "扣款总额","当前余额",
                "消费者红包","平台商佣金",
                "太古佣金","扣款时间",
                "经纬度"
            ];
        var  exportData =[];
        // 渲染表格
        let listUrl = $('#table-container').data('url');
        let insTb = table.render({
            elem: '#table-container',
            toolbar: '#toolbar-tpl',
            defaultToolbar: [{ //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '搜索'
                , layEvent: 'search'
                , icon: 'layui-icon-search'
            },'filter'],
            url: listUrl,
            method: 'post',
            even: true,
            limits: [10,20,50,100,200,500,1000],
            request: {
                pageName: 'page',
                limitName: 'size',
            },
            parseData: function (res) {
                //console.log(res)
                $("#deduction_money_sum").text(res.data.deduction_money_sum?res.data.deduction_money_sum:0)
                $("#deduction_money_sum_red_pack").text(res.data.deduction_money_sum_red_pack?res.data.deduction_money_sum_red_pack:0)
                $("#deduction_money_sum_agency").text(res.data.deduction_money_sum_agency?res.data.deduction_money_sum_agency:0)
                $("#deduction_money_sum_taigu").text(res.data.deduction_money_sum_taigu?res.data.deduction_money_sum_taigu:0)

                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.list //解析数据列表
                };
            },
            page: true,
            cols: [[
                {type: 'checkbox'},
                { field:"nick_name",title: '消费者昵称'},
                { field:"user_coupon_id",title: '核销编码'},
                {templet: "#row-avatar_url",field:"avatar_url",title: '消费者头像'},
                {field:"coupon_main_name",title: '扣款签到券名称'},

                {field:"business_name",title: '所属商家名称'},
                {field:"agency_business_name",title: '商家所属平台商'},
                {field: "money",   title: '扣款总额'},
                {field: "balance",   title: '当前余额'},

                {field: "consumer_money",   title: '消费者红包'},
                {field: "agency_money",   title: '平台商佣金'},
                {field: "admin_money",   title: '太古佣金'},
                {field: "create_time",   title: '扣款时间'},
                {templet: "#row-position",   title: '经纬度'},
                // {templet: '#row-operate', minWidth: 200, fixed: 'right', align: 'center', title: '操作'}
            ]],
            done: function (res, curr, count) {
                console.log( res.data);
                Tools.setInsTb(insTb);
                var rdata=[];
                res.data.forEach(function (d,index) {
                    rdata[index] = [
                        d.nick_name ,
                        d.user_coupon_id ,
                        d.coupon_main_name ,
                        d.business_name ,
                        d.agency_business_name ,
                        d.money,
                        d.balance,
                        d.consumer_money,
                        d.agency_money,
                        d.admin_money,
                        d.create_time,
                        d.lng + "," + d.lat

                         ]
                })
                exportData=rdata;

                $("#exports").on("click",function(){
                    console.log(exportData)
                    table.exportFile(exportDataTitle,exportData, 'xls');
                    return false;
                })
            }
        });





        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            //执行搜索重载
            table.reload('table-container', {
                page: {curr: 1}
                , where: data.field
            }, 'data');

            return false;
        });

        //日期时间选择器
        laydate.render({
            elem: '#start_time'
            ,type: 'datetime'
        });

        //日期时间选择器
        laydate.render({
            elem: '#end_time'
            ,type: 'datetime'
        });




    }
    /*** index end ***/

});