<template>
  <div class="user">
    <div class="user-login">
      <div class="login-type flex">
        <div class="pos">{{message}}</div>
      </div>
      <div class="el-form">
        <!-- 密码登录 -->
        <el-form ref="param" :model="param" :rules="rules" class="ms-content" v-if="type==0">
          <el-form-item prop="username" style="width:3rem;height: 0.45rem;">
            <el-input name="username" v-model="param.account" placeholder="请输入账号" key="param.username">
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input name="password" v-model="param.password" placeholder="请输入密码" key="param.password" show-password>
            </el-input>
          </el-form-item>
          <div class="login-btn">
            <el-button type="primary" @click="submitForm1()" style="width: 100%;">登录</el-button>
          </div>
        </el-form>
        <!-- 手机验证码验证 -->
        <el-form ref="phone" :model="phone" :rules="rules" class="ms-content" v-if="type==2">
          <el-form-item prop="phone" class="item">
            <el-input v-model="phone.phone" placeholder="请输入手机号" maxlength="30"></el-input>
          </el-form-item>
          <div class="getphonecode">
            <el-form-item prop="phonecode" class="item">
              <el-input v-model="phone.phonecode" placeholder="请输入短信验证码" style="width:1.8rem" maxlength="30"></el-input>
            </el-form-item>
            <div class="phonecode handle" @click = "getPhoneCode">获取短信验证码</div>
          </div>
          <div class="login-btn">
            <el-button type="primary" @click="submitForm2()" style="width: 100%;">登录</el-button>
          </div>
        </el-form>
        <div class="login-font flex-between">
          <div @click="forget" class="handle">忘记密码</div>
          <div @click="examine" class="handle">我要注册</div>
        </div>
        <div class="code-login handle" @click="changephone">{{changemessage}}</div>
      </div>
    </div>
  </div>
</template>

<script>
  import {powerLogin} from '~/api/bid/login';
  import {getSecond,sendSms,getVerifyPhone} from '@/common/list.js';
  export default {
    data() {
      return {
        changemessage: "手机验证码登录",
        message: "密码登录",
        type: "0",
        yzshow: true,
        param: {
          account: "",
          password: ""
        },
        phone: {
          phone: "",
          phonecode: ""
        },
        rules: {
          account: [{
            required: true,
            message: '请输入用户名',
            trigger: 'blur'
          }],
          password: [{
            required: true,
            message: '请输入密码',
            trigger: 'blur'
          }],
          phone: [{
            required: true,
            message: '请输入手机号',
            trigger: 'blur'
          }],
          phonecode: [{
            required: true,
            message: '请输入验证码',
            trigger: 'blur'
          }]
        },
      };
    },
    created() {
     },
    methods: {
      //发送短信验证码
      async getPhoneCode(){
        let message = {}
        message.phone = this.phone.phone
        message.type = 2
        const {data} = await sendSms(message)
      },
      async submitForm1() {
        try {
          const valid = await this.$refs['param'].validate()
          if (valid) {
            const {
              msg,
              code,
              data
            } = await powerLogin(this.param)
            //将手机号和密码发送
            console.log(data,code)
            if (code == 200) {
              sessionStorage.setItem("token",data);
              this.$message({
                message: msg,
                type: 'success',
                duration:1500
              });
              setTimeout(()=>{
                this.$router.push("/")
              },1500)
            } else {
              this.$message({
                message: msg,
                type: 'error'
              });
            }
          }
        } catch (err) {
          console.log(err);
        }
      },
      async submitForm2() {
        try {
          const valid = await this.$refs['phone'].validate()
          if (valid) {
            /* const {
              msg,
              code,
              data
            } = await powerLogin(this.param) */
            //将手机号和手机验证码发送
            let mess = {}
            mess.phone = this.phone.phone
            mess.verify = this.phone.phonecode
            const {data,code,msg} = await getVerifyPhone(mess)
            console.log(data,code)
            if (code == 200) {
              sessionStorage.setItem("token",data);
              this.$message({
                message: msg,
                type: 'success',
                duration:1500
              });
              setTimeout(()=>{
                this.$router.push("/")
              },1500)
            } else {
              this.$message({
                message: msg,
                type: 'error'
              });
            }
          }
        } catch (err) {
          console.log(err);
        }
      },
      changephone() {
        let type = this.type;
        if (type == '0' || type == '1') {
          this.type = 2;
          this.changemessage = "密码登录"
          this.message = "手机验证码登录"
        } else if (type == '2') {
          this.type = 0;
          this.changemessage = "手机验证码登录"
          this.message = "密码登录"
        }
      },
      change() {
        let type = this.type;
        if (type == '0') {
          this.type = 1
        } else if (type == '1') {
          this.type = 0
        }
      },
      examine() {
        this.$router.push("/login/examine")
      },
      forget() {
        this.$router.push("/login/seek")
      }
    }
  };
</script>

<style scoped>
.handle{
  cursor: pointer;
}
.pos{
  position: absolute;
  left:25%;
}
  .getphonecode {
    position: relative;
  }

  .getphonecode>.phonecode {
    font-size: .14rem;
    font-weight: 400;
    color: rgba(51, 136, 255, 1);
    position: absolute;
    top: 0.13rem;
    left: 2.0rem;
    width: 2rem;
  }

  .code-login {
    font-size: .14rem;
    color: rgba(4, 134, 254, 1);
    text-align: center;
    margin-top: 0.3rem;
  }

  .login-font {
    margin-top: 0.14rem;
  }

  .user {
    width: 100%;
    height: 6rem;
    background: rgba(216, 216, 216, 1);
    position: relative;
  }

  .user-login {
    width: 3.80rem;
    height: 4.34rem;
    background: rgba(255, 255, 255, 1);
    position: absolute;
    top: 50%;
    right: 10%;
    transform: translateY(-50%);
  }

  .login-type {
    width: 100%;
    height: .77rem;
    border-bottom: 2px solid rgba(4, 134, 254, 1);
  }

  .login-type>div {
    height: 0.77rem;
    width: 1.9rem;
    text-align: center;
    line-height: 0.77rem;
    color: rgba(4, 134, 254, 1);
    font-size: .2rem;
  }

  .login-type>.active {
    border-bottom: 2px solid rgba(4, 134, 254, 1);
  }


  .el-form {
    padding: 0.2rem
  }
  /* .login-btn{
    cursor: pointer;
  } */
</style>
