import request from '@/utils/request'
import XLSX from 'xlsx'
import { VXETable } from 'vxe-table'

const 技术准备=()=>{

  const list=()=>{
    request({url:'/jszb/getFlowInfoList',method:'get',params:data.formData})
      .then(res => {
        data.options.data=res.data
      })
  }

  const importMethod=({file})=>{
    const formBody = new FormData()
    formBody.append('file', file)
    return fetch(`${process.env.VUE_APP_BASE_API}jszb/importFLowInfo`, { method: 'POST', body: formBody })
      .then(response => {
        console.log(response)
      }).then(data => {
        console.log(data)
      }).catch((e) => {
        console.log(e)
        VXETable.modal.message({ content: '导入失败，请检查数据是否正确！', status: 'error' })
      })
  }

  //NCA,NCB,NCC,NCG,NCJ,NCL,NCP,NCT,SG2,SG3,XCX
  const exportMethod=({options})=>{
    const workBook = XLSX.utils.book_new();

    const T型材Group=['S2T','N2T']

    const {sheet:T型材重量sheet}=处理数据(T型材Group,(list)=>{
      return list.map(item=>{
        return {
          船名:item.ch,
          段号:item.fd,
          零件号:item.partName,
          材质:item.material,
          规格:item.specification,
          工序:item.type0,
          左:item.p,
          中:item.c,
          右:item.s,
          重量:item.weight,
          数量:(parseInt(item.p||0)||0)+(parseInt(item.c||0)||0)+(parseInt(item.s||0)||0),
          长度:item.xxSize,
          宽度:item.yySize,
        }
      })
    })
    XLSX.utils.book_append_sheet(workBook, T型材重量sheet, "T型材重量");

    const {sheet:T型材数量sheet}=处理数据(T型材Group,(list)=>{
      const data={}

      list.forEach(item=>{
        let _data=data[item.pl+'_'+item.fd]
        if (_data){
          if (item.option3==='X'){
            _data.小组数量+=1
            _data.小组重量+= parseFloat(item.weight||0)
          }
          if (item.option3!=='X'){
            _data.大组数量+=1
            _data.大组重量+= parseFloat(item.weight||0)
          }
        }else{
          _data={}
          if (item.option3==='X'){
            _data.小组数量=1
            _data.小组重量=parseFloat(item.weight||0)
            _data.大组数量=0
            _data.大组重量 = 0
          }
          if (item.option3!=='X'){
            _data.小组数量=0
            _data.小组重量=0
            _data.大组数量=1
            _data.大组重量=parseFloat(item.weight||0)
          }
          data[item.pl+'_'+item.fd]=_data
        }
        console.log(_data)
      })
      return Object.keys(data).map(key=>{
        const item=data[key]
        const pl=key.split("_")[0]
        const fd=key.split("_")[1]
        return {
          批量:pl,
          分段:fd,
          小组数量:item.小组数量,
          小组:item.小组重量,
          大组数量:item.大组数量,
          大组:item.大组重量
        }
      })
    })
    XLSX.utils.book_append_sheet(workBook, T型材数量sheet, "T型材数量");

    const {sheet:曲平铁sheet}=处理数据(['SG2','SG3','XCX'],(list)=>{
      console.log(list)
      return list.map(item=>{
        return {
          船名:item.ch,
          段号:item.fd,
          批量:item.pl,
          零件号:item.partName,
          材质:item.material,
          规格:item.specification,
          工序:item.type0,
          左:item.p,
          中:item.c,
          右:item.s,
          长度:item.xxSize,
          宽度:item.yySize,
        }
      })
    })
    XLSX.utils.book_append_sheet(workBook, 曲平铁sheet, "曲平铁");

    const {sheet:曲外板sheet}=处理数据(['NCA','NCB','NCC','NCG','NCJ','NCL','NCP','NCT'],(list)=>{
      return list.map(item=>{
        return {
          船名:item.ch,
          段号:item.fd,
          批量:item.pl,
          零件号:item.partName,
          材质:item.material,
          规格:item.specification,
          工序:item.type0,
          左:item.p,
          中:item.c,
          右:item.s,
          长度:item.xxSize,
          宽度:item.yySize,
        }
      })
    })
    XLSX.utils.book_append_sheet(workBook, 曲外板sheet, "曲外板");

    const {sheet:lastSheet}=处理数据(['NCA','NCB','NCC','NCG','NCJ','NCL','NCP','NCT'],(list)=>{
      return list.filter(item=>['Z','D'].includes(item.option3)).map(item=>{
        return {
          船名:item.ch,
          分段号:item.fd,
          批量:item.pl,
          零件名称:item.partName,
          种类:item.type,
          规格:item.specification,
          数量:(item.p||0)+(item.c||0)+(item.s||0),
          材质:item.material,
          工序:item.type0,
          特征:item.feature,
          流向:item.flow,
          下级:item.nextLevel,
          套料图信息:item.nestingInfo,
        }
      })
    })
    XLSX.utils.book_append_sheet(workBook, lastSheet, "流向（M、L）；组立（大、中）；种类（板）");

    const 加强平铁=['SG3']
    const {sheet:加强平铁sheet}=处理数据(加强平铁,(list)=>{
      return list.filter(item=>parseFloat(item.xxSize||'0')>5000).map(item=>{
        return {
          船名:item.ch,
          段号:item.fd,
          零件号:item.partName,
          材质:item.material,
          规格:item.specification,
          工序:item.type0,
          左:item.p,
          中:item.c,
          右:item.s,
          重量:item.weight,
          数量:(parseInt(item.p||0)||0)+(parseInt(item.c||0)||0)+(parseInt(item.s||0)||0),
          长度:item.xxSize,
          宽度:item.yySize,
        }
      })
    })
    XLSX.utils.book_append_sheet(workBook, 加强平铁sheet, "加强平铁");

    const 光电=['8GX']
    const {sheet:光电sheet}=处理数据(光电,(list)=>{
      return list.map(item=>{
        return {
          船名:item.ch,
          段号:item.fd,
          零件号:item.partName,
          材质:item.material,
          规格:item.specification,
          工序:item.type0,
          左:item.p,
          中:item.c,
          右:item.s,
          重量:item.weight,
          数量:(parseInt(item.p||0)||0)+(parseInt(item.c||0)||0)+(parseInt(item.s||0)||0),
          长度:item.xxSize,
          宽度:item.yySize,
        }
      })
    })
    XLSX.utils.book_append_sheet(workBook, 光电sheet, "光电");

    XLSX.writeFile(workBook, `${data.formData.ch}船技术准备.xlsx`)
  }

  const 处理数据=(groups,fun)=>{
    const list=data.options.data.filter(item=>groups.includes(item.nestingInfo?.substring(4,7))||groups.includes(item.type0))
    const _data=fun(list)
    return {list:list,sheet:XLSX.utils.json_to_sheet(_data)}
  }

  const data={
    formData:{
      ch:'',
      begin:'',
      end:'',
    },
    formItems:[
      { field: 'ch', title: '船号', span: 4,
        itemRender: {
          name: '$input', props: {placeholder: '船号' }
        }
      },
      { field: 'begin', title: '批次', span: 4 ,
        itemRender: {
          name: '$input', props: {placeholder: '起始批次' }
        }
      },
      { field: 'end', title: '批次', span: 4 ,
        itemRender: {
          name: '$input', props: {placeholder: '结束批次' }
        }
      },
      {align:'right', span: 12 ,
        itemRender: {
          name: '$button', props: {content:'查询',status:'primary'},events:{click:list}
        }
      },
    ],
    options:{
      height:765,
      align:'center',
      border: true,
      // resizable: true,
      keepSource:true,
      size:'mini',
      showOverflow: true,
      // scrollY:{enable:true,gt:10},
      // scrollX:{enable:true,gt:10,scrollToLeftOnChange:true},
      highlightCurrentRow:true,
      data:[],
      toolbarConfig: {
        import: true,
        export: true,
        custom: true
      },
      importConfig: {
        remote: true,
        importMethod: importMethod,
        types: ['xls','xlsx'],
        modes: ['insert']
      },
      exportConfig: {
        remote: true,
        exportMethod: exportMethod,
        types: ['xlsx'],
        modes: ['current', 'selected', 'all']
      },
    },
    columns:[
      { type: 'seq', width: 60, title: '序号' ,},
      { field: 'ch', title: '船号', width: 120 },
      { field: 'pl', title: '批量', width: 100,
        filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
        filterRender: { name: 'FilterCombination', },
      },
      { field: 'fd', title: '分段号', width: 120 ,
        filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
        filterRender: { name: 'FilterCombination', },
      },

      { field: 'partName', title: '零件名称', width: 150,
        filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
        filterRender: { name: 'FilterCombination', },
      },
      { field: 'type', title: '种类', width: 100,
        filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
        filterRender: { name: 'FilterCombination', },
      },
      { field: 'specification', title: '规格', width: 130 },
      { field: 'quantity', title: '数量', width: 100,
        filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
        filterRender: { name: 'FilterCombination', },
      },
      { field: 'material', title: '材质', width: 120 },
      { field: 'feature', title: '特征', width: 120,
        filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
        filterRender: { name: 'FilterCombination', },
      },
      { field: 'flow', title: '流向', width: 120,
        filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
        filterRender: { name: 'FilterCombination', },
      },
      { field: 'nextLevel', title: '下级', width: 120 },
      { field: 'nestingInfo', title: '套料图信息', width: 150,
        filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
        filterRender: { name: 'FilterCombination', },
      },
      { field: 'location', title: '场地', width: 120 },
      { field: 'project', title: 'PROJECT', width: 120 },
      { field: 'block', title: 'BLOCK', width: 120 },
      { field: 'partName0', title: 'PARTNAME', width: 120 },
      { field: 'partNo', title: 'PARTNO', width: 120,
        filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
        filterRender: { name: 'FilterCombination', },
      },
      { field: 'material0', title: 'MATERIAL', width: 120 },
      { field: 'thickness', title: 'THICK', width: 120 },
      { field: 'type0', title: 'TYPE', width: 120,
        filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
        filterRender: { name: 'FilterCombination', },
      },
      { field: 'p', title: 'P', width: 80 },
      { field: 'c', title: 'C', width: 80 },
      { field: 's', title: 'S', width: 80 },
      { field: 'np', title: 'NP', width: 80 },
      { field: 'nc', title: 'NC', width: 80 },
      { field: 'ns', title: 'NS', width: 80 },
      { field: 'area', title: 'AREA', width: 120 },
      { field: 'weight', title: 'WEIGHT', width: 120 },
      { field: 'cutLength', title: 'CUTLEN', width: 120 },
      { field: 'markLength', title: 'MARKLEN', width: 120 },
      { field: 'xxSize', title: 'X-SIZE', width: 120 },
      { field: 'yySize', title: 'Y-SIZE', width: 120 },
      { field: 'option', title: 'OPTION0', width: 120 },
      { field: 'm_part_name', title: 'MPARTNAME', width: 120 },
      { field: 's_part_name', title: 'SPARTNAME', width: 120 },
      { field: 'lot', title: 'LOT', width: 120 },
      { field: 'bbArea', title: 'BAREA', width: 120 },
      { field: 'ssAngle', title: 'SANGLE', width: 120 },
      { field: 'ssSizeX', title: 'SSIZE-X', width: 120 },
      { field: 'ssSizeY', title: 'SSIZE-Y', width: 120 },
      { field: 'goid', title: 'GOID', width: 120 },
      { field: 'date', title: 'DATE', width: 120 },
      { field: 'time', title: 'TIME', width: 120 },
      { field: 'shape', title: 'SHAPE', width: 120 },
      { field: 'assembly', title: 'ASSEMBLY', width: 120 },
      { field: 'option1', title: 'OPTION1', width: 120 },
      { field: 'option2', title: 'OPTION2', width: 120 },
      { field: 'option3', title: 'OPTION3', width: 120 },
      { field: 'option4', title: 'OPTION4', width: 120 },
      { field: 'option5', title: 'OPTION5', width: 120 },
      { field: 'direction', title: 'DIRECTION', width: 120 },
      { field: 'isModify', title: 'ISMODIFY', width: 120 },
      { field: 'nestName', title: 'NESTNAME', width: 120 }
    ],
  }

  return data
}

export default 技术准备
