<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
    <el-form ref="queryFormRef" :model="queryParams" :inline="true">
      <el-form-item label="数据源" prop="dataName">
        <el-select v-model="queryParams.dataName" filterable placeholder="请选择/输入数据源名称">
          <el-option v-for="item in dataNameList" :key="item" :label="item" :value="item"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="表名称" prop="tableName">
        <el-input v-model="queryParams.tableName" placeholder="请输入表名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="表描述" prop="tableComment">
        <el-input v-model="queryParams.tableComment" placeholder="请输入表描述" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never" v-loading="loading">
      <el-table ref="dbTableRef" :data="dbTableList"
                height="260px"
                @row-click="handleRowClick"
                highlight-current-row
                @selection-change="handleSelectionChange">
<!--        <el-table-column type="selection" width="55"></el-table-column>-->
        <el-table-column prop="tableName" label="表名称" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="tableComment" label="表描述" :show-overflow-tooltip="true"></el-table-column>
      </el-table>
      <pagination v-show="total > 0" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { listDbTable, getDataNames } from '@/api/ai/base/aiFormSetting';
import {DbTableQuery, AiFormSettingVO} from '@/api/ai/base/aiFormSetting/types';

const showSearch = ref(true);
const loading = ref(false);

const total = ref(0);
const visible = ref(false);
const tables = ref<Array<string>>([]);
const selectedTables = ref([]);
const selectedRow = ref<AiFormSettingVO>();

const dbTableList = ref<Array<AiFormSettingVO>>([]);
const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const dbTableRef = ref<ElTableInstance>();
const queryFormRef = ref<ElFormInstance>();

const queryParams = ref<DbTableQuery>({
  pageNum: 1,
  pageSize: 10,
  tableName: '',
  tableComment: '',
  dataName: ''
});

const dataNameList = ref<Array<string>>([]);

const emit = defineEmits(['ok']);

/** 查询参数列表 */
const show = (dataName: string) => {
  getDataNameList();
  if (dataName) {
    queryParams.value.dataName = dataName;
  } else {
    queryParams.value.dataName = 'master';
  }
  getList();
  visible.value = true;
};
/** 单击选择行 */
const clickRow = (row: AiFormSettingVO) => {
  // ele bug
  dbTableRef.value?.toggleRowSelection(row, false);
};
/** 多选框选中数据 */
const handleSelectionChange = (selection: AiFormSettingVO[]) => {
  selectedTables.value = selection.map((item) => {
    return {
      tableName: item.tableName,
      tableComment: item.tableComment,
      dataName: queryParams.value.dataName
    }
  })
  // const index = selectedTables.value.findIndex(t => t.tableName === selection.tableName);
  // if (index >= 0) {
  //   selectedTables.value.splice(index, 1);
  // } else {
  //   selectedTables.value.push({
  //     tableName: selection.tableName,
  //     tableComment: selection.tableComment
  //   });
  // }

};
/** 查询表数据 */
const getList = async () => {
  loading.value = true;
  const res = await listDbTable(queryParams.value);
  dbTableList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};
/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 查询多数据源名称 */
const getDataNameList = async () => {
  const res = await getDataNames();
  dataNameList.value = res.data;
};

/** 选中列赋值 */
const handleRowClick = (row) =>{
  row.dataName = queryParams.value.dataName;
  selectedRow.value = row
};

defineExpose({dbTableRef})

onMounted(() => {
  getDataNameList();
  queryParams.value.dataName = 'master';
  getList();
});
</script>
