<template>
  <div>
    <div class="btn">
      <button @click="reset">重置</button>
      <button @click="btnClick">提交</button>
    </div>
    <VxeGrid ref="vxeGrid" v-bind="gridOptions" @checkbox-change="checkboxChange" />
  </div>
</template>

<script>
import { EventBus } from '@/event-bus.js'
import paoWanPgdConfig from "@/views/zyjhzx/pwanpgong/comps/pgdTable";
import {getDw, pwpg} from "@/api/zyjh";
import * as XLSX from 'xlsx'

export default {
  name:'PwanSgd',
  props:{
    worker:{
      type:Function,
      default:function (){}
    },
  },
  data(){
    return{
      type:'pwpg',
      gridOptions:{},
      list:[],
    }
  },
  mounted() {
    EventBus.$on('pwpg', item => {
      this.gridOptions.data.push(item)
    })
  },
  created(){
    const {options,columns,setExt}=paoWanPgdConfig(window.innerHeight*0.95)
    this.setExt=setExt
    this.gridOptions=options
    this.gridOptions.columns=columns
    getDw({type:'ycl'}).then(res=>{
      this.gridOptions.ycldw=res.data
    })
  },
  methods:{
    btnClick(){
      const data={
        worker:this.$props.worker().userCode,
        list:this.gridOptions.data,
      }
      pwpg(data).then(res=>{
        if (res.success){
          this.export()
          this.reset(false)
        }
      })
    },
    reset(resetDw=true){
      const _dw=[]
      for (const item of this.gridOptions.data) {
        if (!_dw.includes(item.dwId)){
          _dw.push(item.dwId)
        }
      }
      if (resetDw&&_dw.length>1){
        this.$message.warning('不允许多个垛位同时重置')
        return;
      }
      if (resetDw){
        this.gridOptions.data.forEach((item,index)=>{
          item.zt='BF'
          EventBus.$emit('pwtg', {
            ...item
          })
        })
      }
      this.gridOptions.data = []
    },
    export(){
      let data = this.gridOptions.data;
      if (data.length>0){
        const column=this.gridOptions.columns
        const fields={}
        for (const item of column) {
          const name=item.field;
          const value=item.title
          fields[name]=value
        }
        const printData=[]
        data.forEach((item,index)=>{
          const _item={}
          for (const i in fields) {
            _item[fields[i]]=item[i]
          }
          _item.序号=index+1
          // _item.分段=item.fd
          // _item.套料图号=item.tlth
          for (const dw of this.gridOptions.ycldw) {
            if (item.dwId===dw.id){
              _item.垛位=dw.name
              break;
            }
          }
          printData.push(_item)
        })
        const workBook = XLSX.utils.book_new();
        const sheet = XLSX.utils.json_to_sheet(printData)
        XLSX.utils.book_append_sheet(workBook, sheet, "sheetName");
        XLSX.writeFile(workBook, '抛丸计划.xlsx')
      }
    },
    checkboxChange({row}){
      row.zt='BF'
      EventBus.$emit('pwtg', {
        ...row
      })
      const data=[]
      this.gridOptions.data.forEach((item,index)=>{
        if (item.id===row.id){
          return
        }
        data.push(item)
      })
      this.gridOptions.data=data
      console.log(this.gridOptions.data)
    },
  }
}
</script>

<style scoped>
.btn{
  text-align: right;
  button{
    width: 15%;
    margin-right: 5px;
    background-color: blue;
  }
}
</style>
