import { ElMessage } from "element-plus";

export const initheaders = [
  { label : "发票申请编号", width: '160px', prop:'invNo', templateValue: '' },
  { label : "发票代码",  width: '120px', prop: 'inv_code', templateValue: '' },
  { label : "发票号码",  width: '120px', prop:'inv_number', templateValue: '' },
  { label : "数电票号码",  width: '160px', prop: 'inv_number_electionic', templateValue: '' },
  { label : "销方识别号",  width: '160px', prop: 'seller_id', templateValue: '' },
  { label : "销方名称",  width: '120px', prop: 'seller_title', templateValue: '' },
  { label : "购方识别号",  width: '140px', prop: 'buyer_id' , templateValue: ''},
  { label : "购买方名称",  width: '140px', prop: 'buyer_title', templateValue: '' },
  { label : "开票日期",  width: '140px', prop: 'open_date' , templateValue: ''},
  { label : "税收分类编码",  width: '140px', prop: 'SSFL' , templateValue: ''},
  { label : "特定业务类型",  width: '140px', prop: 'TDYWLX' , templateValue: ''},
  { label : "货物或应税劳务名称",  width: '140px', prop: 'XMMC' , templateValue: ''},
  { label : "规格型号",  width: '140px', prop: 'GGXH', templateValue: '' },
  { label : "单位",  width: '140px', prop: 'DW', templateValue: '' },
  { label : "数量",  width: '140px', prop: 'SPSL', templateValue: '' },
  { label : "单价",  width: '140px', prop: 'DJ', templateValue: '' },
  { label : "金额",  width: '140px', prop: 'JE', templateValue: '' },
  { label : "税率",  width: '140px', prop: 'SL', templateValue: '' },
  { label : "税额",  width: '140px', prop: 'SE', templateValue: '' },
  { label : "价税合计",  width: '140px', prop: 'JSHJ', templateValue: '' },
  { label : "发票来源",  width: '140px', prop: 'source', templateValue: '' },
  { label : "发票票种",  width: '140px', prop: 'inv_type', templateValue: '' },
  { label : "发票状态",  width: '140px', prop: 'status', templateValue: '' },
  { label : "是否正数发票",  width: '140px', prop: 'type', templateValue: '' },
  { label : "发票风险等级",  width: '140px', prop: 'warning',templateValue: '' },
  { label : "开票人",  width: '140px', prop: 'person', templateValue: '' },
  { label : "备注",  width: '140px', prop: 'remark', templateValue: '' },
];

export const mapLabel2Field = initheaders.reduce((previous, current) => ({
  ...previous,
  [current.label]: current.prop
}), {})

export const columns = [{ type: "index", width: "50", fixed: "left", label: "序号" }, ...initheaders]
export const template = [initheaders.reduce((prev,current) => ({ ...prev, [current.label]: current.templateValue }), {})]


// 校验数据是否为空
export const isEmpty = (results:any[]) => results.length === 0
// 校验导入模板表头是否一致
export function isHeaderSame(importHeader: any[]){
  if(importHeader.length !== initheaders.length){ return false }
  for(const index in importHeader){
    if(initheaders[index].label !== importHeader[index]){ 
      console.log(initheaders[index], importHeader[index])
      return false 
    } 
  }
  return true
}

// 生成导入数据
export function generateTableData(results: any[]){
  const tableData = []
  for (const item of results) {
    const tableItem = {}
    const labels = Object.keys(item)
    for(const label of labels){
      const field = mapLabel2Field[label]
      tableItem[field] = item[label]
    }
    tableData.push(tableItem)
  }
  return tableData
}


const normalInvoiceTypes = [
  { label: "增值税专用发票", value: "special" },
  { label: "增值税普通发票", value: "normal"  },
  { label: "增值税电子专用发票", value: "special_electronic" },
  { label: "增值税电子普通发票", value: "electronic" }
];


const fullyInvoiceTypes = [
  { label: "数电票（增值税专用发票）", value: "fully_digitalized_special_electronic" },
  { label: "数电票（普通发票）", value: "fully_digitalized_normal_electronic" },
]


const fullyInvoiceLabels = fullyInvoiceTypes.map(({label}) => label)
const normalInvoiceLabels = normalInvoiceTypes.map(({label}) => label)

const isPositiveNumberOptions = [ { label: '是', value: 0 }, { label: '否', value: 1 } ]
const isPositiveNumberLabels = isPositiveNumberOptions.map(({ label }) => label)


const mapIsPositiveNumber = isPositiveNumberOptions.reduce((prev, current) => ({
  ...prev,
  [current.label]: current.value
}), {})


const mapInvoiceTypes = ([...fullyInvoiceTypes, ...normalInvoiceTypes]).reduce((prev, current) => ({
  ...prev,
  [current.label]: current.value
}), {})

export function validate(lines: Record<string, any>[]){
  for(const index in lines){
    const row = lines[index]
    const invoiceType = row['inv_type']
    const isPositiveNumber = row['type']
    if(!([...fullyInvoiceLabels, ...normalInvoiceLabels]).includes(invoiceType)){
      ElMessage.warning(`第${Number(index) + 1}行,发票票种必须为:${([...fullyInvoiceLabels, ...normalInvoiceLabels]).join(',')}`)
      return false
    }


    if(!isPositiveNumberLabels.includes(isPositiveNumber)){
      ElMessage.warning(`第${Number(index) + 1}行,是否为正数发票必须为: 是/否`)
      return false
    }


    if(fullyInvoiceLabels.includes(invoiceType) && (row['inv_number_electionic']).trim() === ''){
      ElMessage.warning(`第${Number(index) + 1}行,发票票种为数电票是,数电票号码不能为空`)
      return false
    }


    if(normalInvoiceLabels.includes(invoiceType) && (row['inv_number']).trim() === ''){
      ElMessage.warning(`第${Number(index) + 1}行,发票票种为非数电票时,发票票号码不能为空`)
      return false
    }
  
  }
  return true
}

export function translateData(data: Record<string, any>[]){
  const target = []
  data.forEach(item => {

    const { 
      inv_number_electionic, 
      inv_number, 
      inv_type,
      type, 
      ...rest 
    } = item
    
    target.push({
      inv_number: fullyInvoiceLabels.includes(inv_type) ? inv_number_electionic : inv_number,
      inv_type: mapInvoiceTypes[inv_type],
      type: mapIsPositiveNumber[type],
      check_code: '',
      seller_addr: '',
      seller_mobile: '',
      seller_bank: '',
      seller_bankNo: '',
      buyer_addr: '',
      buyer_mobile: '',
      buyer_bank: '',
      buyer_bankNo: '',
      SPBM: '',
      ...rest
    })
  })
  return target
}
