import request from '@/utils/request';
import { download } from '@/utils/file';
/**
 * 获取信息列表
 */
export async function getTableList(params) {
  const res = await request.get(`/eom/quote/page`, { params });
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 获取信息详情
 */
export async function getDetail(id) {
  const res = await request.get(`/eom/quote/getById/${id}`, {});
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 更新信息
 */
export async function UpdateInformation(data) {
  const res = await request.put(`/eom/quote/update`, data);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 新增信息
 */
export async function addInformation(data) {
  const res = await request.post(`/eom/quote/save`, data);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除
 */
export async function deleteInformation(data) {
  const res = await request.delete('/eom/quote/delete', { data });
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 导出报价单
 */
export async function getExport(id) {
  const res = await request.get(
    `/eom/quote/export/${id}`,
    {
      responseType: 'blob'
    },

  );

  download(res.data, '报价单.xlsx');
}

/**
 * 产品分类
 */

export async function getProduceTreeByPid() {
  const res = await request.get(
    //`/main/categoryLevel/getTreeByPid/9`,
    `/main/categoryLevel/getProduceTreeByPid?type=1`,
    {}
  );
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 产品列表
 */
export async function getProductList(params) {
  const res = await request.get(`/main/category/getList`, { params });
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 提交
 */
export async function submit(data) {
  const res = await request.post(`/bpm/quoteApprove/submit`, data);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 报价是否生成过合同
 */
export async function isHasGeneratedContractAPI(id) {
  const res = await request.get(`/eom/quote/hasGeneratedContract/${id}`);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return  Promise.resolve(0);
}
