<template>
  <view>
    <cu-custom bgImage="/static/bg.png" :isBack="true">
      <template v-slot:content>
        聊天设置
      </template>
    </cu-custom>
    <view class="padding">
      <form>
        <view class="cu-form-group margin-top">
          <view class="title">允许加好友：</view>
          <switch @change="switchChange('canAddFriend',$event)" :class="settingTemp.canAddFriend?'checked':''" :checked="settingTemp.canAddFriend"/>
        </view>
        <view class="cu-form-group margin-top">
          <view class="title">好友审核：</view>
          <switch @change="switchChange('addFriendValidate',$event)"  :class="settingTemp.addFriendValidate?'checked':''" :checked="settingTemp.addFriendValidate"/>
        </view>
        <view class="cu-form-group margin-top">
          <view class="title">允许私聊：</view>
          <switch @change="switchChange('canSendMessage',$event)"  :class="settingTemp.canSendMessage?'checked':''" :checked="settingTemp.canSendMessage"/>
        </view>
        <view class="cu-form-group margin-top">
          <view class="title">语音消息提醒：</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">展示手机：</view>
          <switch @change="switchChange('showMobile',$event)"  :class="settingTemp.showMobile?'checked':''" :checked="settingTemp.showMobile"/>
        </view>
        <view class="cu-form-group margin-top">
          <view class="title">展示邮箱：</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-cyan 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} from "vue";
import MessageUtils from "@/utils/MessageUtils";
import SettingApi from "@/api/SettingApi";
import type Setting from "@/mode/Setting";
import {useUserStore} from "@/store/userStore";

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>
