import { useUserStore } from '@/store/system/user';
import { reactive, ref } from 'vue';
import { createFormData } from '@pureadmin/utils';
import { fetchUploadFile } from '@/api/v1/system/system';
import { message, messageBox } from '@/utils/message';
import { $t } from '@/plugins/i18n';
import { switchLoadMapControl } from '@/views/hooks';
import { useAdminUserStore } from '@/store/system/adminUser';

// 剪裁完成后头像数据
export const cropperBlob = ref();

// 上传地址路径
export const uploadAvatarSrc = ref();

// 剪裁头像是否显示
export const isShow = ref(false);

// 用户是否停用加载集合
export const switchLoadMap = ref({});

// 用户仓库
const userStore = useUserStore();
const adminUserStore = useAdminUserStore();

// 用户信息内容
export const userInfos = reactive({
	avatar: '',
	username: '',
	nickname: '',
	email: '',
	phone: '',
	summary: '',
	password: '',
	sex: '',
	weekBillReport: undefined,
	mouthBillReport: undefined,
});

/** 获取用户信息内容 */
export const onSearchByUserinfo = async () => {
	const data = await userStore.getUserinfo();
	Object.assign(userInfos, data);
};

/** 修改头像 */
export const handleSubmitImage = async () => {
	// 上传头像表单
	const formData = createFormData({
		files: new File([cropperBlob.value], 'avatar'),
		type: 'avatar',
	});

	// 上传头像
	const result = await fetchUploadFile(formData);

	// 上传成功后关闭弹窗
	if (result.code === 200) {
		uploadAvatarSrc.value = result.data.filepath;
		userInfos.avatar = result.data.url;
		message($t('upload_success'), { type: 'success' });
		isShow.value = false;
	}
};

/** 更新用户周账单提醒是否开启 */
export const updateUserReportStatusByWeek = async (index: number) => {
	// 点击时开始loading加载
	switchLoadMapControl(switchLoadMap, index, true);

	// 是否确认修改弹窗内容
	const confirm = await messageBox({
		title: $t('confirm_update_status'),
		showMessage: false,
		confirmMessage: undefined,
		cancelMessage: $t('cancel'),
	});

	// 如果不修改将值恢复到之前状态
	if (!confirm) {
		userInfos.weekBillReport = !userInfos.weekBillReport;
		switchLoadMapControl(switchLoadMap, index, false);
		return;
	}

	// 修改用户状态
	const data = { type: 'week', status: userInfos.weekBillReport };
	const result = await adminUserStore.updateUserBillReportByLocalUser(data);
	if (!result) {
		userInfos.weekBillReport = !userInfos.weekBillReport;
		switchLoadMapControl(switchLoadMap, index, false);
		return;
	}
	switchLoadMapControl(switchLoadMap, index, false);
};

/** 更新用户周账单提醒是否开启 */
export const updateUserReportStatusByMouth = async (index: number) => {
	// 点击时开始loading加载
	switchLoadMapControl(switchLoadMap, index, true);

	// 是否确认修改弹窗内容
	const confirm = await messageBox({
		title: $t('confirm_update_status'),
		showMessage: false,
		confirmMessage: undefined,
		cancelMessage: $t('cancel'),
	});

	// 如果不修改将值恢复到之前状态
	if (!confirm) {
		userInfos.mouthBillReport = !userInfos.mouthBillReport;
		switchLoadMapControl(switchLoadMap, index, false);
		return;
	}

	// 修改用户状态
	const data = { type: 'mouth', status: userInfos.mouthBillReport };
	const result = await adminUserStore.updateUserBillReportByLocalUser(data);
	if (!result) {
		userInfos.mouthBillReport = !userInfos.mouthBillReport;
		switchLoadMapControl(switchLoadMap, index, false);
		return;
	}

	switchLoadMapControl(switchLoadMap, index, false);
};
