<template>
  <div id="login">
    <login-detail :info="info" :form="form" @submitDate="submitLogin" :forms="forms" @submitDates="submitRegister"></login-detail>
  </div>
</template>

<script>
import loginDetail from '@/layout/login.vue';
import { mapState, createNamespacedHelpers, mapGetters } from 'vuex';
const { mapActions: mapSite } = createNamespacedHelpers('site');
const { mapActions: mapMarket } = createNamespacedHelpers('market');
const { mapActions: login } = createNamespacedHelpers('login');
export default {
  name: 'login',
  props: {},
  components: { loginDetail },
  data: () => {
    return {
      info: {},
      form: {},
      forms: {},
    };
  },
  created() {
    this.searchSite();
  },
  methods: {
    ...mapSite(['showInfo', 'toGetUser']),
    ...mapMarket({ userFetch: 'fetch', userCreate: 'create', userUpdate: 'update' }),
    ...login({ toLogin: 'login' }),
    // 查询站点信息
    async searchSite() {
      let res = await this.showInfo();
      let object = JSON.parse(JSON.stringify(res.data));
      if (object) {
        this.$set(this, `info`, res.data);
      } else {
        this.$message.error(res.errmsg ? res.errmsg : 'error');
      }
    },
    async submitLogin(form) {
      const res = await this.toLogin({ user: form });
      console.log(res);
      if (res.uid) {
        if (res.role == 1 || res.role == 4) window.location.href = 'http://free.liaoningdoupo.com/platadmin';
        else window.location.href = 'http://free.liaoningdoupo.com/platlive/home.html';
      }
    },
    async submitRegister(forms) {
      let data = this.forms;
      let res = await this.userCreate(data);
      let msg = `注册成功`;
      this.$checkRes(res, msg);
    },
  },
  computed: {
    ...mapState(['user']),
    pageTitle() {
      return `${this.$route.meta.title}`;
    },
    redirect_uri() {
      return `${this.$route.query.redirect_uri}`;
    },
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
};
</script>

<style lang="less" scoped></style>
