import request from '@/utils/axios.ts';
// 装车订单 - 列表
export function orderList(data) {
    return request({
        url: '/delivery/pageDeliveryOrderList',
        method: 'POST',
        data,
    });
}
// 装车订单 - 新增
export function orderAdd(data) {
    return request({
        url: '/delivery/addDeliveryOrder',
        method: 'POST',
        data,
    });
}
// 装车订单 - 删除
export function orderDel(id) {
    return request({
        url: `/delivery/deleteDelivery?id=${id}`,
        method: 'GET',
    });
}
// 清空设备delivery_id
export function clearDeviceDeliveryId(deliveryId) {
    return request({
        url: '/deliveryDevice/clearDeliveryId',
        method: 'POST',
        data: { deliveryId },
    });
}

// 测试接口：检查订单设备数据
export function testOrderDevices(deliveryId) {
    return request({
        url: '/deliveryDevice/testOrderDevices',
        method: 'POST',
        data: { deliveryId },
    });
}

// 更新设备delivery_id和weight
export function updateDeviceDeliveryId(data) {
    return request({
        url: '/deliveryDevice/updateDeviceDeliveryId',
        method: 'POST',
        data,
    });
}

// 解绑单个设备（将delivery_id设置为null）
export function unbindDevice(deviceId) {
    return request({
        url: '/deliveryDevice/updateDeviceDeliveryId',
        method: 'POST',
        data: { deviceId, deliveryId: null },
    });
}

// 批量更新设备weight
export function updateDeviceWeights(data) {
    return request({
        url: '/deliveryDevice/updateDeviceWeights',
        method: 'POST',
        data,
    });
}

// 获取订单绑定的智能主机
export function getOrderHostDevice(deliveryId) {
    return request({
        url: '/deliveryDevice/getOrderHostDevice',
        method: 'POST',
        data: { deliveryId },
    });
}
// 装车订单 - 编辑
export function orderEdit(data) {
    return request({
        url: '/delivery/updateDeliveryInfo',
        method: 'POST',
        data,
    });
}
// 查看耳标设备
export function deviceEarList(data) {
    return request({
        url: '/deliveryDevice/pageJbqList',
        method: 'POST',
        data,
    });
}
// 查看运单所有设备（耳标+项圈）
export function deviceAllList(data) {
    return request({
        url: '/deliveryDevice/pageDeviceList',
        method: 'POST',
        data,
    });
}
// 运单分配设备列表查询（支持智能耳标和智能项圈）
export function deviceList(data) {
    // 根据设备类型选择不同的API端点
    if (data.deviceType === 2) {
        // 智能耳标
        return request({
            url: '/jbqClient/pageQuery',
            method: 'POST',
            data,
        });
    } else if (data.deviceType === 3) {
        // 智能项圈
        return request({
            url: '/xqClient/pageQuery',
            method: 'POST',
            data,
        });
    } else {
        // 默认查询智能耳标
        return request({
            url: '/jbqClient/pageQuery',
            method: 'POST',
            data,
        });
    }
}

// 分配耳标设备
export function deviceAssign(data) {
    return request({
        url: '/delivery/arrangeJbq',
        method: 'POST',
        data,
    });
}
// 订单详情
export function orderDetail(id) {
    return request({
        url: `/delivery/viewDeliveryOrder?deliveryId=${id}`,
        method: 'GET',
    });
}

// 主机设备查询
export function hostList(data) {
    return request({
        url: '/jbqServer/serverList',
        method: 'POST',
        data,
    });
}
// 查询详情
export function orderLoadDetail(data) {
    return request({
        url: '/wechatDelivery/carLoadInfo',
        method: 'POST',
        data,
    });
}
// 装车保存接口
export function orderLoadSave(data) {
    return request({
        url: '/wechatDelivery/updateLoadInfo',
        method: 'POST',
        data,
    });
}

// 运送清单 - 创建（PC端）
export function createDelivery(data) {
    return request({
        url: '/delivery/create',
        method: 'POST',
        data,
    });
}

// 查询可用主机设备列表
export function getAvailableServers(data) {
    return request({
        url: '/jbqServer/pageQuery',
        method: 'POST',
        data,
    });
}

// 查询可用耳标设备列表
export function getAvailableEartags(data) {
    return request({
        url: '/jbqClient/pageQuery',
        method: 'POST',
        data,
    });
}

// 查询可用项圈设备列表（如果项目中有项圈设备）
export function getAvailableCollars(data) {
    return request({
        url: '/xqClient/pageQuery',
        method: 'POST',
        data,
    });
}

// 更新运单状态
export function updateDeliveryStatus(data) {
    return request({
        url: '/delivery/updateStatus',
        method: 'POST',
        data,
    });
}

// 运送清单 - 列表查询
export function shippingList(data) {
    return request({
        url: '/delivery/pageQueryList',
        method: 'POST',
        data,
    });
}

// ==================== 订单管理接口 ====================
// 订单列表查询
export function orderPageQuery(data) {
    return request({
        url: '/order/list',
        method: 'POST',
        data,
    });
}

// 新增订单
export function orderAddNew(data) {
    return request({
        url: '/order/add',
        method: 'POST',
        data,
    });
}

// 更新订单
export function orderUpdate(data) {
    return request({
        url: '/order/edit',
        method: 'POST',
        data,
    });
}

// 删除订单
export function orderDelete(id) {
    return request({
        url: `/order/delete?id=${id}`,
        method: 'GET',
    });
}

// 查询订单详情
export function orderGetDetail(id) {
    return request({
        url: `/order/detail?id=${id}`,
        method: 'GET',
    });
}

// 批量更新订单的delivery_id字段
export function updateOrderDeliveryId(data) {
    return request({
        url: '/order/updateDeliveryId',
        method: 'POST',
        data,
    });
}

// 批量导入订单
export function orderBatchImport(data) {
    return request({
        url: '/order/batchImport',
        method: 'POST',
        data,
    });
}

// ==================== 运送清单管理新增 API ====================

// 逻辑删除运送清单
export function deleteDeliveryLogic(id) {
    return request({
        url: `/delivery/deleteLogic?id=${id}`,
        method: 'POST',
    });
}

// 打包运送清单文件（图片、视频、信息）
export function downloadDeliveryPackage(id) {
    return request({
        url: `/delivery/downloadPackage?id=${id}`,
        method: 'GET',
        responseType: 'blob', // 重要：用于下载文件
    });
}

// 下载验收单（生成HTML格式的牛只发车验收单）
export function downloadAcceptanceForm(id) {
    return request({
        url: `/delivery/downloadAcceptanceForm?id=${id}`,
        method: 'GET',
        responseType: 'blob', // 用于下载HTML文件
    });
}

// 获取运送清单详情（用于编辑）
export function getDeliveryDetail(id) {
    return request({
        url: `/delivery/detail?id=${id}`,
        method: 'GET',
    });
}

// 更新运送清单信息
export function updateDeliveryInfo(data) {
    return request({
        url: '/delivery/updateDeliveryInfo',
        method: 'POST',
        data,
    });
}

// ==================== 订单关联管理 API ====================

// 解除订单与运送清单的关联
export function unbindOrderFromDelivery(orderId) {
    return request({
        url: '/order/unbindFromDelivery',
        method: 'POST',
        data: { orderId },
    });
}

// 查询运送清单关联的订单列表
export function getOrdersByDeliveryId(deliveryId) {
    return request({
        url: '/order/list',
        method: 'POST',
        data: {
            deliveryId: deliveryId,
            pageNum: 1,
            pageSize: 1000, // 获取所有关联的订单
        },
    });
}