<template>
    <router-view />
</template>

<script>
export default {
  name: "Lang",
  data() {
    return {
      lang: this.$store.state.langStr
    };
  },
  created() {
    // 默认国家区号
    if (!this.$util.storage.fetch("areaCodeItem")) {
      let data = {
        id: "317",
        name: "中国",
        code: "+86"
      };
      this.$util.storage.save("areaCodeItem", data);
    }
    // 判断登录状态
    if (!this.$util.checkIsLogin()) {
      this.$util.storage.remove("loginUserInfo");
    }
    this.setCountry();
  },
  mounted() {},
  methods: {
    // 重新请求国家城市进行验证
    getCountryList() {
      let params = {};
      params.url = this.$apis.api_national_real_all_city;
      this.$net.req(params, this).then(res => {
        this.$store.commit("setCountryArr", res.data);
        this.setCountry();
      });
    },
    // 验证城市
    verificationCity(val, cityTag) {
      let countryArr = this.$store.state.countryArr;
      let isTrue = null;
      countryArr.some(el => {
        el.son.some(element => {
          if (
            this.$route.params.cityTag == element.other_name ||
            this.$route.query.cityId == element.id
          ) {
            isTrue = true;
            this.$util.storage.save("CityItem", element);
            return true;
          }
        });
      });
      if (!isTrue && countryArr.length) {
        if (cityTag) {
          this.$router.push({
            path: `/`
          });
        } else {
          this.$Dialog
            .alert({
              message: "城市信息错误，请重新选择。"
            })
            .then(() => {
              this.$router.push({
                path: `/${this.lang}/guide`
              });
            });
        }
      }
    },
    // 获取路由的城市
    setCountry() {
      let cityName = this.$route.params.cityTag;
      let cityId = this.$route.query.cityId;
      let countryArr = this.$store.state.countryArr;
      // if (!countryArr.length) {
      //   this.getCountryList();
      // }
      // 落地页路由参数 cityId 验证
      if (cityId) {
        this.verificationCity(cityId);
      }
      // 城市路由 cityName 验证
      if (cityName) {
        this.verificationCity(cityName, "cityTag");
      }
      // 判断路由只存在语言
      if (this.$route.path.length < 5) {
        if (!countryArr) {
          this.$router.push({
            path: `/${this.lang}/guide`
          });
        } else {
          this.$router.push({
            path: `/`
          });
        }
      }
    }
  }
};
</script>
