var pageNumber = 0,pageSize=10;

//初始化mui
mui.init({
    pullRefresh: {
        container: '#pullrefresh_sales_list',
        down: {
            //						callback: pulldownRefresh
        },
        up: {
            contentrefresh: '正在加载...',
            callback: selectSalesList
        }
    }
});

$(function(){
    //添加鼠标点击事件
    $(document).on('tap', '#tixian', function() {
        if($("#money").html() == 0){
            mui.alert("您的余额不能提现哦！");
        }else{
            tixian();
        }
    });

    $(document).on('tap', '#allUser', function() {
        goAllUser();
    });
})

/**
 * 跳转到终端用户页面
 */
function goAllUser() {
    var userType = $("#userType").val();
    if(userType == "1"){    //查询下线代理
        window.location.href = "/tooth/view/xiaxian_list.html"
    }else if(userType == "2"){  //查询终端总用户
        window.location.href = "/tooth/view/end_user_list.html"
    }
}

mui.ready(function() {
    mui.ajax(base_path + '/drp/select_drp_center?dates=' + new Date().getTime(), {
        dataType: 'json',
        type: 'post',
        timeout: 15000,
        success: function(dt) {
            if(dt.isRedirect) {
                location.href = dt.redirectURL;
            } else {
                if(dt.status) {
                    var data = dt.data;
                    switch (data.flag) {
                        case 1:
                            $('#div_userNum').html('分销员数量');
                            $('#div_activateNum').html('激活人数');
                            $("#userType").val(1);  //用于判断 - 查询分销员页面
                            break;
                        case 2:
                            $('#div_userNum').html('终端总用户');
                            $('#div_activateNum').html('未购买用户');
                            $("#userType").val(2);  //用于判断 - 查询用户页面
                            break;
                    }
                    $('#money').html(accDiv(data.money, 100));
                    $('#rebackMoney').html(accDiv(data.rebackMoney, 100));
                    $('#userNum').html(data.userNum);
                    $('#userSalesNum').html(data.userSalesNum);
                    $('#activateNum').html(data.activateNum);

                    //判断是否有二维码 2:没有二维码
                    if(data.isHasQrcode == 0){
                        $("#qrcodeid").hide();
                    }
                } else {
                    mui.alert(dt.message);

                }
            }
        },
        error: function(xhr, type, errorThrown) {
            //异常处理；
            console.log(xhr);
        }
    });
    mui('#pullrefresh_sales_list').pullRefresh().pullupLoading();
    document.querySelector('.loading-bg').style.display = 'none';
});

function selectSalesList() {
    ++pageNumber;
    mui.ajax(base_path + '/drp/select_sales_list?dates=' + new Date().getTime(), {
        data: {
            "pageNO":pageNumber,"pageSize":pageSize
        },
        dataType: 'json',
        type: 'post',
        timeout: 15000,
        success: function(dt) {
            if(dt.isRedirect) {
                location.href = dt.redirectURL;
            } else {
                if(dt.status) {
                    var data = [];
                    data = dt.data;
                    mui('#pullrefresh_sales_list').pullRefresh().endPullupToRefresh(data.rebackList.length < pageSize);
                    var rebackDay = data.rebackDay;
                    var data_reback = dt.data.rebackList;
                    var table = document.body.querySelector('.my-table-list');
                    mui(data_reback).each(function(index) {
                        var li = document.createElement('li');
                        li.className = 'mui-table-view-cell';
                        if(this.status == 2){
                            li.innerHTML =  formatDate(this.createDate) + '<span class="center">提成：¥' + accDiv(this.money, 100) + ' </span><span class="mui-pull-right">已入账</span>';
                        }else{
                            li.innerHTML =  formatDate(this.createDate) + '<span class="center">提成：¥' + accDiv(this.money, 100) + ' </span><span class="mui-pull-right">入账：' + dateDiff(this.createDate,rebackDay) + '天</span>';
                        }
                        table.appendChild(li);
                    });
                } else {
                    mui.alert(dt.message);
                }
            }
        },
        error: function(xhr, type, errorThrown) {
            //异常处理；
            console.log(xhr);
        }
    });
}

function tixian() {
    window.location.href = base_path + "/drp/withdrawalsList"
};

function getIncomeRecord() {
    window.location.href = base_path + "/drp/UnIncomeOrderList2"
};

function xialianlist() {
    if(wm2) {
        window.location.href = base_path + "/drp/xialianUser2"
    } else {
        window.location.href = base_path + "/drp/xialianUser"
    }
};

function weiruzhang() {
    if(wm2) {
        window.location.href = base_path + "/drp/UnIncomeOrderList"
    } else {
        window.location.href = base_path + "/drp/UnIncomeOrderList2"
    }
};

function fenxiao_wm2() {
    window.location.href = "/tooth/wechat/drp/share2WM"
};

function salesNumbers() {
    window.location.href = base_path + "/drp/salesNumbers"
};

//计算天数差的函数，通用
function  dateDiff(startDate,rebackDay){
    var endTime = new Date().getTime();
    var dates = Math.floor((endTime - startDate)/(1000*60*60*24));
    var day = rebackDay - dates;
    return day;
}