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

<script>
import XEUtils from 'xe-utils'
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)
    })
    EventBus.$on('pwtgComp', item => {
      if (item.flag){
        // this.gridOptions.data = []
      }
    })
  },
  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,
      }
      // aa(data).then(()=>{
      //
      // })
      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;
            }
          }
          for (const dw of this.gridOptions.tlt) {
            if (item.kw===dw.bm){
              _item.垮位=dw.bmsm
              break;
            }
          }
          for (const dw of this.gridOptions.zl) {
            if (item.zl===dw.bm){
              _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");
        const date=XEUtils.toDateString(new Date(),'yyyyMMdd')
        XLSX.writeFile(workBook, `抛丸计划${date}.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.reverse()
      this.$refs.vxeGrid.setAllCheckboxRow(false);
    },
  }
}
</script>

<style scoped>
.btn{
  text-align: right;
}
</style>
