<template>
  <div class="case-templates-statistics-container">
      <div class="toolbar">
        <el-space>

          <el-input v-model="queryParams.searchKey" placeholder="请输入名称搜索" clearable />
          <el-button @click="refresh">查询</el-button>
          
          <el-button type="primary" @click="newTemplate">新建</el-button>
        </el-space>
      </div>
      <div class="statistics-data-container">
      <el-table
        :data="statisticsData"
        node-key="id"
      >
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="count" label="数量" />
        <el-table-column  label="操作" >
          <template #default="scope">
            <el-button type="primary"  @click="editStatistics(scope.row)">编辑</el-button>
            <el-button type="danger"  @click="deleteStatistics(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
            </div>
</div>
    <EditStatistics
      v-model:dialogVisible="dialogVisible"
      :title="currentNodeData ? '编辑病历文件信息' : '病历文件基本信息'"
      :formData="formData"
      :currentNodeData="currentNodeData"
      @submitOk="handleSubmitOk"
    />
  
</template>
<script setup>
// 从Vue导入所需的API
import { ref, reactive, onMounted, defineAsyncComponent, nextTick, watch } from 'vue';
import { ElMessageBox, ElMessage,  } from 'element-plus';
import { getPageList, init, } from './api';
import EditStatistics from './components/editStatistics.vue';
const queryParams = ref({
  pageNo: 1,
  pageSize: 10,
  searchKey: ''
});
const currentNodeData = ref(null); // 存储当前选中的节点数据
// 弹窗可见性
const dialogVisible = ref(false);

// 表单数据
const formData = reactive({
  id:''
});

const initFormData = () => {
  formData.id = '';
};

const handleSubmitOk = () => {
  dialogVisible.value = false;
  refresh();
};

// 组件初始化时加载基础数据
onMounted(() => {
  getInit(); // 加载模板初始化数据（一级菜单、使用范围等）
  handleOrgClear(); // 初始化模板树（加载所有科室模板）
});


// 统计数据
const statisticsData = ref([]);



/** 过滤节点（科室TreeSelect搜索） */
const filterNode = (value, data) => {
  if (!value) return true;
  return data?.name.toLowerCase().includes(value.toLowerCase()); // 不区分大小写搜索
};



/** 加载模板初始化数据（一级菜单、使用范围枚举） */
const getInit = async () => {
  try {
    const response = await init();
    templateDataInit.value = response.data || {};
    console.log('模板初始化数据:', templateDataInit.value);
  } catch (error) {
    handleApiError('初始化', '初始化接口异常')(error);
  }
};




/** 编辑模板（打开弹窗并回显数据） */
const editStatistics = async () => {
};

/** 打开编辑弹窗，回显选中的模板数据并正确初始化科室选择 */
const openEditDialog = async (nodeData) => {
 
};

const getList = async () => {
  try {
    const response = await getTreeList();
    statisticsData.value = response?.data || [];
  } catch (error) {
    
  }
};
const refresh = () => {
  getList();
};

// 新建模版统计
const newTemplate = () => {
  dialogVisible.value = true;
};
// 删除统计
const deleteStatistics = async () => {
};
</script>
<style scoped lang="scss">
.case-templates-statistics-container {
  display: flex;
  flex-direction: column;
  height: 91vh;
  .toolbar {
    height: 48px;
    display: flex;
    padding: 0 8px;
    align-items: center;
  }
.statistics-data-container{
  height: calc(100% - 48px);
}
}
</style>
