import { request } from '@/service/request';

/** 获取学校基础信息主表列表 */
export function fetchGetSchoolList(params?: Api.Art.SchoolSearchParams) {
  return request<Api.Art.SchoolList>({
    url: '/art/school/list',
    method: 'get',
    params
  });
}

/** 根据学校ID获取学校基础信息 */
export function fetchGetSchoolInfo(schoolId: CommonType.IdType) {
  return request<Api.Art.School>({
    url: `/art/school/${schoolId}`,
    method: 'get'
  });
}

/** 新增学校基础信息主表 */
export function fetchCreateSchool(data: Api.Art.SchoolWithDetailOperateParams) {
  return request<boolean | Api.Art.SchoolWithDetailSubmitStatus>({
    url: '/art/school',
    method: 'post',
    data
  });
}

/** 修改学校基础信息主表 */
export function fetchUpdateSchool(data: Api.Art.SchoolWithDetailOperateParams) {
  return request<boolean | Api.Art.SchoolWithDetailSubmitStatus>({
    url: '/art/school',
    method: 'put',
    data
  });
}

/** 批量删除学校基础信息主表 */
export function fetchBatchDeleteSchool(schoolIds: CommonType.IdType[]) {
  return request<boolean>({
    url: `/art/school/${schoolIds.join(',')}`,
    method: 'delete'
  });
}

/** 导入预检 */
export function fetchImportPreviewSchool(file: File) {
  const formData = new FormData();
  formData.append('file', file);

  return request<Api.Art.SchoolImportPreviewResult>({
    url: '/art/school/importPreview',
    method: 'post',
    data: formData
  });
}

/** 导入执行 */
export function fetchImportSchoolData(params: Api.Art.SchoolImportParams) {
  const formData = new FormData();
  formData.append('file', params.file);

  if (params.replaceAll !== undefined) {
    formData.append('replaceAll', String(params.replaceAll));
  }

  params.replaceMainCodes?.forEach(mainCode => {
    formData.append('replaceMainCodes', mainCode);
  });

  return request<Api.Art.SchoolImportExecuteResult>({
    url: '/art/school/importData',
    method: 'post',
    data: formData
  });
}
