<template>
  <div>
    <div class="headClass" style="">
      <h3>关联车辆</h3>
      <el-button type="primary" :disabled="disabled" @click="AddPore">选择</el-button>
    </div>
    <MyTable :tableData="pageData.tableData" :tableColumn="pageData.tableColumn" :tableHeight="pageData.tableHeight"
      :key="pageData.keyCount" :tableConfiger="pageData.tableConfiger" :controlsWidth="pageData.controlsWidth">
      <template #clys="{ row }">
        <DictTag :tag="false" :value="row.clys" :options="D_BZ_CLYS" />
      </template>
      <template #cllx="{ row }">
        <DictTag :tag="false" :value="row.cllx" :options="D_BZ_CLLX" />
      </template>
      <template #clpp="{ row }">
        <DictTag :tag="false" :value="row.clpp" :options="D_BZ_CLPP" />
      </template>
      <!-- 操作 -->
      <template #controls="{ row }">
        <el-link type="danger" @click="delDictItem(row.id)">删除</el-link>
        <el-link type="danger" @click="updDictItem(row)">修改</el-link>
      </template>
    </MyTable>
  </div>
  <VehiclDoing v-model="chooseMarksVisible" @comfirm="addMarks" :data="dataModel"
    :dict="{ D_BZ_CLLX, D_BZ_CLYS, D_BZ_CLPP }" />
</template>

<script setup>
import { ref, reactive, watch, toRaw, getCurrentInstance, onMounted, onUnmounted } from "vue";
import MyTable from "@/components/aboutTable/MyTable.vue";
import VehiclDoing from "../component/vehiclDoing.vue";
import { tbZdryClxxAdd, tbZdryClxxBatchAdd, tbZdryClxxDelete, tbZdryClxxSelectPage, tbZdryClxxUpdate } from '@/api/zdr.js'
import { ElMessage, ElMessageBox } from "element-plus";
const { proxy } = getCurrentInstance();
const { D_BZ_CLLX, D_BZ_CLYS, D_BZ_CLPP } = proxy.$dict("D_BZ_CLLX", "D_BZ_CLYS", "D_BZ_CLPP"); //获取字典数据
const chooseMarksVisible = ref(false)
const props = defineProps({
  dataList: {
    type: Object,
    default: () => { },
  }, disabled: {
    type: Boolean,
    default: false
  },
  showBut: {
    type: Boolean,
    default: false
  },
})
const listData = ref({})
const addUpd = ref(true)
watch(() => props.dataList, (val) => {
  if (val) {
    listData.value = val
    gettbZdryClxxSelectPage()
  }
}, { deep: true })
// 表格数据
const pageData = reactive({
  tableData: [],
  tableColumn: [{
    prop: 'cph',
    label: '车牌号',
  }, {
    prop: 'cllx',
    label: '车辆类型',
    showSolt: true,
    prop: 'clpp',
  }, {
    showSolt: true,
    prop: 'clys',
    label: '车辆颜色',
  }, {
    showSolt: true,
    prop: 'clpp',
    label: '车辆品牌',

    prop: 'clsbm',
    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 dataModel = ref()
const addMarks = (val) => {
  const params = {
    ...val,
    zdrid: listData.value.id,
  }
  if (props.showBut && !props.disabled) {
    if (addUpd.value) {
      tbZdryClxxAdd(params).then(res => {
        gettbZdryClxxSelectPage()
        proxy.$message({
          message: '关联车辆添加成功',
          type: 'success'
        })
      })
    }
    else {
      tbZdryClxxUpdate(params).then(res => {
        gettbZdryClxxSelectPage()
        proxy.$message({
          message: '关联车辆修改成功',
          type: 'success'
        })
      })
    }
  } else {
    pageData.tableData.push(val)
  }
}
// 删除车辆
const delDictItem = (val) => {
  if (!props.disabled && props.showBut) {
    ElMessageBox.confirm(
      '是否删除关联车辆',
      '提示',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }
    )
      .then(() => {
        tbZdryClxxDelete({ ids: [val] }).then(res => {
          gettbZdryClxxSelectPage()
          proxy.$message({
            message: '关联车辆删除成功',
            type: 'success'
          })
        })
      })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: '取消删除',
        })
      })
  } else {
    pageData.tableData = pageData.tableData.filter(v => v.id != val)
  }

}

const updDictItem = (val) => {
  chooseMarksVisible.value = true
  addUpd.value = false
  dataModel.value = val
}
const AddPore = () => {
  chooseMarksVisible.value = true
  dataModel.value = {}
  addUpd.value = true
}
// 查询车辆
const gettbZdryClxxSelectPage = () => {
  const promes = {
    pageCurrent: 1,
    pageSize: 20,
    zdrid: listData.value.id
  }
  tbZdryClxxSelectPage(promes).then(res => {
    pageData.tableData = res.records
  })
}
// 抛出数据并验证标签列表不为空
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>
