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

export default {
  name: 'QgePgd',
  props: {
    worker:{
      type:Function,
      default:function (){}
    }
  },
  data() {
    return {
      gridOptions: {},
      list: {},
      currentWorker:null,
      zt:null,
    }
  },
  created() {
    const {options, columns} = paoWanPgdConfig()
    this.gridOptions = options
    this.gridOptions.columns = columns
  },
  methods: {
    initData(worker, zt) {
      this.currentWorker=worker
      this.zt=zt
      getPgd({user: worker, zt: zt, field: 'qgry'}).then(res => {
        this.gridOptions.data = res.data
      })
    },
    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('initPgd', row)
    }
  }
}
</script>
