<template>
  <div>
    <div class="login-wrap">
			<div class="main-login">
				<div class="login-box">
					<div class="login-layout">
						<div class="logo"></div>
						<!-- <form class="layui-form login-form" action=""> -->
              <el-form autoComplete="on"
              class="layui-form login-form"
               :model="loginForm"
               :rules="loginRules"
               ref="loginForm"
               label-position="left">
							<h3 class="formTitle">
								<img src="http://source.gzqlyx.com/lyjlogin/img/login-logo-bg-icon.png">林业发布后台管理系统</h3>
							<!-- <div class="layui-form-item">
								<i class="layui-icon">&#xe66f;</i>
								<input type="text" name="account" required lay-verify="userName" placeholder="请输入账号" autocomplete="off" class="login-box-input layui-input">                
							</div> -->
              <el-form-item prop="username">
                <el-input name="username"
                          type="text"
                          v-model="loginForm.username"
                          autoComplete="on"
                          placeholder="请输入用户名">
                <span slot="prefix">
                  <svg-icon icon-class="login-name" class="color-main login-name"></svg-icon>
                </span>
                </el-input>
              </el-form-item>
              <el-form-item prop="password">
                <el-input name="password"
                          :type="pwdType"
                          @keyup.enter.native="handleLogin"
                          v-model="loginForm.password"
                          autoComplete="on"
                          placeholder="请输入密码">
                <span slot="prefix">
                  <svg-icon icon-class="login-pass" class="color-main"></svg-icon>
                </span>
                <!-- <span slot="suffix" @click="showPwd">
                  <svg-icon icon-class="eye" class="color-main"></svg-icon>
                </span> -->
                </el-input>
              </el-form-item>
              <el-form-item style="margin-bottom: 0;box-shadow: none;">
                <el-button class="submit-btn" style="" type="primary" :loading="loading" @click.native.prevent="handleLogin">
                  登录
                </el-button>
              </el-form-item>
							<!-- <div class="layui-form-item">
								<i class="layui-icon">&#xe673;</i>
								<input type="password" name="pwd" required lay-verify="pwd" placeholder="请输入密码" autocomplete="off" class="login-box-input layui-input">
							</div> -->
							<!-- <button class="layui-btn layui-submit login-btn-submit" lay-submit lay-filter="loginForm">登录</button> -->
					</el-form>
					</div>
				</div>
			</div>
		</div>
    <!-- <el-card class="login-form-layout">
      <el-form autoComplete="on"
               :model="loginForm"
               :rules="loginRules"
               ref="loginForm"
               label-position="left">
        <div style="text-align: center">
          <svg-icon icon-class="login-mall" style="width: 56px;height: 56px;color: #409EFF"></svg-icon>
        </div>
        <h2 class="login-title color-main">林业发布后台管理系统</h2>
        <el-form-item prop="username">
          <el-input name="username"
                    type="text"
                    v-model="loginForm.username"
                    autoComplete="on"
                    placeholder="请输入用户名">
          <span slot="prefix">
            <svg-icon icon-class="user" class="color-main"></svg-icon>
          </span>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input name="password"
                    :type="pwdType"
                    @keyup.enter.native="handleLogin"
                    v-model="loginForm.password"
                    autoComplete="on"
                    placeholder="请输入密码">
          <span slot="prefix">
            <svg-icon icon-class="password" class="color-main"></svg-icon>
          </span>
            <span slot="suffix" @click="showPwd">
            <svg-icon icon-class="eye" class="color-main"></svg-icon>
          </span>
          </el-input>
        </el-form-item>
        <el-form-item style="margin-bottom: 60px;text-align: center">
          <el-button style="width: 45%" type="primary" :loading="loading" @click.native.prevent="handleLogin">
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <img :src="login_center_bg" class="login-center-layout"> -->
  </div>
</template>

<script>
  import {isvalidUsername} from '@/utils/validate';
  import {setSupport,getSupport,setCookie,getCookie} from '@/utils/support';
  import login_center_bg from '@/assets/images/login_center_bg.png'

  export default {
    name: 'login',
    data() {
      const validateUsername = (rule, value, callback) => {
        if (!isvalidUsername(value)) {
          callback(new Error('请输入正确的用户名'))
        } else {
          callback()
        }
      };
      const validatePass = (rule, value, callback) => {
        if (value.length < 3) {
          callback(new Error('密码不能小于3位'))
        } else {
          callback()
        }
      };
      return {
        loginForm: {
          username: '',
          password: '',
        },
        loginRules: {
          username: [{required: true, trigger: 'blur', validator: validateUsername}],
          password: [{required: true, trigger: 'blur', validator: validatePass}]
        },
        loading: false,
        pwdType: 'password',
        login_center_bg,
        dialogVisible:false,
        supportDialogVisible:false
      }
    },
    created() {
      this.loginForm.username = getCookie("username");
      this.loginForm.password = getCookie("password");
      if(this.loginForm.username === undefined||this.loginForm.username==null||this.loginForm.username===''){
        this.loginForm.username = 'admin';
      }
      if(this.loginForm.password === undefined||this.loginForm.password==null){
        this.loginForm.password = '';
      }
    },
    methods: {
      showPwd() {
        if (this.pwdType === 'password') {
          this.pwdType = ''
        } else {
          this.pwdType = 'password'
        }
      },
      handleLogin() {
        this.$refs.loginForm.validate(valid => {
          if (valid) {
            // let isSupport = getSupport();
            // if(isSupport===undefined||isSupport==null){
            //   this.dialogVisible =true;
            //   return;
            // }
            this.loading = true;
            this.$store.dispatch('Login', this.loginForm).then(() => {
              this.loading = false;
              setCookie("username",this.loginForm.username,15);
              setCookie("password",this.loginForm.password,15);
              this.$router.push({path: '/'})
            }).catch(() => {
              this.loading = false
            })
          } else {
            console.log('参数验证不合法！');
            return false
          }
        })
      },
      dialogConfirm(){
        this.dialogVisible =false;
        setSupport(true);
      },
      dialogCancel(){
        this.dialogVisible = false;
        setSupport(false);
      }
    }
  }
</script>

<style>
/* @import url('http://source.gzqlyx.com/lyjlogin/css/style.css'); */
@import url('./login.css');


  /* .login-form-layout {
    position: absolute;
    left: 0;
    right: 0;
    width: 360px;
    margin: 140px auto;
    border-top: 10px solid #409EFF;
  }

  .login-title {
    text-align: center;
  }

  .login-center-layout {
    background: #409EFF;
    width: auto;
    height: auto;
    max-width: 100%;
    max-height: 100%;
    margin-top: 200px;
  } */
</style>
