import request from '@/utils/request'

export async function getModelPage(query) {
  const res = await request({
    url: '/bpm/model/page',
    method: 'get',
    params: query
  })
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

export async function getModel(id) {
  const res = await request({
    url: '/bpm/model/get?id=' + id,
    method: 'get'
  })
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

export async function updateModel(data) {
  const res = await request({
    url: '/bpm/model/update',
    method: 'PUT',
    data: data
  })
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 任务状态修改
export async function updateModelState(id, state) {
  const res = await request({
    url: '/bpm/model/update-state',
    method: 'put',
    data: {
      id,
      state
    }
  })
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

export async function createModel(data) {
  const res = await request({
    url: '/bpm/model/create',
    method: 'POST',
    data: data
  })
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

export async function deleteModel(id) {
  const res = await request({
    url: '/bpm/model/delete?id=' + id,
    method: 'DELETE'
  })
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

export async function deployModel(id) {
  const res = await request({
    url: '/bpm/model/deploy?id=' + id,
    method: 'POST'
  })
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
