// 督查问题整改问责模块

import request from '../../../utils/gw_ajax_request.js';

import {
  pblmRectifyProcState,
  pblmRectifyProcState1,
  pblmRectifyProcStateCity,
} from './rectifyProcState';

const baseUrl = '';

// 生成督查年份
export function getDuChaYears() {
  const duChaYearArray = [];
  const dateNow = new Date();
  const currentYear = dateNow.getFullYear(); // 获取完整的年份(4位,1970-????)
  for (let i = currentYear - 2; i < currentYear + 2; i++) {
    const duChaYear = {};
    duChaYear.value = i;
    duChaYearArray.push(duChaYear);
  }
  return duChaYearArray;
}

// 获取督查类别
export function getDuChaType(persId) {
  return request({
    url: baseUrl + '/supervision/plan/getPersTypeAll',
    method: 'GET',
  });
}

// 获取所有责任处室
export function getDutyDept(condition) {
  return request({
    url: baseUrl + '/bis/insp/zg/pblm/getDutyDept?condition=' + condition,
    method: 'GET',
  });
}

// 获取所有责任处室市级下发
export function getDutyDeptCity() {
  return request({
    url: baseUrl + '/bis/insp/BisInspZgPblmCity/getDutyDept',
    method: 'GET',
  });
}

// 获取督查类别
export function getObjType() {
  return request({
    url: baseUrl + '/dc/insp/plan/getAttInspType',
    method: 'GET',
  });
}

// 获取督查类别
export function getFindListMatter() {
  return request({
    url: baseUrl + '/dc/insp/pblm/getFindListMatter',
    method: 'POST',
  });
}

// 获取问题来源
export function getDuChaResource() {
  return request({
    url: baseUrl + '/bis/insp/zg/pblm/getSource',
    method: 'GET',
  });
}

// 获取问题来源市级下发
export function getDuChaResourceCity() {
  return request({
    url: baseUrl + '/bis/insp/BisInspZgPblmCity/getSource',
    method: 'GET',
  });
}

// 获取行政区划结构树数据
export function getOrgTreeJson() {
  return request({
    url: baseUrl + '/dc/ad/base/getTreeData',
    method: 'GET',
  });
}

// 获取待下发问题列表
export function requestPendingDistPblm(data) {
  return request({
    url: baseUrl + '/dc/insp/pblm/page/rever',
    method: 'POST',
    data: data,
  });
}

// 获取待审核问题列表
export function requestPendingApprovePblm(data) {
  return request({
    url: baseUrl + '/dc/insp/pblm/findZgList',
    method: 'POST',
    data: data,
  });
}

// 获取调度清单问题列表
export function requestDispatchPblm(data) {
  return request({
    url: baseUrl + '/dc/insp/pblm/findZgList',
    method: 'POST',
    data: data,
  });
}

// 获取待整改问题列表
export function requestPendingRectifyPblm(data) {
  return request({
    url: baseUrl + '/dc/insp/pblm/findZgList',
    method: 'POST',
    data: data,
  });
}

// 获取整改中问题列表
export function requestUnderRectifyPblm(data) {
  return request({
    url: baseUrl + '/dc/insp/pblm/findZgList',
    method: 'POST',
    data: data,
  });
}

// 获取所有问题列表(地市、县级单位)(带行政区划过滤)
export function requestAllPblms(data) {
  return request({
    url: baseUrl + '/dc/insp/pblm/listOfZg',
    method: 'POST',
    data: data,
  });
}

// 获取所有问题列表(省级单位)(无行政区划过滤)
export function requestAllPblmsForProvince(data) {
  return request({
    url: baseUrl + '/dc/insp/pblm/listOfZg',
    method: 'POST',
    data: data,
  });
}

// 获取整改流程所有状态
export function getRectifyProcStates() {
  const rectifyProcStates = [];
  pblmRectifyProcState.some((procState) => {
    // console.log(parseInt(procState.code));
    if (parseInt(procState.code) >= 20) {
      rectifyProcStates.push(procState);
    }
  });
  return rectifyProcStates;
}
// 获取整改流程所有状态
export function getRectifyProcStates1() {
  return pblmRectifyProcState1;
}
export function getRectifyProcStatesCity() {
  return pblmRectifyProcStateCity;
}
// 将流程状态代码转换成汉字
export function formatWorkflowStat(stateCode, v) {
  let workflowStateName = '未知状态';
  pblmRectifyProcState.some((procState) => {
    if (procState.code === stateCode) {
      if (v == '41' && stateCode == '101') {
        workflowStateName = '市级监督科初审';
      } else {
        workflowStateName = procState.name;
      }
      return true; // 结束查找
    }
  });
  return workflowStateName;
}

// 问题下发到下级级单位(地市或直管县)
export function distributePblmToSjdw(data) {
  // console.dir(data);
  return request({
    url: baseUrl + '/dc/insp/pblm/AddPblmZg',
    method: 'POST',
    data: data,
  });
}

export function distributePblmToSpecificOrg(data) {
  // console.dir(data);
  return request({
    url: baseUrl + '/dc/insp/pblm/AddPblmZg',
    method: 'POST',
    data: data,
  });
}

export function redistributePblmToSpecificOrg(data) {
  // console.dir(data);
  return request({
    url: baseUrl + '/dc/insp/pblm/AddPblmZg',
    method: 'POST',
    data: data,
  });
}

// 驱动流程向下一步流转(由状态机控制下一步)
export function approvedPblmRectify(data) {
  return request({
    url: baseUrl + '/dc/insp/pblm/AddPblmZg',
    method: 'POST',
    data: data,
  });
}

// 驱动流程向下一步流转(提交到稽察中心经办人)
export function submitJcOeration(data) {
  return request({
    url: baseUrl + '/dc/insp/pblm/subToJc',
    method: 'POST',
    data: data,
  });
}

// 将问题整改结果上报到上级单位
export function savePblmRectifyInfo(data) {
  return request({
    url: baseUrl + '/dc/insp/pblm/updateNew',
    method: 'POST',
    data: data,
  });
}
// 将问题整改结果上报到上级单位
export function savePblmRectifyInfoX(data) {
  return request({
    url: baseUrl + '/bis/insp/zg/pblm',
    method: 'POST',
    data: data,
  });
}

// 获取指定用户的角色列表
export function requestUserRole(data) {
  return request({
    url: '/sys/roles/findRoleListByUserId',
    method: 'POST',
    data: data,
  });
}

// 获取指定用户有权限访问的Tab标签页
// 暂时无用，只能放在index页面中
export function getAuthenticatedTabs(userId, tabs) {
  const currentUser = { userId: userId };
  let userRoles = [];
  requestUserRole(currentUser).then((res) => {
    if (res.data != null) {
      userRoles = res.data;
    }
    let authenticatedTabs = [];
    let tempTabs = [];
    userRoles.forEach((userRole) => {
      tempTabs = tabs.filter(
        (tab) =>
          tab.roles.indexOf(userRole.roleId) > -1 || tab.roles.length === 0,
      );
      tempTabs.forEach((tab) => {
        authenticatedTabs.push(tab);
      });
    });
    authenticatedTabs = this.distinct(authenticatedTabs);
    return authenticatedTabs;
  });
}

// 标签页对象数组去重(配合getAuthenticatedTabs使用)
export function distinct(arr) {
  const result = [];
  const obj = {};
  for (const i of arr) {
    if (!obj[i.value]) {
      result.push(i);
      obj[i.value] = 1;
    }
  }
  return result;
}

// 将流程流转数据添加到问题数据上
export function formatPblms(pblms, isAg, activityComment) {
  for (let i = 0; i < pblms.length; i++) {
    pblms[i].isag = isAg; // isAg=0 反向流转 isAg=1 正向流转
    pblms[i].flowType = 2; // 2=整改流程 1=问题处置流程
    pblms[i].auditidea = activityComment; // 操作意见
  }
  return pblms;
}

// 根据查询条件获取问题列表
export function requestPblmListByQueryParams(queryParams) {
  return request({
    url: baseUrl + '/dc/insp/pblm/findZgList',
    method: 'POST',
    data: queryParams,
  });
}

// 获取指定问题的整改流程历史数据
export function requestPblmProcInstHistory(pblmId) {
  return request({
    url: '/dc/insp/pblmAudit/queryList/' + pblmId + '/2', // 最后参数2=整改流程 1=问题处置流程
    method: 'GET',
  });
}

// 获取指定问题的整改流程历史数据
export function requestPblmProcInstHistoryOver(pblmId) {
  return request({
    url: '/bis/insp/BisInspZgPblmRecord/findList', // 最后参数2=整改流程 1=问题处置流程
    method: 'POST',
    data: {
      pblmId: pblmId,
    },
  });
}

// 预计完成整改时间批量修改
export function updateBatchMendDate(data) {
  return request({
    url: baseUrl + '/dc/insp/pblm/updateYiZgDate',
    method: 'POST',
    data: data,
  });
}

// 整改延期提交申请
export function savePblmDelayInfo(data) {
  return request({
    url: baseUrl + '/dc/insp/pblm/subToDly',
    method: 'POST',
    data: data,
  });
}

// 整改延期提交申请
export function SmsSysConfigUpdate(data) {
  return request({
    url: baseUrl + '/bis/insp/SmsSysConfig/update',
    method: 'POST',
    data: data,
  });
}

// 超期申诉列表查询
export function requestOverdueAppealList(queryParams) {
  return request({
    url: baseUrl + '/dc/insp/pblm/getRecordListByZgParam',
    method: 'POST',
    data: queryParams,
  });
}

// 根据查询条件获取超期列表
export function requestPblmListByQueryParams_over(queryParams) {
  return request({
    url: baseUrl + '/bis/insp/zg/pblm/getRepresentations',
    method: 'POST',
    data: queryParams,
  });
}

export const dispatchPblmRectify = requestDispatchPblm;
export const rejectPblmRectify = requestUnderRectifyPblm;
