import request from '@/utils/request';
import { AxiosPromise } from 'axios';
import { ProjectPurchaseVO, ProjectPurchaseForm, ProjectPurchaseQuery } from '@/api/oa/erp/projectPurchase/types';
import { ProjectPurchaseMaterialVO } from '@/api/oa/erp/projectPurchaseMaterial/types';

export interface ProjectMaterialsSourceDTO {
  relationId?: string | number;
  materialList?: ProjectPurchaseMaterialVO[];
}

/**
 * 查询项目采购信息列表
 * @param query
 * @returns {*}
 */

export const listProjectPurchase = (query?: ProjectPurchaseQuery): AxiosPromise<ProjectPurchaseVO[]> => {
  return request({
    url: '/oa/erp/projectPurchase/list',
    method: 'get',
    params: query
  });
};

/**
 * 查询项目采购信息详细
 * @param projectPurchaseId
 */
export const getProjectPurchase = (projectPurchaseId: string | number): AxiosPromise<ProjectPurchaseVO> => {
  return request({
    url: '/oa/erp/projectPurchase/' + projectPurchaseId,
    method: 'get'
  });
};

/**
 * 新增项目采购信息
 * @param data
 */
export const addProjectPurchase = (data: ProjectPurchaseForm) => {
  return request({
    url: '/oa/erp/projectPurchase',
    method: 'post',
    data: data
  });
};

/**
 * 修改项目采购信息
 * @param data
 */
export const updateProjectPurchase = (data: ProjectPurchaseForm) => {
  return request({
    url: '/oa/erp/projectPurchase',
    method: 'put',
    data: data
  });
};

/**
 * 提交项目采购并发起流程
 * @param data
 */
export const projectPurchaseSubmitAndFlowStart = (data: ProjectPurchaseForm) => {
  return request({
    url: '/oa/erp/projectPurchase/submitAndFlowStart',
    method: 'post',
    data: data
  });
};

/**
 * 删除项目采购信息
 * @param projectPurchaseId
 */
export const delProjectPurchase = (projectPurchaseId: string | number | Array<string | number>) => {
  return request({
    url: '/oa/erp/projectPurchase/' + projectPurchaseId,
    method: 'delete'
  });
};

/**
 * 下拉框查询项目采购信息列表
 * @param query
 * @returns {*}
 */
export function getErpProjectPurchaseList (query) {
    return request({
        url: '/oa/erp/projectPurchase/getErpProjectPurchaseList',
        method: 'get',
        params: query
    });
};

/**
 * 根据项目ID和备件标识查询项目物料
 * @param projectId
 * @param spareFlag
 */
/**
 * 导出项目采购审批单 PDF
 * @param projectPurchaseId 项目采购ID
 */
export const exportProjectPurchaseApprovalPdf = (projectPurchaseId: string | number) => {
  return request({
    url: '/oa/erp/projectPurchase/exportApprovalPdf/' + projectPurchaseId,
    method: 'get',
    responseType: 'blob'
  });
};

export const getProjectMaterialsByProjectId = (
  projectId: string | number,
  spareFlag: string,
  contractId?: string | number
): AxiosPromise<ProjectMaterialsSourceDTO> => {
  return request({
    url: `/oa/erp/projectPurchase/getContractMaterialsByProjectId/${projectId}/${spareFlag}`,
    method: 'get',
    params: contractId ? { contractId } : {}
  });
};
