import moment from 'moment'
import { getOrderPayData } from '@/common/api'

// 获取微信支付并支付
export const toWechatPay = (orderNo, remark,type) => {
	let sendData = { order_no: orderNo }
	if (remark) {
		sendData.order_remark = remark
	}
	getOrderPayData(sendData).then(res => {
		const { appId, nonceStr, paySign, signType, timeStamp } = res.data.param
		uni.requestPayment({
			provider: 'wxpay',
			nonceStr,
			package: res.data.param.package,
			paySign,
			signType,
			timeStamp,
			success: function(res) {
				if (type&&type==2){
					uni.navigateTo({ url: `/subPages/paySuccess/payCourseSuccess?orderNo=${orderNo}` })
				}else{
					uni.navigateTo({ url: `/subPages/paySuccess/paySuccess?orderNo=${orderNo}` })
				}
			},
			fail: function(err) {
				uni.navigateTo({ url: `/subPages/orderDetail/orderDetail?orderNo=${orderNo}` })
			}
		})
	})
}

// 简单对象去空
export const jsonFilterEmpty = data => {
	let tempData = {}
	for (var key in data) {
		if (data[key] === false || data[key] === 0 || !!data[key]) {
			tempData[key] = data[key] instanceof Array ? data[key] : data[key]
		}
	}
	return tempData
}

// 定义一个深拷贝函数  接收目标target参数
export function deepClone(target) {
	// 定义一个变量
	let result
	// 如果当前需要深拷贝的是一个对象的话
	if (typeof target === 'object') {
		// 如果是一个数组的话
		if (Array.isArray(target)) {
			result = [] // 将result赋值为一个数组，并且执行遍历
			for (let i in target) {
				// 递归克隆数组中的每一项
				result.push(deepClone(target[i]))
			}
			// 判断如果当前的值是null的话；直接赋值为null
		} else if (target === null) {
			result = null
			// 判断如果当前的值是一个RegExp对象的话，直接赋值
		} else if (target.constructor === RegExp) {
			result = target
		} else {
			// 否则是普通对象，直接for in循环，递归赋值对象的所有值
			result = {}
			for (let i in target) {
				result[i] = deepClone(target[i])
			}
		}
		// 如果不是对象的话，就是基本数据类型，那么直接赋值
	} else {
		result = target
	}
	// 返回最终结果
	return result
}

export function formatDate(data, type) {
	if (data) {
		let result = data.slice(0, 10).split('-')
		if (type) {
			return result.join(type)
		} else {
			return result[0] + '年' + result[1] + '月' + result[2] + '日'
		}

	} else {
		return ''
	}
}

// 简单对象去null
export const jsonFilterNull = data => {
	let tempData = {}
	for (var key in data) {
		// if (data[key] === false || data[key] === 0 || !!data[key]) {
		// 	tempData[key] = data[key] instanceof Array ? data[key] : data[key];
		// }
		if (data[key] == null) {
			if (data[key] instanceof Array) {
				tempData[key] = []
			} else {
				tempData[key] = ''
			}
		} else {
			tempData[key] = data[key]
		}
	}
	return tempData
}

// 计算生肖
export const getShengXiao = birth => {
	birth += ''
	var len = birth.length
	if (len < 4 && len != 2) {
		return ''
	}
	if (len == 2) {
		birth - 0 > 30 ? birth = '19' + birth : birth = '20' + birth
	}
	var year = (new Date(birth)).getFullYear()
	var arr = ['猴', '鸡', '狗', '猪', '鼠', '牛', '虎', '兔', '龙', '蛇', '马', '羊']
	return /^\d{4}$/.test(year) ? arr[year % 12] : ''
}

/***************************************************************************
 * 计算年龄
 * 
 * 支持传递["yyyy-MM-dd"]等格式的字符串
 * 
 */

// 序列化页面参数
export const stringPageOptions = data => {
	let result = '?'
	if (data) {
		for (let key in data) {
			result += `&${key}=${data[key]}`
		}
		result = result.replace(/&/, '')
		return result
	} else {
		return ''
	}
}


// 手机号格式化
export const string2fourPhone = (data, hideFlag) => {
	let result = data
	if (data) {
		result = data.replace(/s/g, '') //去掉空格
		if (hideFlag) {
			result = data.replace(/^(.{3}).*(.{4})/, '$1****$2')
			console.log(result) // 138****8888
		} else {
			result = data.replace(/^(.{3})(.*)(.{4})/, '$1 $2 $3')
			console.log(result) // 138 8888 8888
		}
	}
	return result || ''
}


// 时间格式化 今天 昨天 年月日
export const getFormatData = dataStr => {
	var time = new Date()
	if (dataStr) {
		if (dataStr.indexOf('T') != -1) {
			dataStr = dataStr.replace('T', ' ')
		}
		if (dataStr.indexOf('-') != -1) {
			dataStr = dataStr.replace(/\-/g, '/')
		}
		if (dataStr.indexOf('.') != -1) {
			dataStr = dataStr.substring(0, dataStr.indexOf('.'))
			console.log(dataStr)
		}
		time = new Date(dataStr)
	} else {
		return '无'
	}
	var nowTime = new Date()
	var month = ((time.getMonth() + 1) + '').length < 2 ? '0' + (time.getMonth() + 1) : (time.getMonth() + 1)
	var dates = (time.getDate() + '').length < 2 ? '0' + time.getDate() : time.getDate()
	var minute = (time.getMinutes() + '').length < 2 ? '0' + time.getMinutes() : time.getMinutes()
	var hours = (time.getHours() + '').length < 2 ? '0' + time.getHours() : time.getHours()
	var minute = (time.getMinutes() + '').length < 2 ? '0' + time.getMinutes() : time.getMinutes()
	var second = (time.getSeconds() + '').length < 2 ? '0' + time.getSeconds() : time.getSeconds()
	var strTime = time.getFullYear() + '.' + month + '.' + dates + ' ' + hours + ':' + minute
	if (time.setHours(0, 0, 0, 0) == nowTime.setHours(0, 0, 0, 0)) {
		strTime = '今天 ' + hours + ':' + minute
	} else if (time.setHours(0, 0, 0, 0) - nowTime.setHours(0, 0, 0, 0) == -86400000) {
		strTime = '昨天 ' + hours + ':' + minute
	} else if (time.setHours(0, 0, 0, 0) - nowTime.setHours(0, 0, 0, 0) == 86400000) {
		strTime = '明天 ' + hours + ':' + minute
	}

	return strTime
}

// 格式化日期
export const commonFormatDate = date => {
	if (date && date != 'null') {
		return moment(date).format('YYYY.MM.DD HH时')
	} else {
		return ''
	}
}

// 格式化日期
export const commonFormatDate2 = date => {
	if (date && date != 'null') {
		return moment(date).format('YYYY.MM.DD')
	} else {
		return ''
	}
}


// 格式化日期
export const commonFormatDate3 = date => {
	if (date && date != 'null') {
		return moment(date).format('YYYY.MM.DD HH:mm')
	} else {
		return ''
	}
}


export const getWeekforStr = dateStr => {
	var time = new Date()
	if (dateStr) {
		if (dateStr.indexOf('T') != -1) {
			dateStr = dateStr.replace('T', ' ')
		}
		if (dateStr.indexOf('-') != -1) {
			dateStr = dateStr.replace(/\-/g, '/')
		}
		if (dateStr.indexOf('.') != -1) {
			dateStr = dateStr.substring(0, dateStr.indexOf('.'))
			console.log(dateStr)
		}
		time = new Date(dateStr)
	}
	var str = ''
	var week = time.getDay()
	if (week == 0) {
		str = '周日'
	} else if (week == 1) {
		str = '周一'
	} else if (week == 2) {
		str = '周二'
	} else if (week == 3) {
		str = '周三'
	} else if (week == 4) {
		str = '周四'
	} else if (week == 5) {
		str = '周五'
	} else if (week == 6) {
		str = '周六'
	}
	return str
}

// 获取显示地址
export const getAddressDisplay = data => {
	const { province = '', city = '', area = '', address = '' } = data
	return province + city + area + address
}

// 获取图片baseUrl
export const getImgBaseUrl = () => {
	return 'http://wx.ruweitg.com/static/images/'
}
