<template>
  <el-card style="margin-top:12px;position: relative;" class="simulation-add-add-step4">
    <template #header>
      <span>数据集列表</span>
      <el-form ref="queryFormRef" :model="queryParams" :inline="true"
        style="flex-grow: 1;text-align: right;position: absolute;top:4px;right:0px;">
        <el-form-item label="" prop="dataset_name">
          <el-input v-model="queryParams.dataset_name" placeholder="请输入数据集名称" clearable style="width: 250px" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery"><i-ep-search />搜索</el-button>
        </el-form-item>
      </el-form>
    </template>
    <el-table v-loading="loading" :data="info.tableData" stripe @row-click="doRowClick">
      <el-table-column align="center" width="55" label="选择">
        <template #default="scope">
          <el-radio v-model="datasSelection" :label="scope.row.dataset_id + ''"
            @change="handleChange(scope.row)">&nbsp;</el-radio>
        </template>
      </el-table-column>
      <el-table-column label="数据集名称" align="left" prop="dataset_name" />
      <el-table-column label="版本" align="left" prop="dataset_version" width="80" />
      <el-table-column label="数据集说明" align="left" prop="dataset_desc" show-overflow-tooltip />

      <el-table-column label="适用任务类型" align="left" prop="modl_sub_type" />
      <el-table-column label="图片数量" align="left" prop="frame_count" width="120" />
      <el-table-column label="数据集格式" width="120" align="left" prop="dats_dataset_format" />
      <el-table-column label="创建时间" align="left" prop="createTime" />

    </el-table>
    <pagination v-if="total > 0" v-model:total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="handleQuery" />
  </el-card>
</template>

<script setup>
import dataSetApi from '@/api/dataSet'
const datasSelection = ref("")//单选id
const loading = ref(false)
const queryFormRef = ref()
const emit = defineEmits(["change"]);
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  dataset_name: ''
});
const info = reactive({
  tableData: []
})
let total = ref(100)
/** 查询 */
function handleQuery(a) {
  if (a) {
    queryParams.pageSize = a.limit;
    queryParams.pageNum = a.page;
  }
  loading.value = true;
  dataSetApi.list(queryParams).then(d => {
    loading.value = false;
    total.value = d.data?.data?.total || 0;
    info.tableData = d.data?.data?.raw_dataset_list || [];
  });

}


const handleChange = (row) => {
  datasSelection.value = row.dataset_id + "";
  emit("change")
}
const doRowClick = (row, col, sel) => {
  datasSelection.value = row.dataset_id + "";
  emit("change")
}


const checkForm = () => {
  if (datasSelection.value) {
    let tmps = info.tableData.filter(d => d.dataset_id == datasSelection.value);
    return tmps.length > 0 ? tmps[0] : null;
  }
  return null;
};
const returnData = (d) => {  
    if(d){
      datasSelection.value=""+d.dataset_id
    }
}

onMounted(() => {
  handleQuery();
});
defineExpose({
  checkForm,
  returnData
})
</script>
<style lang='scss'></style>