import request from '@/utils/request'

// 查询工艺路线列表
export function listRoute(query) {
  return request({
    url: '/system/route/list',
    method: 'get',
    params: query
  })
}

// 查询工艺路线详细
export function getRoute(id) {
  return request({
    url: '/system/route/' + id,
    method: 'get'
  })
}

// 新增工艺路线
export function addRoute(data) {
  return request({
    url: '/system/route',
    method: 'post',
    data: data
  })
}

// 修改工艺路线
export function updateRoute(data) {
  return request({
    url: '/system/route',
    method: 'put',
    data: data
  })
}

// 获取项目令号列表
export function getDistinctProjectCodes(query) {
  return request({
    url: '/system/route/getDistinctProjectCodes', // 后端接口 URL，确保正确
    method: 'get',
    params: { query } // 将查询关键字传递给后端
  })
    .then((response) => {
      return response; // 返回数据部分
    })
    .catch((error) => {
      console.error('Error fetching project codes:', error);
      throw error; // 抛出错误让前端可以捕获
    });
}

export function getProcessInfoList(query) {
  return request({
    url: '/system/route/getProcessInfoList', // 后端接口 URL，确保正确
    method: 'get',
    params: { query } // 将查询关键字传递给后端
  })
    .then((response) => {
      return response; // 返回数据部分
    })
    .catch((error) => {
      console.error('Error fetching project codes:', error);
      throw error; // 抛出错误让前端可以捕获
    });
}

export function onSave(list) {
  return request({
    url: '/system/route/addRoute',
    method: 'post',
    data: list,
  })
  .then((response) => {

    return response;
  })
  .catch((error) => {
    console.error('Error saving route:', error);
    throw error;
  });
}


// 删除工艺路线
export function delRoute(id) {
  return request({
    url: '/system/route/' + id,
    method: 'delete'
  })
}

export function pushRouteBom(rooteProdet, groupName) {
  return request({
    url: '/system/route/pushRouteBom',
    method: 'post',
    params: { rooteProdet, groupName },
  });
}

export function approveRoute(rooteProdet) {
  return request({
    url: '/system/route/approveRoute',
    method: 'post',
    params: { rooteProdet },
  });
}
export function updateProcessPlan(rooteProdet, ids) {
  return request({
    url: '/system/route/updateProcessPlan',
    method: 'post',
    params: { rooteProdet, ids },
  });

}export function updateProcesTime(rooteProdet) {
  return request({
    url: '/system/route/updateProcesTime',
    method: 'post',
    params: { rooteProdet },
  });
}
export function getKindeeExcel(rooteProdet) {
  return request({
    url: '/system/route/getKindeeExcel',
    method: 'get',
    params: { rooteProdet },
  })
  .then(response => {
    return response.data; // 返回数据
  })
  .catch(error => {
    throw new Error('获取标准工艺失败，请重试！');
  });
}
export function getAllRouteAndUse(rooteProdet) {
  return request({
    url: '/system/route/getAllRouteAndUse',
    method: 'post',
    params: { rooteProdet },
  })
    .then(response => {
      return response.data; // 返回数据
    })
    .catch(error => {
      throw new Error('获取全部工艺失败，请重试！');
    });
}
export function generatePDFs(rooteProdet) {
  return request({
    url: '/system/route/generatePDFs',
    method: 'get',
    params: { rooteProdet },
    responseType: 'blob',
    headers:{ 'Content-Type': 'application/json; application/octet-stream'},
  });
}

// 更新生产订单仓库字段（推送工序计划单入库）
export function updateProductionOrders(rooteProdet, cangKuNum) {
  return request({
    url: '/system/route/updateProductionOrders',
    method: 'post',
    params: { rooteProdet, cangKuNum },
  });
}

export function updateCgOrders(rooteProdet, cangKuNum) {
  return request({
    url: '/system/route/updateCgOrders',
    method: 'post',
    params: { rooteProdet, cangKuNum },
  });
}


// 获取物料BOM信息，后端返回物料BOM信息集合
export function getBomInfo(materialCode, materialName, productionOrderNo) {
  return request({
    url: '/system/route/getBomInfo',
    method: 'post',
    params: {
      materialCode,
      materialName,
      productionOrderNo // 添加生产订单号参数
    }
  })
}

// 新增BOM
export function addBom(data) {
  return request({
    url: '/system/materialbom',
    method: 'post',
    data: data
  })
}

// 修改BOM
export function updateBom(data) {
  return request({
    url: '/system/materialbom',
    method: 'put',
    data: data
  })
}

// 删除BOM
export function deleteBom(id) {
  return request({
    url: '/system/materialbom/' + id,
    method: 'delete'
  })
}

// 搜索物料
export function searchMaterial(params) {
  return request({
    url: '/system/material/search',
    method: 'get',
    params: params
  })
}
// 查询物料列表
export function listMaterial(query) {
  return request({
    url: '/system/material/list',
    method: 'get',
    params: query
  })
}

// 获取工艺路线列表
export function getProcessRouteList(materialCode, materialName, productionOrderNo) {
  return request({
    url: '/system/route/getProcessRouteList',
    method: 'get',
    params: {
      materialCode,
      materialName,
      productionOrderNo
    }
  })



}
