<template>
  <VxeGrid ref="vxeGrid" v-bind="gridOptions" @checkbox-change="checkboxChange" @checkbox-all="checkboxAll" />
</template>

<script>
import {EventBus} from '@/event-bus.js'
import { VXETable } from 'vxe-table'
import paoWanPgdConfig from './pgdTable'
import {getDw, pgWithForeman} from "@/api/zyjh";

export default {
  name:'PwanPgd',
  props:{
    height:{
      type:Number,
      default:0
    },
    worker:{
      type:Function,
      default:function (){}
    },
    workers:{
      type:Array,
      default:[]
    }
  },
  data(){
    return{
      type:'pwpg',
      gridOptions:{},
      list:[],
      tmpList:[],
      setExt:null,
      zyq:null,
      deviceId:null,
    }
  },
  mounted() {
    EventBus.$on('pwtg', item => {
      if (this.tmpList[0]?.dwId!==item.dwId){
        this.$message.warning('垛位不符,请选择正确垛位再取消')
        return;
      }
      this.tmpList.forEach(data=>{
        if (data.id===item.id){
          data.zt=item.zt
        }
      })
      // this.gridOptions.data = this.list
      this.showData(this.list,false)
      EventBus.$emit('pwtgComp', {
        flag:true
      })
    })
  },
  created(){
    this.zyq=this.$route.query.zyq
    this.deviceId=this.$route.query.deviceId||''
    const {options,columns,setExt}=paoWanPgdConfig(this.$props.height)
    this.setExt=setExt
    this.gridOptions=options
    this.gridOptions.columns=columns
    getDw({type:'ycl'}).then(res=>{
      this.gridOptions.ycldw=res.data
    })
  },
  sockets:{
    zyjhzx(data){
      if (this.deviceId!==data.msg.deviceId&&data.msg.actionType!=='开始派工'){
        return
      }
      //工单号尾号长度
      const djhWhLengh=3
      const tmpList=this.list.filter(item=>item.plxh===data.msg.orderNumber||item.plxh?.slice(-djhWhLengh)===data.msg.orderNumber)
      if (tmpList.length>1){
        this.list=tmpList
        this.$message.warning('短号有重复,请使用完整工单号重新派工!!!')
        return
      }
      for (const item of this.list) {
        if (item.plxh===data.msg.orderNumber||item.plxh?.slice(-djhWhLengh)===data.msg.orderNumber){
          this.paigong0(item)
          break
        }
      }
    }
  },
  methods:{
    showData(list,flag=false){
      if (this.tmpList.length===0||flag){
        this.tmpList=list
      }

      this.list=this.tmpList.filter(item=>item.zt==='BF')
      this.gridOptions.data = this.list.reverse()
    },
    // initData(){
    //   bgControl({}).then(res=>{
    //     const gx=res.data||'1'
    //     const data={
    //       zt:'21',
    //       wgzt:'32',
    //       wclzt:'31'
    //     }
    //     if (gx==='1'){
    //       getYdjhByZt(data).then(res=>{
    //         this.list=res.data
    //         this.gridOptions.data = res.data
    //       })
    //     }
    //     if (gx==='0'){
    //       data.zt='2'
    //       getYdjhByZt(data).then(res=>{
    //         this.list=res.data
    //         this.gridOptions.data = res.data
    //       })
    //     }
    //   })
    //
    // },
    setWorkers(workers){
      this.setExt('workers',workers)
      // this.initData()
    },
    checkboxAll({checked}){
      if (!this.privateCheck(checked)){
        this.$refs.vxeGrid.setAllCheckboxRow(!checked);
        return
      }
      if (checked){
        const data=this.$refs.vxeGrid.getCheckboxRecords();
        data.forEach(row=>{
          row.zt='PG'
          EventBus.$emit('pwpg', {
            ...row
          })
        })
        this.$refs.vxeGrid.removeCheckboxRow()
      }
    },
    checkboxChange({checked,row}){
      if (!this.privateCheck(checked)){
        this.$refs.vxeGrid.setAllCheckboxRow(!checked);
        return
      }
      if (checked){
        row.zt='PG'
        EventBus.$emit('pwpg', {
          ...row
        })
        this.$refs.vxeGrid.removeCheckboxRow()

        //const worker=this.$props.worker()
        // this.paigong(worker)
      }
    },
    privateCheck(){
      const worker=this.$props.worker()
      if (!worker){
        this.$message.warning('请选择操作人')
        return false;
      }
      return true
    },
    paigong(worker){
      const list=this.$refs.vxeGrid.getCheckboxRecords()
      const tmpArr=[]
      for (const item of list) {
        const data={
          foremanId:worker.userCode,
          dcCh:item.dcCh,
          dcPl:item.dcPl,
          zxZtName:'PWPG',
          zt:'21'
        }
        tmpArr.push(data)
      }
      VXETable.modal.confirm('您确定派工吗？').then(res=>{
        res==='confirm'&&pgWithForeman(tmpArr).then(resp=>{
          this.initData()
        })
      })
    },
    paigong0(item){
      if (!this.privateCheck()){
        return
      }
      const worker=this.$props.worker()
      const data={
        foremanId:worker.userCode,
        dcCh:item.dcCh,
        dcPl:item.dcPl,
        zxZtName:'PWPG',
        zt:'21'
      }
      const tmpArr=[data]
      pgWithForeman(tmpArr).then(()=>{
        this.initData()
      })
    },
    cellClick({row}){
      // this.$emit('initPgd',row)
      // pdfList({
      //   dcCh: row.dcCh,
      //   dcPl: row.dcPl,
      // }).then((res) => {
      //   this.pdfDataList = res.data;
      //   for (const item of this.pdfDataList) {
      //     if (item.indexOf('套料图')>-1){
      //       const url=`${process.env.VUE_APP_BASE_API}/upload/${row.dcCh}/${row.dcPl}/PDF/${item}.pdf`;
      //       this.$emit('loadPdf',url)
      //       return;
      //     }
      //   }
      // });
    }
  }
}
</script>
