<!-- 短信注册 - smsRegister  -->
<template>
  <view>
    <!-- 标题栏 -->
    <view class="head-box ss-m-b-60">
      <view class="head-title ss-m-b-20">{{$t('sheep.auth.register')}}</view>
      <view class="head-subtitle">{{$t('sheep.auth.tipsRegister')}}</view>
    </view>

    <!-- 表单项 -->
    <uni-forms
      ref="smsRegisterRef"
      v-model="state.model"
      :rules="state.rules"
      validateTrigger="bind"
      labelWidth="140"
      labelAlign="center"
    >
	
	<uni-forms-item name="invite_code" :label="$t('sheep.auth.inviteCode')">
	  <uni-easyinput
	    :placeholder="$t('sheep.auth.enterInviteCode')"
	    v-model="state.model.invite_code"
	    :inputBorder="false"
	    type="text"
	    maxlength="6"
	  ></uni-easyinput>
	</uni-forms-item>
      <uni-forms-item name="mobile" :label="$t('sheep.auth.mobile')">
		  <view class="" style="display: inline-block;width: 30px;float: left;margin-top: 8px;color: #333;">
			  <text>+63</text>
		  </view>
        <uni-easyinput
          :placeholder="$t('sheep.auth.enterMobile')"
          v-model="state.model.mobile"
          type="number"
		  style="float: left;width:86%;"
          :inputBorder="false"
        >
          <template v-slot:right>
            <button
              class="ss-reset-button code-btn code-btn-start"
              :disabled="state.isMobileEnd"
              :class="{ 'code-btn-end': state.isMobileEnd }"
              @tap="getSmsCode('smsRegister', state.model.mobile)"
            >
              {{ getSmsTimer('smsRegister') }}
            </button>
          </template>
        </uni-easyinput>
      </uni-forms-item>

      <uni-forms-item name="code" :label="$t('sheep.auth.code')">
        <uni-easyinput
          :placeholder="$t('sheep.auth.enterCode')"
          v-model="state.model.code"
          :inputBorder="false"
          type="number"
          maxlength="4"
        ></uni-easyinput>
      </uni-forms-item>

      <uni-forms-item name="password" :label="$t('sheep.auth.password')">
        <uni-easyinput
          type="password"
          :placeholder="$t('sheep.auth.enterPassword')"
          v-model="state.model.password"
          :inputBorder="false"
        >
          <template v-slot:right>
            <button class="ss-reset-button login-btn-start" @tap="smsRegisterSubmit"> {{$t('sheep.auth.register')}} </button>
          </template>
        </uni-easyinput>
      </uni-forms-item>
    </uni-forms>

    <button class="ss-reset-button type-btn" @tap="showAuthModal('accountLogin')">
      {{$t('sheep.auth.backLogin')}}
    </button>
  </view>
</template>

<script setup>
  import { computed, ref, reactive, unref } from 'vue';
  import sheep from '@/sheep';
  import { code, mobile, password } from '@/sheep/validate/form';
  import { showAuthModal, closeAuthModal, getSmsCode, getSmsTimer } from '@/sheep/hooks/useModal';

  const props = defineProps({
    agreeStatus: {
      type: Boolean,
      default: false,
    },
  });

  const smsRegisterRef = ref(null);
  
  const isLogin = computed(() => sheep.$store('user').isLogin);

  const emits = defineEmits(['onConfirm']);

  // 数据
  const state = reactive({
    isMobileEnd: false, // 手机号输入完毕
    model: {
		invite_code: '',
      mobile: '', // 手机号
      code: '', // 验证码
      password: '', // 密码
    },
    rules: {
      code,
      mobile,
      password,
    },
  });

  // 3.短信注册
  async function smsRegisterSubmit() {
    const validate = await unref(smsRegisterRef)
      .validate()
      .catch((error) => {
        console.log('error: ', error);
      });
    if (!validate) return;

    if (!props.agreeStatus) {
      emits('onConfirm',true);
      sheep.$helper.toast($t('sheep.auth.agree'));
      return;
    }

    const { code } = await sheep.$api.user.smsRegister({
      ...state.model,
      shareInfo: uni.getStorageSync('shareLog') || {},
    });
    if (code === 1) {
      closeAuthModal();
    }
  }
</script>

<style lang="scss" scoped>
  @import '../index.scss';
</style>
