import request from '@/utils/request'
import { getComputedParameter } from '@/blocks/utils/index.js'

import { UPCLOUD_RDC_TMS_MAINDATA_SERVICE, UPCLOUD_RDC_TMS_BMS_SERVICE } from '@/config/serviceSettings.js'
const serviceName = UPCLOUD_RDC_TMS_MAINDATA_SERVICE
//获取证件ID
export const getPaperId = (_this, data, st) => {
  return request({
    url: serviceName + '/paper/getPaperId',
    method: 'post',
    data,
  })
}

export function divider(self, value) {
  return {
    label: '',
    span: 24,
    itemHeight: '60px',
    labelCol: { span: 0 },
    wrapperCol: { span: 24 },
    type: () => {
      return self._c('a-divider', { attrs: { orientation: 'left' } }, [self._v(value)])
    },
  }
}

// carrierCode:"[string] 往来单位编码"
// carrierId:"[integer] 往来单位id"
// carrierName: "[string] 往来单位名称"

// createperson:"[string] undefined"
// createtime:"[string] undefined"
// endDate:"[string] 挂账结束日期"
// enterpriseId:"[integer] undefined"
// enterpriseName:"[string] undefined"
// ids:[]
// memo:"[string] 备注"
// noTaxAmt:"[number] 无税金额"
// settlementCompany:"[string] 结算公司"
// settlementCycle:"[string] 结算周期"
// settlementDepartment:"[string] 结算部门"
// settlementName:"[string] 结算单名称"
// settlementNo:"[string] 结算单号"
// startDate:"[string] 挂账开始日期"
// taxAmt:"[number] 含税金额"
// taxPoint:"[string] 税点"
// totAmt:"[number] 总金额"
// updateperson:"[string] undefined"
// updatetime:"[string] undefined"

export const setFromItems = (self, expData = []) => {
  let e = []
  if (expData.length > 0) {
    e = expData.map((item) => {
      return { ...item }
    })
    e.forEach((item) => {
      let type = item.type
      Object.defineProperty(item, 'type', {
        get() {
          return self.st === 1 ? 'txt' : type
        },
      })
    })
    e = [divider(self, '扩展信息'), ...e]
  }

  return [
    {
      name: 'settlementNo',
      required: true,
      readOnly: true,
      label: '结算单号',
      get type() {
        return self.st === 1 ? 'txt' : 'input'
      },
    },
    {
      name: 'settlementName',
      label: '结算单名称',
      required: true,
      disabled: true,
      get type() {
        return self.st === 1 ? 'txt' : 'input'
      },
    },

    {
      name: 'settlementCycle',
      label: '结算周期',
      get mode() {
        return self.st === 1 ? 'txt' : ''
      },
      type: 'select',
      get options() {
        return self.options.SETTLEMENT_CYCLE
      },
    },
    {
      name: 'carrierId',
      label: '往来单位ID',
      hidden: true,
    },
    {
      name: 'carrierCode',
      label: '往来单位Code',
      hidden: true,
    },
    {
      name: 'carrierName',
      label: '往来单位',
      // type: 'input',
      disabled: true,
      get type() {
        return self.st === 1 ? 'txt' : 'input'
      },
    },
    {
      name: 'settlementCompany',
      label: '结算公司',
      disabled: true,
      required: true,
      get type() {
        return self.st === 1 ? 'txt' : 'input'
      },
    },
    {
      name: 'settlementDepartment',
      label: '结算部门',
      disabled: true,
      required: true,
      get type() {
        return self.st === 1 ? 'txt' : 'input'
      },
    },
    {
      name: 'cardEndDate',
      label: '挂账结束日期',
      format: 'YYYY-MM-DD',
      placeholder: '年/月/日',
      get type() {
        return self.st === 1 ? 'txt' : 'date'
      },
    },
    {
      name: 'invoiceEndDate',
      label: '开票结束日期',
      //showTime: true,
      format: 'YYYY-MM-DD',
      placeholder: '年/月/日',
      get type() {
        return self.st === 1 ? 'txt' : 'date'
      },
    },
    {
      name: 'taxPoint',
      label: '税点',
      get type() {
        return self.st === 1 ? 'txt' : 'select'
      },
      get options() {
        return self.options?.TAX_POINT ?? []
      },
      // get mode() {
      //   return self.st === 1 ? 'txt' : ''
      // },

      on: {
        change(taxPoint, label, options, form) {
          const { noTaxAmt } = form.getFieldsValue()
          let taxAmt = getComputedParameter(noTaxAmt * (1 + taxPoint / 100), 'cost')
          let totTax = getComputedParameter(taxAmt - noTaxAmt, 'cost')
          form.setFieldsValue({ totTax, taxAmt })
        },
      },
    },
    {
      name: 'noTaxAmt',
      label: '未税总额',
      min: 0,
      get type() {
        return self.st === 1 ? 'txt' : 'number'
      },
      on: {
        change(noTaxAmt, form) {
          const { taxPoint } = form.getFieldsValue()
          let noTaxAmtComputed = getComputedParameter(noTaxAmt, 'cost')
          let taxAmt = getComputedParameter(noTaxAmtComputed * (1 + taxPoint / 100), 'cost')
          let totTax = getComputedParameter(taxAmt - noTaxAmtComputed, 'cost')
          form.setFieldsValue({ totTax, taxAmt })
        },
        blur(e, form) {
          let noTaxAmt = getComputedParameter(form.getFieldValue('noTaxAmt'), 'cost')
          form.setFieldsValue({ noTaxAmt })
        },
      },
    },
    {
      name: 'taxAmt',
      label: '含税总额',
      disabled: true,
      get type() {
        return self.st === 1 ? 'txt' : 'input'
      },
    },
    {
      name: 'totTax',
      label: '总税额',
      disabled: true,
      get type() {
        return self.st === 1 ? 'txt' : 'input'
      },
    },
    {
      name: 'memo',
      labelCol: { span: 4 },
      wrapperCol: { span: 20 },
      span: 12,
      label: '备注',
      get type() {
        return self.st === 1 ? 'txt' : 'textarea'
      },
    },
    ...e,
  ]
}
