Vue.config.devtools = true;
var v = new Vue({
    el: ".container",
    data: function () {
        return {
            isVip: localStorage.getItem('isVip') == 1,
            cata: [
                {name: "课程列表", active: 1, id: 1},
                {name: "详情", active: 0, id: 2}
            ],
            list: [],
            info: {},
            isLogin: 0,
            memToken: '',
            memHeadImg: '',
            userJf: -1,
            conferId: -1,
            playIndex: -1,
            stop: 1,
            integralType: "",
            integralCount: ""
        }
    },
    created: function () {
        if (!checkLogin()) {
            // goLogin();
            return false;
        }
        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();
    },
    mounted: function () {
        setTimeout(() => {
            $(".loading-item").fadeOut(200);
        }, 500)
    },
    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;
            var phone = new Array();
            for (var item of noPayPeoples) {
                for (var key in item) {
                    if (key == "id") {
                        continue;
                    }
                    if (key != 'questionAnswers') {
                        console.log(item[key]);
                        if (that.isNull(item[key])) {
                            alert("输入信息有误，请检查后重新输入")
                            that.stop = 1;
                            return false;
                        }
                        ;
                    }
                    if (key == 'mobile') {
                        console.log(item['mobile']);
                        var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
                        if (!myreg.test(item['mobile'])) {
                            alert('手机号非法');
                            that.stop = 1;
                            return false;
                        }
                        phone.push(item['mobile']);
                    }
                    if (key == 'questionAnswers') {
                        console.log(item['questionAnswers']);
                        if (item['questionAnswers'].length > 0) {
                            for (var k of item['questionAnswers']) {
                                console.log(k);
                                for (var j in k) {
                                    if (j == 'answerContent') {
                                        console.log(k['answerContent']);
                                        if (that.isNull(k['answerContent'])) {
                                            alert("输入信息有误，请检查后重新输入");
                                            that.stop = 1;
                                            return false;
                                        }
                                        ;
                                    }

                                }
                            }
                        }
                    }
                }
            }
            var s = phone.join(",") + ",";
            for (var i = 0; i < phone.length; i++) {
                if (s.replace(phone[i] + ",", "").indexOf(phone[i] + ",") > -1) {
                    alert("该手机号重复" + phone[i]);
                    that.stop = 1;
                    return false;
                }
            }
            $.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) {
                        that.stop = 1;
                        console.log(data.data);
                        var orderId = data.data;
                        if (that.isVip) { // svip权限
                            that.addFreeBuyApi(3, that.info.info.id, noPayPeoples.length, orderId);
                        } else {
                            //去支付
                            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 if (data.code == 10008) {
                        //去登录
                        goLogin();
                    } else {
                        console.log(data.message);
                    }
                },
                error: function (data) {

                }
            });
        },
        isNull(str) {
            if (str == "") return true;
            if (str == null) return true;
            var regu = "^[ ]+$";
            var re = new RegExp(regu);
            return re.test(str);
        },
        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("报名成功");
                        that.integralType = "报名会议";
                        that.integralCount = 50;
                        $(".integral-toast").show();
                        playMusic()
                        setTimeout(function () {
                            $(".integral-toast").fadeOut(200);
                            window.location.href = serverHttp + "/meeting/details?from=H5&conferId=" + objId;
                        }, 2000);

                    } else if (data.code == 10008) {
                        //去登录
                        goLogin();
                    } else {
                        alert(data.message);
                    }
                },
                error: function (data) {
                }
            });
        },
        pursePeopleBm() {
            //保存对象
            var that = this;
            if (that.stop == 0) {
                return;
            }
            that.stop = 0;
            var noPayPeoples = that.info.noPayPeoples;
            if (!noPayPeoples || noPayPeoples.length == 0) {
                alert("请添加参会人");
                that.stop = 1;
                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) {

                }
            });
        }
    }
})
