<template>
  <div id="login">
    <el-row>
      <el-col :span="24" class="main">
        <div class="w_1200">
          <el-col :span="24" class="one">
            <el-col :span="24" class="txt">商城管理登陆</el-col>
            <el-col :span="24" class="form">
              <el-form :model="form" :rules="rules" ref="form">
                <el-form-item label="" prop="account">
                  <el-input placeholder="账号" v-model="form.account" @keyup.enter.native="onSubmit('form')">
                    <i slot="prefix" class="el-input__icon el-icon-user-solid"></i>
                  </el-input>
                </el-form-item>
                <el-form-item label="" prop="password">
                  <el-input placeholder="密码" v-model="form.password" type="password" auto-complete="off" @keyup.enter.native="onSubmit('form')">
                    <i slot="prefix" class="el-input__icon el-icon-lock"></i>
                  </el-input>
                </el-form-item>
                <el-form-item class="btn">
                  <el-button type="primary" @click="onSubmit('form')">登录</el-button>
                </el-form-item>
              </el-form>
            </el-col>
            <el-col :span="24" class="other">
              <el-button type="text" style="color: blue; font-size: 14px" @click="toReset()">忘记密码？</el-button>
            </el-col>
          </el-col>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: maU } = createNamespacedHelpers('admin');
const { mapActions: admins } = createNamespacedHelpers('admins');
const _ = require('lodash');
export default {
  name: 'login',
  props: {},
  components: {},
  data: function () {
    return {
      form: {},
      rules: {
        account: [{ required: true, message: '用户名不能为空', trigger: 'blur' }],
        password: [{ required: true, message: '密码不能为空', trigger: 'blur' }],
      },
    };
  },
  created() {},
  methods: {
    ...maU(['login']),
    ...admins(['email']),
    onSubmit(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          const res = await this.login(this.form);
          if (!res) {
            this.$message({ type: `error`, message: `登陆失败` });
          } else {
            if (res.errcode !== 0) {
              this.$message({ type: 'error', message: `${res.errmsg}` });
            } else {
              this.$router.push({ path: '/' });
            }
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    async toReset() {
      if (this.form.account == undefined || this.form.account == '') {
        this.$message.error('请输入账号');
      } else {
        this.$confirm('忘记密码，是否确认重置密码?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(async () => {
          const res = await this.email({ account: this.form.account });
          if (this.$checkRes(res)) {
            this.$message({ type: `success`, message: `重置密码成功` });
          }
        });
      }
    },
  },
  computed: {
    ...mapState(['user']),
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
  watch: {
    test: {
      deep: true,
      immediate: true,
      handler(val) {},
    },
  },
};
</script>

<style lang="less" scoped>
.main {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100vh;
  overflow: hidden;
  background: url('~@/assets/login-background.jpg');
  background-repeat: no-repeat;
  background-size: 100%;
  .one {
    background-color: #ffffff;
    border-radius: 5px;
    padding: 10px 20px;
    margin: 70% 0 0 0;
    .txt {
      text-align: center;
      font-size: 20px;
      font-weight: bold;
      margin: 15px 0;
    }
    .form {
      .btn {
        text-align: center;
        .el-button {
          width: 100%;
        }
      }
    }
    .other {
      text-align: center;
    }
  }
}
</style>
