import type { PageParam, PageResult } from '@vben/request';

import { requestClient } from '#/api/request';

export namespace AssetReturnOrderApi {
  export interface ReturnOrderVehicle {
    id?: number;
    vehicleId?: number;
    plateNo?: string;
    vin?: string;
    brand?: string;
    model?: string;
    returnMileage?: number;
    returnHydrogenLevel?: number;
    deliveryHydrogenLevel?: number;
    hydrogenDiff?: number;
    hydrogenUnitPrice?: number;
    hydrogenRefundAmount?: number;
    checkList?: string;
    defectPhotos?: string;
    vehicleDamageFee?: number;
    toolDamageFee?: number;
    unpaidMaintenanceFee?: number;
    unpaidRepairFee?: number;
    otherFee?: number;
    inspectionRecordId?: number;
  }

  export interface ReturnOrder {
    id?: number;
    orderCode?: string;
    contractId: number;
    contractCode?: string;
    projectName?: string;
    customerId?: number;
    customerName?: string;
    returnDate: string;
    returnPerson: string;
    returnLocation?: string;
    returnReason?: string;
    returnReasonDesc?: string;
    totalRefundAmount?: number;
    depositRefund?: number;
    hydrogenRefund?: number;
    otherCharges?: number;
    returnPhotos?: string;
    sourceType?: number;
    sourceId?: number;
    deliveryOrderId?: number;
    status?: number;
    approvalStatus?: number;
    vehicles?: ReturnOrderVehicle[];
    createTime?: Date;
  }
}

export function getReturnOrderPage(params: PageParam) {
  return requestClient.get<PageResult<AssetReturnOrderApi.ReturnOrder>>(
    '/asset/return-order/page',
    { params },
  );
}

export function getReturnOrder(id: number) {
  return requestClient.get<AssetReturnOrderApi.ReturnOrder>(
    `/asset/return-order/get?id=${id}`,
  );
}

export function createReturnOrder(data: AssetReturnOrderApi.ReturnOrder) {
  return requestClient.post('/asset/return-order/create', data);
}

export function updateReturnOrder(data: AssetReturnOrderApi.ReturnOrder) {
  return requestClient.put('/asset/return-order/update', data);
}

export function deleteReturnOrder(id: number) {
  return requestClient.delete(`/asset/return-order/delete?id=${id}`);
}

export function completeReturnOrderInspection(id: number) {
  return requestClient.put(`/asset/return-order/complete-inspection?id=${id}`);
}

export function settleReturnOrder(id: number) {
  return requestClient.put(`/asset/return-order/settle?id=${id}`);
}

export function createReturnOrderFromDelivery(
  deliveryOrderId: number,
  vehicleIds: number[],
) {
  return requestClient.post('/asset/return-order/create-from-delivery', {
    deliveryOrderId,
    vehicleIds,
  });
}

export function startVehicleInspection(returnOrderVehicleId: number) {
  return requestClient.post(
    `/asset/return-order/start-vehicle-inspection?returnOrderVehicleId=${returnOrderVehicleId}`,
  );
}

export function completeVehicleInspection(returnOrderVehicleId: number) {
  return requestClient.post(
    `/asset/return-order/complete-vehicle-inspection?returnOrderVehicleId=${returnOrderVehicleId}`,
  );
}

export function submitReturnOrderApproval(id: number) {
  return requestClient.post(`/asset/return-order/submit-approval?id=${id}`, {});
}

export function withdrawReturnOrderApproval(id: number) {
  return requestClient.post(
    `/asset/return-order/withdraw-approval?id=${id}`,
    {},
  );
}
