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

export async function getTodoTaskPage(query) {
  const res = await request({
    url: '/bpm/task/todo-page',
    method: 'get',
    params: query
  });
  if (res.data.code == 0) {
    console.log('-----getTodoTaskPage---user--------');
    console.log(store.state.user);
    if (store.state.user.menus?.length) {
      for (const p of store.state.user.menus) {
        console.log('getTodoTaskPage----------------------');
        console.log(p);
        if (p.path === '/todo') {
          console.log('进来了2~~~');
          store.dispatch('user/setMenuBadge', {
            path: '/todo',
            value: res.data.data.count || 0,
            color: 'danger'
          });
          break;
        }
      }
    } else {
      const unwatch = Vue.prototype.$watch(
        '$store.state.user.menus',
        () => {
          console.log('-----getTodoTaskPage---user--------');
          console.log(store.state.user);
          if (store.state.user.menus?.length) {
            for (const p of store.state.user.menus) {
              console.log('getTodoTaskPage----------------------');
              console.log(p);
              if (p.path === '/todo') {
                console.log('进来了2~~~');
                store.dispatch('user/setMenuBadge', {
                  path: '/todo',
                  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 getDoneTaskPage(query) {
  const res = await request({
    url: '/bpm/task/done-page',
    method: 'get',
    params: query
  });

  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

export function completeTask(data) {
  return request({
    url: '/bpm/task/complete',
    method: 'PUT',
    data: data
  });
}

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

export function rejectTask(data) {
  return request({
    url: '/bpm/task/reject',
    method: 'PUT',
    data: data
  });
}

export function backTask(data) {
  return request({
    url: '/bpm/task/back',
    method: 'PUT',
    data: data
  });
}

export function updateTaskAssignee(data) {
  return request({
    url: '/bpm/task/update-assignee',
    method: 'PUT',
    data: data
  });
}

export async function getTaskListByProcessInstanceId(processInstanceId) {
  const res = await request({
    url:
      '/bpm/task/list-by-process-instance-id?processInstanceId=' +
      processInstanceId,
    method: 'get'
  });

  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

export function getReturnList(taskId) {
  return request({
    url: '/bpm/task/return-list?taskId=' + taskId,
    method: 'get'
  });
}

export function returnTask(data) {
  return request({
    url: '/bpm/task/return',
    method: 'PUT',
    data: data
  });
}

export function delegateTask(data) {
  return request({
    url: '/bpm/task/delegate',
    method: 'PUT',
    data: data
  });
}

export function approveTaskWithVariables(data) {
  return request({
    url: '/bpm/task/approveTaskWithVariables',
    method: 'PUT',
    data: data
  });
}

export function approveTaskWithVariablesOther(data) {
  return request({
    url: '/bpm/inwarehouse/assign',
    method: 'post',
    data: data
  });
}

export function outApproveNotPass(data) {
  return request({
    url: '/bpm/outApprove/notPass',
    method: 'PUT',
    data: data
  });
}

export function outinApproveNotPass(data) {
  return request({
    url: '/bpm/outinApprove/notPass',
    method: 'PUT',
    data: data
  });
}

// 我的消息分页
export async function notifyMessagePageAPI(data) {
  const res = await request({
    url: `/sys/notifymessage/page`,
    method: 'post',
    data: data
  });
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 更新已读-指定消息ID
 */
export async function updateNotifyMessageReadByIdAPI(data) {
  const res = await request({
    url: `/sys/notifymessage/updateNotifyMessageRead`,
    method: 'post',
    data: data
  });
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
