/*
 *    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: 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 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'
	});
}

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

