import request from '../http'

// 获取所有全部订单
export const getOrderList = (data) => {
	return request({
		url: '/app/order/list',
		method: 'get',
		data,
		hideLoading: true
	})
}

// 查询是否有订单
export const queryHasOrder = (deviceNo) => {
	return request({
		url: `/app/order/list?deviceNo=${deviceNo}&orderStatus=in_used`,
		method: 'get',
	})
}

// 查询指定设备号下，特定状态的订单列表
export const checkOrdersByStatus = (deviceNo, statuses) => {
	// statuses 是一个包含状态字符串的数组，例如 ['in_used', 'waiting_for_payment']
	const statusQuery = statuses.join(','); // 后端需要支持逗号分隔的状态查询
	return request({
		url: `/app/order/list?deviceNo=${deviceNo}&orderStatus=${statusQuery}`,
		method: 'get',
		hideLoading: true, // 隐藏加载提示，避免干扰用户
		ignoreEmptyError: true // 添加标记，表示即使返回空数据也不视为错误
	})
}

// 创建订单
export const createOrder = (data) => {
	return request({
		url: '/app/order/add',
		method: 'post',
		data,
	})
}

// 查询订单
export const queryById = (id) => {
	console.log(`查询订单详情, orderId: ${id}`)
	return request({
		url: `/app/order/${id}`,
		method: 'get',
		hideLoading: true
	})
}

// 取消订单
export const cancelOrder = (data) => {
	return request({
		url: `/device/order/clear/${data.orderId}`,
		method: 'get',
		data,
	})
}

// 结束订单
export const overOrderById = (orderId) => {
	console.log(`调用结束订单API, orderId: ${orderId}`)
	return request({
		url: `/app/order/close/${orderId}`,
		method: 'get',
	})
}

// 通过订单号获取订单信息
export const getOrderByOrderNo = (orderNo) => {
	return request({
		url: `/app/order/byOrderNo/${orderNo}`,
		method: 'get',
		hideLoading: true
	})
}

// 通过订单号获取支付分订单信息
export const getOrderByOrderNoScore = (orderNo) => {
	console.log('通过订单号获取支付分订单信息', orderNo);
	return request({
		url: `/app/wx-payment/score/create/${orderNo}`,
		method: 'get',
		hideLoading: true
	})
}

// 通过订单号获取支付分订单状态
export const getOrderByOrderNoScorePayStatus = (orderNo) => {
	console.log('通过订单号获取支付分订单状态', orderNo);
	return request({
		url: `/app/wx-payment/score/status/${orderNo}`,
		method: 'get',
		hideLoading: true
	})
}

// 更新订单套餐信息
export const updateOrderPackage = (data) => {
	console.log('更新订单套餐信息：', data)
	return request({
		url: '/app/device/updateOrderPackage',
		method: 'post',
		data
	})
}

/*
 * 弃用
 */
export const getPotionsDetail = (data) => {
	console.log(data);
	return request({
		url: '/device/position/positionDetails',
		method: 'get',
		data
	})
}

