<template>
  <div class="login-container">
    <el-row>
      <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
        <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12" class="flex1">
          <el-image :src="src" style="width: 299px; height: 211px" />
        </el-col>
        <el-col :xs="24" :sm="24" :md="12" :lg="10" :xl="10" class="flex1">
          <div>
            <el-form
              ref="loginForm"
              :model="loginForm"
              :rules="loginRules"
              class="login-form"
              auto-complete="on"
              label-position="left"
            >

              <div class="title-container">
                <h3 class="title">登录</h3>
              </div>
              <div class="checkbox flex1">
                <div class="business " :class="checked==0 ? 'bnesper' : 'actbnes' " @click="checked=0">
                  <p class="asp">业务人员</p>
                </div>
                <div class="business " :class="checked==1 ? 'applyper' : 'actapp' " @click="checked=1">
                  <p class="asp">供应商</p>
                </div>
              </div>
              <el-form-item prop="username">
                <span class="svg-container"> 账户 </span>
                <el-input
                  ref="username"
                  v-model="loginForm.username"
                  placeholder="请输入账户名称"
                  name="username"
                  type="text"
                  tabindex="1"
                  auto-complete="on"
                />
              </el-form-item>

              <el-form-item prop="password">
                <span class="svg-container">
                  密码
                </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>
              </el-form-item>

              <div class="registerbtn flex2">
                <div class="forgect" @click.stop="registerNum">
                  <p class="nes">新用户注册</p>
                </div>
                <div class="forgect" @click="open">
                  <p class="asp">忘记密码？</p>
                </div>
              </div>
              <el-button :loading="loading" type="primary" class="loginbtn" @click.native.prevent="handleLogin">登录
              </el-button>
            </el-form>
          </div>
        </el-col>
      </el-col>

    </el-row>
  </div>
</template>

<script>
import logo from '/src/assets/img/logo.png'
export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (value.length === 0) {
        callback(new Error('请输入账户名称'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('请输入大于6位数密码'))
      } else {
        callback()
      }
    }
    const validateloginType = (rule, value, callback) => {

    }
    return {
      loginForm: {
        username: 'admin',
        password: '123456',
        loginType: 0
      },
      loginRules: {
        username: [{
          required: true,
          trigger: 'blur',
          validator: validateUsername
        }],
        password: [{
          required: true,
          trigger: 'blur',
          validator: validatePassword
        }],
        loginType: [{
          required: true,
          trigger: 'blur',
          validator: validateloginType
        }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      src: logo,
      checked: 0
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  mounted() {
    if (this.$route.query.checked) {
      this.checked = this.$route.query.checked
      console.log(this.checked)
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    registerNum() {
      this.$router.push({
        path: '/register',
        query: {
          checked: this.checked
        }
      })
    },
    open() {
      const h = this.$createElement
      this.$msgbox({
        title: '',
        message: h('p', null, [
          h('span', null, '请电话联系管理员，进行密码确定 ')
        ]),
        showCancelButton: true,
        showConfirmButton: true,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        beforeClose: (action, instance, done) => {
          if (action === 'confirm') {
            done()
          } else {
            done()
          }
        }
      }).then(action => {
        this.$message({
          type: 'info',
          message: 'action: ' + action
        })
      })
    },
    handleLogin() {
      this.loginForm.loginType = this.checked + 1
      this.$refs.loginForm.validate(valid => {
        if (!valid) {
          this.$message.error('请检查表单')
          return
        }
        this.$store.dispatch('user/ActionLogin', this.loginForm)
      })
    }
    //  handleLogin() {
    //   that.loginForm.loginType = that.checked + 1
    //   console.log(this.loginForm, 88)
    //   this.$refs.loginForm.validate(valid => {
    //     if (valid) {
    //       this.loading = true
    //       this.$store.dispatch('user/login', that.loginForm).then((res) => {
    //         // console.log(res.data)
    //         this.$router.push({
    //           path: '/'
    //         })
    //         this.loading = false
    //         this.$store.commit('user/getAdmin', that.checked)
    //       }).catch(() => {
    //         this.loading = false
    //       })
    //     } else {
    //       console.log('error submit!!')
    //       return false
    //     }
    //   })
  }
}
</script>

<style lang="scss" >
  /* 修复input 背景不协调 和光标变色 */
  /* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

  $bg:#F6F6F6;
  $light_gray:#BEBDBB;
  $cursor: #BEBDBB;

  @supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
    .login-container .el-input input {
      color: $cursor;
    }
  }

  /* reset element-ui css */
  .login-container {
    background: url("../../assets/img/loginbg.png") no-repeat;
    background-size: 100%;

    .el-input {
      display: inline-block;
      height: 47px;
      width: 85%;
      background-color: #F6F6F6;

      input {
        background: #F6F6F6;
        border: 0px;
        -webkit-appearance: none;
        border-radius: 0px;
        padding: 12px 5px 12px 15px;
        color: $light_gray;
        height: 47px;
        caret-color: $cursor;

        &:-webkit-autofill {
          box-shadow: 0 0 0px 1000px $bg inset !important;
          -webkit-text-fill-color: $cursor !important;
        }
      }
    }

    .el-form-item {
      border: 1px solid rgba(255, 255, 255, 0.1);
      background: #F6F6F6;
      border-radius: 5px;
      color: #454545;
    }
  }
</style>

<style lang="scss" scoped>
  $bg:#2d3a4b;
  $dark_gray:#F6F6F6;
  $light_gray:#eee;

  .login-container {
    height: 100%;
    width: 100%;
    // background-color: $bg;
    // overflow: hidden;

    .login-form {
      position: relative;
      width: 470px;
      max-width: 100%;
      padding: 0 35px 0;
      margin: 0 auto;
      // overflow: hidden;
    }

    .tips {
      font-size: 14px;
      color: #fff;
      margin-bottom: 10px;

      span {
        &:first-of-type {
          margin-right: 16px;
        }
      }
    }

    .svg-container {
      padding: 6px 5px 6px 15px;
      color: #1F242A;
      vertical-align: middle;
      // width: 30px;
      display: inline-block;
    }

    .title-container {
      position: relative;

      .title {
        font-size: 48px;
        color: #1F242A;
        margin: 0px auto 40px auto;
        // text-align: center;
        font-weight: bold;
      }
    }

    .show-pwd {
      position: absolute;
      right: 10px;
      top: 7px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
      user-select: none;
    }
  }

  .el-row {
    height: 100%;
  }

  .el-col {
    height: 100%;
  }

  .business {
    width: 305px;
    height: 90px;
    display: flex;
    align-items: center;
    justify-content: center;
    text-align: center;
    // background-color: #D8AB5A;
    border-raotherdius: 20rpx;
    color: #FFFFFF;
    margin-top: 57rpx;
  }

  .business {
    width: 305rpx;
    height: 90rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    text-align: center;
    // background-color: #D8AB5A;
    border-raotherdius: 20rpx;
    color: #FFFFFF;
    margin-top: 57rpx;
    // line-height: 110rpx;
  }

  .bnesper {
    background-size: 100%;
    background-image: url("../../assets/img/loginbtn4.png");
    background-repeat: no-repeat;
    padding-top: 10rpx;
    color: #FFF1D8;

    .asp {
      padding-bottom: 5px;
    }
  }

  .applyper {
    background-image: url("../../assets/img/loginbt1.png");
    background-repeat: no-repeat;
    background-size: 100%;
    padding-top: 10rpx;
    color: #FFF1D8;

    .asp {
      padding-bottom: 5px;
    }
  }

  .actapp {
    background-image: url("../../assets/img/loginbtn3.png");
    background-repeat: no-repeat;
    background-size: 100%;
    color: #D05C39;
    margin-bottom: 80rpx;

    .asp {
      padding-bottom: 20px;
    }

    // line-height: 180rpx;
  }

  .actbnes {
    background-image: url("../../assets/img/loginbt2.png");
    background-repeat: no-repeat;
    background-size: 100%;
    color: #D05C39;

    .asp {
      padding-bottom: 20px;
    }
  }

  .loginbtn {
    width: 100%;
    margin-bottom: 30px;
    background: linear-gradient(to right, #FFE1AD, #D07539);
    border: none;

    height: 56px;
    border-radius: 10px;

    font-size: 24px;

  }

  .registerbtn {
    margin-top: -20px;
    margin-bottom: 30px;
    font-size: 16px;

    .forgect {
      cursor: pointer;
    }

    .nes {
      color: #D05C39;
    }

  }
</style>
