// jssdk配置
var app = new Vue({
    el: '#main',
    data: {
        shopLicense: [],
        foodLicense: [],
        logoLicense: [],
        shop: [],
        food: [],
        logo: [],
        addressList: [],
        provinceArr: [],
        cityArr: [],
        areaArr: [],
        communityArr: [],
        province: 0,
        city: 0,
        area: 0,
        community: 0,
        userName: "",
        phone: "",
        address: "",
        storeName: "",
        openid:null,
        confirmTips:"确认上传",
        update:true
    },
    methods: {
        // 添加营业执照
        addShopLicense: function(e) {
            e.preventDefault();
            var that = this
            wx.chooseImage({
                count: 1, 
                sizeType: ['original', 'compressed'], 
                sourceType: ['album', 'camera'], 
                success: function (res) {
                    var localIds = res.localIds; 
                    that.$data.shopLicense.push(localIds[0])
                }
            });
        },
        delShopLicense: function(index) {
            this.shopLicense.splice(index, 1);
        },
        // 添加食品流通证
        addFoodLicense: function(e) {
            e.preventDefault();
            var that = this
            wx.chooseImage({
                count: 1, 
                sizeType: ['original', 'compressed'], 
                sourceType: ['album', 'camera'], 
                success: function (res) {
                    var localIds = res.localIds; 
                    that.$data.foodLicense.push(localIds[0])
                }
            });
        },
        delFoodLicense: function(index) {
            this.foodLicense.splice(index, 1);
        },
        // 添加LOGO
        addLogoLicense: function(e) {
            e.preventDefault();
            var that = this
            wx.chooseImage({
                count: 1, 
                sizeType: ['original', 'compressed'], 
                sourceType: ['album', 'camera'], 
                success: function (res) {
                    var localIds = res.localIds; 
                    that.$data.logoLicense.push(localIds[0])
                }
            });
        },
        
        delLogoLicense: function(index) {
            this.logoLicense.splice(index, 1);
        },
        checkPhone: function(phone){
            var reg = /^1[34578]\d{9}$/;
            if(!(reg.test(phone))){ 
                alert("请输入正确的手机号"); 
                return false;
            }else{
                return true;
            }
        },
        // 上传
        uploadImage: function() {
            var that = this;
            if (this.checkPhone(this.phone)) {
                // 上传食品流通证
                wx.uploadImage({
                    localId: this.foodLicense[0], 
                    isShowProgressTips: 1, 
                    success: function (res) {
                        var foodId = res.serverId;
                        that.$data.food.push(foodId);
                         // 回调上传营业执照
                        while(1){
                            if (foodId) {
                                wx.uploadImage({
                                    localId: that.$data.shopLicense[0], 
                                    isShowProgressTips: 1, 
                                    success: function (res) {
                                        var shopId = res.serverId;
                                        that.$data.shop.push(shopId)
                                        // 回调上传LOGO
                                        while(1){
                                            if (shopId) {
                                                wx.uploadImage({
                                                    localId: that.$data.logoLicense[0], 
                                                    isShowProgressTips: 1, 
                                                    success: function (res) {
                                                         var serverId = res.serverId;
                                                         that.$data.logo.push(serverId)
                                                         // 声明data
                                                    var data = {
                                                        openid: that.$data.openid,
                                                        county_id: that.$data.areaArr[that.$data.area].id,
                                                        contact: that.$data.userName,
                                                        name: that.$data.storeName,
                                                        mobile: that.$data.phone,
                                                        address: that.$data.communityArr[that.$data.community].village + that.$data.address,
                                                        fcc: that.$data.food[0],
                                                        business_licence: that.$data.shop[0],
                                                        logo: that.$data.logo[0]
                                                    };
                                                    // return false;
                                                    $.ajax({
                                                        type: 'post',
                                                        url: "http://ypx.shenmikj.com/am_shop/Wechat/Api/StoreEnter",
                                                        data: data,
                                                        dataType: "json",
                                                        success: function(data) {
                                                            if (data.status) {
                                                                alert(data.msg);
                                                            } else {
                                                                alert(data.msg);
                                                            }
                                                            that.$data.confirmTips = "审核中"
                                                            that.$data.update = false
                                                            return;
                                                        },
                                                        error: function(res){
                                                            console.log(res)
                                                        }
                                                    });
                                                    }
                                                });
                                                break;
                                            }
                                        }
                                    }
                                });
                                break;
                            }
                        }
                    }
                });
            }
        },
        // 生成省市区索引更新地址数组
        createMap: function() {
            var province = Number(this.province),
                provinceId = null,
                cityId = null,
                areaId = null,
                cityList = [],
                areaList = [],
                communityList = []

            // 获取省列表
            this.provinceArr = this.addressList.provinceArr[this.province];
            // 遍历获取省id
            provinceId = this.addressList.provinceArr[province].id;
                // 通过省id 获取 市列表
            for (var i = 0, len = this.addressList.cityArr.length; i < len; i++) {
                if (this.addressList.cityArr[i].province_id === provinceId) {
                    cityList.push(this.addressList.cityArr[i])
                }
            }
            this.cityArr = cityList
            // // 通过市id 获取 区列表
            cityId = this.cityArr[this.city].id
            for (var i = 0, len = this.addressList.countyArr.length; i < len; i++) {
                if (this.addressList.countyArr[i].city_id === cityId) {
                    areaList.push(this.addressList.countyArr[i])
                }
            }
            this.areaArr = areaList
            // // 通过区id 获取 小区列表
            areaId = this.areaArr[this.area].id
            for (var i = 0, len = this.addressList.villageArr.length; i < len; i++) {
                if (this.addressList.villageArr[i].county_id === areaId) {
                    communityList.push(this.addressList.villageArr[i])
                }
            }
            this.communityArr = communityList
        }
    },
    created: function() {
        var that = this
        $.ajax({
            method:"get",
            async: false,
            dataType:"json",
            url:"http://ypx.shenmikj.com/am_shop/Wechat/Api/regionList",
            success: function(res){
                that.$data.addressList = res
            },
            error: function(res){
                console.log("error:"+res)  
            }
        })
        if (this.addressList) {
            this.createMap()
        }
        var arr,reg= /openid=(.*?)(;|$)/;
        if(arr=document.cookie.match(reg)){
            this.openid =  arr[1];
            console.log(this.openid)
        }
    }
})