import {
	getJSONData,
	setJSONData,
	removeJSONData,
	setStrData,
	getStrData
} from '@/utils/auth'
import constant from '@/utils/constant'
import {
	selectMemberRights
} from '@/api/business/member.js'

export function getWXStatusHeight() {
	// #ifdef MP-WEIXIN
	// 获取距上
	const barTop = wx.getSystemInfoSync().statusBarHeight
	// 获取胶囊按钮位置信息
	const menuButtonInfo = wx.getMenuButtonBoundingClientRect()
	// 获取导航栏高度
	const barHeight = menuButtonInfo.height + (menuButtonInfo.top - barTop) * 2
	let barWidth = menuButtonInfo.width
	console.log('menuButtonInfo', menuButtonInfo)
	let barLeftPosition = 375 - menuButtonInfo.right + menuButtonInfo.width
	let menuButtonLeft = menuButtonInfo.left
	let menuButtonRight = menuButtonInfo.right
	return {
		barHeight,
		barTop,
		barWidth,
		barLeftPosition,
		menuButtonLeft,
		menuButtonRight
	}
	// #endif
}
/**
 * 判断某用户是否有某权益
 * @param {Object} code 权益code
 */
export function hasRights(code) {
	return new Promise((resolve, reject) => {
		//会员权益刷新
		selectMemberRights().then(res => {
			setJSONData(constant.userRightsKey, res.data)
			//取用户当前身份
			let userType = getStrData(constant.userTypeKey)
			let bzRights = {}
			let tgzRights = {}
			//博主
			bzRights = getJSONData(constant.userRightsKey)['bz']
			//通告主
			tgzRights = getJSONData(constant.userRightsKey)['tgz']
			//默认无权限
			let rtnData = false
			if (bzRights.hasOwnProperty(code)) {
				rtnData = bzRights[code]
			} else if (tgzRights.hasOwnProperty(code)) {
				rtnData = tgzRights[code]
			}
			console.log(rtnData, 51);
			resolve(rtnData)

		})
	}).catch(e => {
		reject(e)
	})

}
/**
 * 切换用户当前身份
 * @param {Object} value 身份值
 */
export function changeUserType(value) {
	setStrData(constant.userTypeKey, value)
}

/**
 * 获取用户当前身份
 * @param {Object} value 身份值
 */
export function getUserType() {
	return getStrData(constant.userTypeKey) || '02'
}

/**
 * 显示消息提示框
 * @param content 提示的标题
 */
export function toast(content) {
	uni.showToast({
		icon: 'none',
		title: content
	})
}

/**
 * 显示模态弹窗
 * @param content 提示的标题
 */
export function showConfirm(content) {
	return new Promise((resolve, reject) => {
		uni.showModal({
			title: '提示',
			content: content,
			cancelText: '取消',
			confirmText: '确定',
			success: function(res) {
				resolve(res)
			}
		})
	})
}

/**
 * 参数处理
 * @param params 参数
 */
export function tansParams(params) {
	let result = ''
	for (const propName of Object.keys(params)) {
		const value = params[propName]
		var part = encodeURIComponent(propName) + "="
		if (value !== null && value !== "" && typeof(value) !== "undefined") {
			if (typeof value === 'object') {
				for (const key of Object.keys(value)) {
					if (value[key] !== null && value[key] !== "" && typeof(value[key]) !== 'undefined') {
						let params = propName + '[' + key + ']'
						var subPart = encodeURIComponent(params) + "="
						result += subPart + encodeURIComponent(value[key]) + "&"
					}
				}
			} else {
				result += part + encodeURIComponent(value) + "&"
			}
		}
	}
	return result
}

/**
 * 数字单位转换
 * @param {Object} num
 */
export function formatNumberWithUnits(num) {
	if (!num) {
		return '无限制';
	}
	const units = ['', 'k', 'w', 'kw'];
	// 获取绝对值以处理负数
	let absNum = Math.abs(num);
	// 处理小于1000的情况，直接返回原数字的字符串形式
	if (absNum < 1000) {
		return num;
	}


	// 寻找合适的单位
	let unitIndex = 0;
	if (absNum > 10000000) {
		absNum /= 10000000;
		unitIndex = 3;
	} else if (absNum > 10000) {
		absNum /= 10000;
		unitIndex = 2;
	} else if (absNum > 1000) {
		absNum /= 1000;
		unitIndex = 1;
	}
	// 格式化数字，并添加单位
	const formattedNum = absNum.toFixed(2).replace(/\.?0+$/, ''); // 保留两位小数并去除末尾的零
	return formattedNum + units[unitIndex];
}

/**
 * 计算某个时间是几天、几小时之前
 * @param {Object} time
 */
export function calculateTimeDifference(time) {
	// 将时间字符串转换为Date对象
	const nowDate = new Date();
	const lastDate = new Date(time);

	// 计算时间差（毫秒）
	const diffMs = Math.abs(nowDate.getTime() - lastDate.getTime());

	// 将毫秒转换为小时
	const diffHours = diffMs / (1000 * 60 * 60);

	// 计算完整的整天天数和剩余的小时数
	const diffDays = Math.floor(diffHours / 24);
	const remainingHours = diffHours % 24;
	if (diffDays > 0) {
		return parseInt(diffDays) + "天前"
	} else {
		if (parseInt(remainingHours) == 0) {
			return "刚刚"
		} else {
			return parseInt(remainingHours) + "小时前"
		}

	}
}

/**
 * 翻译认证身份类型
 * @param {Object} key
 */
export function getTextByIdentityType(key) {
	if ("01" == key) {
		return "品牌方"
	} else if ("02" == key) {
		return "代运营公司"
	} else if ("03" == key) {
		return "个人PR"
	} else if ("04" == key) {
		return "电商"
	} else if ("05" == key) {
		return "转发PR"
	}
}

/**
 * 格式化数字
 * @param {Object} number
 */
export function formatNumberWithCommas(number) {
	return new Intl.NumberFormat().format(number);
}
