<template>
  <view class="loginBox">
    <view class="logo-content">
      <image style="width: 250rpx" :src="globalConfig" mode="widthFix"></image>
    </view>
    <p class="comBtnTitle">请选择您本次登录的组织</p>
    <view class="comBtnBox">
      <view
        v-for="item in companyList"
        :key="item.companyID"
        class="comBtn"
        @click.native.prevent="handleSelectCompany(item)"
      >
        <text class="comBtnText" :title="item.companyName">
          {{ item.companyName }}
        </text>
        <u-icon name="arrow-right" color="#606266" size="14"></u-icon>
      </view>
    </view>
    <u-toast ref="uToast"></u-toast>
  </view>
</template>

<script>
import { OrganGetUserCompany, ChangeCompany } from "@/api/login.js";
import logo from "@/static/logo.png";
import localStorage from "@/utils/localStorage";
import { getUserInfo, setUserInfo } from "@/utils/auth";
export default {
  data() {
    return {
      globalConfig: logo,
      selectCompany: false,
      companyList: [], //公司账套
    };
  },
  onReady() {
    this.$ddFunction.setTitle("钉钉免登录认证");
  },
  mounted() {
    this.getCompanyList()
  },
  methods: {
    // 获取公司列表数据
    async getCompanyList() {
      let params = {};
      let res = await OrganGetUserCompany(params);
      if (res.code == 1) {
        // 如果只存在一个公司 直接跳转到首页
        if (res.data[0].length == 1) {
          if (
            res.data[0][0].companyID == null ||
            res.data[0][0].companyID == "" ||
            res.data[0][0].companyID == undefined
          ) {
            this.$refs.uToast.show({
              type: "warning",
              message: "当前登录人公司信息为空，请联系管理员处理！",
            });
          } else {
            this.handleSelectCompany(res.data[0][0]);
          }
        } else {
          this.companyList = res.data[0];
          this.selectCompany = true;
        }
      }
    },
    // 当前选择的组织
    handleSelectCompany(item) {
      let obj = getUserInfo();
      obj.companyID = item.companyID;
      obj.companyName = item.companyName;
      setUserInfo(obj);
      this.ChangeCompany(item.companyID);
      uni.switchTab({
        url: "/pages/staging/index",
      });
    },
    async ChangeCompany(CompanyID) {
      let params = {
        CompanyID: CompanyID,
      };
      let res = await ChangeCompany(params);
    },
  },
};
</script>

<style scoped lang="scss">
.comBtnBox {
  display: flex;
  align-items: center;
  flex-direction: column;
}
.comBtn {
  border: 2rpx solid #f1f1f1;
  padding: 20rpx;
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 80%;
  border-radius: 6rpx;
  margin: 16rpx 0;
  color: #606266;
}
.comBtnTitle {
  width: 100%;
  font-size: 32rpx;
  color: #666;
  text-align: center;
  margin: 20rpx 0 20rpx 0;
}
.comBtnText {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin: 0 20rpx 0 0;
}
.loginBox {
  background: #ffffff;

  .logo-content {
    width: 100%;
    font-size: 25px;
    text-align: center;
    padding-top: 20%;

    image {
      border-radius: 4px;
    }

    .title {
      margin-left: 10px;
      display: block;
    }
  }
  .loginForm {
    padding: 70rpx 80rpx;
  }
  .btn {
    padding: 30rpx 80rpx;
  }
}
</style>
<style scoped>
page {
  background: #fff;
}
</style>