<template>
  <div>
    <div class="login-container">
      <i class="login-img"/>
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        auto-complete="on"
        class="card-box login-form"
        label-position="left"
        label-width="0px">
        <div class="content">
          <div style="padding: 30px">
            <h1 class="title">MES工业互联网平台</h1>
          </div>
          <div class="inner">
            <div class="child">
              <el-form-item prop="username">
                <span class="svg-container svg-container_login">
                  <svg-icon icon-class="username"/>
                </span>
                <el-input
                  v-model="loginForm.username"
                  auto-complete="on"
                  name="username"
                  placeholder="请输入用户名"
                  type="text"
                  @input.native="changeUser"/>
              </el-form-item>
              <el-form-item prop="password">
                <span class="svg-container">
                  <svg-icon icon-class="password"/>
                </span>
                <el-input
                  v-model="loginForm.password"
                  :type="pwdType"
                  auto-complete="on"
                  name="password"
                  placeholder="请输入密码"
                  @keyup.enter.native="handleLogin"/>
                <span class="show-pwd" @click="showPwd"><svg-icon icon-class="eye"/></span>
              </el-form-item>
              <div style="float: left">
                <el-checkbox v-model="loginForm.checked" class="remeberPassword">记住账号</el-checkbox>
                <!--<span style="color: #555c66">|</span>-->
                <!--<el-button type="text" @click="handleEdit('form')">修改密码</el-button>-->
              </div>
              <el-button :loading="loading" class="signIn" type="primary" @click.native.prevent="handleLogin">登录
              </el-button>
            </div>
          </div>
        </div>
      </el-form>
    </div>
    <el-dialog
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :show-close="false"
      :visible.sync="dialogVisible"
      align="center"
      style="font-size: 20px"
      title="请选择登录角色"
      width="300px">
      <el-table
        :data="tableData"
        :show-header="false"
        highlight-current-row
        size="mini"
        style="width: 100%"
        @row-click="rowClick">
        <el-table-column label="角色名称">
          <template slot-scope="scope">
            {{ scope.row.Roles_Function }}
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false;loading=false">取消</el-button>
        <el-button type="primary" @click="submitSelect">确定</el-button>
      </div>
    </el-dialog>
    <el-dialog
      :visible.sync="dialogFormVisible"
      center
      style="min-height: 200px;font-size: 16px"
      title="修改密码"
      width="400px">
      <el-form ref="form" :model="form" :rules="rules" label-position="left" label-width="110px">
        <el-form-item label="工号" prop="Account" style="display: inline-block;font-family: sans-serif">
          <el-input v-model="form.Account" size="small" style="width:200px"/>
        </el-form-item>
        <el-form-item label="原始密码" prop="beforPassword" style="display: inline-block;font-family: Arial ">
          <el-input v-model="form.beforPassword" size="small" style="width:200px"/>
        </el-form-item>
        <el-form-item label="新密码" prop="newPassword" style="display: inline-block;font-family: sans-serif">
          <el-input v-model="form.newPassword" size="small" style="width:200px;"/>
        </el-form-item>
        <el-form-item label="确认密码" prop="againPassword" style="display: inline-block;font-family: sans-serif">
          <el-input v-model="form.againPassword" size="small" style="width:200px"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="callOff()">取消</el-button>
        <el-button size="small" type="primary" @click="EditPassword('form')">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
// import { isvalidUsername } from '@/utils/validate'
import { editPassword } from '@/api/login'
import { setToken } from '@/utils/auth'
import Cookie from 'js-cookie'

export default {
  name: 'Login',
  data() {
    const validateValue = (rules, value, callback) => {
      if (typeof (this.form.againPassword) === 'string' || typeof (this.form.againPassword) === 'string' || typeof (this.form.againPassword) === 'string') {
        callback(new Error('请输入英文字母组合'))
      } else {
        callback()
      }
      if (this.form.newPassword !== this.form.againPassword) {
        callback(new Error('两次输入密码不同'))
      } else {
        callback()
      }
    }
    return {
      dialogVisible: false,
      tableData: [],
      commit: null,
      row: null,
      rules: {
        againPassword: [
          { required: true, message: '请输入确认密码', trigger: 'blur' },
          { required: true, trigger: 'blur', validator: validateValue }],
        Account: [{ required: true, message: '请输入考勤编号', trigger: 'blur' },
          { required: true, trigger: 'blur', validator: validateValue }],
        beforPassword: [{ required: true, message: '请输入原始密码', trigger: 'blur' },
          { required: true, trigger: 'blur', validator: validateValue }],
        newPassword: [{ required: true, message: '请输入新密码', trigger: 'blur' },
          { required: true, trigger: 'blur', validator: validateValue }]
      },
      list: [],
      form: {
        againPassword: '',
        Account: '',
        beforPassword: '',
        newPassword: ''
      },
      dialogFormVisible: false,
      loginForm: {
        username: '',
        password: '',
        checked: false
      },
      loginRules: {
        // username: [{ required: true, trigger: 'blur', validator: validateUsername }]
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      },
      loading: false,
      pwdType: 'password'
    }
  },
  // 页面加载调用获取cookie值
  mounted() {
    this.getCookie()
  },
  methods: {
    showPwd() {
      if (this.pwdType === 'password') {
        this.pwdType = ''
      } else {
        this.pwdType = 'password'
      }
    },
    changeUser() {
      this.loginForm.password = ''
    },
    handleLogin() {
      this.row = null
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('Login', this.loginForm).then(res => {
            if (res.data.length > 1) {
              this.tableData = res.data
              this.commit = res.commit
              this.dialogVisible = true
            } else {
              this.loading = false
              this.$router.push({ path: '/' })
            }
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
        if (this.loginForm.checked === true) { // 传入账号名，密码，和保存天数3个参数
          this.setCookie(this.loginForm.username, this.loginForm.password, this.loginForm.checked)
        } else {
          this.clearCookie()
        }
      })
    },
    // 选择登录角色
    rowClick(row) {
      this.row = row
    },
    // 确认选择登录角色
    submitSelect() {
      if (this.row) {
        setToken(this.row.Roles_number)
        Cookie.set('id', this.row.Personnel_Number, { expires: 1 })
        Cookie.set('name', this.row.Personnel_Name, { expires: 1 })
        Cookie.set('account', this.row.account, { expires: 1 })
        this.commit('SET_TOKEN', this.row.Roles_number)
        this.commit('SET_NAME', this.row.Personnel_Name)
        this.commit('SET_ID', this.row.Personnel_Number)
        this.commit('SET_account', this.row.account)
        this.loading = false
        this.$router.push({ path: '/' })
      } else {
        this.$message({
          type: 'warning',
          message: '请选择登录角色!'
        })
      }
    },
    handleEdit() {
      this.addForm('form', [this.dialogFormVisible = true])
    },
    callOff() {
      this.dialogFormVisible = false
    },
    // 设置cookie
    setCookie(username, password, checked) {
      Cookie.set('username', username, { expires: 7 })
      Cookie.set('password', password, { expires: 7 })
      Cookie.set('checked', checked, { expires: 7 })
    },
    // 读取 cookie
    getCookie() {
      this.loginForm.username = Cookie.get('username')
      this.loginForm.password = Cookie.get('password')
      this.loginForm.checked = Boolean(Cookie.get('checked'))
    },
    // 清除cookie
    clearCookie() {
      Cookie.remove('username')
      Cookie.remove('password')
      Cookie.remove('checked')
    },
    EditPassword(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          editPassword(this.form.Account, this.form.beforPassword, this.form.newPassword, 5).then(response => {
            if (response.data.output[0].返回 === '1') {
              this.dialogFormVisible = false
              this.$message.success('密码修改成功，请重新登陆')
            } else {
              this.$message.error('密码修改失败，若忘记原密码请联系管理员')
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" rel="stylesheet/scss">
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  position: fixed;
  height: 100%;
  width: 100%;
  background-color: #ebebeb;
  background-image: url("../../../static/loginBackground.jpg");
  background-repeat: no-repeat;
  background-size: 100% 100%;
  -moz-background-size: 100% 100%;
  overflow: scroll;

  input:-webkit-autofill {
    -webkit-box-shadow: 0 0 0 1000px #293444 inset !important;
    -webkit-text-fill-color: #fff !important;
  }

  /*.login-img{*/
  /*position: absolute;*/
  /*width: 100%;*/
  /*height: 100%;*/
  /*background: url("../../../static/login.png") no-repeat;*/
  /*}*/
  input {
    background: transparent;
    border: 0;
    -webkit-appearance: none;
    border-radius: 0;
    padding: 12px 5px 12px 15px;
    color: $light_gray;
    height: 47px;
  }

  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: #4D77D6;
    font-size: 20px !important;
    vertical-align: middle;
    width: 30px;
    display: inline-block;

    &_login {
      font-size: 20px;
    }
  }

  .title {
    font-size: 22px;
    line-height: 30px;
    /*color: #2D3C67;*/
    color: #4D77D6;

    margin: 20px auto 20px auto;
    text-align: center;
    font-weight: bold;
  }

  .el-table--mini {
    font-size: 12px;
  }

  .el-form-item__label {
    font-family: "sans-serif";
    text-align: right;
    float: left;
    font-size: 12px;
    color: #606266;
    line-height: 40px;
    padding: 0 12px 0 0;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
  }

  .content {
    width: 450px;
    height: 500px;
    background-image: url("../../../static/loginWindowBackground.png");
    background-repeat: no-repeat;
    background-size: 100% 100%;
    -moz-background-size: 100% 100%;
    margin: 5px auto 0 auto;
  }

  /*.line{*/
  /*width: 70%;*/
  /*margin: 0 auto 25px auto;*/
  /*background-color: #ffffff;*/
  /*height: 1px;*/
  /*}*/
  .login-form {
    position: absolute;
    left: 0;
    right: 0;
    width: 400px;
    padding: 35px 35px 15px 35px;
    margin: 150px auto;
  }

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

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

  .el-input__inner {
    border: none !important;
    font-size: 16px !important;
    color: #29335b !important;
  }

  .el-form-item {
    border: 1px solid #ebf1ee;
    background: #ffffff;
    border-radius: 5px;
    color: #454545;
  }

  /*登录button*/
  .signIn {
    width: 100%;
    border: none;
    margin-top: 20px;
    border-radius: 5px;
    font-size: 18px;
    height: 50px;
    color: white;
    background-color: #4D77D6;
  }

  .remeberPassword {
    color: $dark_gray;
  }

  .inner {
    margin: 10% auto;
    text-align: center;
  }

  .child {
    display: inline-block;
    vertical-align: top;
  }

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

  @media (min-width: 1024px) {
    /*根据屏幕大小确定显示的位置*/
    .login-form {
      position: absolute;
      left: 40%;
      top: 0;
      width: 600px;
    }
    .title {
      font-size: 40px !important;
      line-height: 32px;
    }
    .el-form-item {
      width: 400px;
      height: 50px;
    }
    .content {
      width: 450px;
    }
  }
  @media (min-width: 1281px) {
    .login-form {
      position: absolute;
      left: 60%;
      /*top: 2%;*/
      width: 600px;
    }
  }
}
</style>
<style>
.remeberPassword .el-checkbox__inner:hover {
  border: 1px solid #4D77D6;
}

.remeberPassword .el-checkbox__input.is-checked .el-checkbox__inner {
  background-color: #4D77D6;
  border-color: #4D77D6;
}

.remeberPassword .el-checkbox__input.is-checked + .el-checkbox__label {
  color: #C0C4CC;
}

.remeberPassword .el-checkbox__input.is-focus .el-checkbox__inner {
  border-color: #4D77D6;
}

.login-container input:-webkit-autofill {
  -webkit-box-shadow: 0 0 0 1000px #fff inset !important;
  -webkit-text-fill-color: #000000 !important;
}

</style>
