Vue.config.devtools = true;
var v = new Vue({
    el:".index-box",
    data:function () {
        return {
            cata:[
                {name:"课程列表",active:1,id:1},
                {name:"详情",active:0,id:2}
            ],
            list:[],
            info:{},
            isLogin: 0,
            memToken: '',
            memHeadImg: '',
            userJf:-1,
            conferId:-1,
            playIndex:-1
        }
    },
    created:function (){
        if($.cookie("memToken")){
            this.memToken = $.cookie("memToken");
            this.isLogin=1;
        }
        if($.cookie("memHeadImg")){
            this.memHeadImg = $.cookie("memHeadImg");
        }
        this.conferId = $("#conferId").val();
        this.findUserIntegral();
        this.getIndexData();
    },
    methods:{
        findUserIntegral () {
            var that = this;
            $.ajax({
                type: 'POST',
                url: serverHttp + "/user/findUserIntegral",
                data: {
                    memToken: that.memToken
                },
                success: function (data) {
                    if (data.code == 200) {
                        that.userJf = data.data;
                    }
                },
                error: function (data) {
                }
            });
        },
        getIndexData () {
            //id：分类id id = -1 是全部分类
            var that = this;
            $.ajax({
                type : 'POST',
                url : serverHttp+"/meeting/getPeopleList",
                data : {
                    conferId:that.conferId,
                    memToken:that.memToken
                },
                success : function(data) {
                    if (data.code == 200) {
                        that.info = data.data;
                    }
                },
                error : function(data) {

                }
            });
        },
        gotoAdHtml (url){
            window.location.href = url;
        },
        addUser (id){
            var item = JSON.stringify(this.info.peopleAttributes);
            this.info.noPayPeoples = this.info.noPayPeoples.concat(JSON.parse(item));
        },
        deleteItem (i){
            console.log(i);
            this.info.noPayPeoples.splice(i,1);
        },
        saveUser (){
            //保存对象
            var that = this;
            that.pursePeopleBm();
        },
        save (){
            //保存对象
            var that = this;
            var noPayPeoples = that.info.noPayPeoples;
            $.ajax({
                type : 'POST',
                url : serverHttp+"/meeting/peopleBm",
                data : {
                    conferId:that.conferId,
                    memToken:that.memToken,
                    peopleList:JSON.stringify(noPayPeoples)
                },
                success : function(data) {
                    if (data.code == 200) {
                        console.log(data.data);
                        var orderId = data.data;
                        //去支付
                        if((that.info.info.price==0 && that.info.info.pointFlag==0)
                            || (that.info.info.price==0 && that.info.info.pointFlag==1 && that.info.info.point==0)){
                            that.addFreeBuyApi(3, that.info.info.id, noPayPeoples.length, orderId);
                        }else{
                            toPay(3, that.info.info.id, noPayPeoples.length, orderId);
                        }
                    }else{
                        console.log(data.message);
                    }
                },
                error : function(data) {

                }
            });
        },
        addFreeBuyApi(orderType, objId, objCount, orderId){
            if(!checkLogin()){
                goLogin();
                return false;
            }
            var that = this;
            $.ajax({
                type : 'POST',
                url : serverHttp+"/pay/addFreeBuyApi",
                data : {
                    memToken:that.memToken,
                    orderId: orderId,
                    orderType: orderType,
                    objId: objId,
                    objCount: objCount,
                    ispackage: 1,
                    paySource: 3,
                    fromMemCard: ''
                },
                success : function(data) {
                    if (data.code == 200) {
                        alert("报名成功");
                        window.location.href=serverHttp+"/meeting/details?from=web&conferId="+objId;
                    }else if(data.code == 10008){
                        //去登录
                        goLogin();
                    }else{
                        alert(data.data.message);
                    }
                },
                error : function(data) {
                }
            });
        },
        pursePeopleBm (){
            //保存对象
            var that = this;
            var noPayPeoples = that.info.noPayPeoples;
            if (!noPayPeoples || noPayPeoples.length == 0) {
                alert("请添加参会人")
                return ;
            }
            $.ajax({
                type : 'POST',
                url : serverHttp+"/meeting/pursePeopleBm",
                data : {
                    conferId:that.conferId,
                    memToken:that.memToken,
                    peopleList:JSON.stringify(noPayPeoples)
                },
                success : function(data) {
                    if (data.code == 200) {
                        if (data.data.code == 0) {
                            that.save();
                        } else {
                            alert(data.data.errorMsg);
                        }
                    }
                },
                error : function(data) {

                }
            });
        }
    }
})
