import { config, networkUtils } from '../../utils';

const { request } = networkUtils;
const { api } = config;
const { bid } = api;
const { tender } = bid;

export async function query(params: object) {
  return request({
    url: `${tender}/tenders`,
    method: 'get',
    data: params
  });
}

export async function getSingle(id: number) {
  return request({
    url: `${tender}/tenders/${id}`,
    method: 'get'
  });
}

export async function create(params: object) {
  return request({
    url: `${tender}/tenders/saveOrUpdate`,
    method: 'post',
    data: params
  });
}

export async function remove(ids: number[]) {
  return request({
    url: `${tender}/tenders/del`,
    method: 'post',
    data: ids
  });
}

export async function update(params: object) {
  return request({
    url: `${tender}/tenders/approveStatus`,
    method: 'post',
    data: params
  });
}

export async function itemCheck(params: object) {
  return request({
    url: `${tender}/tenders/itemCheck`,
    method: 'post',
    data: params
  });
}

export async function queryMaterial(params: object) {
  return request({
    url: `${tender}/tenders/item`,
    method: 'post',
    data: params
  });
}
export async function querySupp(id: number) {
  return request({
    url: `${tender}/tenders/supp/${id}`,
    method: 'get'
  });
}

export async function querySuppItem(id: number) {
  return request({
    url: `${tender}/tenders/suppItem/${id}`,
    method: 'get'
  });
}

export async function checkBidInfo(id: number) {
  return request({
    url: `${tender}/tenders/check/${id}`,
    method: 'get'
  });
}
export async function review(id: number) {
  return request({
    url: `${tender}/tenders/review/${id}`,
    method: 'get'
  });
}

export async function groupView(id: number) {
  return request({
    url: `${tender}/tenders/groups/${id}`,
    method: 'get'
  });
}

export async function status(params: object) {
  return request({
    url: `${tender}/tenders/docStatus`,
    method: 'post',
    data: params
  });
}
export async function finalDocStatus(params: object) {
  return request({
    url: `${tender}/tenders/finalDocStatus`,
    method: 'post',
    data: params
  });
}
export async function suppScore(params: object) {
  const { viewId: id, itemId } = params;
  return request({
    url: `${tender}/tenders/suppScore/${id}`,
    method: 'get',
    data: params
  });
}
export async function suppHas(id: number) {
  return request({
    url: `${tender}/tenders/suppHas/${id}`,
    method: 'get'
  });
}

export function checkPercent(list) {
  // 没有填比例的默认为0
  list.map(i => {
    i.bidPercent = i.bidPercent ? i.bidPercent : 0;
  });
  // 中标比例 > 0 && 非正式供应商
  const supp = list.filter(i => i.bidPercent > 0 && i.suppStatus !== '02');
  if (supp.length > 0) {
    return supp.map(
      i => `${i.suppName}(${i.suppCode})不是正式供应商，不能分配中标比例。`
    );
  } else {
    // 构造数据结果
    const groups = {};
    list.forEach(i => {
      const dataKey = i.itemId;
      groups[dataKey] = groups[dataKey] || [];
      groups[dataKey].push(i);
    });
    // 检查每个物料的百分比
    return Object.keys(groups)
      .map(itemId => {
        const group = groups[itemId];
        const { itemCode, itemDesc: itemName } = group[0];
        const percent = group
          .map(i => i.bidPercent)
          .reduce((prev, next) => prev + next, 0);
        return percent === 100
          ? ''
          : `${itemName}(${itemCode})分配比例总和必须等于100%`;
      })
      .filter(i => i !== '');
  }
}
