<template>
  <div>
    <div class="headClass" style="">
      <h3>群体性质</h3>
      <el-button type="primary" :disabled="disabled" @click="showDialog = true">选择</el-button>
    </div>
    <MyTable :tableData="pageData.tableData" :tableColumn="pageData.tableColumn" :tableHeight="pageData.tableHeight"
      :key="pageData.keyCount" :tableConfiger="pageData.tableConfiger" :controlsWidth="pageData.controlsWidth">
      <!-- 操作 -->
      <template #controls="{ row }">
        <el-link type="danger" @click="delDictItem(row)">删除</el-link>
      </template>
    </MyTable>
  </div>
  <ShowXz v-model="showDialog" @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 ShowXz from '../component/showXz'
import {  ElMessageBox } from "element-plus";
import { tbZdqtQtxzBatchAdd, tbZdqtQtxzBatchDelete, tbZdqtQtxzSelectPage } from '@/api/qt.js'
const { proxy } = getCurrentInstance()
const props = defineProps({
  dataList: {
    type: Object,
    default: () => { },
  }, disabled: {
    type: Boolean,
    default: false
  },
  showBut: {
    type: Boolean,
    default: false
  },
})

const pageData = reactive({
  tableData: [],
  tableColumn: [{
    prop: 'zdmc',
    label: '性质名称',
  }, {
    prop: 'dm',
    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: 120,

})
const listData = ref([])
watch(() => props.dataList, (val) => {
  if (val) {
    listData.value = val
    getTbZdqtQtxzSelectPage({ qtid: val.id, pageCurrent: 1, pageSize: 1000 })
  }
}, { deep: true })
const showDialog = ref(false)
const roleIds = ref([])
//新增接口
const gettbZdqtQtxzBatchAdd = (val) => {
  tbZdqtQtxzBatchAdd(val).then(res => {
    let str = ''
    if (!props.disabled && props.showBut) {
      str = '修改成功'
    } else {
      str = '添加成功'
    }
    proxy.$message({
      message: str,
      type: 'success',
    })
  })
}
// 删除接口
const deleteTbZdqtQtxzBatchDelete = (val) => {
  tbZdqtQtxzBatchDelete(val).then(res => {
    proxy.$message({
      message: '删除成功',
      type: 'success',
    })
  })
}
// 查询接口
const getTbZdqtQtxzSelectPage = (val) => {
  tbZdqtQtxzSelectPage(val).then(res => {
    roleIds.value = res.records.map(item => item.xzdm)
    pageData.tableData = res.records.map(item => {
      return {
        zdmc: item.xzmc,
        dm: item.xzdm,
        id: item.id
      }
    })
  })
}
// 添加性质标签
const addMarks = (val) => {
  pageData.tableData = val.map(v => {
    return { zdmc: v.zdmc, dm: v.dm }
  });
  roleIds.value = val.map(v => v.dm)
  const promes = {
    list: pageData.tableData.map(v => {
      return {
        qtid: listData.value.id,
        xzdm: v.dm,
        xzmc: v.zdmc,
      }
    })
  }
  if (props.disabled && props.showBut) {
    gettbZdqtQtxzBatchAdd(promes)
  }
}
// 删除性质标签
const delDictItem = (row) => {
  if (!props.disabled && props.showBut) {
    ElMessageBox.confirm(
      '是否删除性质标签',
      '提示',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }
    )
      .then(() => {
        pageData.tableData = pageData.tableData.filter(v => v.dm != row.dm)
        roleIds.value = roleIds.value.filter(v => v != row.dm)
        const promes = {ids: [row.id]}
        deleteTbZdqtQtxzBatchDelete(promes)
      })
      .catch(() => {
        proxy.$message({
          message: '删除失败',
          type: 'info',
        })
      })
  } else {
    pageData.tableData = pageData.tableData.filter(v => v.dm != row.dm)
    roleIds.value = roleIds.value.filter(v => v != row.dm)
  }
}
// 抛出数据并验证性质列表不为空
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>
