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

  const butClick=function (){
    getData().then(res=>{
      options.data=res.data
    })
  }

  const add=()=>{
    that.$refs.vGrid.insert({bmlb:'yldw'})
  }

  const remove=()=>{
    that.$refs.vGrid.removeCheckboxRow()
  }

  const 保存=async ()=>{
    const {insertRecords, removeRecords}=that.$refs.vGrid.getRecordset()
    if (insertRecords.length>0){
      for (const item of insertRecords) {
        await addZhbm(item)
      }
    }

    if (removeRecords.length>0){
      for (const item of removeRecords) {
        await deleteZhbm(item)
      }
    }
    VXETable.modal.message({ content: '成功', status: 'success' })
  }

  const options={
      height:800,
      align:'center',
      border: true,
      resizable: true,
      keepSource:true,
      size:'mini',
      showOverflow: true,
      showHeaderOverflow:true,
      editConfig:{trigger: 'click', mode: 'cell', showStatus: true},
      highlightCurrentRow:true,
      formConfig:{
        items:[
          {span: 24,
            children:[
              { span: 20,align:'right',itemRender: {
                  name: '$button', props: {content:'查询',status:'primary'},events:{click:butClick}
                }
              },
              { align:'right',itemRender: {
                  name: '$button', props: {content:'新增'},events:{click:add}
                }
              },
              { align:'right',itemRender: {
                  name: '$button', props: {content:'删除',status:'success'},events:{click:remove}
                }
              },
              { align:'right',itemRender: {
                  name: '$button', props: {content:'保存',status:'success'},events:{click:保存}
                }
              },
            ]
          },
        ],
      },
      columns:[
        {type:'checkbox',title:'',width:80},
        {type:'seq',title:'序号',width:60},
        { field: 'bmsm', title: '垛位名称',
          editRender:{name:'$input'},
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination' }
        },
        { field: 'bm', title: '垛位编码',
          editRender:{name:'$input'},
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination' }
        },
      ],
      data:[],
    }

  butClick()
  return options
}

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

const addZhbm=(data)=>{
  return request({
    url: "/api/sy/addZhbm",
    method: "post",
    data,
  });
}

const deleteZhbm=(data)=>{
  return request({
    url: "/api/sy/deleteZhbm",
    method: "post",
    data,
  });
}

export default init
