<template>
  <CommonRoot>
    <FlexCol :gap="20" :padding="30">
      <Image src="https://mn.wenlvti.net/app_static/xiangyuan/images/bind-banner.jpg" width="100%" height="400" />
      <Button type="primary" @click="submit" :loading="loading">立即绑定</Button>
      <Button @click="doBack">稍后再说</Button>
    </FlexCol>
  </CommonRoot>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { useAppInit } from '@/common/composeabe/AppInit';
import { useLoadQuerys } from '@/common/composeabe/LoadQuerys';
import { toast } from '@/components/dialog/CommonRoot';
import { showError } from '@/common/composeabe/ErrorDisplay';
import { back, redirectTo } from '@/components/utils/PageAction';
import Button from '@/components/basic/Button.vue';
import FlexCol from '@/components/layout/FlexCol.vue';
import CommonRoot from '@/components/dialog/CommonRoot.vue';
import VillageApi from '@/api/inhert/VillageApi';
import Image from '@/components/basic/Image.vue';

/**
 *  账号绑定微信页面
 */

const { querys } = useLoadQuerys({
  fromLogin: false
});
const { init } = useAppInit();
const loading = ref(false);

async function submit() {
  try {
    loading.value = true;
    const res = await uni.login({ provider: 'weixin' });
    await VillageApi.bindWechat({ code: res.code });
    await init();
    toast({ content: '绑定成功' });
    setTimeout(doBack, 800);
  } catch (e) {
    showError(e);
  } finally {
    loading.value = false;
  }
}
function doBack() {
  if (querys.value.fromLogin) {
    redirectTo('/pages/index');
  } else {
    back();
  }
}
</script>
