<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, zx} from "@/api/zyjh";

export default {
  name:'PwanPgdTz',
  props:{
    height:{
      type:Number,
      default:0
    },
    worker:{
      type:Function,
      default:function (){}
    },
    workers:{
      type:Array,
      default:[]
    }
  },
  data(){
    return{
      type:'pwpg',
      gridOptions:{},
      list:{},
      setExt:null,
      deviceId:null,
    }
  },
  created(){
    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.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 worker=this.$props.worker()
          if (!worker){
            this.$message.warning('请选择施工人员后再次派工!!!')
            return
          }
          const tmpArr=[{
            id:item.id,
            pwry:worker.userCode
          }]
          this.paigong0(tmpArr)
          break
        }
      }
    }
  },
  methods:{
    initData(){
      bgControl({}).then(res=>{
        const gx=res.data||'1'
        if (gx==='1'){
          const data={
            zt:'21',
          }
          getPgd(data).then(res => {
            this.list=res.data
            this.gridOptions.data = res.data
          })
        }
        if (gx==='0'){
          getPgd2({field:'pwry'}).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 worker=this.$props.worker()
        this.paigong(worker)
      }
    },
    checkboxChange({checked}){
      if (!this.privateCheck(checked)){
        this.$refs.vxeGrid.setAllCheckboxRow(!checked);
        return
      }
      if (checked){
        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={
          id:item.id,
          pwry:worker.userCode,//抛丸施工人员姓名
        }
        tmpArr.push(data)
      }
      const data={
        type:this.type,
        field:'pwpgrq',
        zxZtName:'PWPG',
        list:tmpArr
      }
      VXETable.modal.confirm('您确定派工吗？').then(res=>{
        res==='confirm'&&zx(data).then(resp=>{
          this.initData()
        })
      })
    },
    paigong0(arr){
      const data={
        type:this.type,
        field:'pwpgrq',
        zxZtName:'PWPG',
        list:arr
      }
      zx(data).then(resp=>{
        this.initData()
      })
    },
    cellClick({row}){
      this.$emit('initPgd',row)
    }
  }
}
</script>
