<template>
  <div>
    <div class="headClass" style="">
      <h3>群体标签</h3>
      <el-button type="primary" :disabled="disabled" @click="chooseMarksVisible = true">选择</el-button>
    </div>
    <MyTable :tableData="pageData.tableData" :tableColumn="pageData.tableColumn" :tableHeight="pageData.tableHeight"
      :key="pageData.keyCount" :tableConfiger="pageData.tableConfiger" :controlsWidth="pageData.controlsWidth">
      <template #bqLx="{ row }">
        <DictTag :tag="false" :value="row.bqLx" :options="D_GS_BQ_DJ" />
      </template>
      <template #bqLb="{ row }">
        <DictTag :tag="false" :value="row.bqLb" :options="D_GS_SSYJ" />
      </template>
      <!-- 操作 -->
      <template #controls="{ row }">
        <el-link type="danger" @click="delDictItem(row.bqId)">删除</el-link>
      </template>
    </MyTable>
  </div>
  <ChooseMarks v-model="chooseMarksVisible" @choosed="addMarks" :roleIds="roleIds" />
</template>

<script setup>
import { ref, reactive, watch, toRaw, getCurrentInstance, onMounted, onUnmounted } from "vue";
import MyTable from "@/components/aboutTable/MyTable.vue";
import ChooseMarks from "@/components/ChooseList/ChooseMarks/index.vue";
import { tbGsxtZdqtUpdate } from '@/api/qt.js'
import { ElMessage, ElMessageBox } from "element-plus";
const { proxy } = getCurrentInstance();
const { D_GS_BQ_DJ, D_GS_SSYJ } = proxy.$dict("D_GS_BQ_DJ", "D_GS_SSYJ"); //获取字典数据
const chooseMarksVisible = ref(false)
const props = defineProps({
  dataList: {
    type: Object,
    default: () => { },
  }, disabled: {
    type: Boolean,
    default: false
  },
  showBut: {
    type: Boolean,
    default: false
  },
})
const listData = ref({})
watch(() => props.dataList, (val) => {
  if (val) {
    listData.value = val
    pageData.tableData = val.bqList
  }
}, { deep: true })
const roleIds = ref([])
// 表格数据
const pageData = reactive({
  tableData: [],
  tableColumn: [{
    prop: 'bqMc',
    label: '标签名称',
    showOverflowTooltip: true
  }, {
    prop: 'bqDm',
    label: '标签代码',
  }, {
    showSolt: true,
    prop: 'bqLx',
    label: '标签类型',
  }, {
    showSolt: true,
    prop: 'bqLb',
    label: '标签类别',
  }],
  tableHeight: '200px',
  keyCount: 0,
  tableConfiger: {
    border: true,
    stripe: true,
    showHeader: true,
    showIndex: true,
    indexLabel: '序号',
    indexWidth: 60,
    align: 'center',
    showOverflowTooltip: true,
    haveControls: !props.disabled
  },
  controlsWidth: 200,
})
// 修改数据接口
const zdqtUpdate = (val) => {
  const params = {
    id: listData.value.id,
    bqList: pageData.tableData
  }
  tbGsxtZdqtUpdate(params).then(res => {
    proxy.$message({
      message: val,
      type: 'success'
    })
  })
}
// 新增标签
const addMarks = (val) => {
  pageData.tableData = val.map(v => {
    return { bqDm: v.bqDm, bqId: v.id, bqLb: v.bqLb, bqLx: v.bqLx, bqMc: v.bqMc }
  });
  roleIds.value = val.map(v => v.id)
  if (!props.disabled && props.showBut) {
    zdqtUpdate("标签添加成功")
  }
}
// 删除标签
const delDictItem = (val) => {
  if (!props.disabled && props.showBut) {
    ElMessageBox.confirm(
    '是否删除标签',
    '提示',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(() => {
      pageData.tableData = pageData.tableData.filter(v => v.bqId != val)
      roleIds.value = roleIds.value.filter(v => v != val)
        zdqtUpdate("标签删除成功")
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '取消删除',
      })
    })

  } else {
    pageData.tableData = pageData.tableData.filter(v => v.bqId != val)
    roleIds.value = roleIds.value.filter(v => v != val)
  }

}
// 抛出数据并验证标签列表不为空
const throwData = () => {
  return new Promise((resolve) => {
    // 验证：确保标签列表不为空
    if (!pageData.tableData || pageData.tableData.length === 0) {
      throw new Error('请选择群体标签');
    }
    
    resolve(pageData.tableData);
  });
}
defineExpose({
  throwData
})
</script>

<style lang="scss" scoped>
@import "~@/assets/css/layout.scss";
@import "~@/assets/css/element-plus.scss";

.backinfo-container {
  padding: 10px;
}

.left_box {
  width: 200px;
  border: 1px solid #c8c8c89a;
  border-radius: 5px;
  padding: 5px;
}

.right_box {
  width: calc(100% - 230px);
  overflow-y: auto;
  padding: 5px;
}

::v-deep .el-form-item__content {
  display: block !important;
}

.headClass {
  font-size: 18px;
  font-weight: 600;
  color: #303133;
  margin: 0 0 10px 0;
  padding-bottom: 10px;
  border-bottom: 2px solid #409eff;
  position: relative;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.headClass::after {
  content: '';
  position: absolute;
  left: 0;
  bottom: -2px;
  width: 60px;
  height: 2px;
  background-color: #409eff;
}

h3 {
  margin: 0;
}
</style>
