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

const { request } = networkUtils;
const { api } = config;
const { order } = api;
const { accounts, accountDetails, accountRecps } = order;
const { diffType } = enums;


/**
 * 更新明细的差异类型
 * @param item
 */
export function updateDiffType(item) {
  if (item.diffQty === 0 && item.diffPrice === 0) {
    item.diffType = diffType.NO_DIFF;
  } else if (item.diffPrice === 0) {
    item.diffType = diffType.QTY_DIFF;
  } else if (item.diffQty === 0) {
    item.diffType = diffType.PRICE_DIFF;
  } else {
    item.diffType = diffType.BOTH_DIFF;
  }
}

/**
 * 初始化明细的一些数据
 * @param data
 * @param isAllAccount
 */
export function initialDetailInfo(data) {
  for (let i in data.data.rows) {
    // if (mode === 'create' && data.data.rows[i].accOver === 1) data.data.rows[i].diffType = '0';
    // 如果没有点击 全部对账/全不对账 按钮，则默认按照明细本身的字段显示
    data.data.rows[i].item = (data.data.rows[i].itemCode || '') + (data.data.rows[i].itemName || '') + '(' + (data.data.rows[i].spec || '') + ')' + (data.data.rows[i].uomDesc || '');
    // data.data.rows[i].diffType === '0' ? data.data.rows[i].editable = false : data.data.rows[i].editable = true;
    // data.data.rows[i].deliAmt = data.data.rows[i].deliAmt ? data.data.rows[i].deliAmt : data.data.rows[i].amt;
    // data.data.rows[i].deliPrice = data.data.rows[i].deliPrice ? data.data.rows[i].deliPrice : data.data.rows[i].price;
    // data.data.rows[i].deliQty = data.data.rows[i].deliQty ? data.data.rows[i].deliQty : data.data.rows[i].qty;
  }
}

export async function query(params: object) {
  return request({
    url: accounts,
    method: 'get',
    data: params,
  });
}

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

export async function create(params: object) {
  return request({
    url: accounts,
    method: 'post',
    data: params,
  });
}

export async function remove(ids: number[]) {
  return request({
    url: accounts,
    method: 'delete',
    data: ids,
  });
}

export async function save(params: object) {
  return request({
    url: `${accounts}/add`,
    method: 'post',
    data: params,
  });
}

export async function submit(params: object) {
  return request({
    url: `${accounts}/update`,
    method: 'put',
    data: params,
  });
}

export async function queryBillList(params: object) {
  return request({
    url: accountDetails,
    method: 'get',
    data: params,
  });
}

export async function queryBillListSummary(params: object) {
  return request({
    url: `${accountDetails}/list/summary`,
    method: 'get',
    data: params,
  });
}


export async function queryDetails(params: object) {
  return request({
    url: `${accountDetails}/detail`,
    method: 'get',
    data: params,
  });
}

export async function querySummary(params: object) {
  return request({
    url: `${accountDetails}/summary`,
    method: 'get',
    data: params,
  });
}

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

export async function getAccountRecps(params: object) {
  return request({
    url: accountRecps,
    method: 'get',
    data: params,
  });
}

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

export async function updateDetails(params: object) {
  return request({
    url: '/api/accountCheckDetail/accountLists',
    method: 'put',
    data: params,
  });
}

export async function isDiff(id: number, ouId: number) {
  return request({
    url: `${accountDetails}/${id}/${ouId}/diff`,
    method: 'get',
  });
}

export async function remoteCfg(params) {
  return request({
    url: `${accounts}/config`,
    method: 'get',
    data: params,
  });
}

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

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

export async function reAcc(id: number) {
  return request({
    url: `${accounts}/${id}/reAcc`,
    method: 'post',
  });
}
