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

<script>
import { VXETable } from 'vxe-table'
import paoWanPgdConfig from './pgdTable'
import {bgControl, getDw, getPgd, getPgd2, getYdjhByZt, pdfList, pgWithForeman, zx} from "@/api/zyjh";

export default {
  name:'QgePgd',
  props: {
    worker:{
      type:Function,
      default:function (){}
    },
    workers:{
      type:Array,
      default:[]
    }
  },
  data(){
    return{
      type:'qgpg',
      gridOptions:{},
      list:{},
      setExt:null,
    }
  },
  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
        }
      }
    }
  },
  created(){
    const {options,columns,setExt}=paoWanPgdConfig()
    this.setExt=setExt
    this.gridOptions=options
    this.gridOptions.columns=columns
    this.setExt('workers',this.$props.workers)
    getDw({type:'llj'}).then(res=>{
      this.gridOptions.ycldw=res.data
    })
  },
  methods:{
    initData() {
      bgControl({}).then(res=>{
        const gx=res.data||'1'
        const data={
          zt:'52',
          wgzt:'62',
          wclzt:'61'
        }
        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}) {
      const worker = this.$props.worker()
      if (!worker) {
        this.$message.warning('请选择操作人')
        return;
      }
      if (checked) {
        this.paigong(worker)
      }
    },
    checkboxChange({checked, row}) {
      const worker = this.$props.worker()
      if (!worker) {
        this.$message.warning('请选择操作人')
        return;
      }
      if (checked) {
        this.paigong(worker)
      }
    },
    paigong0(item){
      if (!this.privateCheck()){
        return
      }
      const worker=this.$props.worker()
      const data={
        foremanId:worker.userCode,
        dcCh:item.dcCh,
        dcPl:item.dcPl,
        zxZtName:'QGPG',
        zt:'52'
      }
      const tmpArr=[data]
      pgWithForeman(tmpArr).then(()=>{
        this.initData()
      })
    },
    paigong(worker) {
      const list = this.$refs.vxeGrid.getCheckboxRecords()
      const tmpArr = []
      for (const item of list) {
        const data={
          id:item.id,
          foremanId:worker.userCode,
          dcCh:item.dcCh,
          dcPl:item.dcPl,
          zxZtName:'QGPG',
          zt:'52'
        }
        tmpArr.push(data)
      }
      // const data = {
      //   type: this.type,
      //   field: 'qgpgrq',
      //   zxZtName: 'QGPG',
      //   list: tmpArr
      // }
      VXETable.modal.confirm('您确定派工吗？').then(res => {
        res === 'confirm' && pgWithForeman(tmpArr).then(res=>{
          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>
