<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 #type="{ row }">
        <DictTag :tag="false" :value="row.type" :options="D_BZ_RYJZLB" />
      </template>
      <!-- 操作 -->
      <template #controls="{ row }">
        <el-link type="danger" @click="delDictItem(row.id)">删除</el-link>
      </template>
    </MyTable>
  </div>
  <ChooseUser :Single="false" v-model="chooseMarksVisible" @choosedUsers="addMarks" :roleIds="roleIds" />
</template>

<script setup>
import { ref, reactive, watch, toRaw, getCurrentInstance, onMounted, onUnmounted } from "vue";
import MyTable from "@/components/aboutTable/MyTable.vue";
import ChooseUser from "@/components/ChooseList/ChooseUser/index.vue";
import { tbZdqtGkxxBatchAdd, tbZdqtGkxxDelete, tbZdqtGkxxSelectPage } from '@/api/qt.js'
import { ElMessageBox } from "element-plus";
const { proxy } = getCurrentInstance();
const props = defineProps({
  dataList: {
    type: Object,
    default: () => { },
  }, disabled: {
    type: Boolean,
    default: false
  },
  showBut: {
    type: Boolean,
    default: false
  },
})
const { D_BZ_RYJZLB } = proxy.$dict('D_BZ_RYJZLB')
const pageData = reactive({
  tableData: [],
  tableColumn: [{
    prop: 'userName',
    label: '管控人员',
  }, {
    prop: 'type',
    label: '管控警种',
    showOverflowTooltip: true,
    showSolt: true,

  }, {
    prop: 'inDustRialId',
    label: '警号',

  }, {
    prop: 'deptName',
    label: '管控单位',
  }],
  tableHeight: '250px',
  keyCount: 0,
  tableConfiger: {
    border: true,
    stripe: true,
    showHeader: true,
    showIndex: true,
    indexLabel: '序号',
    indexWidth: 60,
    align: 'center',
    showOverflowTooltip: true,
    haveControls: !props.disabled
  },
  controlsWidth: 120,

})
onMounted(() => {
  console.log('ControlInfo 组件已挂载！')
})
const listData = ref({})
watch(() => props.dataList, (val) => {
  if (val) {
    listData.value = val
    pageData.tableData = val.zdryList
    gettbZdqtGkxxSelectPage()
  }
}, { deep: true })
const roleIds = ref([])
const chooseMarksVisible = ref(false)
// 修改函数
const addMarks = (val) => {
  pageData.tableData = val
  roleIds.value = val.map(item => item.id)
  if (!props.disabled && props.showBut) {
    const promes = {
      list: pageData.tableData.map(item => {
        return {
          qtid: listData.value.id,
          gkdwdm: item.deptId,
          gkdwmc: item.deptName,
          gkjz: item.type,
          jh: item.inDustRialId,
          xm: item.userName,
        }
      })
    }
    gettbGsxtZdqtAdd(promes)
  }
}
// 删除函数
const delDictItem = (row) => {

  if (!props.disabled && props.showBut) {
    ElMessageBox.confirm(
      '是否删除管控信息',
      '提示', {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    }
    )
      .then(() => {
        pageData.tableData = pageData.tableData.filter(item => item.id !== row)
        roleIds.value = roleIds.value.filter(id => id !== row)
        const promes = { ids: [row] }
        gettbGsxtZdqtDelete(promes)
      })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: '取消删除',
        })
      })
  } else {
    pageData.tableData = pageData.tableData.filter(item => item.id !== row)
    roleIds.value = roleIds.value.filter(id => id !== row)
  }
}
// 新增接口
const gettbGsxtZdqtAdd = (val) => {
  tbZdqtGkxxBatchAdd(val).then((res) => {
    proxy.$message.success('添加成功')
  }).catch((err) => {
    proxy.$message.error('操作失败')
  });
}
// 删除接口
const gettbGsxtZdqtDelete = (val) => {
  tbZdqtGkxxDelete(val).then((res) => {
    proxy.$message.success('删除成功')
  }).catch((err) => {
    proxy.$message.error('操作失败')
  });
}
// 查询接口
const gettbZdqtGkxxSelectPage = (val) => {
  const promes = {
    pageSize: 1000,
    pageCurrent: 1,
    qtid: listData.value.id,
  }
  tbZdqtGkxxSelectPage(promes).then(res => {
    pageData.tableData = res.records.map(item => {
      return {
        id: item.id,
        deptId: item.gkdwdm,
        deptName: item.gkdwmc,
        type: item.gkjz,
        inDustRialId: item.jh,
        userName: item.xm,
      }
    })
    roleIds.value = res.records.map(item => item.id)
  })
}

// 抛出数据
const throwData = () => {
  return new Promise((resolve, reject) => {
    // 验证是否有管控人员
    // if (pageData.tableData.length === 0) {
    //   reject(new Error('请选择管控人员'));
    //   return;
    // }

    const transformedData = pageData.tableData.map(item => {
      return {
        gkdwdm: item.deptId,
        gkdwmc: item.deptName,
        gkjz: item.type,
        jh: item.inDustRialId,
        xm: item.userName,
      }
    });

    resolve(transformedData);
  });
}
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>
