<template>
  <div class="modern-page-container">
    <div class="page-wrapper">
      <!-- 内容卡片 -->
      <el-card class="content-card" shadow="never">
        <template #header>
          <div class="card-header">
            <span class="card-title">
              <el-icon class="title-icon"><Document /></el-icon>
              采购品目管理
            </span>
            <div class="header-actions">
              <el-button 
                icon="FolderAdd" 
                type="primary" 
                @click="formDialogRef.openDialog('add')">
                新增
              </el-button>
              <right-toolbar class="ml10" />
            </div>
          </div>
        </template>

        <!-- 树形表格 -->
        <el-table 
          ref="tableRef"
          :data="state.dataList" 
          v-loading="state.loading" 
          stripe
          :cell-style="tableStyle.cellStyle" 
          :header-cell-style="tableStyle.headerCellStyle"
          class="modern-table"
          row-key="code"
          :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
          <el-table-column type="index" label="序号" width="70" align="center">
            <template #header>
              <el-icon><List /></el-icon>
            </template>
            <template #default="{ $index, row }">
              {{ getRowIndex($index, row) }}
            </template>
          </el-table-column>
          <el-table-column prop="code" label="品目编码" min-width="150" show-overflow-tooltip>
            <template #header>
              <el-icon><DocumentCopy /></el-icon>
              <span style="margin-left: 4px">品目编码</span>
            </template>
          </el-table-column>
          <el-table-column prop="name" label="品目名称" min-width="200" show-overflow-tooltip>
            <template #header>
              <el-icon><Document /></el-icon>
              <span style="margin-left: 4px">品目名称</span>
            </template>
          </el-table-column>
          <el-table-column prop="remark" label="备注" min-width="200" show-overflow-tooltip>
            <template #header>
              <el-icon><EditPen /></el-icon>
              <span style="margin-left: 4px">备注</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" fixed="right" width="150">
            <template #default="scope">
              <el-button 
                icon="Edit" 
                link 
                type="primary" 
                @click="formDialogRef.openDialog('edit', scope.row)">
                编辑
              </el-button>
              <el-button 
                icon="Delete" 
                link 
                type="danger" 
                @click="handleDelete(scope.row)">
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>

    <!-- 编辑、新增表单对话框 -->
    <FormDialog ref="formDialogRef" @refresh="getDataList" />
  </div>
</template>

<script setup lang="ts" name="PurchasingCategory">
import { ref, reactive, defineAsyncComponent } from 'vue'
import { BasicTableProps, useTable } from "/@/hooks/table";
import { getTree, delObj } from "/@/api/finance/purchasingcategory";
import { useMessage, useMessageBox } from "/@/hooks/message";
import { List, Document, DocumentCopy, EditPen } from '@element-plus/icons-vue'

// 引入组件
const FormDialog = defineAsyncComponent(() => import('./form.vue'));

// 定义变量内容
const tableRef = ref()
const formDialogRef = ref()

/**
 * 查询树形数据方法
 * @param params - 查询参数
 * @returns Promise<any>
 */
const queryTree = (params?: any) => {
  return getTree(params);
};

/**
 * 定义响应式表格数据
 */
const state: BasicTableProps = reactive<BasicTableProps>({
  pageList: queryTree,
  queryForm: {},
  isPage: false, // 树形表格不分页
});

/**
 * 使用 useTable 定义表格相关操作
 */
const { getDataList, tableStyle } = useTable(state);

/**
 * 计算行序号（考虑树形结构）
 */
const getRowIndex = (index: number, row: any) => {
  // 对于树形表格，序号需要根据实际显示的行来计算
  // 这里简化处理，直接返回 index + 1
  return index + 1;
};

/**
 * 删除当前行
 * @param row - 当前行数据
 */
const handleDelete = async (row: any) => {
  try {
    await useMessageBox().confirm('确定要删除该记录吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    });
  } catch {
    return;
  }

  try {
    // 使用 id 或 code 作为删除参数（根据后端接口决定）
    const deleteId = row.id || row.code;
    await delObj(deleteId);
    useMessage().success('删除成功');
    getDataList();
  } catch (err: any) {
    useMessage().error(err.msg || '删除失败');
  }
};
</script>

<style scoped lang="scss">
@import '/@/assets/styles/modern-page.scss';
</style>

