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

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

export default {
  name: 'QgePgd',
  props: {
    worker:{
      type:Function,
      default:function (){}
    },
    workers:{
      type:Array,
      default:[]
    },
    dw:{
      type:Array,
      default: []
    }
  },
  data() {
    return {
      gridOptions: {},
      list: {},
      currentWorker:null,
      zt:null,
      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.djh===data.msg.orderNumber||item.djh.slice(-djhWhLengh)===data.msg.orderNumber)
      if (tmpList.length>1){
        this.list=tmpList
        this.$message.warning('短号有重复,请使用全工单号重新派工!!!')
        return
      }
      for (const item of this.list) {
        if (item.djh===data.msg.orderNumber||item.djh.slice(-djhWhLengh)===data.msg.orderNumber){
          const tmpArr=[{
            id:item.id,
            qgfkry:item.hxry
          }]
          this.baogong(tmpArr,item)
          break
        }
      }
    }
  },
  created() {
    const {options,columns,setExt}=paoWanPgdConfig()
    this.setExt=setExt
    this.gridOptions = options
    this.gridOptions.columns = columns
  },
  methods: {
    baogong(tmpArr,data){
      console.log(data)
      let ycldwInfo=null
      for (const dw of this.dw) {
        for (const info of dw.list) {
          if (data.dcCh===info.czbh&&data.dcPl===info.pl&&data.dcFd===info.fd&&data.tzbh===info.tlth){
            ycldwInfo=info
            break
          }
        }
      }
      if (!ycldwInfo){
        ycldwInfo={}
      }
      zx({
        field:'qgfkrq',
        zxZtName:'QGFK',
        list:tmpArr,
        ycldwInfo:ycldwInfo
      }).then(res=>{
        if (res.success){
          window.localStorage.removeItem('zyjh')
          this.initData(data.qgry,'61')
        }
      })
    },
    initData(worker, zt) {
      this.currentWorker=worker
      this.zt=zt
      getDw({type:'llj'}).then(res=>{
        this.gridOptions.ycldw=res.data
        getPgd({user:worker,zt:zt,field:'qgry'}).then(res => {
          this.list=res.data
          this.gridOptions.data = res.data
          this.setExt('workers',this.$props.workers)
        })
      })
    },
    getCurrentData() {
      const data = this.$refs.qgpgd.getCurrentRecord()
      window.localStorage.setItem("qgfk", JSON.stringify(data))
    },
    checkboxAll({checked}) {
      const worker = this.$props.worker
      if (!worker) {
        this.$message.warning('请选择操作人')
        return;
      }
      if (checked) {
        this.fankgui(worker)
      }
    },
    checkboxChange({checked, row}) {
      const worker = this.$props.worker()
      if (!worker) {
        this.$message.warning('请选择操作人')
        return;
      }
      if (checked) {
        this.fankgui(worker)
      }
    },
    fankgui(worker) {
      const list = this.$refs.qgpgd.getCheckboxRecords()
      const tmpArr = []
      for (const item of list) {
        const data = {
          id: item.id,
          qgfkry: worker.userCode,
          zt:item.zt
        }
        tmpArr.push(data)
      }
      const data = {
        type: this.type,
        field: 'qgfkrq',
        zxZtName: 'QGFK',
        list: tmpArr
      }
      VXETable.modal.confirm('您确定报工吗？').then(res => {
        res === 'confirm' && zx(data).then(res=>{
          this.initData(this.currentWorker,this.zt)
        })
      })
    },
    cellClick({row}) {
      this.$emit('pgdClick',row)
      this.$emit('initPgd', row)
    }
  }
}
</script>
