/**
 * 物料分类接口
 */
import { request } from '../../utils/request'

/**
 * 根据父ID查询子分类列表
 * @param parentId 父分类ID
 * @returns 
 */
export const getItemTypeListByParentId = (parentId: number): Promise<any> => {
	return request({
		url: `/mes/md/itemtype/listByParentId?parentId=${parentId}`,
		method: 'GET'
	})
}

/**
 * 物料列表查询
 * @param itemTypeCode 物料分类编码（模糊查询）
 * @param pageNum 页码
 * @param pageSize 每页数量
 * @param keyword 物料名称关键字
 * @returns 
 */
export const getItemList = (itemTypeCode: string, pageNum: number, pageSize: number, keyword: string): Promise<any> => {
	let url = `/mes/md/mditem/listByTypeCode?itemTypeCode=${itemTypeCode}&pageNum=${pageNum}&pageSize=${pageSize}`
	if (keyword != null && keyword.length > 0) {
		url += `&itemName=${encodeURIComponent(keyword)}`
	}
	return request({
		url: url,
		method: 'GET'
	})
}

/**
 * 保存采购申请
 * @param data 采购申请数据
 * @returns 
 */
export const savePurchaseApply = (data: UTSJSONObject | null): Promise<any> => {
	return request({
		url: '/mes/wm/purchaseApply',
		method: 'POST',
		data: data
	})
}

/**
 * 确认采购申请
 * @param data 采购申请数据
 * @returns 
 */
export const confirmPurchaseApply = (data: UTSJSONObject | null): Promise<any> => {
	return request({
		url: '/mes/wm/purchaseApply/confirm',
		method: 'PUT',
		data: data
	})
}

/**
 * 获取采购申请列表
 * @param pageNum 页码
 * @param pageSize 每页数量
 * @param keyword 关键字
 * @param status 状态
 * @param userId 用户ID
 * @param pendingReceive 是否待领取筛选
 * @returns 
 */
export const getPurchaseApplyList = (pageNum: number, pageSize: number, keyword: string, status: string | null, userId: string, pendingReceive: boolean): Promise<any> => {
	let url = `/mes/wm/purchaseApply/mobileList?pageNum=${pageNum}&pageSize=${pageSize}&applyUserId=${userId}`
	if (keyword != null && keyword.length > 0) {
		url += `&applyCode=${encodeURIComponent(keyword)}`
	}
	if (status != null && status.length > 0) {
		url += `&status=${encodeURIComponent(status)}`
	}
	if (pendingReceive != null && pendingReceive === true) {
		url += `&pendingReceive=true`
	}
	return request({
		url: url,
		method: 'GET'
	})
}

/**
 * 获取采购申请详情
 * @param applyId 申请ID
 * @returns 
 */
export const getPurchaseApplyById = (applyId: string): Promise<any> => {
	return request({
		url: `/mes/wm/purchaseApply/${applyId}`,
		method: 'GET'
	})
}

/**
 * 新增物料
 * @param data 物料数据
 * @returns 
 */
export const addMaterial = (data: UTSJSONObject | null): Promise<any> => {
	return request({
		url: '/mes/md/mditem',
		method: 'POST',
		data: data
	})
}

/**
 * 删除采购申请
 * @param applyId 申请ID
 * @returns 
 */
export const deletePurchaseApply = (applyId: string): Promise<any> => {
	return request({
		url: `/mes/wm/purchaseApply/${applyId}`,
		method: 'DELETE'
	})
}

/**
 * 获取单位列表
 * @returns 
 */
export const getMeasureList = (): Promise<any> => {
	return request({
		url: '/mes/md/unitmeasure/selectall',
		method: 'GET'
	})
}

/**
 * 删除物料
 * @param itemId 物料ID
 * @returns 
 */
export const deleteMaterial = (itemId: string): Promise<any> => {
	return request({
		url: `/mes/md/mditem/${itemId}`,
		method: 'DELETE'
	})
}

/**
 * 获取物料申请待领取数量
 * @param userId 用户ID
 * @returns 
 */
export const getPendingReceiveApplyCount = (userId: string): Promise<any> => {
	return request({
		url: `/mes/wm/purchaseApply/countPendingReceive?applyUserId=${userId}`,
		method: 'GET'
	})
}
