<template>
  <view class="register-bg">
    <auth-wrap class="register-container" ref="authWrap">
      <view class="form-content" slot="content">
        <RegistForm
            v-if="rules"
            :rules="rules"
            ref="registForm"
            @onSubmitHandle="onFormSubmit"
            :regist-info="model1.userInfo">

        </RegistForm>
      </view>
    </auth-wrap>
  </view>
</template>

<script>

import { fetchUsrApplyDetail, fetchUsrInfo, formCfg, putUsrRegist } from '@/common/api';
import { FORM_MOD, ICON_CFG, MEETING_TYPE, USR_TYPE_LIST } from '@/common/EnumConst';
import AuthWrap from '@/components/AuthComp/index.vue';
import RegistForm from '@/components/RegistForm';
import SingleDropList from '@/components/SingleDropList/index.vue';
import { getUserInfo, setUserInfo } from '@/util';

const staticRules = {
  'userInfo.email': {
    pattern: /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/,
  },
};
export default {
  name: 'login',
  components: {
    RegistForm,
    AuthWrap,
    SingleDropList,
  },
  props: {},
  data() {
    return {
      ICON_CFG,
      USR_TYPE_LIST,
      showEnterprise: false,
      loading: false,
      model1: {
        userInfo: {
          usrName: '',
          conferenceFlag: '1',
        },
      },
      rules: null,
    };
  },
  created() {

  },
  async onLoad(res) {
    const { msg } = await formCfg('meet_form_cfg');
    if (msg) {
      const cfgJson = JSON.parse(msg);
        Object.keys(staticRules).forEach(item => {
          if (!cfgJson[item]) {
            Object.assign(cfgJson[item], staticRules[item]);
          }
        });
        this.rules = cfgJson;
      }
    //如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则。
    this.$refs.authWrap.reloadPage();
    this.$refs.registForm?.setFormRules();
    if (res.mode === FORM_MOD.modify) {
      await this.getUsrMeetingInfo(res.apply);
    }
  },
  onReady() {

  },
  methods: {
    hideKeyboard() {
      uni.hideKeyboard();
    },
    async onFormSubmit(userInfo) {
      const { code } = await putUsrRegist(userInfo, MEETING_TYPE.meeting.value);
      if (code !== 200) {
        return;
      }
      uni.showToast({
        mask: true,
        title: '报名完成',
        icon: 'success',
        success: async () => {
          let pages = getCurrentPages();  //获取当前页面
          let beforePage = pages[pages.length - 2];  //获取上一个页面的实例
          //1、调用上一个页面onLoad
          beforePage.onLoad();
          uni.navigateBack({
            delta: 1,
          });
        },
      });
    },
    async getUsrMeetingInfo(id) {
      const { data } = await fetchUsrApplyDetail(id);
      if (!data) {
        return;
      }
      this.model1.userInfo = data;
    },
  },
};
</script>
<style lang="scss" src="./index.scss" />;
