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 useStatus=[
    {label:'未入库',value:'0'},
    {label:'未使用',value:'1'},
    {label:'未出库',value:'2'},
  ]

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

  const 提取=()=>{
    const data=that.$refs.vGrid.getCheckboxRecords()
    if (data.length===0){
      VXETable.modal.message({ content: '请选择入库余料', status: 'warning' })
      return
    }
    EventBus.$emit('ylrkSelect', data)
    that.showFlag=false
  }

  const data={
    options:{
      height:730,
      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:'0',
          xqch:'',
          xqpl:'',
        },
        items:[
          { field: 'dcCh', title: '来源船只', span: 5,slots: { default: 'name_czbh' } },
          { field: 'dcPl', title: '来源批量', span: 4 ,itemRender: {name: '$input'}},
          { field: 'tzbh', title: '来源图号', span: 5 ,itemRender: {name: '$input'}},
          { field: 'status', span: 3 ,
            itemRender: {name: '$select',props:{options:useStatus,disabled:true}}
          },
          {span: 7,
            children:[
              { span: 19,align:'right',itemRender: {
                  name: '$button', props: {content:'查询',status:'primary'},events:{click:butClick}
                }
              },
              { align:'right',itemRender: {
                  name: '$button', props: {content:'提取',status:'success'},events:{click:提取}
                }
              },
            ]
          },
        ],
      },
      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
