import { defHttp } from '/@/utils/http/axios';
import { useMessage } from "/@/hooks/web/useMessage";
const { createConfirm } = useMessage();

// 查询流程定义列表
export const listDefinition = async (params) => {
  const list =  await defHttp.get({ 
    url:"/flowable/definition/list", 
    params 
  },{ 
    joinParamsToUrl: true 
  });
  return list
};


// 部署流程实例
export function definitionStart(procDefId, data) {
  return defHttp.post({
    url: '/flowable/definition/startByProcDefId/' + procDefId,
    data: data
  })
}


// 获取流程变量
export function getProcessVariables(taskId) {
  return defHttp.get({
    url: '/flowable/task/processVariables/' + taskId,
    method: 'get'
  })
}

export const updateState = (params,handleSuccess) => {
  return defHttp.put({ url:"/flowable/definition/updateState",params }, { joinParamsToUrl: true }).then(() => {
    handleSuccess();
  });
};


// 指定流程办理人员列表
export function userList(query) {
  return defHttp.get({
    url: '/flowable/definition/userList',
    method: 'get',
    params: query
  })
}

// 指定流程办理组列表
export function roleList(query) {
  return defHttp.get({
    url: '/flowable/definition/roleList',
    method: 'get',
    params: query
  })
}

// 指定流程表达式
export function expList(query) {
  return defHttp.get({
    url: '/flowable/definition/expList',
    method: 'get',
    params: query
  })
}

// 读取xml文件
export function readXml(deployId) {
  return defHttp.get({
    url: '/flowable/definition/readXml',
    params: {deployId:deployId}
  })
}

// 读取image文件
export function readImage(deployId) {
  return defHttp.get({
    url: '/flowable/definition/readImage',
    params: {deployId:deployId}
  })
}

// 获取流程执行节点
export function getFlowViewer(procInsId, executionId) {
  return defHttp.get({
    url: '/flowable/task/flowViewer',
    params: {procInsId:procInsId,executionId:executionId}
  })
}

// 流程节点数据
export function flowXmlAndNode(query) {
  return defHttp.get({
    url: '/flowable/task/flowXmlAndNode',
    params: query
  })
}

// 读取xml文件
export function saveXml(data) {
  return defHttp.post({
    url: '/flowable/definition/save',
    method: 'post',
    data: data
  })
}

// 新增流程定义
export function addDeployment(data) {
  return defHttp.post({
    url: '/system/deployment',
    method: 'post',
    data: data
  })
}

// 修改流程定义
export function updateDeployment(data) {
  return defHttp.put({
    url: '/system/deployment',
    method: 'put',
    data: data
  })
}

// 导出流程定义
export function exportDeployment(query) {
  return defHttp.get({
    url: '/system/deployment/export',
    method: 'get',
    params: query
  })
}

export const batchDelete = (params, handleSuccess) => {
  createConfirm({
    iconType: 'warning',
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete(
        {
          url: "/flowable/definition/batchDelete", 
          data: params},
          {joinParamsToUrl: true}
      ).then(() => {
        handleSuccess();
      });
    }
  });
}


// 删除流程定义
export function delDeployment(deployId,handleSuccess) {

  createConfirm({
    iconType: 'warning',
    title: '确认删除',
    content: '是否删除？',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete(
        {
          url: `/flowable/definition/delete?deployId=${encodeURIComponent(deployId)}`,
        },
      ).then(() => {
        handleSuccess();
      });
    }
  });

}

// 查询流含bpm_status 流程状态字段的 业务表列表
export function selectTableNameList() {
  return defHttp.get({
    url: '/flowable/definition/selectTableNameList',
    method: 'get'
  })
}

// 部署流程实例
export function definitionStartByDeployId(deployId, data) {
  data["deployId"] = deployId
  return defHttp.post({
    url: '/flowable/definition/startByDeployId',
    data: data
  })
}

