<template>
  <el-row :gutter="20">
    <el-col :span="24">
      <el-button type="primary" plain @click="selectBmObj">选择对象</el-button>
    </el-col>
    <el-col :span="24">
      <bm-table :listData="listData" :loading="loading" :selection="false">
        <template #action="scope">
          <el-popconfirm title="是否要删除该条对象?" @confirm="delBm({...scope.row})" width="auto">-->
            <template #reference>
            <el-button type="text" size="small" icon="delete">删除</el-button>-->
            </template>
            </el-popconfirm>
        </template>
      </bm-table>
    </el-col>
  </el-row>

  <el-dialog title="选择对象" v-model="open" width="800px" append-to-body>
    <bm-table :listData="selectBmData" :loading="loading" @selectObj="selectObj" :selection="true" />
    <template #footer>
      <div style="text-align: center">
        <el-button type="primary" @click="handleAddBm">添加</el-button>
        <el-button  @click="open = false">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup lang="ts">
import {listBd,delBd} from "@/api/hl/bd"
import {selectBm,addBmModel} from "@/api/hl/bm"
import {getCurrentInstance, watchEffect} from "vue";
import bmTable from "./bmTable.vue"
const {proxy} = getCurrentInstance()
const props = defineProps({
  modelId:{type:Number,default:null}
})
const {biz_type} = proxy.useDict('biz_type');
const listData = ref([])
const loading = ref(false)
const open = ref(false)
const selectBmData = ref([])
const objIds = ref([])

watchEffect(()=>{
  getList()
})

function selectObj(ids){
  objIds.value = ids
}

function getList(){
  loading.value = true
  listBd({modelId:props.modelId}).then(res=>{
    listData.value = res.rows
    loading.value = false
  })
}

function delBm(row){
  delBd(row.bizDetailId).then(res=>{
    proxy.$modal.msgSuccess(res.msg);
    getList()
  })
}

function handleAddBm(){
  if(!objIds.value.length){
    return proxy.$modal.msgError("请选择对象")
  }
  addBmModel(props.modelId,{objIds:objIds.value.join(",")}).then(res=>{
      proxy.$modal.msgSuccess("添加成功")
      open.value = false
      getList()

  })
}

function selectBmObj(){
  open.value = true
  loading.value = true
  objIds.value = []
  selectBm(props.modelId).then(res=>{
    selectBmData.value = res.rows.map(p=>{
      return {bizObj:p}
    })
    loading.value = false
  })
}
</script>
<style scoped lang="scss">
</style>
