<template>
  <div id="bfangTable">
    <VxeGrid ref="bFangRef" v-bind="gridOptions" @cell-dblclick="showDetail" />
  </div>
</template>

<script>
import tableConfig from "@/views/zyjhzx/bfang/comps/table";
import {EventBus} from "@/event-bus";
import {getBfwz} from "@/api/jhzxgl/ypp";
import {ppBGong, wbfList, yppbg} from "@/api/zyjh";

export default {
  name: "BFangTable",
  props:{
    height:{
      type:Number|String,
      default:()=>786
    }
  },
  data(){
    return {
      gridOptions:{},
      zyq:'',
    }
  },
  created(){
    const {options,columns} = tableConfig();
    this.gridOptions = options;
    this.gridOptions.height=this.$props.height
    this.gridOptions.columns = columns;
    this.zyq=this.$route.query.zyq
    this.yppShow()
    EventBus.$on('yppbg', param=>{
      this.bgong()
    })
  },
  mounted(){
    EventBus.$on('pl', params => {
      this.pl(params)
    })
    EventBus.$on('bfSave', params => {
      this.save()
    })
  },
  methods:{
    bgong(){
      const data=this.$refs.bFangRef.getCheckboxRecords()
      if(data.length>0){
        yppbg(data).then(()=>{
          this.$message.success('成功');
          this.wzxjc()
        })
      }
    },
    showDetail({row}){
      EventBus.$emit('detailShow', row)
    },
    pl(params){
      const data=this.$refs.bFangRef.getCheckboxRecords()
      if (data.length>0){
        data.forEach(item=>{
          item.ljList.forEach(lj=>{
            lj.qq=params.qy
            lj.dw=params.dw
          })
        })
      }
    },
    save(){
      const data=this.$refs.bFangRef.getCheckboxRecords()
      if(data.length >0){
        const saveList=[]
        data.forEach(item=>{
          item.ljList.filter(it=>!!it.qq&&!!it.dw).forEach(lj=>{
            const _lj={
              id:lj.id,
              qq:lj.qq,
              dw:lj.dw,
              ppzt:'02'
            }
            saveList.push(_lj)
          })
        })
        ppBGong(saveList).then((res)=>{
          this.$message.success('成功')
          this.initData()
        })
      }
    },
    initData() {
      const params=this.dataInitParam;
      !params.zyq&&(params.zyq=this.zyq||'')
      wbfList(params).then((res) => {
        res.data.forEach(item => {
          item.selected = false
          item.total=item.ljList.length||0
          item.wbf=item.ljList.filter(it=>it.ppzt==='01').length
          item.ybf=item.total-item.wbf

          const dw=new Set()
          item.ljList?.forEach(it=>{
            dw.add(it.dw||'')
          })

          item.dwh=[...dw].join(',');

          // for (const dw of this.dwxx) {
          //   if (item.dwxxId === dw.id) {
          //     item.qy = dw.qy
          //     item.dwh = dw.dwh
          //   }
          // }
        })
        this.gridOptions.data = res.data.filter(item => item.ljList.length > 0)
      })
    },
    yppShow(){
      this.initDw()
      EventBus.$on('bfSearch', params => {
        this.dataInitParam=params;
        this.initData()
      })
    },
    initDw(){
      getBfwz().then((res)=>{
        this.dwxx=res.data
      })
    },
  }
}
</script>

<style scoped>

</style>
