<template>
  <CommonRoot>
    <FlexCol :gap="20" :padding="30">
      <Result 
        v-if="authStore.isLogged" 
        status="success"
        title="您已经是志愿者"
        desc="赶快去采编村社文化资源信息吧"
      >
        <Height :size="20" />
        <Button type="primary" @click="navTo('/pages/index')">返回首页</Button>
      </Result>
      <FlexCol v-else center :height="400">
        
        <Icon icon="smile-filling" color="primary" :size="156" />
        <Height :height="20" />
        <Text :fontSize="26" color="primary" text="欢迎注册，加入志愿者队伍" />
        <Height :size="40" />

        <!-- #ifdef MP-WEIXIN -->
        <Button type="primary" block text="微信登录" @click="loginWechat" />
        <Height :size="20" />
        <!-- #endif -->
      </FlexCol>
    </FlexCol>
  </CommonRoot>
</template>

<script setup lang="ts">
import Button from '@/components/basic/Button.vue';
import Result from '@/components/feedback/Result.vue';
import FlexCol from '@/components/layout/FlexCol.vue';
import Height from '@/components/layout/space/Height.vue';
import Icon from '@/components/basic/Icon.vue';
import Text from '@/components/basic/Text.vue';
import CommonRoot from '@/components/dialog/CommonRoot.vue';
import { navTo } from '@/components/utils/PageAction';
import { useAuthStore } from '@/store/auth';
import { closeToast, toast } from '@/components/dialog/CommonRoot';
import { showError } from '@/common/composeabe/ErrorDisplay';

const authStore = useAuthStore();

function loginWechat() {
  toast({
    type: 'loading',  
    content: '登录中...',
  })

  Promise.all([
    uni.login({ provider: 'weixin' }),
    uni.getUserProfile({ desc: '用于完善会员资料' }),
  ])
    .then((res) => {
      authStore.loginWechart(res[0].code, res[1]).then(() => {
        toast({
          type: 'success',  
          content: '登录成功',
        });
        //collectStore.loadCollectableModules();
        //setTimeout(() => redirectToIndex(), 200);
      }).catch(showError);
    })
    .catch(showError)
    .finally(() => closeToast());
}
</script>
