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

/**
 * 月度扣分返回对象
 */
export interface PointDeductionItem {
  id: number;
  deductionTitle: string; // 扣分标题
  deductionDate: string; // 扣分日期 (YYYY-MM-DD)
  deductionScore: number; // 扣分值
  departmentName: string; // 部门名称
  hiddenDangerId?: number; // 隐患 id（与 deductionReason 平级）
  deductionReason?: string; // 扣分原因名称（与 hiddenDangerId 平级）
  status: boolean; // 状态 (true-启用, false-禁用)
  statusName: string; // 状态名称
}

/**
 * 月度扣分查询参数
 */
export interface PointDeductionQueryParam {
  deductionTitle?: string; // 扣分标题
  status?: boolean; // 状态
  选择数据的ID?: string[]; // 选择数据的ID
}

/**
 * 查询月度扣分列表
 */
export function queryMonthlyDeduction(query: QueryPageRequest<PointDeductionQueryParam>) {
  return http.request<QueryPageResponse<PointDeductionItem>>({
    url: '/monthlyDeduction/queryMonthlyDeduction',
    method: 'post',
    data: query,
  });
}

/**
 * 保存月度扣分请求参数（deductionReason、hiddenDangerId 与其他字段平级）
 */
export interface SaveMonthlyDeductionRequest {
  deductionTitle: string; // 扣分标题
  deductionDate: string; // 扣分日期 (YYYY-MM-DD)
  deductionScore: number; // 扣分值
  departmentName: string; // 部门名称
  hiddenDangerId?: number; // 隐患 id
  deductionReason?: string; // 扣分原因名称
  status: boolean; // 状态 (true-启用, false-禁用)
  id?: number; // ID（编辑时需要）
}

/**
 * 保存月度扣分（新增）
 */
export function saveMonthlyDeduction(data: SaveMonthlyDeductionRequest) {
  return http.request({
    url: '/monthlyDeduction/saveMonthlyDeduction',
    method: 'post',
    data,
  });
}

/**
 * 更新月度扣分（编辑）
 */
export function updateMonthlyDeduction(data: SaveMonthlyDeductionRequest) {
  return http.request({
    url: '/monthlyDeduction/updateMonthlyDeduction',
    method: 'put',
    data,
  });
}

/**
 * 删除月度扣分
 */
export function deleteDeduction(id: number) {
  return http.request({
    url: `/monthlyDeduction/deleteDeduction?id=${id}`,
    method: 'delete',
  });
}

/**
 * 查询月度扣分详情
 */
export function queryDeductionDetail(id: number) {
  return http.request<PointDeductionItem>({
    url: `/monthlyDeduction/queryDeductionDetail?id=${id}`,
    method: 'get',
  });
}

/**
 * 导出月度扣分请求参数
 */
export interface ExportMonthlyDeductionRequest {
  deductionTitle?: string; // 扣分标题
  status?: boolean; // 状态
}

/**
 * 导出月度扣分
 */
export function exportMonthlyDeduction(query: ExportMonthlyDeductionRequest) {
  return http.request({
    url: '/monthlyDeduction/exportMonthlyDeduction',
    method: 'post',
    data: query,
    responseType: 'blob',
  }, {
    isTransformResponse: false,
  });
}
