import { http } from '@/utils/http/axios';
import type { QueryPageRequest, QueryPageResponse } from '@/types/basic-query';

/**
 * 隐患台账单条记录（与接口文档 ProductionHiddenDangerPageRes 一致）
 */
export interface HiddenDangerItem {
  id: number;
  sourceType?: number; // 数据来源：1-员工上报 2-台账新增 3-台账导入 4-区域检查
  sourceRefId?: number;
  dangerCode: string;
  dangerProblem: string;
  /** 扣分原因名称（下拉显示，若无则用 dangerProblem） */
  deductionReason?: string;
  typeId: number;
  typeName: string;
  reasonId: number;
  reasonName: string;
  taskSource: string;
  rectificationRequirement: string;
  rectificationDeadline: string;
  rectificationDepartmentIds: string;
  rectificationDepartmentNames: string;
  rectificationResponsiblePerson: string;
  reviewDepartmentId?: number;
  reviewDepartmentName?: string;
  reviewPersonId?: number;
  reviewPersonName: string;
  isDrawLessonsPush: number;
  drawLessonsContent?: string;
  drawLessonsDepartmentIds?: string;
  drawLessonsDepartmentNames?: string;
  drawLessonsDeadline?: string;
  entryTime?: string;
  currentNode?: string;
  associationOtId?: number;
  statusId: number;
  statusName: string;
  statusOrder?: number;
  creatorId?: number;
  creatorName: string;
  createdAt: string;
  updatedAt?: string;
  isDeleted?: number;
  rectificationCompletionStatus?: string;
  rectificationCompletionTime?: string;
  attachments?: string;
  canEdit?: boolean;
  canDelete?: boolean;
  canIssue?: boolean;
  canRectify?: boolean;
  canReview?: boolean;
  canDeduct?: boolean;
  canView?: boolean;
  /** 审查不通过原因（详情/整改页展示） */
  reviewRejectReason?: string;
}

/**
 * 分页查询 queryParam（与接口文档一致）
 * status: 0全部 1上级检查 2院内自查 3专家安全评估 4待下发 5待整改 6待复查 7已完成
 */
export interface HiddenDangerQueryParam {
  dangerCode?: string;
  typeId?: number;
  reasonId?: number;
  rectificationDepartmentId?: number;
  status?: number;
  startDate?: string;
  endDate?: string;
  keyword?: string;
  /** 兼容旧字段，映射到 keyword 或单独传 */
  dangerProblem?: string;
  taskSource?: string;
  statusId?: number;
}

/**
 * 1.1 分页查询隐患台账
 * POST /api/productionHiddenDanger/queryPage
 */
export function queryHiddenDangerPage(
  query: QueryPageRequest<HiddenDangerQueryParam>,
) {
  return http.request<QueryPageResponse<HiddenDangerItem>>({
    url: '/productionHiddenDanger/queryPage',
    method: 'post',
    data: query,
  });
}

/**
 * 1.2 查询隐患台账详情
 * GET /api/productionHiddenDanger/queryDetail?id=xxx
 */
export function getHiddenDangerDetail(id: number) {
  return http.request<HiddenDangerItem>({
    url: '/productionHiddenDanger/queryDetail',
    method: 'get',
    params: { id },
  });
}

/**
 * 1.3 删除隐患台账
 * DELETE /api/productionHiddenDanger/delete?id=xxx
 */
export function deleteHiddenDanger(id: number) {
  return http.request({
    url: '/productionHiddenDanger/delete',
    method: 'delete',
    params: { id },
  });
}

/**
 * 1.4 新增隐患台账
 * POST /api/productionHiddenDanger/saveHiddenDanger
 */
export interface SaveHiddenDangerRequest {
  typeId: number;
  reasonId: number;
  dangerProblem: string;
  taskSource?: string;
  rectificationRequirement?: string;
  rectificationDepartmentIds?: string;
  rectificationResponsiblePerson?: string;
  rectificationDeadline: string;
  isDrawLessonsPush?: number;
  drawLessonsContent?: string;
  drawLessonsDepartmentIds?: string;
  drawLessonsDeadline?: string;
  reviewDepartmentId?: number;
  reviewPersonId?: number;
  reviewPersonName?: string;
  /** 附件（详情中附件参数） */
  attachments?: string;
}

export function saveHiddenDanger(data: SaveHiddenDangerRequest) {
  return http.request({
    url: '/productionHiddenDanger/saveHiddenDanger',
    method: 'post',
    data,
  });
}

/**
 * 1.5 修改隐患台账
 * PUT /api/productionHiddenDanger/updateHiddenDanger
 */
export interface UpdateHiddenDangerRequest extends SaveHiddenDangerRequest {
  id: number;
  statusId?: number;
}

export function updateHiddenDanger(data: UpdateHiddenDangerRequest) {
  return http.request({
    url: '/productionHiddenDanger/updateHiddenDanger',
    method: 'put',
    data,
  });
}

/**
 * 隐患台账下发
 * POST /api/productionHiddenDanger/issueHiddenDanger
 */
export interface IssueHiddenDangerRequest {
  danger_id: number;
  rectification_department_ids: string;
  rectification_responsible_person: string;
  dept_name: string;
  rectification_responsible_ids: string;
}

export function issueHiddenDanger(data: IssueHiddenDangerRequest) {
  return http.request({
    url: '/productionHiddenDanger/issueHiddenDanger',
    method: 'post',
    data,
  });
}

/**
 * 1.6 整改隐患
 * POST /api/productionHiddenDanger/rectifyHiddenDanger
 */
export interface RectifyHiddenDangerRequest {
  dangerId: number;
  typeId?: number;
  reasonId?: number;
  rectificationDeadline?: string;
  drawLessonsDepartmentIds?: string;
  drawLessonsDeadline?: string;
  rectificationPlan?: string;
  rectificationResponsiblePerson?: string;
  rectificationStartTime?: string;
  rectificationEndTime?: string;
  rectificationCompletionStatus?: string;
  rectificationCompletionTime?: string;
  attachments?: string;
  isCompleted?: number;
}

export function rectifyHiddenDanger(data: RectifyHiddenDangerRequest) {
  return http.request({
    url: '/productionHiddenDanger/rectifyHiddenDanger',
    method: 'post',
    data,
  });
}

/**
 * 1.7 复查隐患
 * POST /api/productionHiddenDanger/reviewHiddenDanger
 */
export interface ReviewHiddenDangerRequest {
  dangerId: number;
  typeId?: number;
  reasonId?: number;
  rectificationDeadline?: string;
  drawLessonsDepartmentIds?: string;
  drawLessonsDeadline?: string;
  reviewResult?: number;
  reviewConclusion?: string;
  reviewComments?: string;
  reviewReason?: string;
  reviewTime?: string;
  attachments?: string;
}

export function reviewHiddenDanger(data: ReviewHiddenDangerRequest) {
  return http.request({
    url: '/productionHiddenDanger/reviewHiddenDanger',
    method: 'post',
    data,
  });
}

/**
 * 1.8 导出隐患台账
 * POST /api/productionHiddenDanger/exportHiddenDanger
 * 请求体：直接传 queryParam 对象，不要包在 pageNumber/pageSize/queryParam 里
 */
export function exportHiddenDanger(query: HiddenDangerQueryParam) {
  return http.request(
    {
      url: '/productionHiddenDanger/exportHiddenDanger',
      method: 'post',
      data: query,
      responseType: 'blob',
    },
    {
      isTransformResponse: false,
    },
  );
}

/**
 * 1.9 导入隐患台账
 * POST /api/productionHiddenDanger/importHiddenDanger
 * multipart/form-data，字段名 file
 */
export function importHiddenDanger(data: FormData) {
  return http.request<string>({
    url: '/productionHiddenDanger/importHiddenDanger',
    method: 'post',
    data,
  });
}

/**
 * 扣分记录（接口文档未单独列出，保留原路径供联调）
 */
export interface HiddenDangerDeductPointsRequest {
  dangerId: number;
  initialScore?: number;
  deductionScore: number;
  deductionReason?: string;
  remark?: string;
  /** 扣分部门，多个用逗号分隔 */
  departmentIds?: string;
}

export function deductHiddenDangerPoints(data: HiddenDangerDeductPointsRequest) {
  return http.request({
    url: '/production/hiddenDanger/deductPoints',
    method: 'post',
    data,
  });
}
