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

/**
 * 查询量具送检信息
 */
export async function processById(params) {
  console.log('---parmas--parmas-----------');
  console.log(params);
  const res = await request.get(`/eam/planmaintenance/processById`, { params });
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 提交日志-处理流程
export async function surrender(data) {
  const res = await request.post('/eam/planTool/surrender', data);
  if (res.data.code == 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 个人日志列表
export async function getList(params) {
  const res = await request.get('/eam/planToolLog/list', { params });
  if (res.data.code == 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 我的工单统计
export async function statistics(params) {
  const res = await request.get('/eam/PdaWorkOrder/statistics', { params });
  if (res.data.code == 0) {
    console.log('-----statistics---user--------');
    console.log(store.state.user);
    if (store.state.user.menus?.length) {
      for (const p of store.state.user.menus) {
        console.log('statistics----------------------');
        console.log(p);
        if (p.path === '/tickets') {
          console.log('进来了！~~~~~');
          store.dispatch('user/setMenuBadge', {
            path: '/tickets',
            value: res.data.data.total || 0,
            color: 'danger'
          });
          break;
        }
      }
    } else {
      const unwatch = Vue.prototype.$watch(
        '$store.state.user.menus',
        () => {
          console.log('-----statistics---user--------');
          console.log(store.state.user);
          if (store.state.user.menus?.length) {
            for (const p of store.state.user.menus) {
              console.log('statistics----------------------');
              console.log(p);
              if (p.path === '/tickets') {
                console.log('进来了！~~~~~');
                store.dispatch('user/setMenuBadge', {
                  path: '/tickets',
                  value: res.data.data.total || 0,
                  color: 'danger'
                });
                break;
              }
            }
            unwatch();
          }
        },
        {
          immediate: true
        }
      );
    }
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 管理员确认收到
export async function adminVerify(data) {
  const res = await request.post('/eam/planTool/adminVerify', data);
  if (res.data.code == 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.message));
}
