import request from '@/utils/request';
import { AxiosPromise } from 'axios';
import { SubOrderVO, SubOrderQuery, SubOrderDispatchQuery } from './types';

/**
 * 派单中心查询子订单列表
 */
export const listSubOrderOnDispatch = (query?: SubOrderDispatchQuery): AxiosPromise<SubOrderVO[]> => {
    return request({
        url: '/order/subOrder/listOnDispatch',
        method: 'get',
        params: query
    });
};

/**
 * 查询子订单列表
 * @param query
 * @returns {*}
 */
export const listSubOrder = (query?: SubOrderQuery): AxiosPromise<{ total: number, rows: SubOrderVO[] }> => {
    return request({
        url: '/order/subOrder/list',
        method: 'get',
        params: query
    });
};

export const dispatchSubOrder = (data: { orderId: string | number; fulfiller: string | number; fulfillmentCommission: number; orderCommission: number; }) => {
    return request({
        url: '/order/subOrder/dispatch',
        method: 'put',
        data
    });
};

export const getSubOrderInfo = (id: string | number): AxiosPromise<SubOrderVO> => {
    return request({
        url: '/order/subOrder/getInfo',
        method: 'get',
        params: { id }
    });
};

export const cancelSubOrder = (data: { orderId: string | number; reason?: string; }) => {
    return request({
        url: '/order/subOrder/cancel',
        method: 'put',
        data
    });
};

export const remarkSubOrder = (data: { orderId: string | number; remark: string; }) => {
    return request({
        url: '/order/subOrder/remark',
        method: 'put',
        data
    });
};

export const confirmSubOrder = (data: { id: string | number; }) => {
    return request({
        url: '/order/subOrder/confirm',
        method: 'put',
        data
    });
};

export const nursingSummarySubOrder = (data: { orderId: string | number; content: string; }) => {
    return request({
        url: '/order/subOrder/nursingSummary',
        method: 'put',
        data
    });
};

/**
 * 查询客户涉及的子订单列表
 * @param customerId
 */
export const listSubOrderOnCustomer = (customerId: string | number): AxiosPromise<SubOrderVO[]> => {
    return request({
        url: '/order/subOrder/listOnCustomer',
        method: 'get',
        params: { customerId }
    });
};

/**
 * 查询宠物涉及的子订单列表
 * @param petId
 */
export const listSubOrderOnPet = (petId: string | number): AxiosPromise<SubOrderVO[]> => {
    return request({
        url: '/order/subOrder/listOnPet',
        method: 'get',
        params: { petId }
    });
};

/**
 * 查询履约者涉及的子订单列表
 * @param fulfillerId
 */
export const listSubOrderOnFulfiller = (fulfillerId: string | number): AxiosPromise<SubOrderVO[]> => {
    return request({
        url: '/order/subOrder/listOnFulfiller',
        method: 'get',
        params: { fulfillerId }
    });
};

/**
 * 分页查询门店关联的子订单列表
 * @param query
 */
export const listSubOrderOnStore = (query: any): AxiosPromise<SubOrderVO[]> => {
    return request({
        url: '/order/subOrder/listOnStore',
        method: 'get',
        params: query
    });
};

/**
 * 导出子订单列表为Excel
 * @param data 查询参数（status、service、content）
 */
export const exportSubOrder = (data: { status?: number; service?: number; content?: string }) => {
    return request({
        url: '/order/subOrder/export',
        method: 'post',
        data,
        responseType: 'blob'
    });
};

export const activateSubOrder = (data: { id: string | number; service: string | number; fulfillmentCommission: number; }) => {
    return request({
        url: '/order/subOrder/activate',
        method: 'put',
        data
    });
};

export const closeSubOrder = (data: { orderId: string | number; reason?: string; }) => {
    return request({
        url: '/order/subOrder/close',
        method: 'put',
        data
    });
};
