import { tableList, wrapTreeNode } from '@/utils/format';
import request from '@/utils/request';
import type { CommdityModel } from './data';

// 获取列表数据
export async function getList(params?: any) {
  const search = params ? { ...params, PageIndex: params.current } : {};
  const data = await request(`/BasicInfo/GetCommodityList`, {
    method: 'GET',
    params: search,
  });

  if (data.Result_Code === 500) {
    return [];
  }

  return tableList(data.Result_Data);
}

export async function delCommdity(commodityid: string) {
  return request(`/BasicInfo/DeleteCommodity?commodityid=${commodityid}`, {
    method: 'POST',

    requestType: 'form',
  });
}

export async function updateCommodity(commodity: CommdityModel) {
  return request(`/BasicInfo/SynchroCommodity`, {
    method: 'POST',
    data: commodity,
    requestType: 'form',
  });
}

//
export async function getCommodityTypeTree(CommodityTypePID?: string): Promise<any[]> {
  const data: any = await request(`/BasicInfo/GetCommodityTypeDDL`, {
    method: 'GET',
    params: {
      CommodityTypePID,
    },
  });

  if (data.Result_Code !== 100) {
    return [];
  }

  const cateOptions = wrapTreeNode(data.Result_Data.List);

  return cateOptions;
}
