<template>
  <div class="login">
    <div class="logo">
      <img src="../../assets/images/logo.png" alt="" />
    </div>
    <div class="container" v-show="isTrue">
      <div class="input-group">
        <input
          type="number"
          @keyup="onKeyUp()"
          placeholder="请输入手机号"
          class="input"
          v-model="phoneNumber"
          maxlength="11"
          minlength="11"
          pattern="[0-9]"
        />
      </div>
      <div class="input-group">
        <input
          type="password"
          @keyup="onKeyUp()"
          placeholder="请输入密码"
          class="input"
          v-model="password"
        />
      </div>

      <van-button round size="large" @click="onLogin" :disabled="pwdDisable"
        >登录</van-button
      >
      <span class="login-change" @click="isTrue = false">验证码登录</span>
      <span class="login-change" @click="onRegister" style="float: left"
        >立即注册</span
      >
    </div>

    <div class="container" v-show="!isTrue">
      <div class="input-group">
        <input
          type="number"
          @keyup="onKeyUp()"
          placeholder="请输入手机号"
          class="input"
          v-model="phoneNumber"
          maxlength="11"
          minlength="11"
          pattern="[0-9]"
        />
      </div>
      <div class="input-group">
        <input
          type="text"
          @keyup="onKeyUp()"
          placeholder="请输入验证码"
          class="input"
          minlength="6"
          maxlength="6"
          v-model="code"
        />
        <span class="code-text" v-show="countDownStatus" @click="onSendCode">{{
          codeText
        }}</span>
        <span class="code-text" v-show="!countDownStatus">
          <van-count-down
            ref="countdown"
            :auto-start="false"
            :time="countDownTime"
            @finish="onFinished"
            format="ss秒"
          />
        </span>
      </div>

      <van-button
        round
        size="large"
        @click="onCodeLogin"
        :disabled="codeDisable"
        >登录</van-button
      >
      <span class="login-change" @click="isTrue = true">密码登录</span>
      <span class="login-change" @click="onRegister" style="float: left"
        >立即注册</span
      >

      <!-- 图形验证码 -->
      <m-img-code
        v-if="showImgCode"
        v-model="showImgCode"
        :phone="phoneNumber"
        @onCodeSend="onCodeSend"
      />
    </div>
  </div>
</template>
<script>
/* eslint-disable */
import {
  usernameLogin,
  sendSms,
  verifySmsCode,
  smsLogin,
  queryUserByPhone,
} from "@/api/app";
import MImgCode from "@/components/MImgCode";
export default {
  name: "login",
  components: {
    MImgCode,
  },
  data() {
    return {
      groupId: this.$route.query.groupId,
      pwdDisable: true, // 密码登录按钮状态
      codeDisable: true, // 验证码登录按钮状态
      isTrue: true,
      countDownStatus: true, // 到计时状态
      phoneNumber: null,
      password: null,
      code: null,
      codeText: "获取验证码",
      countDownTime: 1000 * 120, // 倒计时时间
      isClick: false,
      showImgCode: false,
    };
  },
  mounted() {
    // 登录时删除无用的token
    localStorage.removeItem("userInfo");
    localStorage.removeItem("Authorization");
    // 登录时间判断用户是Token是否有效
  },
  methods: {
    onKeyUp() {
      if (this.isTrue) {
        if (!this.phoneNumber || !this.password) {
          this.pwdDisable = true;
        } else {
          this.pwdDisable = false;
        }
      } else {
        if (!this.phoneNumber || !this.code) {
          this.codeDisable = true;
        } else {
          this.codeDisable = false;
        }
      }
    },
    onRegister() {
      this.$router.push({
        path: "/appRegister",
        query: {
          groupId: this.groupId,
        },
      });
    },
    onSendCode() {
      // 发送验证码
      if (!this.checkPhone(this.phoneNumber)) {
        return;
      }
      queryUserByPhone({ mobile: this.phoneNumber }).then((res) => {
        let result = res.data;
        if (result.code == 200) {
          if (result.data == 1) {
            // sendSms({
            //     mobile: this.phoneNumber
            // }).then(res => {
            //     let result = res.data
            //     if(result.code == 200) {
            //         this.countDownStatus = false
            //         this.$refs.countdown.start() // 倒计时开始
            //     } else {
            //         this.$toast(result.msg)
            //     }
            // })
            this.showImgCode = true;
          } else {
            this.$dialog
              .confirm({
                message: "此手机号尚未注册<br/>是否注册？",
                confirmButtonText: "注册",
              })
              .then(() => {
                this.$router.push({
                  path: "/appRegister",
                  query: {
                    groupId: this.groupId,
                  },
                });
              })
              .catch(() => {});
          }
        } else {
          this.$toast(result.msg);
        }
      });
    },
    onCodeSend() {
      this.countDownStatus = false;
      this.$refs.countdown.start(); // 倒计时开始
    },
    onCodeLogin() {
      // 短信登录
      if (this.isClick) {
        return;
      }
      this.isClick = true;
      let params = {
        clientId: "student",
        clientSecret: "student",
        phone: this.phoneNumber,
        smsCode: this.code,
        channel: "H5",
      };
      smsLogin(params).then((sms) => {
        let s = sms.data;
        this.isClick = false;
        // 保存用户信息
        if (s.code == 200) {
          let auth = s.data.authentication;
          localStorage.setItem(
            "userInfo",
            auth.token_type + " " + auth.access_token
          );
          if (s.data.password) {
            this.$router.push({
              path: "/classDetail",
              query: {
                groupId: this.groupId,
              },
            });
          } else {
            this.$router.push({
              path: "/appSetInfo",
              query: {
                groupId: this.groupId,
              },
            });
          }
        } else if (s.code == 99) {
          this.$dialog
            .confirm({
              message: "此手机号尚未注册<br/>是否注册？",
              confirmButtonText: "注册",
            })
            .then(() => {
              this.$router.push({
                path: "/appRegister",
                query: {
                  groupId: this.groupId,
                },
              });
            })
            .catch(() => {});
        } else {
          this.$toast(s.msg);
        }
      });
      // queryUserByPhone({ mobile: this.phoneNumber }).then(res => {
      //     let result = res.data
      //     if(result.code == 200) {
      //         this.isRegister = result.data
      //     }
      //     if(this.isRegister == 1) {
      //         this.onLoginStr()
      //     } else {
      //         this.$dialog.confirm({
      //             message: '此手机号尚未注册<br/>是否注册？',
      //             confirmButtonText: '注册'
      //         }).then(() => {
      //             this.$router.push({
      //                 path: '/appRegister',
      //                 query: {
      //                     groupId: this.groupId
      //                 }
      //             })
      //         }).catch(() => { });
      //     }
      // })
    },
    onLogin() {
      // 账号登录
      if (this.isClick) {
        return;
      }
      this.isClick = true;

      usernameLogin({
        clientId: "student",
        clientSecret: "student",
        username: this.phoneNumber,
        password: this.password,
      }).then((sms) => {
        let s = sms.data;
        this.isClick = false;
        // 保存用户信息
        if (s.code == 200) {
          let auth = s.data.authentication;
          localStorage.setItem(
            "userInfo",
            auth.token_type + " " + auth.access_token
          );

          this.$router.push({
            path: "/classDetail",
            query: {
              groupId: this.groupId,
            },
          });
        } else if (s.code == 99) {
          this.$dialog
            .confirm({
              message: "此手机号尚未注册<br/>是否注册？",
              confirmButtonText: "注册",
            })
            .then(() => {
              this.$router.push({
                path: "/appRegister",
                query: {
                  groupId: this.groupId,
                },
              });
            })
            .catch(() => {});
        } else {
          this.$toast(s.msg);
        }
      });
    },
    onFinished() {
      // 倒计时结束
      this.countDownStatus = true;
      this.$refs.countdown.reset();
    },
    checkPhone(phoneNumber) {
      let result = true;
      if (!/^1(3|4|5|6|7|8|9)\d{9}$/.test(phoneNumber)) {
        this.$toast("手机号输入有误");
        result = false;
      }
      return result;
    },
  },
};
</script>
<style lang='less' scoped>
@import url("../../assets/commonLess/variable.less");

.login {
  min-height: 100vh;
//   background: -webkit-gradient(
//     linear,
//     left bottom,
//     left top,
//     from(#01c1b5),
//     to(#1bcbbf)
//   );
  background: linear-gradient(1turn, #01c1b5, #1bcbbf);
}
.container {
  padding: 0 0.48rem;
}
.logo {
  padding-top: 1rem;
  padding-bottom: 0.9rem;
  width: 1.6rem;
  margin: 0 auto;
  img {
    width: inherit;
  }
}
.input-group {
  position: relative;
  height: 0.44rem;
  border-radius: 0.5rem;
  border: 0.02rem solid @whiteColor;
  margin-bottom: 0.2rem;
  padding-left: 0.3rem;
  padding-right: 0.3rem;
  display: flex;
  align-items: center;
  .input {
    flex: 1;
    font-size: 0.14rem;
    color: @whiteColor;

    background: transparent;
    border: none;
    &::placeholder {
      color: @whiteColor!important;
    }
  }
  .code-text {
    position: absolute;
    right: 0;
    flex: 1;
    display: block;
    width: 0.94rem;
    text-align: center;
    border-left: 0.02rem solid @whiteColor;
    font-size: 0.14rem;
    color: @whiteColor;
    line-height: 0.3rem;
    height: 0.3rem;
  }
}
/deep/.van-count-down {
  font-size: 0.14rem;
  color: @whiteColor;
  line-height: 0.3rem;
  height: 0.3rem;
}
/deep/.van-button--large {
  height: 0.44rem;
  line-height: 0.42rem;
  color: @mColor;
  border: 0;
}
/deep/.van-button:active::before {
  opacity: 0.05;
}
/deep/.van-button--disabled {
  opacity: 1;
  color: rgba(0, 0, 0, 0.25);
}
.login-change {
  padding-top: 0.08rem;
  font-size: 0.14rem;
  color: @whiteColor;
  float: right;
}
</style>