import { message } from 'antd';
import { config, networkUtils } from '../../../utils';
import * as taxService from "../../mdata/tax";
import numberUtils from "../../../utils/numberUtils";

const { request } = networkUtils;
const { api } = config;
const { order, common } = api;
const { invoices, invFormAccount, invApproval, invoiceItems, invoiceDetailAccounts } = order;
const { invTax } = common.suggest;
const { query } = common.udc;

/**
 * 查找税率
 * @param {Object} params
 * @returns {Promise<T>}
 */
export async function queryTax() {
  return request({
    url: invTax,
    method: 'get',
  });
}

/**
 * 查找定额
 * @returns {Promise<T>}
 */
export async function queryQuota() {
  return request({
    url: `${query.replace('{udc}', 'SRM.INV_FIXAMT')}`,
    method: 'get',
  });
}

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

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

export async function getSingleFromAcc(checkId: number) {
  return request({
    url: `${invFormAccount}/${checkId}`,
    method: 'get',
  });
}

export async function queryInvDetailAccounts(checkId: number) {
  return request({
    url: invoiceDetailAccounts,
    method: 'get',
    data: checkId,
  });
}

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

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

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

// 查询发票明细
// export async function queryDetails(params: object) {
//   return request({
//     url: invoiceDetails,
//     method: 'get',
//     data: params,
//   });
// }
//
// export async function getSingleDetails(id: number) {
//   return request({
//     url: `${invoiceDetails}/${id}`,
//     method: 'get',
//   });
// }

// 查询正式发票明细
export async function queryItems(params: object) {
  return request({
    url: invoiceItems,
    method: 'get',
    data: params,
  });
}

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

// 按税率计算税额, 未税金额    该方法计算有问题，但是先不改了，只取invAmt，不取其它数值了
export function countAmt(invAmt: number, taxVal: number, sumTaxAmt: number) {
  let taxAmt = invAmt * taxVal / 100;
  let netAmt = invAmt - taxAmt;
  if (sumTaxAmt - taxAmt <= 0) {
    taxAmt = sumTaxAmt;
    sumTaxAmt = 0.00;
    netAmt = invAmt - taxAmt;
  } else {
    sumTaxAmt = sumTaxAmt - taxAmt;
  }
  return { invAmt, taxAmt, netAmt, sumTaxAmt };
}

// 按输入项计算税额
export function countTaxAmt(payload, list) {
  const { index, value, key } = payload;
  let currentRow = list[index]; // 当前行
  let lastRow = list[list.length - 1]; // 最后一行

  if (key === 'invAmt') {

    const sumAmt = (+currentRow.invAmt) + (+lastRow.invAmt); // 总金额
    if (+value >= sumAmt) {
      message.error('金额不能大于总金额', 3);
    } else {
      currentRow = { ...currentRow, invAmt: +value, taxAmt: (+value - (+currentRow.netAmt)) };
      lastRow = { ...lastRow, invAmt: (sumAmt - (+value)), taxAmt: (sumAmt - (+value) - (+lastRow.netAmt)) }; // 发票金额 = 税额 + 未税金额
    }

  } else if (key === 'netAmt') {

    const sumNetAmt = (+currentRow.netAmt) + (+lastRow.netAmt); // 总未税金额
    if (value >= sumNetAmt) {
      message.error('金额不能大于总未税金额', 3);
    } else {
      currentRow = { ...currentRow, netAmt: +value, taxAmt: ((+currentRow.invAmt) - value) };
      lastRow = { ...lastRow, netAmt: (sumNetAmt - (+value)), taxAmt: ((+lastRow.invAmt) - (sumNetAmt - (+value))) }; // 发票金额 = 税额 + 未税金额
    }
  }

  list[index] = currentRow;
  list[list.length - 1] = lastRow;
  return list;
}

// 重新统计发票总金额
export function countFormActAmt(formData, list) {
  let invAmtSum = 0;
  let netAmtSum = 0;
  let taxAmtSum = 0;
  list.map(inv => {
    invAmtSum += inv.invAmt == null ? 0 : parseFloat(inv.invAmt);
    netAmtSum += inv.netAmt == null ? 0 : parseFloat(inv.netAmt);
    taxAmtSum += inv.taxAmt == null ? 0 : parseFloat(inv.taxAmt);
  });
  formData.actInvAmt = invAmtSum;
  formData.actInvAmtStr = numberUtils.round_half_up_format(invAmtSum, 2);
  formData.actNetAmt = netAmtSum;
  formData.actNetAmtStr = numberUtils.round_half_up_format(netAmtSum, 2);
  formData.actTaxAmt = taxAmtSum;
  formData.actTaxAmtStr = numberUtils.round_half_up_format(taxAmtSum, 2);

  // 计算差异金额
  formData = countFormGapAmt(formData);

  return formData;
}

// 重新计算 开-发票差异金额
export function countFormGapAmt(formData) {

  const invAmt = formData.invAmt == null ? 0 : parseFloat(formData.invAmt); // 开票总金额(含税)
  const netAmt = formData.netAmt == null ? 0 : parseFloat(formData.netAmt); // 开票总金额(不含税)
  const taxAmt = formData.taxAmt == null ? 0 : parseFloat(formData.taxAmt); // 开票总税额

  const actInvAmt = formData.actInvAmt == null ? 0 : parseFloat(formData.actInvAmt); // 发票总金额(含税)
  const actNetAmt = formData.actNetAmt == null ? 0 : parseFloat(formData.actNetAmt); // 发票总金额(不含税)
  const actTaxAmt = formData.actTaxAmt == null ? 0 : parseFloat(formData.actTaxAmt); // 发票总税额

  const gapAmt = (invAmt - actInvAmt).toFixed(2);
  const gapNetAmt = (netAmt - actNetAmt).toFixed(2);
  const gapTaxAmt = (taxAmt - actTaxAmt).toFixed(2);


  formData.gapAmt = gapAmt;
  formData.gapAmtStr = numberUtils.round_half_up_format(gapAmt, 2);
  formData.gapNetAmt = gapNetAmt;
  formData.gapNetAmtStr = numberUtils.round_half_up_format(gapNetAmt, 2);
  formData.gapTaxAmt = gapTaxAmt;
  formData.gapTaxAmtStr = numberUtils.round_half_up_format(gapTaxAmt, 2);

  return formData;
}

// 校验金额
export function numValidation(value) {
  const reg = /^((\d{1,3}(,\d{3})*(.((\d{3},)*\d{1,3}))?)|(\d+(.\d+)?))$/;
  if (!reg.test(value)) {
    message.error('请输入正整数', 3);
    return false;
  }
  if (value <= 0) {
    message.error('金额不能小于0', 3);
    return false;
  }
  return true;
}

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

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

export async function updateRows(params) {
  return request({
    url: `${invoices}/item`,
    method: 'post',
    data: params,
  });
}

export async function checkInv(params) {
  return request({
    url: `${invoices}/check`,
    method: 'post',
    data: params,
  });
}

