import { apiEnterpriseList, apiShopList, apiShopListByPickSelfID } from './index';

// 获取企业列表
export const getEnterpriseList = async (param = {}) => {
  const res = await apiEnterpriseList({
    page: 1,
    size: 10000,
    data: param,
  });
  if (res?.data?.records?.length) {
    const data = res.data.records;
    const arr = data.map(item => ({
      label: item.name,
      value: item.id,
      key: item.id,
    }));
    return {
      id: data[0].id,
      list: arr,
    };
  }
  return {
    id: '',
    list: [],
  };
};
// 获取店铺列表
export const getShopList = async e => {
  if (e.keyWords) {
    const res = await apiShopList({ name: e.keyWords });
    if (res && res.data) {
      const { data } = res;
      return data.map(item => ({
        value: item.id,
        label: item.name,
      }));
    }
  }
  return [];
};
// 获取店铺列表通过自提点ID
export const getShopListByPickSelf = async e => {
  const { name } = e;
  const res = await apiShopListByPickSelfID({ name });
  if (res && res.data && res.data.records) {
    const data = res.data.records;
    const json = {};
    data.forEach(item => {
      json[item.id] = { text: item.name };
    });
    return {
      id: res[res.length - 1].id,
      list: json,
    };
  }
  return {
    id: '',
    list: {},
  };
};
// 获取店铺名称
export const getEnterpriseName = (arr, id) => {
  let name = '';
  if (arr && arr.length) {
    arr.forEach(item => {
      if (+item.value === +id) {
        name = item.label;
      }
    });
  }
  return name;
};
