import {
	buildPageUrl,
	formatDate
} from '../common/util.js'

export default {
	data() {
		const pages = getCurrentPages()
		console.log(pages);
		const PageTitle = '闽南魂';
		if (pages.length > 0) {
			const currentPage = pages[pages.length - 1]
			const currentPageUrl = buildPageUrl(currentPage.route, currentPage.options)
			return {
				PageTitle,
				currentPage,
				currentPageUrl
			}
		} else {
			return {
				PageTitle
			}
		}

	},
	onLoad(options) {
		console.log('page onLoad', this.currentPageUrl);
	},
	methods: {
		onCopy(text) {
			uni.setClipboardData({
				data: text + ''
			}).then(([err, res]) => {
				console.log('uni.setClipboardData', err, res);
				if (!err) {
					uni.showToast({
						title: '复制成功',
						icon: 'none',
					});
				}
			});
		}
	},
	computed: {
		costTime() {
			return (seconds, type) => {
				let second = parseInt(seconds)
				if (second <= 0) {
					if (type === 'countdown') {
						return '已结束'
					}
					return '0秒'
				}
				let minute = 0 // 初始化分
				let hour = 0 // 初始化小时
				if (second >= 60) { // 如果秒数大于等于60，将秒数转换成分钟
					minute = parseInt(second / 60) // 获取分钟
					second = parseInt(second % 60) // 获取秒数
					if (minute >= 60) { // 如果分钟大于等于60，将分钟转换成小时
						hour = parseInt(minute / 60) // 获取小时
						minute = parseInt(minute % 60) // 获取小时后取余的分
					}
				}
				if (type === 'countdown') {
					return `${hour.toString().padStart(2,'0')}:${minute.toString().padStart(2,'0')}:${second.toString().padStart(2,'0')}`
				}
				let desc = ''
				if (hour > 0) {
					desc = hour + '小时'
				}
				if (minute > 0 || hour > 0) {
					desc += minute + '分'
				}
				desc += second + '秒'
				return desc
			}
		},
		timeFormat() {
			return (timestamp) => {
				if (!timestamp) {
					return ''
				}
				return formatDate(timestamp, 'yyyy-MM-dd hh:mm:ss')
			}
		},
	},
}