import request from '@/utils/request';
import store from '@/store';
import Vue from 'vue';


export async function getQueryFactory() {
  const res = await request.post('/aps/presalesorder/queryFactory');
  if (res.status == 200) {
    return res.data;
  }
  return Promise.reject(res.data); //报错处理

}


//+交期预估
export async function estimateDeliveryDates(data) {
  const res = await request.post('/aps/presalesorder/estimateDeliveryDates', data);
  console.log(res, 'lll')

  if (res.data.code == 0) {
    return res.data;
  }
  return Promise.reject(res.data); //报错处理

}

// 转销售订单
export async function convertToSalesOrder(data) {
  const res = await request.post('/aps/presalesorder/convertToSalesOrder', data);

  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(res.data); //报错处理

}

// 一次性转销售订单
export async function createAll(data) {
  const res = await request.post('/aps/presalesorder/createAll', data);

  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(res.data); //报错处理

}

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

  );

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

// 批量导出文件
export async function exportFile(params) {
  const res = await request.post('/aps/presalesorder/export', params, {
    responseType: 'blob'
  });
  download(res.data, `交期预估导出.xlsx`)
}

//下载模板
export async function importTemplate(url,name) {
  const res = await request.post(url||'/aps/presalesorder/importTemplate', {}, {
    responseType: 'blob'
  });
  download(res.data, name||`交期预估下载模板.xlsx`)
}

export function download(data, name) {
  const a = document.createElement('a');
  const url = window.URL.createObjectURL(data);
  const filename = name;
  a.href = url;
  a.download = filename,
    a.click();
  window.URL.revokeObjectURL(url);
}




// 批量导入文件
export async function importFile(data) {
  const res = await request.post('/aps/presalesorder/importFile', data);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(res.data); //报错处理
}

// 查询销售订单列表
export async function listSalesorder(query) {
  const res = await request.post('/aps/presalesorder/page', query);
  if (res.data.code == '0') {
    return res.data.data;
  }
  return Promise.reject(res.data); //报错处理
}


// 查询销售订单预估列表
export async function listSalesOrderExpect(data) {
  const res = await request.post('/aps/presalesorder/expect', data);
  if (res.status == 200) {
    return res.data;
  }
  return Promise.reject(res.data); //报错处理
}

// 查询销售订单预估列表
export async function listSalesorderExpectAll() {
  const res = await request.get('/aps/presalesorder/expectAll', null);
  if (res.status == 200) {
    return res.data;
  }
  return Promise.reject(res.data); //报错处理

}

// 查询销售订单预估列表
export async function listSalesOrderOccupyRes(query) {
  const res = await request.post('/aps/presalesorder/queryOccupyRes', query);
  if (res.status == 200) {
    return res.data;
  }
  return Promise.reject(res.data); //报错处理
}


// 查询销售订单预估列表
export async function occupyRes(data) {
  const res = await request.post('/aps/presalesorder/occupyRes', data);
  if (res.data.code == '0') {
    return res.data;
  }
  return Promise.reject(res.data); //报错处理

}

// 一次性占用资源
export async function occupyAllRes(param) {
  const res = await request.post('/aps/presalesorder/occupyAllRes', param);
  if (res.data.code == '0') {
    return res.data;
  }
  return Promise.reject(res.data); //报错处理
}

// 一次性占用资源
// export async function occupyAllRes(data) {
//   console.log(data);
//   return await request({
//     url: '/aps/presalesorder/occupyAllRes',
//     method: 'post',
//     data: JSON.stringify(data)
//   })
// }

// 查询销售订单详细
export async function getSalesOrder(id) {
  const res = await request.get('/aps/presalesorder/' + id, null);
  if (res.data.code == '0') {
    return res.data;
  }
  return Promise.reject(res.data); //报错处理
}

// 新增销售订单
export async function addSalesorder(data) {
  const res = await request.post('/aps/presalesorder/addOne', data);
  if (res.data.code == '0') {
    return res.data.data;
  }
  return Promise.reject(res.data); //报错处理
}

// 修改销售订单
export async function updateSalesorder(data) {
  const res = await request.post('/aps/presalesorder/editOne', data);
  if (res.data.code == '0') {
    return res.data.data;
  }
  return Promise.reject(res.data); //报错处理

}

// 删除销售订单
export async function delSalesorder(data) {
  const res = await request.post('/aps/presalesorder/deleteOne', data);
  if (res.data.code == '0') {
    return res.data.data;
  }
  return Promise.reject(res.data); //报错处理

}
