var avaVm = avalon.define({
    $id: "real-manage",
    formData: {
        step:1,
        wximag:'',
        pay_money:0,
        transaction_id:'',
        agree:false
    },
    openpay:function(){
        var _this = this;
        _this.formData.step = 2;
    },
    isOpenReal:function () {
        var _this = this;
        if(!_this.formData.agree){
            layer.msg('请同意协议');
            return false;
        }
        _this.openReal();

    },
    openReal:function () {
        var _this = this;
        var index = layer.load(2, {shade: false});
        $.ajax({
            type: 'post',
            url: '/realHouse/openReal',
            data: {},
            dataType: 'json',
            success: function (re) {
                if(re.result != 1){
                    _this.waringOpen(re.msg);
                    return false;
                }
                var data = re.data;
                _this.formData.wximag = data.pay_url;
                _this.formData.step = 3;
                _this.formData.pay_money = data.money;
                _this.formData.transaction_id = data.transaction_id;
                //启动轮询
                _this.setInOrder();
                layer.closeAll();
            },
            complete:function () {
                layer.close(index);
            }
        });
    },
    waringOpen:function (msg) {
        layer.alert(msg, {
            title:'放心看',
            icon: 0,
            skin: 'layer-skin-yellow' //该皮肤由layer.seaning.com友情扩展。关于皮肤的扩展规则，去这里查阅
        })
    },
    checkOrder:function () {
        var _this = this
        $.post('/realHouse/realCheckPay',{transaction_id:_this.formData.transaction_id},function (re) {
            if(re.result == 1){
                layer.msg('放心看已开通,正在跳转');
                setTimeout(function () {
                    window.location.href = '/realHouse/marginAccount/';
                },1000);
            }
        },'json')
    },
    setInOrder:function () {
        var _this = this
        setInterval(function(){
            _this.checkOrder()
        }, 2000);
    },
    //提现
    remit:function () {
        var _this = this;

        var msg = '';
        if(real_count > 0){
            msg = '您的账户中仍有'+real_count+'条"放心房"在线展示，提现后"放心房"标签将被去除哦！';
        }else{
            msg = '申请提现后在保证金到账前，您将无法参与放心看计划，是否继续提现？';
        }
        layer.confirm(msg, {
            btn: ['继续提现','取消'] //按钮
        }, function(){
            var index = layer.load(2, {shade: false});
            $.ajax({
                type: 'POST',
                url: '/realHouse/realHouseRemit',
                data: {},
                dataType: 'JSON',
                success: function (re) {
                    if(re.result != 1){
                        _this.waringOpen(re.msg);
                        return false;
                    }
                    layer.alert('您的提现申请已提交成功，预计15工作日内将保证金原路退回，请您注意余额变动',function () {
                        window.location.href = '/realHouse/marginAccount/';
                    });
                },
                complete:function () {
                    layer.close(index);
                }
            });
        });


    },
    agreement:function(e){
        var _this = this
        var checked = e.target.checked
        _this.formData.agree = checked
    },

});
$(function(){
    layer.config({
        extend: 'myskin/style.css' //同样需要加载新皮肤
    });

});