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

// 获取销售订单列表
export async function getPageList(data) {
  const res = await request.post(`/aps/salesorder/page`, data);
  if (res.data.code == 0) {
    if (data.status.length == 1 && data.status[0] == 1) {
      if (store.state.user.menus?.length) {
        for (const p of store.state.user.menus) {
          if (p.path === '/saleOrder') {
            store.dispatch('user/setMenuBadge', {
              path: '/saleOrder',
              value: res.data.data.count || 0,
              color: 'danger'
            });
            break;
          }
        }
      } else {
        const unwatch = Vue.prototype.$watch(
          '$store.state.user.menus',
          () => {
            if (store.state.user.menus?.length) {
              for (const p of store.state.user.menus) {
                if (p.path === '/saleOrder') {
                  store.dispatch('user/setMenuBadge', {
                    path: '/saleOrder',
                    value: res.data.data.count || 0,
                    color: 'danger'
                  });
                  break;
                }
              }
              unwatch();
            }
          },
          {
            immediate: true
          }
        );
      }
    }
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 销售订单转生产计划
export async function productionToPlan(data) {
  const res = await request.post(
    `/aps/salesorder/productionPlanGenerator`,
    data
  );
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 生产版本列表
export async function versionPage(params) {
  let par = new URLSearchParams(params);
  const res = await request.get(`/main/produceversion/page?` + par);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 刷新销售订单
export async function pullSalesOrder(params) {
  const res = await request.get(`/aps/salesorder/pullSalesOrder`);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 销售订单转生产计划 直接发布
export async function releaseSave(data) {
  const res = await request.post(`/aps/productionplan/releaseSave`, data);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
// 销售订单转生产计划保存
export async function saveSaleToPlan(data) {
  const res = await request.post(`/aps/productionplan/save`, data);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
// 销售订单转生产计划更新
export async function updateSaleToPlan(data) {
  const res = await request.put(`/aps/productionplan/update`, data);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

//创建更新销售订单
export async function createOrUpdate(data) {
  const res = await request.post(`/aps/salesorder/createOrUpdate`, data);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 销售订单获取生产版本
export async function getProductVersion(code) {
  const res = await request.get(
    `/aps/productionplan/getProductVersion/${code}`
  );
  if (res.data.code == 0) {
    return res.data.data || [];
  }
  return Promise.reject(new Error(res.data.message));
}

// 配料计划获取生产版本
export async function getPlanProductVersion(code) {
  const res = await request.get(`/aps/batchingplan/getProductVersion/${code}`);
  if (res.data.code == 0) {
    return res.data.data || [];
  }
  return Promise.reject(new Error(res.data.message));
}

// 选择工艺路线
export async function routeList(params) {
  const res = await request.get('/main/producerouting/page', { params });
  if (res.data.code == 0) {
    return res.data.data;
  }
}

// 获取订单详情
export async function getOrderDetail(code) {
  const res = await request.get(`/aps/salesorder/getByCode/${code}`);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
// 获取计划详情
export async function getUpdateInfoById(id) {
  const res = await request.get(`/aps/productionplan/getUpdateInfoById/${id}`);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

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

// 获取详情
export async function getSalesDetail(id) {
  const res = await request.get(`/aps/salesorder/getSalesOrderById/${id}`);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
// 获取库存
export async function getInventory(materialCode, planType) {
  const res = await request.get(
    `/aps/productionplan/getInventory/${materialCode}/${planType}`
  );
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 修改优先级
export async function updatePriority(data) {
  const res = await request.post(`/aps/salesorder/updatePriority`,  data);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}


// 动态表头
export async function fieldModel(params) {
  const res = await request.get(
    `/main/fieldmodel/list`, { params }
  );
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}