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

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



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

import { ElMessageBox, ElSpace } from 'element-plus';

import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { getEmployeePerformanceList } from '#/api/archive/personnelTraining/personnelTraining';

import { queryFormSchema, tableColumns, mapFormToApi } from './employeePerfor-data';
import EmployeePerforDrawerComp from './employeePerfor-drawer.vue';

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

const gridOptions: VxeGridProps = {
  checkboxConfig: {
    highlight: true,
    reserve: true,
    trigger: 'default',
  },
  columns: tableColumns,
  size: 'medium',
  height: 'auto',
  proxyConfig: {
    ajax: {
      query: async ({ page }, formValues = {}) => {
        // 映射表单字段到 API 字段
        const apiParams = mapFormToApi(formValues, page);

        // 调用API
        const response = await getEmployeePerformanceList(apiParams);

        // 映射 API 响应数据到页面显示字段
        const items = response.rows;

        return {
          items,
          total: response.total,
        };
      },
    },
  },
  rowConfig: {
    keyField: 'id',
  },
  toolbarConfig: {
    custom: true,
    refresh: true,
    zoom: true,
  },
  id: 'archive-personnelTraining-employeePerfor-index',
};

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

const [EmployeePerforDrawer, drawerApi] = useVbenDrawer({
  connectedComponent: EmployeePerforDrawerComp,
});



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

// 单个删除员工绩效
async function confirmEvent(row: any) {
  // 模拟删除操作
  console.log('删除员工绩效:', row.id);
  await basicTableApi.reload();
}

// 批量删除员工绩效
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 () => {
    // 模拟批量删除操作
    console.log('批量删除员工绩效:', ids);
    await basicTableApi.reload();
  });
}

// 考核说明提示框
function handleAssessmentInstructions() {
  ElMessageBox.alert(
    '1. 顾客服务：服务质量与速度/服务流程/服务态度等<br/>' +
    '2. 安全考核：前厅站位/指引车辆/安全例会学习/安全事故等<br/>' +
    '3. 日常综合表现：出勤/执行力/工作态度<br/>' +
    '4. 业绩表现：促销/拉新等<br/>' +
    '5. 自主评分：获得顾客表扬/拾金不昧/高峰主动协助加班/正确处理事件，避免事故的发生/外部参加会议等',
    '考核说明',
    { dangerouslyUseHTMLString: true }
  );
}
</script>

<template>
  <Page :auto-content-height="true">
    <BasicTable table-title="员工绩效">
      <template #toolbar-tools>
        <ElSpace>
          <ElButton
            type="info"
            @click="handleAssessmentInstructions"
          >
            考核说明
          </ElButton>
          <!-- <ElButton
            type="danger"
            :disabled="
              !(basicTableApi?.grid?.getCheckboxRecords?.()?.length > 0)
            "
            @click="deleteHandle"
          >
            批量删除
          </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>
    <EmployeePerforDrawer @reload="basicTableApi.reload()" />
  </Page>
</template>

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