import request from '@/utils/request'
import { VXETable } from 'vxe-table'
import {EventBus} from "@/event-bus";
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 入库=()=>{
    const {insertRecords,updateRecords}=that.$refs.vGrid.getRecordset()
    const data=[]
    if (insertRecords.length>=0){
      data.push(...insertRecords)
    }
    if (updateRecords.length>=0){
      data.push(...updateRecords)
    }
    for (const item of data) {
      if (!item.垛位){
        that.$refs.vGrid.setCurrentRow(item)
        VXETable.modal.message({ content: `${item.余料编号}请选择摆放位置`, status: 'warning' })
        return
      }
      if (!item.入库日期){
        that.$refs.vGrid.setCurrentRow(item)
        VXETable.modal.message({ content: `${item.余料编号}请选择入库日期`, status: 'warning' })
        return
      }
    }
    rk(data).then(res=>{
      VXETable.modal.message({ content: '成功', status: 'success' })
      butClick()
    })
  }

  const 移垛=()=>{
    const data=that.$refs.vGrid.getCheckboxRecords()
    if (data.length>0){
      const list=data.map(item=>{
        return {id:item.id,垛位:item.垛位,层数:item.层数}
      })
      edit(list).then(res=>{
        VXETable.modal.message({ content: '成功', status: 'success' })
        butClick()
      })
    }
  }

  const 提取=()=>{
    that.$refs.ylModal.show()
  }

  const useStatus=[
    {label:'未使用',value:'1'},
    {label:'未出库',value:'2'},
  ]

  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,
      scrollY:{enable:true},
      editConfig:{trigger: 'click', mode: 'cell', showStatus: true},
      highlightCurrentRow:true,
      formConfig:{
        data:{
          dcCh:'',
          dcPl:'',
          tzbh:'',
          status:'1',
        },
        items:[
          { field: 'dcCh', title: '来源船只', span: 4,slots: { default: 'name_czbh' } },
          { field: 'dcPl', title: '来源批量', span: 4 ,itemRender: {name: '$input'}},
          { field: 'tzbh', title: '来源图号', span: 4 ,itemRender: {name: '$input'}},
          { field: 'status', title: '使用状态', span: 3 ,
            itemRender: {name: '$select',props:{options:useStatus}}
          },
          {span: 9,
            children:[
              { span: 15,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:'warning'},events:{click:移垛}
                }
              },
            ]
          },
        ],
      },
      columns:[
        {type:'checkbox',title:'序号'},
        {type:'seq',title:''},
        { 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: 150,
          editRender:{name:'$input',props: {type:'date', labelFormat:'yyyy/MM/dd',valueFormat:'yyyy/MM/dd',placeholder: '请选择日期' }},
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination' }
        },
        { field: '垛位', title: '垛位', width: 100,
          editRender:{name:'$select',options:[]},
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination' }
        },
        { field: '层数', title: '层数', width: 80,
          editRender:{name:'$input'},
          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:[],
    },
  }

  getDw().then(res=>{
    const 垛位=that.$refs.vGrid.getColumnByField('垛位')
    垛位.editRender.options=res.data.map(item=>{
      return {label:item.bmsm,value:item.bm}
    })
  })

  const tmpSelectId=[]
  EventBus.$on('ylrkSelect', _data=>{
    _data.forEach(item=>{
      if (tmpSelectId.includes(item.id)){
        return
      }
      tmpSelectId.push(item.id)
      that.$refs.vGrid.insert(item)
    })
  })
  return data
}

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

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

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

const getDw=()=>{
  return request({
    url: "/api/sy/getZhbmList",
    method: "post",
    data:{bmlb:'yldw'}
  });
}

export default init
