import request from '/@/utils/request';
import type {
	CooperationUnitFormT,
	CooperationUnitQueryT,
	CooperationUnitPageT,
	CooperationUnitDetailT,
} from '/@/views/investment/cooperationUnit/interface/types';
import { startFlow } from '/@/api/flow/flow/index'

/**
 * 获取对外合作单位登记分页列表
 * @param query 查询参数
 * @returns 返回分页数据
 */
export const getCooperationUnitPageAPI = (query?: CooperationUnitQueryT) => {
	return request<CooperationUnitPageT>({
		url: '/biz/investment/cooperation-unit/page',
		method: 'get',
		params: query,
	});
};

export interface ExternalCooperationUnitQueryT {
	page?: number;
	size?: number;
	cooperationName?: string;
	cooperationNature?: string;
	unitContactPerson?: string;
}

export interface ExternalCooperationUnitItemT {
	id: number | string;
	cooperationName: string;
	province?: number | string;
	city?: number | string;
	cooperationNature?: string;
	cooperationIndustry?: string;
	proposedBusinessCooperation?: string;
	cooperationAdvantage?: string;
	cooperationStatus?: number | string;
	startTime?: string;
	endTime?: string;
	unitAddress?: string;
	unitContactPerson?: string;
	contactPersonDept?: string;
	contactPersonPhone?: string;
	potentialCooperation?: string;
	businessConnectionCompany?: string;
	shareholdingRatio?: number | string;
	createBy?: string;
	createTime?: string;
	updateBy?: string;
	updateTime?: string;
	delFlag?: string;
	processInstanceId?: string;
	status?: string;
}

export interface ExternalCooperationUnitPageT {
	records: ExternalCooperationUnitItemT[];
	total: number;
	current?: number;
	size?: number;
}

export const getExternalCooperationUnitsPageAPI = (query?: ExternalCooperationUnitQueryT) => {
	return request<ExternalCooperationUnitPageT>({
		url: '/admin/externalCooperationUnits/page',
		method: 'post',
		data: query,
	});
};

export const viewExternalCooperationUnitAPI = (id: string | number) => {
	return request({
		url: '/admin/externalCooperationUnits/view',
		method: 'get',
		params: { id },
	});
};

export const addExpertInformationAPI = (data: Record<string, any>) => {
	return request({
		url: '/admin/expertInformation/add',
		method: 'post',
		data,
	});
};
// 根据模板id 获取专家信息
export const getExpertInformationByTemplateIdAPI = (templateId: string) => {
    return request({
        url: '/admin/expertInformation/getByTemplateId',
        method: 'get',
        params: {
            templateId
        }
    });
}
export const getExpertInformationPageAPI = (query?: {
	page?: number;
	size?: number;
	expertName?: string;
	technologyField?: string;
	externalStatus?: string;
}) => {
	return request({
		url: '/admin/expertInformation/page',
		method: 'post',
		data: query,
	});
};

export const updateExpertInformationAPI = (data: Record<string, any>) => {
	return request({
		url: '/admin/expertInformation/update',
		method: 'put',
		data,
	});
};

export const deleteExpertInformationsAPI = (ids: Array<string | number>) => {
	return request({
		url: '/admin/expertInformation/delete',
		method: 'put',
		headers: {
			'Content-Type': 'application/x-www-form-urlencoded',
			'Enc-Flag': true,
		},
		data: new URLSearchParams({
			ids: ids.join(','),
		}).toString(),
	});
};

export const getExpertInformationDetailAPI = (id: string | number) => {
	return request({
		url: '/admin/expertInformation/view',
		method: 'get',
		params: { id },
	});
};
export const getExpertInformationDetailByProcessInstanceId = (processInstanceId: any) => {
    return request({
        url: '/admin/expertInformation/getByProcessInstanceId',
        method: 'get',
        params: {
            processInstanceId
        }
    });
}

export const startExpertInformationFlow = async (params:{paramMap:Object,startUserId?:string,processInstanceId?:string}) => {
	return startFlow({ flowId: import.meta.env.VITE_FLOWID_13, ...params})
};

export const updateExternalCooperationUnitAPI = (data: Record<string, any>) => {
	return request({
		url: '/admin/externalCooperationUnits/update',
		method: 'put',
		data,
	});
};

export const deleteExternalCooperationUnitsAPI = (ids: Array<string | number>) => {
	return request({
		url: '/admin/externalCooperationUnits/delete',
		method: 'put',
		headers: {
			'Content-Type': 'application/x-www-form-urlencoded',
			'Enc-Flag': true,
		},
		data: new URLSearchParams({
			ids: ids.join(','),
		}).toString(),
	});
};

export const addExternalCooperationUnitAPI = (data: Record<string, any>) => {
	return request({
		url: '/admin/externalCooperationUnits/add',
		method: 'post',
		data,
	});
};

/**
 * 获取对外合作单位登记详情
 * @param id 主键ID
 * @returns 返回详情数据
 */
export const getCooperationUnitDetailAPI = (id: string) => {
	return request<CooperationUnitDetailT>({
		url: '/biz/investment/cooperation-unit/' + id,
		method: 'get',
	});
};

/**
 * 新增对外合作单位登记
 * @param data 表单数据
 * @returns 返回操作结果
 */
export const addCooperationUnitAPI = (data: CooperationUnitFormT) => {
	return request({
		url: '/biz/investment/cooperation-unit',
		method: 'post',
		data: data,
	});
};

/**
 * 更新对外合作单位登记
 * @param data 表单数据
 * @returns 返回操作结果
 */
export const updateCooperationUnitAPI = (data: CooperationUnitFormT) => {
	return request({
		url: '/biz/investment/cooperation-unit',
		method: 'put',
		data: data,
	});
};

/**
 * 删除对外合作单位登记
 * @param ids 主键ID数组
 * @returns 返回操作结果
 */
export const deleteCooperationUnitAPI = (ids: string[]) => {
	return request({
		url: '/biz/investment/cooperation-unit',
		method: 'delete',
		data: ids,
	});
};

/**
 * 保存为草稿
 * @param data 表单数据
 * @returns 返回操作结果
 */
export const saveDraftAPI = (data: CooperationUnitFormT) => {
	return request({
		url: '/biz/investment/cooperation-unit/draft',
		method: 'post',
		data: data,
	});
};

/**
 * 发起审批
 * @param id 主键ID
 * @returns 返回操作结果
 */
export const initiateApprovalAPI = (id: string) => {
	return request({
		url: '/biz/investment/cooperation-unit/approval/' + id,
		method: 'post',
	});
};

/**
 * 调用模板
 * @param templateId 模板ID
 * @returns 返回模板数据
 */
export const loadTemplateAPI = (templateId: string) => {
	return request<CooperationUnitFormT>({
		url: '/biz/investment/cooperation-unit/template/' + templateId,
		method: 'get',
	});
};

/**
 * 存为模板
 * @param data 表单数据
 * @returns 返回操作结果
 */
export const saveTemplateAPI = (data: { name: string; data: CooperationUnitFormT }) => {
	return request({
		url: '/biz/investment/cooperation-unit/template',
		method: 'post',
		data: data,
	});
};

/**
 * 导出Excel
 * @param query 查询参数
 * @returns 返回文件流
 */
export const exportCooperationUnitAPI = (query?: CooperationUnitQueryT) => {
	return request({
		url: '/biz/investment/cooperation-unit/export',
		method: 'get',
		params: query,
		responseType: 'blob',
	});
};

/**
 * 导入Excel
 * @param file 文件对象
 * @returns 返回操作结果
 */
export const importCooperationUnitAPI = (file: File) => {
	const formData = new FormData();
	formData.append('file', file);
	return request({
		url: '/biz/investment/cooperation-unit/import',
		method: 'post',
		data: formData,
		headers: {
			'Content-Type': 'multipart/form-data',
		},
	});
};
/**
 * /admin/externalCooperationUnits/getByTemplateId
 * */
export const getExternalCooperationUnitsByTemplateId = (templateId: string) => {
    return request({
        url: '/admin/externalCooperationUnits/getByTemplateId',
        method: 'get',
        params: {
            templateId
        }
    });
}