/*
 * @Author: liuJie
 * @Date: 2026-02-06 16:29:04
 * @LastEditors: liuJie
 * @LastEditTime: 2026-03-07 22:19:23
 * @Describe: 安全体系建设工作计划管理（管理员）
 */
import { http } from '@/utils/http/axios';
import type { QueryPageRequest, QueryPageResponse } from '@/types/basic-query';

/**
 * 安全体系建设工作计划列表项
 * 对应列表页表格字段
 */
export interface WorkPlanItem {
  categoryName: string;
  cooperateDeptIds: string;
  cooperateDeptNames: string;
  createdAt: Date;
  createdBy: number;
  createdByName: string;
  executGroupIds: string;
  executGroupNames?: any;
  feedbackCount: number;
  feedbackRatio: number;
  fileUrl: string;
  id: number;
  issuedCount: number;
  plannedComplateTime: Date;
  plannedEndTime?: any;
  plannedStartTime?: any;
  responsibleDeptIds: number;
  responsibleDeptNames: string;
  status: number;
  statusName: string;
  trainingPlanName: string;
  updatedAt: Date;
  workContent: string;
}

/**
 * 工作计划列表查询条件
 */
export interface WorkPlanQueryParam {
  keyword?: string;
  status?: number | string;
  categoryName?: string;
  startDate?: string;
  endDate?: string;
  /** 排序 */
  sortField?: string;
  sortOrder?: Boolean;
}

interface FileType {
  contentType: string;
  url: string;
}
/**
 * 新增/编辑工作计划请求体
 */
export interface SaveWorkPlanRequest {
  id?: number;
  workContent: string;
  categoryName: string;
  trainingPlanName: string;
  responsibleDeptIds: string | number[] | string[];
  cooperateDeptIds: string | number[] | string[];
  executGroupIds: string | number[] | string[];
  plannedComplateTime: string;
  fileUrl: string;
  /** 其他业务字段按需扩展 */
  [key: string]: unknown;
}
export interface IssueWorkPlanParams {
  id: number;
  executGroupIds: string | number[] | string[];
  plannedStartTime: string;
  plannedEndTime: string;
}

export interface QueryParam {
  planId: number | string;
  keyword: string;
  status: number | string;
  startDate: string;
  endDate: string;
  responsibleDeptIds: string | number[] | string[];
  sortOrder: boolean;
}

export interface ViewSenderParamsType {
  pageNumber: number;
  pageSize: number;
  queryParam: QueryParam;
}
/** 编辑时必传 id */
export interface UpdateWorkPlanRequest extends SaveWorkPlanRequest {
  id: number;
}

export interface ViewSenderQueryPageResponse {
  categoryName: string;
  cooperateDeptNames: string;
  createdAt: string;
  createdBy?: any;
  employeeId?: any;
  employeeName?: any;
  executGroupNames: string;
  fileUrl?: any;
  id: number;
  issuedByName?: any;
  planId: number;
  plannedComplateTime: string;
  responsibleDeptName: string;
  responsibleDeptNames: string;
  status: number;
  statusName?: any;
  trainingPlanName: string;
  updatedAt: string;
  workContent: string;
}

// 接口基础路径区分管理员 / 部门
// 实际路径如有差异，统一调整
const ADMIN_BASE = '/safetyWorkPlan/workPlan';

// -----------管理员端-----------------
/**
 * 查询安全体系建设工作计划列表（管理员端）
 */
export function queryWorkPlanPage(data: QueryPageRequest<WorkPlanQueryParam>) {
  return http.request<QueryPageResponse<WorkPlanItem>>({
    url: `${ADMIN_BASE}/queryPageAdmin`,
    method: 'post',
    data,
  });
}

/**
 * 新增安全体系建设工作计划
 */
export function saveWorkPlan(data: SaveWorkPlanRequest) {
  return http.request<number | void>({
    url: `${ADMIN_BASE}/save`,
    method: 'post',
    data,
  });
}

/**
 * 编辑安全体系建设工作计划
 */
export function updateWorkPlan(data: UpdateWorkPlanRequest) {
  return http.request<void>({
    url: `${ADMIN_BASE}/update`,
    method: 'post',
    data,
  });
}

/**
 * 查询安全体系建设工作计划详情
 */
export function queryWorkPlanDetail(id: number) {
  return http.request<SaveWorkPlanRequest>({
    url: `${ADMIN_BASE}/queryDetail`,
    // url: `/safetyWorkPlan/workPlanSendObj/queryDetail?id=${id}`,
    method: 'get',
    params: { id },
  });
}

/**
 * 查询部门安全体系建设工作计划详情
 */
export function queryWorkPlanDepartmentDetail(id: number) {
  return http.request<SaveWorkPlanRequest>({
    url: `/safetyWorkPlan/workPlanSendObj/queryDetail?id=${id}`,
    method: 'get',
  });
}

/**
 * 删除安全体系建设工作计划
 */
export function deleteWorkPlan(id: number) {
  return http.request<void>({
    url: `${ADMIN_BASE}/delete?id=${id}`,
    method: 'post',
  });
}

/**
 * 下发安全体系建设工作计划
 */
export function issueWorkPlan(data: IssueWorkPlanParams) {
  return http.request<void>({
    url: `${ADMIN_BASE}/assign`,
    method: 'post',
    data,
  });
}

/**
 * 作废安全体系建设工作计划
 */
export function cancelWorkPlan(id: number) {
  return http.request<number | void>({
    url: `${ADMIN_BASE}/invalid?id=${id}`,
    method: 'post',
  });
}

/**
 * @description: 查看发送对象列表
 */
export function queryViewSender(data: ViewSenderParamsType) {
  return http.request<QueryPageResponse<ViewSenderQueryPageResponse>>({
    url: `/safetyWorkPlan/workPlanSendObj/queryPage`,
    method: 'post',
    data,
  });
}

/**
 * @description: 导出
 * @return {*}
 */
export const exportTableData = (params)=> {
    return http.request({
        url: `/safety-culture/exportProductionEducationTrainingPlan`,
        method: 'get',
        params,
        responseType: 'blob',
    }, {
        isTransformResponse: false,
    });
}

// —————————————————部门端———————————————————
/**
 * 查询安全体系建设工作计划列表（部门端分页）
 */
export function queryWorkPlanDeptPage(query: QueryPageRequest<WorkPlanQueryParam>) {
  return http.request<QueryPageResponse<WorkPlanItem>>({
    url: `/safetyWorkPlan/workPlan/queryPageDept`,
    method: 'post',
    data: query,
  });
}

/**
 * 删除安全体系建设工作计划（部门）
 */
export function deleteWorkPlanDept(id: number) {
  return http.request<void>({
    url: `/safetyWorkPlan/workPlanSendObj/delete?id=${id}`,
    method: 'post',
  });
}
/**
 * 反馈安全体系建设（部门）
 */
export function issueWorkPlanDept(data) {
  return http.request<void>({
    url: `/safetyWorkPlan/workPlanSendObj/feedback`,
    method: 'post',
    data,
  });
}
