<!-- 用户管理 -->
<template>
  <div class="app-container">
    <div class="search-container" style="position: relative;">
      <el-button type="primary" @click="doAddByModel"
        style="position: absolute;left:10px;"><i-ep-plus />新建互联</el-button>
      <el-form ref="queryFormRef" :model="queryParams" :inline="true" style="flex-grow: 1;text-align: right;">
        <el-form-item label="" prop="connection_name">
          <el-input v-model="queryParams.connection_name" placeholder="请输入互联名将，标签，模型名称" clearable style="width: 250px"
            @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery"><i-ep-search />搜索</el-button>
          <el-button @click="resetQuery">
            <i-ep-refresh />
            重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">

      <el-table v-loading="loading" :data="info.data" stripe @selection-change="handleSelectionChange">
        <el-table-column label="模型名称" align="left" prop="model_name" />
        <el-table-column label="网络名称" align="left" prop="modl_net_type"  />
        <el-table-column label="模型类型" align="left" prop="modl_net_type" v-if="false">
          <template #default="{ row }">{{ row.modl_main_type_name }}/{{ row.modl_sub_type_name }}</template>
        </el-table-column>
        <el-table-column label="版本" align="left" prop="connection_version" />
        <el-table-column label="互联名称" align="left" prop="connection_name" />
        <el-table-column label="互联说明" align="left" prop="connection_desc" />
        <el-table-column label="互联创建时间" width="180" align="left" prop="create_time" />
        <el-table-column label="创建用户" width="100" align="left" prop="user_name" />

        <el-table-column label="操作" fixed="right" align="center" width="320">
          <template #default="scope">
            <el-button text type="primary" size="small" @click="doShowDetail(scope.row)"><i-ep-edit />查看</el-button>
            <el-button text type="primary" size="small" style="margin:0px;"
              @click="doEdit(scope.row)"><i-ep-link />修改互联</el-button>
            <el-button text type="primary" size="small" @click="doAdd(scope.row)"
              style="margin:0px;"><i-ep-plus />新建互联</el-button>
            <el-button text type="primary" size="small" @click="handleDelete(scope.row)"
              style="margin:0px;"><i-ep-delete />删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-card class="card-footer">
      <pagination v-if="info.total > 0" v-model:total="info.total" v-model:page="queryParams.page_num"
        v-model:limit="queryParams.page_size" @pagination="handleQuery" />
    </el-card>
    <selectModelDlg ref="selModelDlg" @success="doSelectSuccess"></selectModelDlg>
  </div>
</template>

<script setup lang="ts">

import { UserQuery } from "@/api/user/model";
import ConnApi from '@/api/connection'
import selectModelDlg from './selectModelDlg.vue'

const queryFormRef = ref(ElForm); // 查询表单
const router = useRouter();
const selModelDlg = ref();
const loading = ref(false); //  加载状态
const removeIds = ref([]); // 删除用户ID集合 用于批量删除
const queryParams = reactive<any>({
  page_num: 1,
  page_size: 10,
  connection_name: ''
});
const dateTimeRange = ref("");
const total = ref(100); // 数据总数
const info = reactive({
  total: 0,
  data: []
})
watch(dateTimeRange, (newVal) => {
  if (newVal) {
    queryParams.startTime = newVal[0];
    queryParams.endTime = newVal[1];
  }
});

const doAddByModel = () => {
  selModelDlg.value.showDialog();
}
/** 查询 */
function handleQuery() {
  loading.value = true;
  ConnApi.list(queryParams).then(d => {
    loading.value = false;
    info.total = d.data?.data?.total || 0;
    info.data = d.data?.data?.connection_list || [];
  });

}

function doUploadModel() {
  router.push({ path: "/modelMgr/uploadModel" });
}

/** 重置查询 */
function resetQuery() {
  queryFormRef.value.resetFields();
  dateTimeRange.value = "";
  queryParams.pageNum = 1;
  queryParams.deptId = undefined;
  queryParams.startTime = undefined;
  queryParams.endTime = undefined;
  handleQuery();
}

/** 行选中 */
function handleSelectionChange(selection: any) {
  removeIds.value = selection.map((item: any) => item.id);
}

/** 删除数据 */
function handleDelete(row: { [key: string]: any }) {
  ElMessageBox.confirm("确认删除互联?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(function () {
    ConnApi.deleteConnect(row.connection_id).then(d => {
      if (d.data.code == 0) {
        ElMessage.success("删除互联成功");
        handleQuery();
      }
    });
  });
}

const doShowDetail = (row: any) => {
  router.push({ path: "/connection/detail", query: { id: row.connection_id } })
}
const doSelectSuccess = (row: any) => {
  router.push({ path: "/connection/edit", query: { id: row.model_id, type: 'addByModel' } })
}
const doEdit = (row: any) => {
  router.push({ path: "/connection/edit", query: { id: row.connection_id, type: 'edit' } })
}

const doAdd = (row: any) => {
  router.push({ path: "/connection/edit", query: { id: row.connection_id, type: 'addByConn' } })
}


onMounted(() => {
  handleQuery();
});
</script>


<style scoped lang='scss'>
.search-container {
  display: flex;
}

.card-footer {
  position: fixed;
  width: calc(100% - 215px);
  bottom: 0px;

  :deep(.el-card__body) {
    padding: 0px;

    .el-pagination {
      justify-content: end;
    }
  }
}
</style>
