<template>
  <view class="page_css">
    <view class="container">
      <view class="title">科研医疗建筑运维平台</view>
      <view class="sp-login-form">
        <betone-new-input :value="userInfo.username" placeholder="请输入用户名" icon="account-fill"
          style="margin-bottom: 32rpx" @changevalue="userInfo.username=$event" />
        <betone-new-input style="margin-bottom: 32rpx" v-model="userInfo.pwd" placeholder="请输入密码" :type="'password'"
          icon="lock-fill" @changevalue="userInfo.pwd=$event" />
        <u-checkbox-group>
          <u-checkbox @change="rememberPwdChange" v-model="rememberPwd" key="index" name="记住密码">记住密码</u-checkbox>
        </u-checkbox-group>
        <u-button class="item-bottom" type="primary" :disabled="disabled" @click="formSubmit()">
          登录
        </u-button>
        <view class="footer">
          <span @click="linkRegister()">注册账号</span>
          <!-- <span>忘记密码</span> -->
        </view>
      </view>
    </view>
    <betone-loading ref="BetLoading"></betone-loading>
  </view>
</template>

<script>
import { getTopUrl, bjectToQueryString } from "@/utils/util";
import md5 from "@/uni_modules/md5/md5.min.js";

export default {
  data() {
    return {
      clientid: "",
      userInfo: {
        tenantId: "",
        username: "",
        pwd: "",
      },
      rules: {},
      rememberPwd: false, //是否记住密码
    };
  },
  computed: {
    disabled() {
      console.log(11111111111,this.userInfo)
      return !this.userInfo.username || !this.userInfo.pwd;
    },
  },
  mounted() {
    // var info = plus.push.getClientInfo();
    // this.clientid = info.clientid

    this.getTenant();
    this.rememberFn()
  },

  methods: {
    rememberPwdChange() {
      this.rememberPwd = !this.rememberPwd;
      if (this.rememberPwd) {
        uni.setStorage({
          key: "loginInfo",
          data: {...this.userInfo,'rememberPwd':true},
        });
      }
    },
    rememberFn(){
      let rememberInfo = uni.getStorageSync('loginInfo')
      if(rememberInfo.rememberPwd){
        this.userInfo.username = rememberInfo.username
        this.userInfo.pwd = rememberInfo.pwd
        this.rememberPwd = true
      }
    },
    formSubmit() {
      //获取当前手机的cid
      if (this.userInfo.username == "") {
        uni.showToast({ title: "请输入手机号", icon: "none" });
        return false;
      }
      if (this.userInfo.pwd == "") {
        uni.showToast({ title: "请输入密码", icon: "none" });
        return false;
      }
      this.$refs.BetLoading.show();
      let formdata = {
        tenantId: this.userInfo.tenantId,
        username: this.userInfo.username,
        password: md5(this.userInfo.pwd),
        grant_type: "captcha",
        scope: "all",
        type: "account",
      };
      if (this.rememberPwd) {
        uni.setStorage({
          key: "loginInfo",
          data: {...this.userInfo,'rememberPwd':true},
        });
      }
      // 登录成功之后处理
      this.$u.api
        .login(bjectToQueryString(formdata))
        .then((res) => {
          uni.setStorage({
            key: "userinfo",
            data: res,
          });
          uni.setStorage({
            key: "token",
            data: res.access_token,
          });
        
          this.$store.dispatch("changeTabbar", res.role_id);
         
          this.$refs.BetLoading.hide();
        })
        .catch((err) => {
          this.$refs.BetLoading.hide();
        });
    },

    getTenant() {
      // let domain = getTopUrl();
      let domain = "";
      // 临时指定域名，方便测试
      // domain = "https://bladex.vip";
      this.$u.api.getTenantInfo(domain).then((res) => {
        const data = res.data;
        if (data.success && data.data.tenantId) {
          this.tenantMode = false;
          this.userInfo.tenantId = data.data.tenantId;
          this.$parent.$refs.login.style.backgroundImage = `url(${data.data.backgroundUrl})`;
        } else {
          this.userInfo.tenantId = "000000";
        }
      });
    },

    linkRegister() {
      uni.navigateTo({
        url: "/pages/register/index",
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.page_css {
  display: flex;
  align-items: center;
  /* 垂直居中 */
  justify-content: center;
  /* 水平居中，如需 */
  width: 100%;

  .container {
    width: 100%;
    border: 2rpx solid #ccc;
    border-radius: 6rpx;
    background-color: rgba(255, 255, 255, 1);
    box-shadow: 0px 2rpx 10rpx 0px rgba(0, 0, 0, 0.2);
    margin: 350rpx 64rpx 0;

    .title {
      font-size: 48rpx;
      text-align: center;
      line-height: 60rpx;
      color: rgb(16, 16, 16);
      padding-top: 60rpx;
      padding-bottom: 60rpx;
      font-weight: 550;
    }

    .u-form {
      padding: 0 72rpx;
    }

    .sp-login-form {
      padding: 0 72rpx;
    }

    .item-bottom {
      margin-bottom: 32rpx;
    }

    .footer {
      margin-bottom: 32rpx;
      overflow: hidden;

      span {
        display: block;
        width: 50%;
        float: left;
        font-size: 28rpx;
        line-height: 40rpx;
        color: rgb(58, 98, 215);
        cursor: pointer;

        &:nth-child(1) {
          text-align: left;
          position: relative;
          // &::before {
          //   content: "";
          //   width: 1px;
          //   height: 20rpx;
          //   position: absolute;
          //   top: 10rpx;
          //   right: 0;
          //   background: rgb(58, 98, 215);
          // }
        }

        &:nth-child(2) {
          text-align: right;
        }
      }
    }
  }
}
</style>