/*
 *    Copyright (c) 2018-2025, cyweb All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 */

import request from '/@/utils/request';

/**
 * 分页查询
 * @param params 查询参数
 */
export function getPage(params?: any) {
	return request({
		url: '/purchase/purchasingapply/page',
		method: 'get',
		params
	});
}

/**
 * 通过id查询
 * @param id ID
 */
export function getObj(id: string | number) {
	return request({
		url: '/purchase/purchasingapply/' + id,
		method: 'get'
	});
}

/**
 * 新增采购申请（提交并启动流程）
 * @param obj 对象数据
 */
export function addObj(obj: any) {
	return request({
		url: '/purchase/purchasingapply/submit',
		method: 'post',
		data: obj
	});
}

/**
 * 暂存采购申请
 * @param obj 对象数据
 */
export function tempStore(obj: any) {
	return request({
		url: '/purchase/purchasingapply/temp-store',
		method: 'post',
		data: obj
	});
}

/**
 * 提交采购申请（启动流程）
 * @param obj 对象数据
 */
export function submitObj(obj: any) {
	return request({
		url: '/purchase/purchasingapply/submit',
		method: 'post',
		data: obj
	});
}

/**
 * 分配招标代理（指定或随机），仅学校统一采购或部门自行采购且委托采购中心采购时可用
 * @param applyId 采购申请ID
 * @param mode random | designated
 * @param agentId 指定模式时的代理ID
 */
export function assignAgent(applyId: number | string, mode: 'random' | 'designated', agentId?: string) {
	return request({
		url: '/purchase/purchasingapply/assign-agent',
		method: 'post',
		data: { id: String(applyId), mode, agentId: agentId || undefined }
	});
}

/**
 * 发送招标代理：将采购项目发送给已分配的招标代理
 * @param applyId 采购申请ID
 */
export function sendToAgent(applyId: number | string) {
	return request({
		url: '/purchase/purchasingapply/sendToAgent',
		method: 'post',
		data: { id: String(applyId) }
	});
}

/**
 * 撤回招标代理：撤回已发送的招标代理
 * @param applyId 采购申请ID
 */
export function revokeAgent(applyId: number | string) {
	return request({
		url: '/purchase/purchasingapply/revokeAgent',
		method: 'post',
		data: { id: String(applyId) }
	});
}

/**
 * 保存实施采购方式（分步骤实施采购-第一步）
 * @param id 采购申请ID
 * @param implementType 实施采购方式：1-自行组织采购，2-委托代理采购
 */
export function saveImplementType(id: number | string, implementType: string) {
	return request({
		url: '/purchase/purchasingapply/save-implement-type',
		method: 'post',
		data: { id: id, implementType }
	});
}

/**
 * 修改采购申请
 * @param obj 对象数据
 */
export function editObj(obj: any) {
	return request({
		url: '/purchase/purchasingapply/edit',
		method: 'post',
		data: obj
	});
}

/**
 * 删除采购申请
 * @param id ID
 */
export function delObj(id: number) {
	return request({
		url: '/purchase/purchasingapply/delete',
		method: 'post',
		data: id
	});
}

/**
 * 获取采购申请附件列表
 * @param purchaseId 采购申请ID
 */
export function getApplyFiles(purchaseId: string | number) {
	return request({
		url: '/purchase/purchasingfiles/applyFiles',
		method: 'post',
		params: { purchaseId }
	});
}

/**
 * 履约验收关联的合同列表（未被使用的合同）
 * @param params 可选参数，如 id 等
 */
export function getContracts(params?: any) {
	return request({
		url: '/purchase/purchasingapply/getContracts',
		method: 'get',
		params
	});
}

/**
 * 实施采购：上传招标文件并关联到申请单（可同时保存采购代表人方式与人员）
 * @param id 采购申请ID
 * @param fileIds 已上传的招标文件ID列表（fileType=130）
 * @param implementType 实施采购方式 1:自行组织采购 2:委托代理采购
 * @param representorTeacherNo 需求部门初审-指定采购代表人（单人）
 * @param representors 需求部门初审-部门多人逗号分隔
 */
export function implementApply(
	id: number,
	fileIds: string[],
	implementType?: string,
	representorTeacherNo?: string,
	representors?: string
) {
	return request({
		url: '/purchase/purchasingapply/implement',
		method: 'get',
		params: { id, fileIds, implementType, representorTeacherNo, representors }
	});
}

/**
 * 发起招标文件审批流程（需已实施采购并上传招标文件）
 * @param id 采购申请ID
 * @param representorTeacherNo 需求部门初审-指定采购代表人（单人，用户ID或工号）
 * @param representors 需求部门初审-部门多人由系统抽取（多人，用户ID或工号逗号分隔）
 */
export function startFileFlow(
	id: number,
	representorTeacherNo?: string,
	representors?: string
) {
	return request({
		url: '/purchase/purchasingapply/startFileFlow',
		method: 'post',
		data: { id, representorTeacherNo, representors }
	});
}

/**
 * 获取部门下人员（用于选采购代表人）
 */
export function getDeptMembers() {
	return request({
		url: '/purchase/purchasingapply/getDeptMembers',
		method: 'get'
	});
}

/**
 * 获取二级部门列表（供履约验收选择部门用）
 */
export function getSecondDeptList() {
	return request({
		url: '/purchase/purchasingapply/getSecondDeptList',
		method: 'get'
	});
}

/**
 * 搜索教职工（供履约验收选择人员用）
 * @param keyword 搜索关键字（姓名/工号）
 */
export function searchTeachers(keyword: string) {
	return request({
		url: '/purchase/purchasingapply/searchTeachers',
		method: 'get',
		params: { keyword }
	});
}

/**
 * 保存采购代表（指定单人或部门多人）
 * @param id 采购申请ID
 * @param representorTeacherNo 指定采购代表人（单人，用户ID或工号）
 * @param representors 部门多人由系统抽取（多人，用户ID或工号逗号分隔）
 * @param identity 身份：purchase_rep-采购代表，judge-评委
 */
export function saveRepresentor(id: number, representorTeacherNo?: string, representors?: string, identity?: string) {
	return request({
		url: '/purchase/purchasingapply/save-representor',
		method: 'post',
		data: { id, representorTeacherNo, representors, representorType: identity }
	});
}

/**
 * 文件归档：按文件类型打包下载该申请单下所有附件的下载地址（GET 请求，浏览器直接下载 zip）
 * @param purchaseId 采购申请ID
 */
export function getArchiveDownloadUrl(purchaseId: string | number) {
	return `/purchase/purchasingfiles/archive?purchaseId=${encodeURIComponent(String(purchaseId))}`;
}

/**
 * 下载审批表：导出采购审批表 Word 文档（apply.docx 模板，仅占位符替换）
 * @param id 采购申请ID
 */
export function getApplyTemplateDownloadUrl(id: string | number) {
	return `/purchase/purchasingapply/export-apply-template?id=${encodeURIComponent(String(id))}`;
}

/**
 * 下载文件审批表：导出招标文件审批表 Word 文档（fileapply.docx 模板）
 * @param id 采购申请ID
 */
export function getFileApplyTemplateDownloadUrl(id: string | number) {
	return `/purchase/purchasingapply/export-file-apply-template?id=${encodeURIComponent(String(id))}`;
}

// ==================== 招标代理专用接口 ====================

/**
 * 招标代理获取待处理列表
 * @param params 分页参数
 */
export function getAgentPendingList(params?: any) {
	return request({
		url: '/purchase/purchasingdoc/agent/list',
		method: 'get',
		params
	});
}

/**
 * 招标代理获取采购需求文件列表
 * @param applyId 采购申请ID
 */
export function getAgentRequirementFiles(applyId: number | string) {
	return request({
		url: `/purchase/purchasingdoc/agent/requirement/${applyId}`,
		method: 'get'
	});
}

/**
 * 招标代理获取项目详情（仅返回采购编号和项目名称）
 * @param applyId 采购申请ID
 */
export function getAgentApplyDetail(applyId: number | string) {
	return request({
		url: `/purchase/purchasingdoc/agent/detail/${applyId}`,
		method: 'get'
	});
}

/**
 * 招标代理上传招标文件
 * @param data 文件数据
 */
export function uploadAgentDoc(data: any) {
	return request({
		url: '/purchase/purchasingdoc/upload',
		method: 'post',
		data
	});
}

/**
 * 招标代理重新上传招标文件
 * @param data 文件数据
 */
export function reuploadAgentDoc(data: any) {
	return request({
		url: '/purchase/purchasingdoc/reupload',
		method: 'post',
		data
	});
}

/**
 * 获取招标文件列表
 * @param applyId 采购申请ID
 */
export function getDocList(applyId: number | string) {
	return request({
		url: `/purchase/purchasingdoc/list/${applyId}`,
		method: 'get'
	});
}

/**
 * 根据文件ID下载采购附件（返回blob）
 * @param fileId 文件ID
 */
export function downloadFileById(fileId: string) {
	return request({
		url: '/purchase/purchasingfiles/downloadById',
		method: 'get',
		params: { fileId },
		responseType: 'blob'
	});
}

export function previewFileById(fileId: string) {
	return request({
		url: '/purchase/purchasingfiles/previewById',
		method: 'get',
		params: { fileId },
		responseType: 'blob'
	});
}

/**
 * 批量获取文件下载地址列表
 * @param purchaseId 采购申请ID
 */
export function listDownloadUrls(purchaseId: string | number) {
	return request({
		url: '/purchase/purchasingfiles/listDownloadUrls',
		method: 'get',
		params: { purchaseId }
	});
}

/**
 * 更新采购材料（部门申请人或负责人重新上传文件）
 * @param data 包含purchaseId和fileIds的对象
 */
export function updateFiles(data: { purchaseId: string; fileIds: string[] }) {
	return request({
		url: '/purchase/purchasingfiles/updateFiles',
		method: 'post',
		data
	});
}

