<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 {
  queryFormSchema,
  tableColumns,
  formActions,
  mockData,
} from './taskTemplate-data';
import taskTemplateDrawerComp from './taskTemplate-drawer.vue';
import {
  querybaseApilist,
  addbaseApi,
} from '#/api/system/tasksettings/tasktemplate';

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

const gridOptions: VxeGridProps = {
  checkboxConfig: {
    highlight: true,
    reserve: true,
    trigger: 'default',
  },
  columns: tableColumns,
  size: 'medium',
  height: 'auto',
  proxyConfig: {
    ajax: {
      query: async ({ page }, formValues = {}) => {
        const res = await querybaseApilist({
          ...formValues,
          pageNum: page.currentPage,
          pageSize: page.pageSize,
        });

        return {
          items: res?.rows,
          total: res?.total,
        };
      },
    },
  },
  rowConfig: {
    keyField: 'id',
  },
  toolbarConfig: {
    custom: true,
    refresh: true,
    zoom: true,
  },
  id: 'system-checklis-taskTemplate-index',
};

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

const [taskTemplateDrawer, drawerApi] = useVbenDrawer({
  connectedComponent: taskTemplateDrawerComp,
  // 设置抽屉宽度为700px，使用class属性覆盖默认宽度
  class: 'w-[850px]',
});

// 新增任务
function openDrawer() {
  drawerApi.setData({ isUpdate: false }).open();
}

// 查看任务
function handleView(row: any) {
  drawerApi.setData({ ...row, isView: true }).open();
}

// 编辑任务
function handleEdit(row: any) {
  drawerApi.setData({ ...row, isUpdate: true }).open();
}

// 复制任务
function handleCopy(row: any) {
  drawerApi.setData({ ...row, isCopy: true, id: '' }).open();
}

// 分发任务
function handleDistribute(row: any) {
  console.log('分发任务:', row.id);
  ElMessageBox.alert('任务分发功能待实现', '提示');
}

// 导出任务模板
function handleExport() {
  console.log('导出任务模板');
  ElMessageBox.alert('任务模板导出功能待实现', '提示');
}

const handleConfirm = async (data: any) => {
  drawerApi.drawerLoading(true);
  try {
    await addbaseApi(data);

  } catch (error) {
    console.error('保存任务模板失败:', error);
  } finally {
    drawerApi.drawerLoading(false);
  }
};
</script>

<template>
  <Page :auto-content-height="true">
    <BasicTable table-title="任务模板列表">
      <template #toolbar-tools>
        <ElSpace>
          <ElButton type="primary" @click="openDrawer"> 新增任务 </ElButton>
          <ElButton type="primary" ghost @click="handleExport"> 导出 </ElButton>
        </ElSpace>
      </template>
      <template #status="{ row }">
        <ElTag
          :type="
            row.status === '启用' || row.status === 0 ? 'success' : 'danger'
          "
        >
          {{
            row.status === 0 ? '启用' : row.status === 1 ? '关闭' : row.status
          }}
        </ElTag>
      </template>
      <template #timedDistribution="{ row }">
        <ElTag>
          {{
            row.timedDistribution === 0
              ? '否'
              : row.timedDistribution === 1
                ? '是'
                : row.timedDistribution
          }}
        </ElTag>
      </template>
      <template #action="{ row }">
        <ElSpace>
          <ElButton size="small" type="primary" plain @click="handleView(row)">
            查看
          </ElButton>
          <ElButton size="small" type="primary" plain @click="handleEdit(row)">
            编辑
          </ElButton>
          <ElButton size="small" type="primary" plain @click="handleCopy(row)">
            复制
          </ElButton>
          <ElButton
            size="small"
            type="primary"
            plain
            @click="handleDistribute(row)"
          >
            分发
          </ElButton>
        </ElSpace>
      </template>
    </BasicTable>
    <taskTemplateDrawer @Confirm="handleConfirm" />
  </Page>
</template>

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