import request from '../request'

//订单模块
const order = {
	/**
	 * 查询订单列表
	 * @param {Object} params
	 */
	async getOrderList(params) {
		let res = await request({
			url: '/student/wx/order/list',
			params
		})

		const orderStatusMap = new Map([
			['1', '等待发货'],
			['2', '交易完成'],
			['3', '退款/售后']
		])

		const orderNameMap = new Map([
			['1', (json) => {
				return json.cinemaData.filmName
			}],
			['2', (json) => {
				let nameArr = json.wxFuluOrderDetailDTO.product_name.split('-')
				return nameArr[1] + '-' + nameArr[2]
			}],
			['3', (json) => {
				let nameArr = json.rechargePhoneNo+'话费充值'
				return nameArr
			}]
		])

		res.rows = res.rows.map(val => {

			try {
				var orderDataJson = JSON.parse(val.orderDataJson)
			} catch {
				orderDataJson = {
					wxFuluOrderDetailDTO: {
						product_name: ''
					}
				}
			}

			return {
				createTime: val.createTime,
				goodsPictureUrl: val.goodsPictureUrl,
				orderName: orderNameMap.get(val.goodsType)(orderDataJson),
				outTradeNo: val.outTradeNo,
				total: val.total / 100,
				orderStatus: orderStatusMap.get(val.orderStatus),
				id: val.id,
				goodsType: val.goodsType
			}
		})

		return res
	},
	/**
	 * 查询订单详情
	 * @param path:string 订单号
	 */
	async getWxOrder(path) {
		let res = await request({
			url: `/student/wx/${path}`,
		})

		const orderStatusMap = new Map([
			['1', '等待发货'],
			['2', '交易完成'],
			['3', '退款/售后']
		])

		const jsonMap = new Map([
			['1', (data) => {
				return {
					filmName: data.orderDataJson.cinemaData.filmName,
					showTime: data.orderDataJson.cinemaData.showTime,
					cinemaName: data.orderDataJson.cinemaData.cinemaName,
					hallName: data.orderDataJson.cinemaData.hallName,
					seat: data.orderDataJson.wxOrderCreateDTO.seat.split(','),
					total: data.total / 100,
					orderStatus: data.orderStatus,
					orderStatusDesc: orderStatusMap.get(data.orderStatus),
				}
			}],
			['2', (data) => {
				return {
					charge_account: data.orderDataJson.wxFuluOrderDetailDTO.charge_account,
					product_name: data.orderDataJson.wxFuluOrderDetailDTO.product_name,
					create_time: data.orderDataJson.wxFuluOrderDetailDTO.create_time,
					buy_num: data.orderDataJson.wxFuluOrderDetailDTO.buy_num,
					recharge_description: data.orderDataJson.wxFuluOrderDetailDTO.recharge_description,
					total: data.total / 100,
					orderStatus: data.orderStatus,
					orderStatusDesc: orderStatusMap.get(data.orderStatus),
					goodsPictureUrl: data.goodsPictureUrl
				}
			}],
			['3', (data) => {
				let operatorsMap = new Map([
					[1,'移动'],
					[2,'联通'],
					[3,'电信'],
				])
				return {
					rechargePhoneNo: data.orderDataJson.rechargePhoneNo,
					tmallNo: data.orderDataJson.tmallNo,
					operators: operatorsMap.get(data.orderDataJson.operators),
					create_time: data.createTime,
					total: data.total / 100,
					orderStatus: data.orderStatus,
					orderStatusDesc: orderStatusMap.get(data.orderStatus),
					goodsPictureUrl: data.goodsPictureUrl
				}
			}]
		])

		try {
			res.data.orderDataJson = JSON.parse(res.data.orderDataJson)
		} catch {
			res.data.orderDataJson = {
				wxFuluOrderDetailDTO: {
					product_name: ''
				}
			}
		}
		res.data = jsonMap.get(res.data.goodsType)(res.data)

		return res
	}
}

export default order
