// 订单相关接口
import { get, post } from '@/core/http/http-client'
import { buildRestPath } from '@/core/config/api-paths'

/**
 * 获取确认订单商品列表
 * 对接 88live 的 /wanlshop/order/getOrderGoodsList 接口
 */
export async function postOrderGetGoodsList({ data, address_id = 0 } = {}) {
	const { data: resData } = await post(buildRestPath('order/getOrderGoodsList'), {
		data,
		address_id
	})
	return resData
}

/**
 * 提交订单
 * 对接 88live 的 /wanlshop/order/addOrder 接口
 */
export async function postOrderAddOrder({ order, token } = {}) {
	const { data: resData } = await post(buildRestPath('order/addOrder'), {
		order,
		token
	})
	return resData
}

/**
 * 获取订单列表
 * state: 0=全部, 1=待付款, 2=待发货, 3=待收货, 4=待评价
 * page: 分页页码
 */
export async function pullOrderList({ state = 0, page = 1 } = {}) {
	const { data } = await get(buildRestPath('order/getOrderList'), { state, page })
	return {
		rows: Array.isArray(data.data) ? data.data : [],
		currentPage: data.current_page || 1,
		lastPage: data.last_page || 1,
		total: data.total || 0
	}
}

/**
 * 获取订单详情
 * id: 订单ID
 */
export async function pullOrderInfo({ id } = {}) {
	const { data } = await get(buildRestPath('order/getOrderInfo'), { id })
	return data
}

/**
 * 取消订单
 * id: 订单ID
 */
export async function postOrderCancel({ id } = {}) {
	const { data } = await post(buildRestPath('order/cancelOrder'), { id })
	return data
}


/**
 * 删除订单（已关闭的订单才能删除）
 * id: 订单ID
 */
export async function postOrderDelete({ id } = {}) {
	const { data } = await post(buildRestPath('order/delOrder'), { id })
	return data
}

/**
 * 确认收货
 * id: 订单ID
 */
export async function postOrderConfirm({ id } = {}) {
	const { data } = await post(buildRestPath('order/confirmOrder'), { id })
	return data
}

/**
 * 提交订单评价
 * data: { order_id, shop: { id, describe, logistics, service, deliver }, goodsList: [...] }
 */
export async function postOrderComment(data) {
	const { data: resData } = await post(buildRestPath('order/commentOrder'), data)
	return resData
}

/**
 * 获取物流信息
 * id: 订单ID
 */
export async function pullOrderLogistics({ id } = {}) {
	const { data } = await post(buildRestPath('order/getLogistics'), { id })
	return data
}

/**
 * 修改订单收货地址
 * id: 订单ID
 * address_id: 新地址ID
 */
export async function postOrderEditAddress({ id, address_id } = {}) {
	const { data } = await post(buildRestPath('order/editOrderAddress'), { id, address_id })
	return data
}

/**
 * 获取订单统计
 * 对接 88live 的 /wanlshop/order/getOrderStats 接口
 * */ 
export async function fetchOrderStats() {
	const states = [
		{ key: 'pay', state: 1 },
		{ key: 'delive', state: 2 },
		{ key: 'receiving', state: 3 },
		{ key: 'evaluate', state: 4 }
	]
	const results = await Promise.all(
		states.map(async ({ key, state }) => {
			try {
				const { total } = await pullOrderList({ state, page: 1 })
				return { key, total: total || 0 }
			} catch (_) {
				return { key, total: 0 }
			}
		})
	)
	const stats = { pay: 0, delive: 0, receiving: 0, evaluate: 0, customer: 0, whole: 0, groups: 0 }
	results.forEach(({ key, total }) => { stats[key] = total })
	return stats
}