<!-- 算子程序管理 -->
<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
            >
            <el-button
              type="primary"
              :disabled="removeIds.length === 0"
              @click="handleDelete()"
              ><i-ep-delete />算子例程下载</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" />
        <el-table-column label="算子子类型" align="left" prop="oper_sub_type_name" />
        <el-table-column label="程序版本" width="180" align="left" prop="program_version" />
        <el-table-column label="程序适应硬件" width="180" align="left" prop="cmpt_hardware_type" />
        <el-table-column label="算子文件" width="180" align="left" prop="program_file_name" />
        <el-table-column label="创建时间" width="180" align="left" prop="create_time" />
        <el-table-column label="操作" fixed="right" width="220">
          <template #default="scope">
            <el-button
            text type="primary" size="small"
              @click="handleDetail(scope.row.program_id)"
              ><i-ep-view />查看</el-button
            >
            <el-button text type="primary" size="small" @click="handleDelete(scope.row.program_id)"
              ><i-ep-delete />删除</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>
  </div>
</template>

<script setup lang="ts">
import OperatorApi from '@/api/operator'

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,
  operation_id: null,
  oper_main_type: null,
  oper_sub_type: null,
  cmpt_hardware_type: null,
  operator_name: null,
});

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

/** 重置查询 */
function resetQuery() {
  queryFormRef.value.resetFields();
  queryParams.page_num = 1;
  queryParams.operation_id = null;
  queryParams.oper_main_type = null;
  queryParams.oper_sub_type = null;
  queryParams.cmpt_hardware_type = null;
  queryParams.operator_name = null;
  handleQuery();
}

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

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

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

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

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