import request from '@/utils/request'
import { VXETable } from 'vxe-table'
import XLSX from 'xlsx'
const init=function (that){

  const butClick=function (){
    data.options.formConfig.data.dcCh=that.$refs.projectSelectRef.getDcch()
    if (!data.options.formConfig.data.dcCh){
      VXETable.modal.message({ content: '请选择船号', status: 'warning' })
      return
    }
    getData(data.options.formConfig.data).then(res=>{
      data.options.data=res.data
    })
  }

  const 核对=function (){
    const dcCh=that.$refs.xqProjectSelectRef.getDcch();
    const dcPl=data.options.formConfig.data.xqpl
    if (!dcCh||!dcPl){
      VXETable.modal.message({ content: '请选择需求船号和批量', status: 'warning' })
      return
    }
    const table=that.$refs.vGrid
    const list=table.getCheckboxRecords()
    if (list.length===0){
      VXETable.modal.message({ content: '请选择数据', status: 'warning' })
      return
    }

    const _data={
      dcCh:dcCh,
      dcPl:dcPl,
      ylList:list
    }
    核对数据(_data).then(res=>{
      const data=res.data
      VXETable.modal.message({ content: `核对结束,匹配${data.length}张套料图`, status: 'warning' })
      for (const item of data) {
        for (let it of list) {
          if (it.id===item.id){
            it=Object.assign(it,item)
            break;
          }
        }
      }
    })
  }

  const 保存=()=>{
    const list=that.$refs.vGrid.getCheckboxRecords()
    if (list.length>0){
      edit(list).then(res=>{
        VXETable.modal.message({ content: '成功', status: 'success' })
        butClick()
      })
    }
  }

  const 打印=()=>{
    const list=that.$refs.vGrid.getCheckboxRecords()
    if (list.length>0){
      print(list).then(res=>{
        const workBook = XLSX.utils.book_new();
        const _data=list.map(item=>{
          return {
            船号:item.使用船号,
            批量:item.使用批量,
            图纸编号:item.使用图纸,
            余料编号:item.余料编号,
            垛位:item.垛位
          }
        })
        const sheet=XLSX.utils.json_to_sheet(_data)
        XLSX.utils.book_append_sheet(workBook, sheet, "sheet");
        XLSX.writeFile(workBook, `${data.options.formConfig.dcCh}-${data.options.formConfig.dcPl}余料上料计划.xlsx`)
        butClick()
      })
    }
  }

  const 退回=()=>{
    const list=that.$refs.vGrid.getCheckboxRecords()
    if(list.length>0){
      rollback(list).then(()=>{
        VXETable.modal.message({ content: '成功', status: 'success' })
        butClick()
      })
    }
  }


  const useStatus=[
    {label:'未使用',value:'1'},
    {label:'未打印',value:'2'},
    {label:'已打印',value:'3'},
    {label:'已切割',value:'4'},
  ]

  const formatUseStatus=({cellValue})=>{
    for (const item of useStatus) {
      if(item.value===cellValue){
        return item.label
      }
    }
    return cellValue
  }

  const data={
    options:{
      height:830,
      align:'center',
      border: true,
      resizable: true,
      keepSource:true,
      size:'mini',
      showOverflow: true,
      showHeaderOverflow:true,
      editConfig:{trigger: 'click', mode: 'cell', showStatus: true},
      highlightCurrentRow:true,
      formConfig:{
        data:{
          dcCh:'',
          dcPl:'',
          tzbh:'',
          status:'1',
          xqch:'',
          xqpl:'',
        },
        items:[
          { field: 'dcCh', title: '来源船只', span: 3,slots: { default: 'name_czbh' } },
          { field: 'dcPl', title: '来源批量', span: 3 ,itemRender: {name: '$input'}},
          { field: 'tzbh', title: '来源图号', span: 3 ,itemRender: {name: '$input'}},
          { field: 'status', title: '使用状态', span: 3 ,
            itemRender: {name: '$select',props:{options:useStatus}}
          },
          {span: 12,
            children:[
              { span: 16,align:'right',itemRender: {
                  name: '$button', props: {content:'查询',status:'primary'},events:{click:butClick}
                }
              },
              { align:'right',itemRender: {
                  name: '$button', props: {content:'核对',status:'success'},events:{click:核对}
                }
              },
              { align:'right',itemRender: {
                  name: '$button', props: {content:'保存',status:'primary'},events:{click:保存}
                }
              },
              { align:'right',itemRender: {
                  name: '$button', props: {content:'打印',status:'success'},events:{click:打印}
                }
              },
              { align:'right',itemRender: {
                  name: '$button', props: {content:'退回',status:'primary'},events:{click:退回}
                }
              },
            ]
          },
          { field: 'xqch', title: '需求船只', span: 3,slots: { default: 'name_xqch' } },
          { field: 'xqpl', title: '需求批量', span: 3 ,itemRender: {name: '$input'}},
        ],
      },
      columns:[
        {type:'checkbox',title:'序号'},
        {type:'seq',title:''},
        // { field: 'id', title: 'ID', width: 150 },
        { field: '余料编号', title: '余料编号', width: 150,
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination' }
        },
        { field: '规格', title: '规格', width: 150,
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination' }
        },
        { field: '板厚', title: '板厚', width: 100 ,
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination' }
        },
        { field: '板宽', title: '板宽', width: 100 ,
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination' }
        },
        { field: '板长', title: '板长', width: 100 ,
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination' }
        },
        { field: '炉批号', title: '炉批号', width: 140,
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination' }
        },
        { field: '重量', title: '重量', width: 120,
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination' }
        },
        { field: '数量', title: '数量', width: 80,
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination' }
        },
        { field: '垛位', title: '垛位', width: 100,
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination' }
        },
        { field: '层数', title: '层数', width: 80,
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination' }
        },
        { field: '使用船号', title: '使用船号', width: 120,
          editRender:{name:'$input'},
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination' }
        },
        { field: '使用批量', title: '使用批量', width: 120,
          editRender:{name:'$input'},
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination' }
        },
        { field: '使用分段', title: '使用分段', width: 120,
          editRender:{name:'$input'},
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination' }
        },
        { field: '使用图纸', title: '使用图纸', width: 150,
          editRender:{name:'$input'},
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination' }
        },
        { field: '使用日期', title: '使用日期', width: 150,
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination' }
        },
        { field: '来源船号', title: '来源船号', width: 120,
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination' }
        },
        { field: '来源批量', title: '来源批量', width: 120,
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination' }
        },
        { field: '来源图纸', title: '来源图纸', width: 150,
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination' }
        },
        { field: '切割日期', title: '切割日期', width: 150,
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination' }
        },
        { field: '物品型号', title: '物品型号', width: 150,
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination' }
        },
        { field: '物品规格', title: '物品规格', width: 150,
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination' }
        },
        {field: '使用状态', title: '使用状态', width: 120, formatter:formatUseStatus}
      ],
      data:[],
    },
  }

  return data
}

const getData=(data)=>{
  return request({
    url: "/yl/list",
    method: "post",
    data,
  });
}

const 核对数据=(data)=>{
  return request({
    url: "/yl/gdsl",
    method: "post",
    data,
  });
}

const edit=(data)=>{
  return request({
    url: "/yl/edit",
    method: "post",
    data,
  });
}

const print=(data)=>{
  return request({
    url: "/yl/print",
    method: "post",
    data,
  });
}

const rollback=(data)=>{
  return request({
    url: "/yl/rollback",
    method: "post",
    data,
  });
}

export default init
