<template>
  <div id="login">
    <el-row>
      <el-col :span="24" class="main">
        <div class="w_1200">
          <el-col :span="24" class="info">
            <el-col :span="24" class="title">
              中科在线（长春）-平台用户登录
            </el-col>
            <el-col :span="24" class="form">
              <el-form :model="form" :rules="rules" ref="form" label-width="100px">
                <el-form-item label="账号" prop="phone">
                  <el-input v-model="form.phone" placeholder="请输入账号"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                  <el-input v-model="form.password" placeholder="请输入密码" show-password></el-input>
                </el-form-item>
                <el-form-item label="用户类别" prop="type">
                  <el-radio-group v-model="form.type">
                    <el-radio label="4">个人用户</el-radio>
                    <el-radio label="5">企业用户</el-radio>
                  </el-radio-group>
                </el-form-item>
                <el-col :span="24" class="loginBtn">
                  <el-button type="danger" @click="resetForm()">取消登录</el-button>
                  <el-button type="primary" @click="submitForm('form')">确认登录</el-button>
                </el-col>
              </el-form>
            </el-col>
            <el-col :span="24" class="btn">
              <el-link type="danger" @click="registerBtn">注册账号</el-link>
            </el-col>
          </el-col>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: personal } = createNamespacedHelpers('personal');
const { mapActions: organization } = createNamespacedHelpers('organization');
export default {
  metaInfo() {
    return { title: this.$route.meta.title };
  },
  name: 'login',
  props: {},
  components: {},
  data: function() {
    return {
      form: {},
      rules: {
        phone: [{ required: true, message: '请输入账号', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        type: [{ required: true, message: '请选择用户类别', trigger: 'change' }],
      },
    };
  },
  created() {},
  methods: {
    ...personal(['perLogin']),
    ...organization(['orgLogin']),
    // 确认登录
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          let data = this.form;
          if (data.type == '4') {
            let res = await this.perLogin({ user: data });
            if (this.$checkRes(res)) {
              this.$message.success('登录成功');
              this.$router.push('/userCenter');
            }
          } else if (data.type == '5') {
            data.institution_code = data.phone;
            let res = await this.orgLogin({ user: data });
            if (this.$checkRes(res)) {
              this.$message.success('登录成功');
              this.$router.push('/userCenter');
            }
          }
          // 存储用户类别
          localStorage.setItem('type', data.type);
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    // 取消登录
    resetForm() {
      this.$router.push({ path: '/' });
    },
    // 注册
    registerBtn() {
      this.$router.push({ path: '/register' });
    },
  },
  computed: {
    ...mapState(['user']),
  },
  watch: {},
};
</script>

<style lang="less" scoped>
.main {
  background: url('~@common/src/assets/login.jpg');
  background-repeat: no-repeat;
  background-size: 100% 100%;
  height: 100%;
  .info {
    width: 40%;
    height: 430px;
    background: #ffffff;
    position: relative;
    margin: 21% 30%;
    border-radius: 10px;
    .title {
      text-align: center;
      font-family: fangsong;
      color: #409eff;
      font-size: 28px;
      text-shadow: -1px 0 #fff, 0 1px #fff, 1px 0 #fff, 0 -1px #fff;
      font-weight: 600;
      padding: 30px 0;
    }
    .form {
      padding: 0 25px;
      height: 300px;
      overflow: hidden;
      /deep/.el-form-item__label {
        font-size: 18px;
      }
      .loginBtn {
        text-align: center;
        padding: 20px 0;
      }
    }
    .btn {
      text-align: right;
      padding: 0 10px;
      .el-link {
        font-size: 18px;
      }
    }
  }
}
</style>
