$(function(){
    var $inp = $(".user");
    $inp.change(function(){
        var v = $(this).val();
        if(!v){
            $(this).val("1");
        }
        excprice($(this));
    }).on("keyup", function(){
        var v = $(this).val();
        if(!v){
            $(this).val("1");
        }
        excprice($(this));
    });

    $(".adj>span.up").on("click", function(){
        var that = $(this).parent().next();
        var num = parseInt(that.val());
        that.val(num + 1);
        excprice(that);
        layer.closeAll();
    });

    $(".adj>span.down").on("click", function(){
        var that = $(this).parent().next();
        var num = parseInt(that.val());
        if(num > 1){
            that.val(num - 1);
            excprice(that);
        } else {
            layer.msg("嘿！我是有底线的。")
        }
    });


});

function excprice($inp){
    var num = parseInt($inp.val());
    var flag = $inp.data("flag");
    var p = flag == "vvip" ? 699 : 69;
    price = p * num;
    $("." + flag + " .price").text(price);
}

function purchase(level){
    var price = $("." + level + " .price").text();
    var user = $("." + level + " .user").val();
    parent.SendSoftMessage("purchase", (level == 'vip'? 2 : level == 'vvip' ? 3 : 4) + "," + user + "," + price);
}

parent.window["__recharge"] = function(data){
    data = JSON.parse(data);
    if(data.error){
        layer.msg("数据错误");
    } else if(data.ing){
        var a = layer.confirm('在打开的支付页面中完成支付！', {
            btn: ['已完成支付', '支付遇到了问题？']
        }, function() {
            parent.SendSoftMessage("recharge_ok", "");
            layer.close(a);
            parent.closeRecharge();
        }, function() {
            parent.SendSoftMessage("recharge_error", "");
            layer.close(a);
            parent.closeRecharge();
        });
    } else {
        var msg = '购买服务：' + (data.vip == 2 ? '月度VIP' : data.vip == 3  ? '年度VIP' : '永久VIP');
        msg = msg + '<br>购买时长：' + data.num + (data.vip == 2 ? '月' : data.vip == 3  ? '年' : '永久');
        msg = msg + '<br>订单金额：￥ ' + data.price + " 元";
        msg = msg + '<br>服务时长：' + data.num * (data.vip == 2 ? 30 : data.vip == 3 ? 365 : 999999999) + '天';
        var a = layer.confirm(msg + '<br><br>您确定要购买此服务吗？', {
            btn: ['确定购买', '不了']
        }, function() {
            parent.SendSoftMessage("purchase_sure", "chromeplus");
            layer.close(a);
        }, function() {
            layer.msg('已取消', {
                time: 2000
            });
        });
    }
}