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

const init=function (that){

  let list=[]
  const initList=function (){
    data.options.formConfig.data.dcCh=that.$refs.projectSelectRef.getDcch()
    getData(data.options.formConfig.data).then(res=>{
      list=res.data
    })
  }

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

  const importMethod=({file})=>{
    const formBody = new FormData()
    formBody.append('file', file)
    return fetch(`${process.env.VUE_APP_BASE_API}dmQfxq/uploadNew`, { 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' })
      })
  }

  const 提交=()=>{
    const list=that.$refs.vGrid.getCheckboxRecords()
    if(list.length>0){
      const _list=list.map(item=>{
        return {id:item.id,zt:item.zt}
      })
      submit(_list).then(()=>{
        VXETable.modal.message({ content: '成功', status: 'success' })
        initList()
      })
    }
  }

  const data={
    options:{
      height:800,
      align:'center',
      border: true,
      resizable: true,
      keepSource:true,
      size:'mini',
      showOverflow: true,
      editConfig:{trigger: 'click', mode: 'cell', showStatus: true},
      highlightCurrentRow:true,
      toolbarConfig: {
        import: true,
        custom: true
      },
      importConfig: {
        remote: true,
        importMethod: importMethod,
        types: ['xls','xlsx'],
        modes: ['insert']
      },
      formConfig:{
        data:{
          dcCh:'',
        },
        items:[
          { field: 'dcCh', title: '船只', span: 3,slots: { default: 'name_czbh' } },
          {span: 21,
            children:[
              { align:'right',itemRender: {
                  name: '$button', props: {content:'查询',status:'primary'},events:{click:initList}
                }
              },
              { align:'right',itemRender: {
                  name: '$button', props: {content:'保存',status:'primary'},events:{click:save}
                }
              },
              { align:'right',itemRender: {
                  name: '$button', props: {content:'提交',status:'primary'},events:{click:提交}
                }
              },
            ]
          },
        ],
      },
      columns:[
        { field: 'dcCh', title: '船号', width: 100,
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination', },
        },
        { field: 'dcPl', title: '批量', width: 100,
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination', },
        },
        { field: 'dcFd', title: '分段', width: 100,
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination', },
        },
        //=============小组相关========================
        { field: 'xzcd', title: '小组场地', width: 100,
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination', },
        },
        { field: 'xdb', title: '大板', width: 100 },
        { field: 'xskj', title: '数控件', width: 100 },
        { field: 'xpt', title: '平铁', width: 100 },
        { field: 'xyw', title: '压弯', width: 100 },
        { field: 'xxc', title: '型材', width: 100 },
        { field: 'xok', title: '钢加回复', width: 100,
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination', },
          editRender: {name: '$input'}
        },
        { field: 'xbz', title: '小组备注', width: 100,
          editRender: {name: '$input'}
        },
        //=============大组相关========================
        { field: 'dzcd', title: '大组场地', width: 100,
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination', },
        },
        { field: 'ddb', title: '大板', width: 100 },
        { field: 'dxc', title: '型材', width: 100 },
        { field: 'dsj', title: '散件', width: 100 },
        { field: 'dr150', title: 'R150', width: 100 },
        { field: 'dqb', title: '曲外板', width: 100 },
        { field: 'dok', title: '钢加回复', width: 100,
          filters: [{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }],
          filterRender: { name: 'FilterCombination', },
          editRender: {name: '$input'}
        },
        { field: 'dbz', title: '大组备注', width: 150,
          editRender: {name: '$input'}
        },
      ],
      data:list,
    },
  }

  return data
}

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

const saveData=(data)=>{
  return request({
    url: "/dmQfxq/save",
    method: "post",
    data,
  });
}

const submit=(data)=>{
  return request({
    url: "/dmQfxq/qfxqTJ",
    method: "post",
    data,
  });
}

export default init
