<script setup lang="ts">
import type { VbenFormProps } from '@vben/common-ui';

import type { VxeGridProps } from '#/adapter/vxe-table';

import { onMounted, ref } from 'vue';

import { Page, useVbenDrawer } from '@vben/common-ui';

import { ElMessageBox } from 'element-plus';

import { useVbenVxeGrid } from '#/adapter/vxe-table';

import { tableColumns } from './employeeSettings-data';
import EmployeeSettingsDrawerComp from './employeeSettings-drawer.vue';
import { deleteEmployeeSettings, employeeSettingsList } from '#/api/system/infoEntry/employeeSettings/employeeSettings';

const formOptions: VbenFormProps = {
  commonConfig: {
    labelWidth: 80,
    componentProps: {
      allowClear: true,
    },
  },
  wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-4',
};

const gridOptions: VxeGridProps = {
  checkboxConfig: {
    highlight: true,
    reserve: true,
    trigger: 'default',
  },
  useSearchForm: false,
  columns: tableColumns,
  size: 'medium',
  height: 'auto',
  proxyConfig: {
    ajax: {
      query: async ({ page }, formValues = {}) => {
        // 真实API请求
        console.log('查询参数:', formValues, page);
        const response = await employeeSettingsList({
          ...formValues,
          pageNum: page.currentPage,
          pageSize: page.pageSize,
        });
        return { 
          items: response.rows || [], 
          total: response.total || 0 
        };
      },
    },
  },
  rowConfig: {
    keyField: 'id',
  },
  toolbarConfig: {
    custom: true,
    refresh: true,
    zoom: true,
  },
  id: 'system-infoEntry-employeeSettings-index',
};

const [BasicTable, basicTableApi] = useVbenVxeGrid({
  gridOptions,
});

const [EmployeeSettingsDrawer, drawerApi] = useVbenDrawer({
  connectedComponent: EmployeeSettingsDrawerComp,
});

// 新增员工评价项
function openDrawer() {
  drawerApi.setData({ isUpdate: false }).open();
}

// 编辑员工评价项
function handleEdit(row: any) {
  drawerApi.setData({ ...row, isUpdate: true }).open();
}

// 单个删除员工评价项
async function confirmEvent(row: any) {
  try {
    await deleteEmployeeSettings([row.id]);
    await basicTableApi.reload();
  } catch (error) {
    console.error('删除员工评价项失败:', error);
  }
}

// 批量删除员工评价项
function deleteHandle() {
  const checkRecords = basicTableApi.grid.getCheckboxRecords();
  const ids = checkRecords.map((item: any) => item.id);
  if (ids.length <= 0) {
    return;
  }

  ElMessageBox.confirm(`确认删除选中的${ids.length}条数据吗?`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(async () => {
    try {
      await deleteEmployeeSettings(ids);
      await basicTableApi.reload();
    } catch (error) {
      console.error('批量删除员工评价项失败:', error);
    }
  });
}
</script>

<template>
  <Page :auto-content-height="true">
    <BasicTable table-title="员工评价项设置">
      <template #toolbar-tools>
        <ElSpace>
          <ElButton
            type="danger"
            :disabled="
              !(basicTableApi?.grid?.getCheckboxRecords?.()?.length > 0)
            "
            @click="deleteHandle"
          >
            批量删除
          </ElButton>
          <ElButton
            type="primary"
            @click="openDrawer"
          >
            新增评价项
          </ElButton>
        </ElSpace>
      </template>
      <template #action="{ row }">
        <ElSpace>
          <ElButton
            size="small"
            type="primary"
            plain
            @click="handleEdit(row)"
          >
            编辑
          </ElButton>
          <ElPopconfirm title="确认删除" @confirm="confirmEvent(row)">
            <template #reference>
              <ElButton
                size="small"
                type="danger"
                plain
              >
                删除
              </ElButton>
            </template>
          </ElPopconfirm>
        </ElSpace>
      </template>
    </BasicTable>
    <EmployeeSettingsDrawer @reload="basicTableApi.reload()" />
  </Page>
</template>

<style scoped lang="scss">
:deep(.el-tooltip__trigger:focus) {
  outline: none;
}
</style>

