<template>
  <div>
    <div class="login-container">
      <i class="login-img"/>
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        auto-complete="on"
        label-position="left"
        label-width="0px"
        class="card-box login-form">
        <div class="content">
          <div style="padding: 20px">
            <!--<h3 style="color: #2D3C67; margin: 0 auto 20px auto; text-align: center; line-height: 30px; font-size: 23px"></h3>-->
            <h1 class="title">EMS智能设备维修</h1>
            <h1 class="title">信息管理系统</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" name="username" type="text" auto-complete="on" placeholder="请输入用户名" @input.native="changeUser" />
              </el-form-item>
              <el-form-item prop="password">
                <span class="svg-container">
                  <svg-icon icon-class="password"/>
                </span>
                <el-input
                  :type="pwdType"
                  v-model="loginForm.password"
                  name="password"
                  auto-complete="on"
                  placeholder="请输入密码"/>
                <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>-->
                <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>
              <div style="margin-top: 70px">
                <el-button :loading="loading" class="signIn" type="primary" @click.native.prevent="handleLogin">登录</el-button>
                <el-button :loading="loading" class="signIn" type="primary" @click.native.prevent="handleEdit2('form')">注册</el-button>
              </div>
            </div>
          </div>
        </div>
      </el-form>
    </div>
    <el-dialog :visible.sync="dialogVisible" :close-on-click-modal="false" :close-on-press-escape="false" :show-close="false" title="请选择登录角色" width="300px" align="center" style="font-size: 20px">
      <el-table :data="tableData" :show-header="false" style="width: 100%" size="mini" highlight-current-row @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" title="修改密码" center style="min-height: 200px;font-size: 16px" 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 type="primary" size="small" @click="EditPassword('form')">确定</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="dialogFormVisible2" title="注册" center style="min-height: 200px;font-size: 16px" width="400px">
      <el-form ref="form2" :model="form2" :rules="rules2" label-position="left" label-width="110px">
        <!--        <el-form-item label="企业名称:" prop="company" style="display: inline-block;font-family: sans-serif">-->
        <!--          <el-input v-model="form2.company" size="small" style="width:200px"/>-->
        <!--        </el-form-item>-->
        <el-form-item label="账号:" prop="Account" style="display: inline-block;font-family: sans-serif">
          <el-input v-model="form2.Account" 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="form2.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="form2.againPassword" size="small" style="width:200px"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="callOff2()">取消</el-button>
        <el-button type="primary" size="small" @click="EditPassword2('form2')">确定</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="dialogFormVisible3" title="注册" center style="min-height: 200px;font-size: 16px" width="400px">
      <el-form ref="form3" :model="form3" :rules="rules3" label-position="left" label-width="110px">
        <el-form-item label="姓名:" prop="name" style="display: inline-block;font-family: sans-serif">
          <el-input v-model="form3.name" size="small" style="width:200px"/>
        </el-form-item>
        <el-form-item label="账号:" prop="Account" style="display: inline-block;font-family: sans-serif">
          <el-input v-model="form3.Account" 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="form3.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="form3.againPassword" size="small" style="width:200px"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="callOff3()">取消</el-button>
        <el-button type="primary" size="small" @click="EditPassword3('form3')">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { editPassword } from '@/api/login'
import { setToken } from '@/utils/auth'
import Cookie from 'js-cookie'
import { ExecDatabase } from '@/main'

export default {
  name: 'Login',
  data() {
    const Account2 = (rules, value, callback) => {
      // var reg = /^(?=.*[a-zA-Z]+)(?=.*[0-9]+)[a-zA-Z0-9]+$/
      // if (!reg.test(this.form2.Account)) {
      //   callback(new Error('请输入英文字母组合'))
      // } else {
      if (this.form2.Account.length < 5) {
        callback(new Error('账号长度不能小于5位'))
      } else {
        callback()
      }
      // }
    }
    const againPassword2 = (rules, value, callback) => {
      if (this.form2.newPassword !== this.form2.againPassword) {
        callback(new Error('两次输入密码不同'))
      } else {
        callback()
      }
    }
    const againPassword = (rules, value, callback) => {
      if (this.form.newPassword !== this.form.againPassword) {
        callback(new Error('两次输入密码不同'))
      } else {
        callback()
      }
    }
    const Account3 = (rules, value, callback) => {
      var reg = /^(?=.*[a-zA-Z]+)(?=.*[0-9]+)[a-zA-Z0-9]+$/
      if (!reg.test(this.form3.Account)) {
        callback(new Error('请输入英文字母组合'))
      } else {
        if (this.form3.Account.length < 7) {
          callback(new Error('账号长度不能小于7位'))
        } else {
          callback()
        }
      }
    }
    const againPassword3 = (rules, value, callback) => {
      if (this.form3.newPassword !== this.form3.againPassword) {
        callback(new Error('两次输入密码不同'))
      } else {
        callback()
      }
    }
    return {
      dialogVisible: false,
      tableData: [],
      commit: null,
      row: null,
      rules: {
        Account: [
          { required: true, message: '请请输入账号', trigger: 'blur' }],
        beforPassword: [
          { required: true, message: '请输入原始密码', trigger: 'blur' }],
        newPassword: [
          { required: true, message: '请输入新密码', trigger: 'blur' }],
        againPassword: [
          { required: true, message: '请输入确认密码', trigger: 'blur' },
          { required: true, trigger: 'blur', validator: againPassword }]
      },
      rules2: {
        Account: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { required: true, trigger: 'blur', validator: Account2 }],
        againPassword: [
          { required: true, message: '请输入确认密码', trigger: 'blur' },
          { required: true, trigger: 'blur', validator: againPassword2 }],
        company: [
          { required: true, message: '请输入企业名称', trigger: 'blur' }],
        newPassword: [
          { required: true, message: '请输入密码', trigger: 'blur' }]
      },
      rules3: {
        Account: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { required: true, trigger: 'blur', validator: Account3 }],
        againPassword: [
          { required: true, message: '请输入确认密码', trigger: 'blur' },
          { required: true, trigger: 'blur', validator: againPassword3 }],
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }],
        newPassword: [
          { required: true, message: '请输入密码', trigger: 'blur' }]
      },
      list: [],
      form: {
        againPassword: '',
        Account: '',
        beforPassword: '',
        newPassword: ''
      },
      form2: {
        againPassword: '',
        Account: '',
        company: '',
        newPassword: ''
      },
      form3: {
        againPassword: '',
        Account: '',
        name: '',
        newPassword: ''
      },
      dialogFormVisible: false,
      dialogFormVisible2: false,
      dialogFormVisible3: false,
      loginForm: {
        username: '',
        password: '',
        checked: false
      },
      loginRules: {
        // username: [{ required: true, trigger: 'blur', validator: validateUsername }]
      },
      loading: false,
      pwdType: 'password'
    }
  },
  // 页面加载调用获取cookie值
  mounted() {
    this.getCookie()
    this.autoLogin()
  },
  methods: {
    autoLogin() {
      this.loginForm.username = 'demo'
      this.loginForm.password = 'demo'
      setTimeout(() => {
        this.handleLogin()
      }, 1000)
    },
    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
          Cookie.set('type', 1, { expires: 1 })
          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()
        }
      })
    },
    // 标签审核
    handleLogin2() {
      this.row = null
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          Cookie.set('type', 2, { expires: 1 })
          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])
    },
    // 企业注册
    handleEdit2() {
      this.addForm('form2', [this.dialogFormVisible2 = true])
    },
    callOff() {
      this.dialogFormVisible = false
    },
    callOff2() {
      this.dialogFormVisible2 = false
    },
    callOff3() {
      this.dialogFormVisible3 = 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).then(response => {
            if (response.data[0].结果 === '成功') {
              this.dialogFormVisible = false
              this.$message.success('密码修改成功，请重新登陆')
            } else {
              this.$message.error('密码修改失败，若忘记原密码请联系管理员')
            }
          })
        }
      })
    },
    EditPassword2(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$confirm('确认注册, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            if (this.form2.againPassword !== this.form2.newPassword) {
              this.$message.error('两次密码不相同')
            } else {
              var param = []
              // param[0] = ['企业名称', this.form2.company]
              param[0] = ['账号', this.form2.Account]
              param[1] = ['密码', this.form2.newPassword]
              var Data = this.CreateData('12', '基础数据_注册账号', param)
              ExecDatabase(Data).then(response => {
                console.log(response.data)
                if (response.data[0].result === '1') {
                  this.$message.success('注册成功，请重新登录')
                  this.dialogFormVisible2 = false
                } else {
                  this.$message.error('该用户名已存在')
                }
              }).catch(() => {
                this.$message({
                  type: 'info',
                  message: '已取消删除'
                })
              })
            }
          })
        }
      })
    },
    EditPassword3(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$confirm('确认注册, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            if (this.form3.againPassword !== this.form3.newPassword) {
              this.$message.error('两次密码不相同')
            } else {
              var param = []
              param[0] = ['姓名', this.form3.name]
              param[1] = ['账号', this.form3.Account]
              param[2] = ['密码', this.form3.newPassword]
              var Data = this.CreateData('11', '基础数据_专家注册', param)
              ExecDatabase(Data).then(response => {
                if (response.data[0].结果 === '成功') {
                  this.$message.success('注册成功，请重新登录')
                  this.dialogFormVisible3 = false
                } else {
                  this.$message.error('该用户名已存在')
                }
              }).catch(() => {
                this.$message({
                  type: 'info',
                  message: '已取消删除'
                })
              })
            }
          })
        }
      })
    }
  }
}
</script>

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

  .login-container {
    position: fixed;
    height: 100%;
    width:100%;
    background-color: #ebebeb;
    background-image:url("../../../static/login12.jpg");background-repeat:no-repeat; background-size:100% 100%;-moz-background-size:100% 100%;
    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: #2D3C67;
      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;
      margin: 20px auto 20px auto;
      text-align: center;
      font-weight: bold;
      font-family: 微软雅黑Regular＆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: 120px 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: 45%;
      border: none;
      margin-top: 20px;
      border-radius: 5px;
      font-size: 18px;
      color:white;
      background-color: #2D3C67;
    }
    .remeberPassword{
      color: $dark_gray;
    }
    .inner{
      margin:3% 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: 33px!important;
        line-height: 30px;

      }
      .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 #2D3C67;
  }
  .remeberPassword .el-checkbox__input.is-checked .el-checkbox__inner{
    background-color: #2D3C67;
    border-color: #2D3C67;
  }
  .remeberPassword .el-checkbox__input.is-checked+.el-checkbox__label{
    color: #C0C4CC;
  }
  .remeberPassword .el-checkbox__input.is-focus .el-checkbox__inner{
    border-color: #2D3C67;
  }
  .login-container input:-webkit-autofill{
    -webkit-box-shadow:0 0 0 1000px #fff inset !important;
    -webkit-text-fill-color: #29335b !important;
  }
</style>
