<!-- 算子管理 -->
<template>
  <div class="app-container">
    <el-card shadow="never" class="table-container">
      <template #header>
        <div class="flex justify-between">
          <div>
            <el-button type="primary" @click="handleAdd"
              ><i-ep-plus />添加算子</el-button
            >
          </div>
          <div>
            <el-form ref="queryFormRef" :model="queryParams" :inline="true">
              <el-form-item label="算子名称" prop="operator_name">
                <el-input
                  v-model="queryParams.operator_name"
                  placeholder="请输入算子名称"
                  clearable
                  style="width: 200px"
                  @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>
        </div>
      </template>

      <el-table
        v-loading="loading"
        :data="pageData"
        stripe
        @selection-change="handleSelectionChange"
      >
        <el-table-column label="算子名称" align="left" prop="operator_name" width="300" />
        <el-table-column label="算子主类型" align="left" prop="oper_main_type_name" width="180"/>
        <el-table-column label="算子子类型" align="left" prop="oper_sub_type_name" width="180" />
        <el-table-column label="算子说明" align="left" prop="operator_desc" />
        <el-table-column label="创建时间" width="180" align="left" prop="create_time" />
        <el-table-column label="操作" fixed="right" align="center" width="280">
          <template #default="scope">
            <el-button
              text
              type="primary"
              size="small"
              @click="handleDetail(scope.row.operator_id)"
              ><i-ep-view />查看</el-button
            >
            <el-button text type="primary" size="small" @click="handleDelete(scope.row.operator_id)"
              ><i-ep-delete />删除</el-button
            >
            <el-button text type="primary" size="small" @click="handlePushParams(scope.row)"
              ><i-ep-plus />添加算子程序</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.page_num"
        v-model:limit="queryParams.page_size"
        @pagination="handleQuery"
      />
    </el-card>
    <paramDialog ref="paramDialogRef"></paramDialog>
  </div>
</template>

<script setup lang="ts">
import OperatorApi from '@/api/operator'
import paramDialog from "../calculateParam/dialog.vue";

const router = useRouter();

const loading = ref(false); //  加载状态
const total = ref(0); // 数据总数
const pageData = ref([]); // 分页数据
const removeIds = ref([]); // 删除ID集合 用于批量删除
const queryFormRef = ref(ElForm); // 查询表单
const queryParams = reactive({
  page_num: 1,
  page_size: 10,
  order_type:null,
  order_by:null,
  operator_name:null,
  oper_main_type:null
});
const paramDialogRef = ref(""); // 算子程序子组件

/** 查询 */
function handleQuery() {
  loading.value = true;
  OperatorApi.list(queryParams)
    .then((res) => {
      pageData.value = res.data.data.operator_list;
      total.value = res.data.data.total;
    })
    .finally(() => {
      loading.value = false;
  });
}

/** 重置查询 */
function resetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageNum = 1;
  queryParams.order_type = null,
  queryParams.order_by = null,
  queryParams.operator_name = null,
  queryParams.oper_main_type = null
  handleQuery();
}

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

/** 删除数据 */
function handleDelete(id?: number) {
  ElMessageBox.confirm("确认删除算子?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(function () {
    OperatorApi.deleteOperator(id).then((res) => {
      ElMessage.success("删除成功");
      resetQuery();
    });
  });
}

/** 添加算子程序 */
function handlePushParams(data){
  paramDialogRef.value.show(data);
}

/** 查看算子 */
function handleDetail(operator_id?: number) {
  router.push({path:"/operatorLibrary/calculateDetail",query:{id:operator_id}})
}

/** 新增算子 */
function handleAdd() {
  router.push({ path: "/operatorLibrary/calculateEdit" });
}

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