<template>
  <FlexCol :padding="[40,30,50,30]">
    <HomeLargeTitle title="我的" />
    <Touchable 
      direction="row"
      align="center"
      justify="space-between"
      touchable 
      :gap="25"
      :padding="[0,30]"
      @click="goUserProfile"
    >
      <FlexRow align="center" :gap="25">
        <Image 
          :src="userInfo?.avatar || UserHead"
          :defaultImage="UserHead"
          :failedImage="UserHead"
          mode="aspectFill" 
          class="avatar" 
          width="75rpx"
          height="75rpx" 
          :showFailed="false"
          round
        />
        <H4 v-if="userInfo">{{ userInfo.nickname }}</H4>
        <H4 v-else>欢迎登录</H4>
      </FlexRow>
      <Icon icon="arrow-right" size="30" />
    </Touchable>
    <ProvideVar :vars="{
      GridItemIconSize: 90,
      GridItemBackgroundColor: 'transparent',
      GridItemPaddingHorizontal: 0,
    }">
      <Grid :borderGrid="false" :mainAxisCount="4">
        <GridItem title="乡源等级" icon="https://xy.wenlvti.net/app_static/images/mine/IconLevel.png" touchable @click="toast('暂未开放，敬请期待')" />
        <GridItem title="我的成就" icon="https://xy.wenlvti.net/app_static/images/mine/IconArchive.png" touchable @click="navTo('/pages/home/village/task/archivement')" />
        <GridItem title="任务中心" icon="https://xy.wenlvti.net/app_static/images/mine/IconTask.png" touchable @click="navTo('/pages/home/village/task/index')" />
        <GridItem title="兑换商城" icon="https://xy.wenlvti.net/app_static/images/mine/IconShop.png" touchable @click="toast('暂未开放，敬请期待')" />
      </Grid>
    </ProvideVar>
    <ProvideVar :vars="{
      CellBackground: 'background.tertiary',
      CellBottomBorder: false,
    }">
      <CellGroup round>
        <Cell v-if="userInfo" icon="https://xy.wenlvti.net/app_static/images/mine/IconMyArticle.png" title="我的投稿" showArrow touchable @click="navTo('/pages/dig/forms/submits', { 
          villageVolunteerId: volunteerInfoLoader.content.value?.id || 0 
        })" />
        <Cell icon="https://xy.wenlvti.net/app_static/images/mine/IconMyRecord.png" title="福泽记录" showArrow touchable @click="requireLogin(() => navTo('/pages/home/village/bless/my-orders'), '登录后查看我的福泽记录哦')" />
        <Cell icon="https://xy.wenlvti.net/app_static/images/mine/IconMyReward.png" title="兑换记录" showArrow touchable @click="toast('暂未开放，敬请期待')" />
        <Cell icon="https://xy.wenlvti.net/app_static/images/mine/IconAbout.png" title="关于我们" showArrow touchable @click="navTo('/pages/home/about/about')" />
        <button open-type="contact" class="remove-button-style">
          <Cell icon="https://xy.wenlvti.net/app_static/images/mine/IconContract.png" title="联系客服" showArrow touchable />
        </button>
        <Cell v-if="userInfo" icon="https://xy.wenlvti.net/app_static/images/mine/IconQuit.png" title="退出登录" showArrow touchable @click="doLogout" />
      </CellGroup>
    </ProvideVar>
    <DebugButton />
  </FlexCol>
</template>

<script setup lang="ts">
import { computed } from 'vue';
import { navTo } from '@/components/utils/PageAction';
import { confirm } from '@/components/dialog/CommonRoot';
import { useAuthStore } from '@/store/auth';
import { useRequireLogin } from '@/common/composeabe/RequireLogin';
import { useSimpleDataLoader } from '@/components/composeabe/loader/SimpleDataLoader';
import { toast } from '@/components/utils/DialogAction';
import CellGroup from '@/components/basic/CellGroup.vue';
import Cell from '@/components/basic/Cell.vue';
import Image from '@/components/basic/Image.vue';
import H4 from '@/components/typography/H4.vue';
import Height from '@/components/layout/space/Height.vue';
import Touchable from '@/components/feedback/Touchable.vue';
import FlexCol from '@/components/layout/FlexCol.vue';
import AppCofig from '@/common/config/AppCofig';
import VillageApi from '@/api/inhert/VillageApi';
import DebugButton from './debug/DebugButton.vue';
import FlexRow from '@/components/layout/FlexRow.vue';
import Icon from '@/components/basic/Icon.vue';
import HomeLargeTitle from '@/common/components/parts/HomeLargeTitle.vue';
import ProvideVar from '@/components/theme/ProvideVar.vue';
import Grid from '@/components/layout/grid/Grid.vue';
import GridItem from '@/components/layout/grid/GridItem.vue';

const UserHead = 'https://mncdn.wenlvti.net/app_static/xiangyuan/images/user/avatar.png';

const authStore = useAuthStore();
const userInfo = computed(() => authStore.isLogged ? authStore.userInfo : null);
const volunteerInfoLoader = useSimpleDataLoader(async () => await VillageApi.getVolunteerInfo(), true);
const { requireLogin } = useRequireLogin();

function goUserProfile() {
  userInfo.value ? navTo('/pages/user/update/profile') : navTo('/pages/user/login');
}
function doLogout() {
  confirm({
    content: '您确定要退出登录吗？',
  }).then((res) => {
    if (res) {
      authStore.logout();
      if (AppCofig.requireLogin)
        uni.reLaunch({ url: '/pages/user/login' });
    }
  });
}
</script>
