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

// 资产列表查询参数
export interface AssetQueryParams {
	name?: string;
	type?: string;
	status?: number;
	pageNum?: number;
	pageSize?: number;
}

// 获取资产列表
export function listAssets(params?: AssetQueryParams) {
	return request({
		url: '/assets/asset/listAssets',
		method: 'get',
		params,
	});
}

// 更新资产状态
export function updateAssetStatus(id: string, status: number) {
	return request({
		url: '/assets/asset/updateAssetStatus',
		method: 'put',
		data: { id, status },
	});
}

// 删除资产
export function deleteAsset(id: string) {
	return request({
		url: '/assets/asset/deleteAsset',
		method: 'delete',
		params: { id },
	});
}

// 获取资产详情
export function getAsset(id: string) {
	return request({
		url: '/assets/asset/getAsset',
		method: 'get',
		params: { id },
	});
}

// 获取分类选项
export function getAssetCategories() {
	return request({
		url: '/assets/asset/getAsset',
		method: 'get',
	});
}

// 新增资产
export function createAsset(data: any) {
	return request({
		url: '/assets/asset/createAsset',
		method: 'post',
		data,
	});
}

// 修改资产
export function updateAsset(data: any) {
	return request({
		url: '/assets/asset/updateAsset',
		method: 'put',
		data,
	});
}

// 上传资产图片
export function uploadAssetImage(file: File) {
	const formData = new FormData();
	formData.append('file', file);
	return request({
		url: '/oss/file/uploadFile',
		method: 'post',
		data: formData,
		headers: {
			'Content-Type': 'multipart/form-data',
		},
	});
}

// SKU 列表查询参数
export interface SkuQueryParams {
	assetId: string;
	status?: number;
	keyword?: string;
	minPrice?: number;
	maxPrice?: number;
	pageNum?: number;
	pageSize?: number;
}

// SKU 创建参数
export interface CreateSkuParams {
	assetId: string;
	assetName: string;
	skuName: string;
	imageUrl?: string;
	specValues?: Record<string, any>;
	price: number;
	unlimitedStock: boolean;
	stock: number;
	sort?: number;
	status?: number;
}

// 获取 SKU 列表
export function listAssetSkus(params: SkuQueryParams) {
	return request({
		url: '/assets/asset/sku/listAssetSkus',
		method: 'get',
		params,
	});
}

// 根据assetId获取资产和SKU信息（用于套餐开通弹窗）
export function getAssetAndSku(params: { assetId: string }) {
	return request({
		url: '/assets/asset/getAssetAndSku',
		method: 'get',
		params,
	});
}

// 创建 SKU
export function createAssetSku(data: CreateSkuParams) {
	return request({
		url: '/assets/asset/sku/createAssetSku',
		method: 'post',
		data,
	});
}

// 获取 SKU 详情
export function getAssetSku(id: string) {
	return request({
		url: '/assets/asset/sku/getAssetSku',
		method: 'get',
		params: { id },
	});
}

// 修改 SKU（支持部分更新，只传递修改过的字段）
export function updateAssetSku(data: Partial<CreateSkuParams> & { id: string }) {
	return request({
		url: '/assets/asset/sku/updateAssetSku',
		method: 'put',
		data,
	});
}

// 删除 SKU
export function deleteAssetSku(id: string) {
	return request({
		url: '/assets/asset/sku/deleteAssetSku',
		method: 'delete',
		params: { id },
	});
}

// 获取规格单位选项
export function getSpecsUnitOptions(assetType: string) {
	return request({
		url: '/assets/enum/getSpecsUnit',
		method: 'get',
		params: { assetType },
	});
}

// 获取库存表单字段
export function getStockFormFields(assetSkuId: string) {
	return request({
		url: '/assets/stock/manage/getStockFormFields',
		method: 'get',
		params: { assetSkuId },
	});
}

// 库存操作
export interface StockOperationParams {
	assetSkuId: string;
	stock?: number;
	batchNo?: string;
	productionDate?: string;
	expiryDate?: string;
	expiryWarningDate?: string;
	[key: string]: any; // 支持动态字段
}

export function stockOperation(data: StockOperationParams) {
	return request({
		url: '/assets/stock/manage/stockOperation',
		method: 'post',
		data,
	});
}

// 操作日志查询参数
export interface LogQueryParams {
	collection_id: string;
	pageNum?: number;
	pageSize?: number;
}

// 操作日志信息
export interface OperationLogInfo {
	id: string;
	service_name: string;
	collection: string;
	collection_id: string[];
	operation: string;
	creator: string;
	createdAt: string;
	data: { FieldName: string; FieldValue: any }[] | null;
	ip_address: string;
}

// 查询操作日志
export function listLogs(params: LogQueryParams) {
	return request({
		url: '/assets/log/listLogs',
		method: 'get',
		params,
	});
}

// 订阅/开通资产服务参数
export interface SubscribeAssetParams {
	skuId: string;
	assetId?: string;
}

// 订阅/开通资产服务
export function subscribeAsset(data: SubscribeAssetParams) {
	return request({
		url: '/assets/asset/subscribe',
		method: 'post',
		data,
	});
}
