<template>
    <el-dialog
      title="选择企业"
      :visible.sync="dialogVisible"
      width="30%">
      <el-select v-model="chooseCompanyId" placeholder="请选择">
        <el-option
          v-for="item in companyList"
          :key="item.scyId"
          :label="item.scyName"
          :value="item.scyId">
        </el-option>
      </el-select>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="chooseLogin()">确 定</el-button>
      </span>
    </el-dialog>
</template>
<script>
  import {chooseCompanyLogin} from "@/api/login";
export default {
  data() {
    return {
      companyList : [],
      chooseCompanyId : '',
      unionId : '',
      dialogVisible : false
    }
  },
  created() {
    const code = this.getUrlKey("code")
    if(code){
      let params = {}
      params.code = code
      this.$store.dispatch("WxLogin", params).then((response) => {
        var data = response.data;
        var unionId = data.unionId;
        if (data.code == "0") {
          this.$router.push({ path: "/certification" });
        // 认证单个企业
        } else if (data.code == "1") {
          // 认证通过
          if (data.loginUser.sysUser.companyStatus == "00") {
            this.$router.push({ path: this.redirect || "/" });
          // 认证未通过
          } else {
            console.log("未认证通过!")
            this.$router.push({ path: "/certification" });
          }
        // 多家企业
        } else if (data.code == "2") {
          this.companyList = data.companyList;
          this.chooseCompanyId = this.companyList[0].scyId;
          this.dialogVisible = true;
        // 未绑定用户
        } else {
          this.$router.push({ path: "/register?unionId="+ unionId});
        }
      }).catch((res) => {
        console.log(res)
          // console.log(code)
          //this.$router.push({ path: "/login" });
          // this.$router.push({ path: "/register" });
      });
    }else{
      this.$router.push({ path: "/login" });
          //this.$router.push({ path: "/register" });
    }
  },
  render: function(h) {
    return h() // avoid warning message
  },
  methods: {
      getUrlKey(name) {
          return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ""])[1].replace(/\+/g, '%20')) || null
      },
      chooseLogin(){
        chooseCompanyLogin(this.chooseCompanyId).then(response => {
          if (response.code == "200") {
            // 认证通过
            if (response.data.sysUser.companyStatus == "00") {
              this.$store.dispatch('GetInfo').then(res => {
                this.$router.push({ path: this.redirect || "/" });
              })
            // 认证未通过
            } else {
              console.log("未认证通过!")
              this.$router.push({ path: "/certification" });
            }
          }
        }).catch((response)=>{

        });
      }
  }
}
</script>
