import { getDetail, addPrint, getDtailPrintDis } from '@/api/supply/pickup'
import { getCompanyList } from '@/api/user'

let detailArr = []
let detailData = []
let company = ''
let cNewIds = []
// 获取详情

async function getDetails(ids, nickName) {
  let invoicePickBeans = []
  detailArr = []
  let newIds = []
  newIds = ids
  let filterId = []
  const obj = []
  filterId = newIds.map(k => {
    obj.push({
      id: k.id,
      invoiceId: k.invoiceId
    })
    return k.id
  })
  filterId = [...new Set(filterId)]
  cNewIds = JSON.parse(JSON.stringify(filterId))
  await getCompanyLists()
  await getDtailPrintDis(obj).then(res => {
    const item = res.data
    item.forEach(k => {
      detailData.push(JSON.parse(JSON.stringify(k)))
      invoicePickBeans = k.invoicePickBeans
      let len = Math.ceil(invoicePickBeans.length / 5)
      console.log(len, '长度')
      for (let index = 0; index < len; index++) {
        const table = []
        if (invoicePickBeans.length) {
          invoicePickBeans.splice(0, 5).forEach(e => {
            table.push({
              salesId: e.salesOrderId,
              invoiceId: e.invoiceId,
              id: e.id,
              createTime: e.id ? dateToDayFilter(k.createTime) : '',
              enginOrderType: e.orderType == 'HOME' || e.orderType == 'TRADE' ? e.enginOrderNo : e.mainOrderId,
              materialName: e.materialName || '',
              specification: e.specification || '',
              refundableQty: e.refundableQty || 0,
              // headerRemark:e.headerRemark,
              pjxh1Text: e.pjxh1Text || ''
            })
          })
        }
        detailArr.push({
          pageNumber: `${len}-${index + 1}`,
          type: k.type,
          tiTui: k.type === 2 ? `退货人` : `提货人`,
          takerPhone: k.takerPhone || '',
          headerRemark: k.remark,
          total_num: k.total_num,
          company: k.type === 2 ? `${company}销售退货单` : `${company}销售发货单`,
          pickOrderWater: k.pickOrderWater,
          customerNumber: k.customerNumber,
          takerDa: '',
          nowDate: nowDate(),
          takerName: k.type === 2 ? `退货人：${k.takerName || ''}` : `提货人：${k.takerName || ''}`,
          customerName: k.customerName || '',
          correspondName: k.correspondName,
          correspondNames: '',
          pickCar: k.pickCar || '',
          createBy: JSON.parse(localStorage.getItem('supply_user')).nickName,
          table
        })
      }
    })
  })
}

function nowDate() {
  var date = new Date()
  var seperator1 = '-'
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var strDate = date.getDate()
  if (month >= 1 && month <= 9) {
    month = '0' + month
  }
  if (strDate >= 0 && strDate <= 9) {
    strDate = '0' + strDate
  }
  var currentdate = year + seperator1 + month + seperator1 + strDate
  console.log(currentdate)
  return currentdate
}

function dateToDayFilter(date) {
  if (!date) {
    return ''
  }
  return date.slice(0, 10)
}

function getCompanyLists() {
  getCompanyList().then(res => {
    company = res.data ? res.data[0].companyName : ''
  })
  return company
}

// 添加次数
function addPrints() {
  addPrint({ ids: cNewIds.join(',') }).then(res => {})
}

export { getDetails, getCompanyLists, detailArr, addPrints }
