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

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

export namespace InspectionApi {
  export interface Template {
    id?: number;
    code: string;
    name: string;
    bizType: number;
    vehicleType?: string;
    status: number;
    remark?: string;
    items?: TemplateItem[];
  }

  export interface TemplateItem {
    id?: number;
    category: string;
    itemName: string;
    itemCode: string;
    inputType: string;
    sort: number;
    required: number;
  }

  export interface RecordDetail {
    id: number;
    recordCode: string;
    templateId: number;
    sourceType: number;
    sourceId: number;
    vehicleId: number;
    inspectorName?: string;
    inspectionTime?: string;
    status: number;
    overallResult?: number;
    remark?: string;
    items: RecordItem[];
  }

  export interface RecordItem {
    id: number;
    itemCode: string;
    category: string;
    itemName: string;
    inputType: string;
    result?: number;
    value?: string;
    remark?: string;
    imageUrls?: string;
  }
}

export function getInspectionTemplatePage(params: PageParam) {
  return requestClient.get<PageResult<InspectionApi.Template>>(
    '/asset/inspection-template/page',
    { params },
  );
}

export function getInspectionTemplate(id: number) {
  return requestClient.get<InspectionApi.Template>(
    `/asset/inspection-template/get?id=${id}`,
  );
}

export function createInspectionTemplate(data: InspectionApi.Template) {
  return requestClient.post('/asset/inspection-template/create', data);
}

export function updateInspectionTemplate(data: InspectionApi.Template) {
  return requestClient.put('/asset/inspection-template/update', data);
}

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

export function getInspectionRecord(id: number) {
  return requestClient.get<InspectionApi.RecordDetail>(
    `/asset/inspection-record/get?id=${id}`,
  );
}

export function updateInspectionRecordItem(data: {
  id: number;
  result?: number;
  value?: string;
  remark?: string;
  imageUrls?: string;
}) {
  return requestClient.put('/asset/inspection-record/update-item', data);
}

export function completeInspection(id: number, inspectorName: string) {
  return requestClient.post(
    `/asset/inspection-record/complete?id=${id}&inspectorName=${inspectorName}`,
  );
}
