<!-- 微信授权信息 mpAuthorization  -->
<template>
  <view>
    <!-- 标题栏 -->
    <view class="head-box ss-m-b-60 ss-flex-col">
      <view class="ss-flex ss-m-b-20">
        <view class="head-title ss-m-r-40 head-title-animation">授权信息</view>
      </view>
      <view class="head-subtitle">完善您的头像、昵称、性别、年龄</view>
    </view>

    <!-- 表单项 -->
    <uni-forms
      ref="accountLoginRef"
      v-model="state.model"
      :rules="state.rules"
      validateTrigger="bind"
      labelWidth="140"
      labelAlign="center"
    >
      <!-- 获取头像昵称：https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/userProfile.html -->
      <uni-forms-item name="avatar" label="头像">
        <button
          class="ss-reset-button avatar-btn"
          @click="onChooseAvatar"
        >
          <image
            class="avatar-img"
            :src="sheep.$url.cdn(state.model.avatar)"
            mode="aspectFill"
          />
          <text class="cicon-forward" />
        </button>
      </uni-forms-item>
      <uni-forms-item name="nickname" label="昵称">
        <uni-easyinput
          type="text"
          placeholder="请输入昵称"
          v-model="state.model.nickname"
          :inputBorder="false"
        />
      </uni-forms-item>
	  <form-sex v-model="state.model.sex"></form-sex>
	  <uni-forms-item name="age" label="年龄">
	    <uni-easyinput
	      type="number"
	      placeholder="请输入年龄"
	      v-model="state.model.age"
	      :inputBorder="false"
	    />
	  </uni-forms-item>
      <view class="foot-box">
        <button class="ss-reset-button authorization-btn" @tap="onConfirm"> 确认授权 </button>
      </view>
    </uni-forms>
  </view>
</template>

<script setup>
  import { computed, ref, reactive } from 'vue';
  import sheep from '@/sheep';
  import $store from '@/sheep/store';
  import { closeAuthModal, showAuthModal } from '@/sheep/hooks/useModal';
  import formSex from '@/sheep/components/s-auth-modal/components/formSex.vue';
  import FileApi from '@/sheep/api/infra/file';
  import UserApi from '@/sheep/api/member/user';

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

  const userInfo = computed(() => sheep.$store('user').userInfo);
  
  const isPass = computed(() => sheep.$store('user').tradeConfig.weixinEnabled);
  
  const friendEnabled = computed(() => sheep.$store('user').tradeConfig.friendEnabled);

  const accountLoginRef = ref(null);

  // 数据
  const state = reactive({
    model: {
      nickname: userInfo.value.nickname,
      avatar: userInfo.value.avatar,
	  sex: userInfo.value.sex,
	  age: userInfo.value.age,
    },
    rules: {},
    disabledStyle: {
      color: '#999',
      disableColor: '#fff',
    },
  });

  function onChooseAvatar() {
	uni.chooseImage({
		count: 1, //默认9
		sourceType: ['album', 'camera'],
		sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
		success: (res) => {
			for (let i = 0; i < res.tempFilePaths.length; i++) {
				uni.getImageInfo({
					src: res.tempFilePaths[i],
					success: (image) => {
						uploadAvatar(image.path);
					}
				});
			}
		}
	});
    
  }

  // 选择头像（来自文件系统）
  async function uploadAvatar(tempUrl) {
    if (!tempUrl) {
      return;
    }
    let { data } = await FileApi.uploadFile(tempUrl);
    state.model.avatar = data;
  }

  // 确认授权
  async function onConfirm() {
    const { model } = state;
    const { nickname, avatar, sex, age } = model;
	if (!avatar) {
	  sheep.$helper.toast('请选择头像');
	  return;
	}
    if (!nickname) {
      sheep.$helper.toast('请输入昵称');
      return;
    }
	if (!sex) {
	  sheep.$helper.toast('请选择性别');
	  return;
	}
	if (!age || age < 18 || age > 100) {
	  sheep.$helper.toast('请输入正确的年龄');
	  return;
	}
	if (age < 18) {
	  sheep.$helper.toast('未成年禁止注册');
	  return;
	}
    // 发起更新
    const { code } = await UserApi.updateUser({
      avatar: state.model.avatar,
      nickname: state.model.nickname,
	  sex: state.model.sex,
	  age: state.model.age,
    });
    // 更新成功
    if (code === 0) {
      sheep.$helper.toast('授权成功');
      await sheep.$store('user').getInfo();
	  closeAuthModal();
    }
  }
</script>

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

  .foot-box {
    width: 100%;
    display: flex;
    justify-content: center;
  }
  .authorization-btn {
    width: 686rpx;
    height: 80rpx;
    background-color: var(--ui-BG-Main);
    border-radius: 40rpx;
    color: #fff;
  }
  .avatar-img {
    width: 72rpx;
    height: 72rpx;
    border-radius: 36rpx;
  }
  .cicon-forward {
    font-size: 30rpx;
    color: #595959;
  }
  .avatar-btn {
    width: 100%;
    justify-content: space-between;
  }
</style>
