<template>
	<page-meta :page-font-size="fontValue+'px'" :root-font-size="fontValue+'px'"></page-meta>
  <view>
    <cu-custom bgImage="/static/bg.png" :isBack="true">
      <template v-slot:content>
        <text class="text-black">聊天设置</text>
      </template>
    </cu-custom>
    <view class="padding">
      <form>
        <view class="cu-form-group margin-top">
          <view class="title text-bold text-black">允许加好友：</view>
          <switch @change="switchChange('canAddFriend',$event)" :class="settingTemp.canAddFriend?'checked':''" :checked="settingTemp.canAddFriend"/>
        </view>
        <view class="cu-form-group margin-top">
          <view class="title text-bold text-black">好友审核：</view>
          <switch @change="switchChange('addFriendValidate',$event)"  :class="settingTemp.addFriendValidate?'checked':''" :checked="settingTemp.addFriendValidate"/>
        </view>
        <view class="cu-form-group margin-top">
          <view class="title text-bold text-black">允许私聊：</view>
          <switch @change="switchChange('canSendMessage',$event)"  :class="settingTemp.canSendMessage?'checked':''" :checked="settingTemp.canSendMessage"/>
        </view>
        <view class="cu-form-group margin-top">
          <view class="title text-bold text-black">语音消息提醒：</view>
          <switch @change="switchChange('canSoundRemind',$event)"  :class="settingTemp.canSoundRemind?'checked':''" :checked="settingTemp.canSoundRemind"/>
        </view>
<!--        <view class="cu-form-group margin-top">-->
<!--          <view class="title">通话消息提醒：</view>-->
<!--          <switch @change="switchChange('canVoiceRemind',$event)"  :class="settingTemp.canVoiceRemind?'checked':''" :checked="settingTemp.canVoiceRemind"/>-->
<!--        </view>-->
        <view class="cu-form-group margin-top">
          <view class="title text-bold text-black">展示手机：</view>
          <switch @change="switchChange('showMobile',$event)"  :class="settingTemp.showMobile?'checked':''" :checked="settingTemp.showMobile"/>
        </view>
        <view class="cu-form-group margin-top">
          <view class="title text-bold text-black">展示邮箱：</view>
          <switch @change="switchChange('showEmail',$event)"  :class="settingTemp.showEmail?'checked':''" :checked="settingTemp.showEmail"/>
        </view>
        <view class="margin-top text-center">
          <button class="cu-btn bg-zblue border lg margin-top" style="width: 100%;" @tap="submit">保存</button>
        </view>
      </form>
    </view>
  </view>
</template>

<script setup lang="ts">
import CuCustom from '@/colorui/components/cu-custom.vue'
import {reactive, ref} from "vue";
import MessageUtils from "@/utils/MessageUtils";
import SettingApi from "@/api/SettingApi";
import type Setting from "@/mode/Setting";
import {useUserStore} from "@/store/userStore";
import Auth from "@/api/Auth";
const fontValue=ref(Auth.getfontSize());

const YES = "1";
const NO = "0";


const settingTemp = reactive({
  canAddFriend: false,
  addFriendValidate: false,
  canSendMessage: false,
  canSoundRemind: false,
  canVoiceRemind: false,
  showMobile: false,
  showEmail: false,
});

const userId = useUserStore().getUser()?.id;

const loadData = () => {
  if (userId) {
    SettingApi.get(userId).then((res) => {
      const setting_ = res.data;
      settingTemp.canAddFriend = setting_.canAddFriend === "1";
      settingTemp.addFriendValidate = setting_.addFriendValidate === "1";
      settingTemp.canSendMessage = setting_.canSendMessage === "1";
      settingTemp.canSoundRemind = setting_.canSoundRemind === "1";
      settingTemp.canVoiceRemind = setting_.canVoiceRemind === "1";
      settingTemp.showMobile = setting_.showMobile === "1";
      settingTemp.showEmail = setting_.showEmail === "1";
    });
  }
};

loadData();
/** 提交按钮 */
function submit() {
  //userId必须传回，清理缓存用
  if (userId) {
    const setting: Setting = {
      canAddFriend: settingTemp.canAddFriend ? YES : NO,
      addFriendValidate: settingTemp.addFriendValidate ? YES : NO,
      canSendMessage: settingTemp.canSendMessage ? YES : NO,
      canSoundRemind: settingTemp.canSoundRemind ? YES : NO,
      canVoiceRemind: settingTemp.canVoiceRemind ? YES : NO,
      showMobile: settingTemp.showMobile ? YES : NO,
      showEmail: settingTemp.showEmail ? YES : NO,
      createBy: userId,
    };
    SettingApi.update(setting).then(() => {
      MessageUtils.success("修改成功");
    });
  }
}

const switchChange = (name:string,e: any) => {
  if(name === 'canAddFriend'){
    settingTemp.canAddFriend = e.detail.value
  }else if (name === 'addFriendValidate'){
    settingTemp.addFriendValidate = e.detail.value
  }else if (name === 'canSendMessage'){
    settingTemp.canSendMessage = e.detail.value
  }else if (name === 'canSoundRemind'){
    settingTemp.canSoundRemind = e.detail.value
  }else if (name === 'canVoiceRemind'){
    settingTemp.canVoiceRemind = e.detail.value
  }else if (name === 'showMobile'){
    settingTemp.showMobile = e.detail.value
  }else if (name === 'showEmail'){
    settingTemp.showEmail = e.detail.value
  }
}
</script>

<style scoped>

</style>
