<template>
  <div class="login-container" :style="{background: 'url(' + bg2 + ')' }">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left">
      <div class="title-container">
        <span class="title">印刷MIS系统</span>
      </div>
<!--   <div style="margin-bottom: 40px;margin-left: 80px">-->
<!--     <el-dropdown @command="handleCommand">-->
<!--  <span class="el-dropdown-link">-->
<!--    {{itemNameCheck}}<i class="el-icon-arrow-down el-icon&#45;&#45;right"></i>-->
<!--  </span>-->
<!--       <el-dropdown-menu slot="dropdown">-->
<!--         <el-dropdown-item-->
<!--           v-for="(order,index) in ERPSupplierList"-->
<!--           :key="order.index"-->
<!--           :command="order"-->
<!--           @click="clickSupplier(order.id,order.itemName)"-->
<!--         >{{order.itemName}}-->
<!--         </el-dropdown-item>-->

<!--       </el-dropdown-menu>-->
<!--     </el-dropdown>-->
<!--   </div>-->

      <el-form-item prop="loginName">
        <span class="svg-container">
          <svg-icon icon-class="user"/>
        </span>
        <el-input
          class="input-login"
          ref="loginName"
          v-model="loginForm.loginName"
          placeholder="账号"
          name="loginName"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password"/>
        </span>
        <el-input
          class="input-login"
          :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>

      <el-button
        :loading="loading"
        type="primary"
        style="width:100%;margin-bottom:30px;"
        @click.native.prevent="handleLogin">登 录
      </el-button>
    </el-form>
    <div class="tips-foter">
    </div>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
// import Bg2 from '@/assets/background.svg'
import Bg2 from '@/assets/background.jpg'
import LogoImg from '@/assets/logo.png'
import { loginSupplierList } from '@/api/login/login.js'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入账号'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 4) {
        callback(new Error('请输入不少于4位数的密码'))
      } else {
        callback()
      }
    }
    return {
      ERPSupplierList: [],
      itemNameCheck: '',
      idCheck: '',
      bg2: Bg2,
      // logoImg: LogoImg,
      loginForm: {
        loginName: '',
        password: ''
      },
      loginRules: {
        loginName: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  mounted() {
    this.getInfo()
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    getInfo() {
      const _that = this
      loginSupplierList().then(response => {
        const data = response.data
        // console.log(data);
        if (data.length > 0) {
          _that.ERPSupplierList = response.data
          _that.itemNameCheck = data[0].itemName
          _that.idCheck = data[0].id
        }

        // //  组合表单数据
      })
    },
    handleCommand(order) {
      const _that = this
      _that.idCheck = order.id
      _that.itemNameCheck = order.itemName
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        // console.log(valid);
        if (valid) {
          this.loading = true
          const data = {
            'user': JSON.stringify(this.loginForm),
            'supplierId': this.idCheck
          }
          // console.log(data)
          this.$store.dispatch('login', data).then(() => {
            this.$router.push({ path: '/' }, () => {})
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>
<style>
  .input-login > .el-input__inner {
    padding-left: 40px !important;
  }
</style>
<style lang="scss" scoped>
  $bg: #f6f6f6;
  $dark_gray: rgba(0, 0, 0, 0.25);
  $light_gray: #eee;

  .login-container {
    width: 100%;
    min-height: 100%;
    background-size: 100%;
    padding: 110px 0 144px;
    box-sizing: border-box;

    .login-form {
      width: 350px;
      max-width: 100%;
      padding: 40px 35px 0;
      margin: 5% 30% 0 auto;
      overflow: hidden;
      position: relative;
      background: #fefeff;
    }

    .tips-foter {
      width: 100%;
      height: 30px;
      line-height: 30px;
      font-size: 14px;
      color: #909399;
      position: fixed;
      bottom: 0px;
      text-align: center;
      margin-bottom: 50px;

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

    .svg-container {
      padding: 6px 5px 6px 15px;
      color: $dark_gray;
      vertical-align: middle;
      width: 30px;
      display: inline-block;
      position: absolute;
      left: 0px;
      top: -5px;
      z-index: 999;
    }

    .title-container {
      width: 100%;
      height: auto;
      line-height: 60px;
      position: relative;
      margin-bottom: 10px;
      text-align: center;

      .title {
        font-size: 26px;
        color: #606266;
        font-weight: bold;
        display: inline-block;

      }

      img {
        display: inline-block;
        width: 60px;
        height: 60px;
        position: absolute;
        top: 0px;
        left: 80px;
      }
    }

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

    .el-dropdown-link {
      cursor: pointer;
      color: #409EFF;
    }

    .el-icon-arrow-down {
      font-size: 16px;
    }
  }
</style>
