import { request } from 'umi';
import { stringify } from 'qs';

export async function queryAuditList(params) {
  let res = await request(`/api/v1/purchase/flow/info?${stringify(params)}`);
  res.data = res.data.map((item) => ({
    ...item,
    list: null,
    ...item.list,
  }));
  return res;
}

export async function addAudit(data) {
  return request(`/api/v1/purchase/flow/info`, {
    method: 'POST',
    data,
  });
}

export async function audit(data) {
  return request(`/api/v1/oa/audit/status`, {
    method: 'GET',
    params: data,
  });
}

export async function queryAuditDetail(params) {
  return request(`/api/v1/oa/audit/detail`, {
    method: 'GET',
    params,
  });
}

const getFlowDetail = (data) => {
  const groups = {
    top: {
      position: { name: 'top' },
      attrs: {
        circle: {
          r: 4,
          magnet: true,
          stroke: '#31d0c6',
          strokeWidth: 2,
          fill: '#fff',
          style: { visibility: 'hidden' },
        },
      },
      zIndex: 10,
    },
    right: {
      position: { name: 'right' },
      attrs: {
        circle: {
          r: 4,
          magnet: true,
          stroke: '#31d0c6',
          strokeWidth: 2,
          fill: '#fff',
          style: { visibility: 'hidden' },
        },
      },
      zIndex: 10,
    },
    bottom: {
      position: { name: 'bottom' },
      attrs: {
        circle: {
          r: 4,
          magnet: true,
          stroke: '#31d0c6',
          strokeWidth: 2,
          fill: '#fff',
          style: { visibility: 'hidden' },
        },
      },
      zIndex: 10,
    },
    left: {
      position: { name: 'left' },
      attrs: {
        circle: {
          r: 4,
          magnet: true,
          stroke: '#31d0c6',
          strokeWidth: 2,
          fill: '#fff',
          style: { visibility: 'hidden' },
        },
      },
      zIndex: 10,
    },
  };
  const attrs = {
    line: {
      stroke: '#A2B1C3',
      targetMarker: { name: 'block', width: 12, height: 8 },
      strokeDasharray: '5 5',
      strokeWidth: 1,
    },
  };
  let nodes = data.nodes.map((item) => {
    let node = { ...item };
    node.ports.groups = groups;
    node.parentKey = '1';

    return node;
  });
  let edges = data.edges.map((item) => {
    let edge = { ...item };
    try {
      edge.attrs = item.attr ? JSON.parse(item.attr) : attrs;
    } catch (error) {
      edge.attrs = attrs;
    }
    return edge;
  });
  return {
    ...data,
    nodes,
    edges,
  };
};
export async function queryProcessFlows(params) {
  let res = await request(
    `/api/v1/purchase/process/get-flows?${stringify(params)}`,
    {
      method: 'GET',
    },
  );
  let data = res.data[0];
  let item = {
    ...data,
    flowDetail: data.process_json
      ? getFlowDetail(JSON.parse(data.process_json))
      : { nodes: [], edges: [] },
    formData: data.form_json ? JSON.parse(data.form_json) : [],
    simpleFlowDteail: data.process_simple_json,
  };
  // 旧字段formItems  替换为新字段条件组 formItemsOr
  item.flowDetail.nodes.forEach((node) => {
    if (node.formItems) {
      node.formItemsOr = [node.formItems];
      node.formItems = '';
    } else {
      node.formItemsOr = node.formItemsOr || [];
    }
  });
  return { data: item };
}
export async function saveAuditFlowInfo(data) {
  return request(`/api/v1/purchase/flow/info`, {
    method: 'POST',
    data,
  });
}
export async function queryUserListByRoleID(params) {
  let res = await request(
    `/api/v1/purchase/process/get-role-user?${stringify(params)}`,
    {
      method: 'GET',
    },
  );
  return res.data;
}
export async function queryGetBomForm(params) {
  let res = await request(
    `/api/v1/purchase/bom/get-bom-form?${stringify(params)}`,
    {
      method: 'GET',
    },
  );
  return res;
}
//获取分类列表
export async function queryClassify(data) {
  let res = await request(`/api/v1/purchase/bom/get-classify`, {
    method: 'GET',
    params: data,
  });
  return res;
}

export async function queryOSSData() {
  return request(`/api/v1/config/chart-template-img?destDir=doc/bom`);
}

// 提交审批
export async function createAduit(data) {
  return request(`/api/v1/oa/submit`, {
    method: 'POST',
    data,
  });
}
// 计算审批节点
export async function advanceSubmitNextNode(params) {
  let data = {
    flow_id: 0,
    node_level_id: 0,
    id: 0,
    project_id: 0,
    cur_template_node_id: 0,
    next_template_node_id: 0,
    template_node_id: null,
    flow_path: null,
    template_id: 0,
    cur_template_id: 0,
    next_template_id: 0,
    ...params,
    form_list: [JSON.stringify(params)],
  };
  return request(`/api/v1/oa/next/node/advance-submit`, {
    method: 'POST',
    data,
  });
}

export async function queryProfileList(params) {
  let profileList = await request(`/api/v1/oa/audit/list`, { params });
  let auditList = await request(`/api/v1/purchase/flow/info?flow_type=1`);
  profileList?.data?.list.map((item) => {
    var audit = auditList?.data?.find(
      (child) => item.flow_id === child?.list.id,
    );
    if (audit) {
      item.CName = item.AuthorInfo.CName;
      item.table_name = item.name;
      item.table_desc = audit.list.desc;
      item.status = '待审批';
    }
  });
  console.log(profileList);
  return profileList?.data;
}
export async function queryApplyList(params) {
  const res = await request(`/api/v1/oa/audit/my/list`, { params });
  if (res?.data) {
    const list = res.data?.list?.map((item) => {
      return {
        ...item,
        CName: item.AuthorInfo.CName,
        table_desc: item.table_desc,
        table_name: item.name,
      };
    });
    return { list, pagination: res.data?.pagination };
  } else {
    return { list: [], pagination: {} };
  }
}

//获取多级主管
export async function queryLeader() {
  return request(`/api/v1/oa/get-leader/list`);
}

//撤回申请
export async function applyRepeal(params) {
  return request(`/api/v1/oa/repeal/${params.id}`);
}

// 查询立项 id=&pageSize=&currentPage=&user_id=
export async function queryApproval(data) {
  return request(`/api/v2/approval/record?${stringify(data)}`);
}

//删除审批流
export async function queryFlowDelete(id) {
  return request(`/api/v1/oa/flow/delete/${id}`);
}

export async function updateAuditList(data, isAfter) {
  let res = await request(`/api/v1/oa/submit/add-auditor`, {
    method: 'POST',
    data,
  });
  if (isAfter) {
    await audit({
      id: data.oa_id,
      status: 1,
    });
  }
  return res;
}
