var mark = '';
var token = "";
var check = false;
var hasOpened = false;

function login(){
    if(token != ""){
        if(check && !hasOpened){
            hasOpened = true;
            //httpOpen()
        }
        return
    }
    $('.mark').css('display','block');
}

function unlogin(){
    $('.mark').css('display','none');
     // mark.style.display = 'none';
}

var countdown=60; 
var timer ; 

function settime() {  
    var mobile = $('.input_box').val();
    if(mobile == ''){
        Toast('手机号不能为空',2000)
    }else{
        if ($('#sendbtn').html() == '获取验证码') {
            httpSendCode(mobile);
        }
        if ($('#sendbtn').html() == '重新获取验证码') {
            httpSendCode(mobile);
        }
    }
}
function startTimer(){
    if (countdown == 0) {  
        $('#sendbtn').html("重新获取验证码");  
        countdown = 60;  
        clearInterval(timer)
        return false;  
    } else {  
        $('#sendbtn').html("" + countdown + "s");  
        countdown--;  
    }  
            
}
function httpDoLogin(username,verification_code){
    $.ajax({
        type: "POST",
        url: "/api/user/public/vcode_login",
        data: 'username='+username+"&verification_code="+verification_code+'&invitation_code='+code,
        success: function (data) {
            if(data.code == 1){
                login();
                token = data.data.token;
                httpCheck();
                httpGetRedenvelopeInfo();
            }else{
                Toast(data.msg,2000);
            }
        }
    });
}

function httpGetRedenvelopeInfo(){
    $.ajax({
        headers: {
            'XX-Token': token,
            'XX-Device-Type':'web'
        },
        type: "POST",
        url: "/api/wallet/red_envelope/info",
        data: 'red_envelope_id='+id+"&red_envelope_key="+key,
        success: function (data) {
            if(data.code == 1){
                var user_list = data.data.child_red_envelope.list;
                for (i = 0; i < user_list.length; i++) { 
                    $('.open_log ul').append('<li><img src="' + user_list[i].avatar +'"><div class="hb-js"><div class="hb-js-top"><p class="num fr">'+ user_list[i].amount + ' ' + user_list[i].coin_symbol +'</p><p class="name fl">' + user_list[i].open_user +'</p></div><span class="time">' + user_list[i].open_time +'</span></div></li>');
                    $('.open_num').html(data.data.red_envelope_data.open_num);
                    $('.total_num').html(data.data.red_envelope_data.total_num);
                    $('.total_amount').html(prettyNum(data.data.red_envelope_data.total_amount));
                }
            }else{

            }
        }
    });
}

function prettyNum(num) {
    num = num - 0;
    var m = num.toExponential().match(/\d(?:\.(\d*))?e([+-]\d+)/);
    return num.toFixed(Math.max(0, (m[1] || '').length - m[2]));
}


function httpCheck(){
    $.ajax({
        headers: {
            'XX-Token': token,
            'XX-Device-Type':'web'
        },
        type: "POST",
        url: "/api/wallet/red_envelope/checkOpen",
        data: 'red_envelope_id='+id+"&red_envelope_key="+key,
        success: function (data) {
            if(data.code == 1){
                //红包可领
                check = true;
                httpOpen();
            }else{
                check = false;

                if(data.data.self_envelope_data.have == 1){
                    $('.my_num').html(data.data.self_envelope_data.amount);
                    show_already_open();
                    Toast("使用手机号和密码登陆Hello Token APP领取(新用户的账户初始密码会以短信形式发送)" );  
                }else{
                   show_qiangwan();
                }
            }
        }
    });
}

function httpOpen(){
    $.ajax({
        headers: {
            'XX-Token': token,
            'XX-Device-Type':'web'
        },
        type: "POST",
        url: "/api/wallet/red_envelope/open",
        data: 'red_envelope_id='+id+"&red_envelope_key="+key,
        success: function (data) {
            if(data.code == 1){
                //抢到
                $('.my_num').html(data.data.amount);
                show_already_open();
                Toast("使用手机号和密码登陆Hello Token APP领取(新用户的账户初始密码会以短信形式发送)" );    
            }else{
                //没抢到
                show_already_open();
                Toast(data.msg,2000);
            }
        }
    });
}

 function httpSendCode(username){
    $.ajax({
        type: "POST",
        url: "/api/user/verification_code/send",
        data: 'username='+username,
        success: function (data) {
            if(data.code == 1){
                Toast(data.msg,5000);
                timer = setInterval(function() {  
                    startTimer();
                },1000);
            }else{
                Toast(data.msg,2000);
            }
            
        }
    });
}

function Toast(msg,duration){
    duration=isNaN(duration)?3000:duration;
    var m = document.createElement('div');
    m.innerHTML = msg;
    m.style.cssText="width: 60%;min-width: 150px;opacity: 0.7;height: 30px;color: rgb(255, 255, 255);line-height: 30px;text-align: center;border-radius: 5px;position: fixed;top: 40%;left: 20%;z-index: 999999;background: rgb(0, 0, 0);font-size: 12px;";
    document.body.appendChild(m);
    setTimeout(function() {
        var d = 0.5;
        m.style.webkitTransition = '-webkit-transform ' + d + 's ease-in, opacity ' + d + 's ease-in';
        m.style.opacity = '0';
        setTimeout(function() { document.body.removeChild(m) }, d * 1000);
    }, duration);
}  
function loginSuccess(){
    var mobile = $('#mobile').val();
    var code = $('#code').val();
    httpDoLogin(mobile,code);
}

//没抢到
function show_qiangwan(){
    $('.wait_open,.qiangwan,.already_open,.open_log,.download_url').addClass('hidden');
    $('.qiangwan,.download_url').removeClass('hidden');
    $('.tantit3').removeClass('visible3');
}

//已经抢过
function show_already_open(){
    $('.wait_open,.qiangwan,.already_open,.open_log,.download_url').addClass('hidden');
    $('.already_open,.download_url,.open_log_btn').removeClass('hidden');
    $('.tantit3').removeClass('visible3');
}

//领取详情
function show_open_log(){
    $('.wait_open,.qiangwan,.already_open,.open_log,.download_url').addClass('hidden');
    $('.open_log,.open_log').removeClass('hidden');
    $('.tantit3').removeClass('visible3');
}


//打开窗口
$('.tancon3').on('click', function(event){
    event.preventDefault();
    $('.tantit3').addClass('visible3');
    //$(".dialog-addquxiao").hide()
});


// //关闭窗口
// $('.tantit3').on('click', function(event){
//     if( $(event.target).is('.tantit-close') || $(event.target).is('.tantit3') ) {
//         event.preventDefault();
//         $(this).removeClass('visible3');
//     }
// });

//关闭窗口
$('.tantit-close').on('click', function(event){
        $('.tantit3').removeClass('visible3');
});