<template>
  <div>
    <div class="login-content">
      <el-form ref="loginForm" :model="loginForm" :rules="loginFormRules" class="login-form" auto-complete="on" label-position="left">
        <div class="title">
          <h2>欢迎登录国内电商</h2>
        </div>
        <template v-if="showVerifyLogin">
          <el-form-item prop="loginName" style="margin-bottom: 50px;">
            <div class="input-box">
              <span class="input-title">
                手机号码
              </span>
              <el-input
                ref="username"
                v-model="loginForm.loginName"
                placeholder="请输入手机号码"
                name="username"
                type="text"
                tabindex="1"
                auto-complete="on"
              />
            </div>

          </el-form-item>
          <el-form-item prop="password">
            <div class="input-box">
              <span class="input-title">
                密码
              </span>
              <el-input
                :key="passwordType"
                ref="password"
                v-model="loginForm.password"
                :type="passwordType"
                placeholder="请输入密码"
                name="password"
                tabindex="2"
                auto-complete="on"
                @keyup.enter.native="handleLogin"
              />
              <span class="show-pwd" @click="showPwd">
                <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
              </span>
            </div>

          </el-form-item>
        </template>
        <template v-if="!showVerifyLogin">
          <el-form-item style="width: 100%;margin-bottom: 50px;" class="form-required" prop="loginName">
            <div class="input-box">
              <span class="input-title">
                手机号码
              </span>
              <el-input
                v-model="loginForm.loginName"
                placeholder="请输入手机号码"
                type="text"
                tabindex="1"
                auto-complete="on"
                class="verify-cont"
              >
                <template #append v-if="!showVerifyLogin">
                  <verification-code class="get-verify-code" :code="loginForm.loginName" module="other" type="phone" placeholder="请输入手机号码" />
                </template>
              </el-input>
            </div>
          </el-form-item>
          <el-form-item style="width: 100%" class="form-required" prop="password">
            <div class="input-box">
              <span class="input-title">
                验证码
              </span>
              <el-input
                v-model="loginForm.password"
                placeholder="请输入验证码"
                tabindex="2"
                auto-complete="on"
                @keyup.enter.native="handleLogin"
              />
            </div>
          </el-form-item>
        </template>
        <div class="tips-cont">
          <div v-if="!showVerifyLogin" class="tips">
            <span @click="changeLoginType">密码登录</span>
          </div>
          <div v-if="showVerifyLogin" class="tips">
            <span @click="changeLoginType">验证码登录</span>
          </div>
          <div v-if="showVerifyLogin" class="tips">
            <span style="margin-right:20px; cursor: pointer;" @click="changePassword">忘记密码？</span>
          </div>
        </div>
        <el-button
          :loading="loading"
          type="primary"
          class="btn"
          @click.native.prevent="handleLogin"
        >
          登录
        </el-button>
      </el-form>
    </div>
    <cy-dialog ref="dialog" :modal="false" :style="isMobile() ? 'transform: scale(0.9)' : ''" title="修改密码">
      <change-password />
    </cy-dialog>
  </div>

</template>

<script>
import { validUsername } from '@/utils/validate'
import { isMobileDevice } from '@/utils/index'

export default {
  name: 'Login',
  components: {
    ChangePassword: () => import('./ChangePassword'),
    VerificationCode: () => import('@/components/VerificationCode/index.vue')
  },
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('请输入用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('请输入正确的密码'))
      } else {
        callback()
      }
    }
    return {
      showVerifyLogin: true,
      loginForm: {
        loginName: '',
        password: ''
      },
      loginFormRules: {
        loginName: [{ required: true, message: '请输入手机号码', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      },
      dialogKey: 0,
      // validator: validateUsername
      // validator: validatePassword
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    // 显示密码
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    changeLoginType() {
      this.showVerifyLogin = !this.showVerifyLogin
      this.$refs.loginForm.resetFields()
    },
    handleLogin() {
      if (this.showVerifyLogin) {
        this.loginFormRules['password'][0].message = '请输入密码'
      } else {
        this.loginFormRules['password'][0].message = '请输入验证码'
      }
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          const postData = {
            loginName: this.loginForm.loginName,
            password: !this.showVerifyLogin ? this.loginForm.password : this.$md5(this.loginForm.password)
          }
          this.$store.dispatch('user/login', postData).then(() => {
            this.$router.push({ path: '/' })
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    changePassword() {
      // this.dialogKey += 1
      this.$refs.dialog.show = true
    },
    isMobile() {
      return isMobileDevice() && window.screen.width < 768
    }
  }
}
</script>

<style lang="scss">
  .verify-cont {
    max-width: 68%;
    display: flex !important;
    .el-input-group__append {
      border: none;
    }
    .get-verify-code {
      line-height: 28px;
    }
  }
</style>
<style lang="scss" scoped>
.login-content {
  margin-left: 40px;
  .login-form {
    position: relative;
    width: 480px;
    max-width: 100%;
    padding: 60px 35px 0;
    margin: 0 auto;
    overflow: hidden;
    margin-top: 50px;
  }

  .title {
    text-align: left;
    margin-bottom: 50px;
    font-size: 30px;
    color: #2C4270;
  }
  .tips-cont {
    display: flex;
    width: 100%;
    justify-content: space-between;
    .tips {
      font-size: 14px;
      color: #BABABA;
      margin-bottom: 10px;
      text-align: right;
      span {
        margin-right:20px;
        cursor: pointer;
      }
    }
  }
  .btn {
    width:100%;
    height: 43px;
    font-size: 16px;
    margin-bottom: 30px;
    margin-top: 30px;
  }
  .input-box {
    display: flex;
  }
  .input-title {
    font-size: 18px;
    color: #666;
    display: inline-block;
    width: 80px;
  }
  /*.svg-container {
    padding: 6px 5px 6px 15px;
    color: #5AC4FC;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }*/
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: #889aa4;
    cursor: pointer;
    user-select: none;
  }
}

@media screen and (max-width: 768px) {
  .login-content {
    margin-left: 0px;
    .login-form {
      position: relative;
      width: 480px;
      max-width: 100%;
      padding: 20px 35px 0;
      margin: 0 auto;
      overflow: hidden;
      margin-top: 50px;
    }
    .title {
      text-align: center;
      margin-bottom: 50px;
      font-size: 20px;
      color: #2C4270;
    }
    .input-box {
      display: flex;
    }
    .input-title {
      font-size: 16px;
      color: #666;
      display: inline-block;
      width: 80px;
    }
    .btn {
      width:100%;
      height: 43px;
      font-size: 16px;
      margin-bottom: 30px;
      margin-top: 30px;
    }
  }
}
</style>

<style lang="scss">
.login-content {
  .el-button--primary {
    background-color: #E9423A;
    border-color: #E9423A;
  }
  .el-button--primary:focus, .el-button--primary:hover {
    background-color: #E9423A;
    border-color: #E9423A;
  }
  .el-input {
    display: inline-block;
    width: 85%;
    input {
      background: transparent;
      border: 0px;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
    }
  }

  .el-form-item {
    border-bottom: 1px solid #dcdfe6;
    background: #fff;
    border-radius: 5px;
    color: #454545;
  }
}
</style>
