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

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

import { ref } from 'vue';
import { useRoute, useRouter } from 'vue-router';

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

import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { getCompetitorSalesSurveyList } from '#/api/archive/operationsManagement/operationsManagement';

import {
  columns,
  mapApiToView,
  mapFormToApi,
  querySchema,
} from './config-data';

const showSearchForm = ref(true) as any;
const router = useRouter();
const route = useRoute();
const formOptions: VbenFormProps = {
  commonConfig: {
    labelWidth: 80,
    componentProps: {
      allowClear: true,
    },
  },
  schema: querySchema(),
  wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4',
  // 日期选择格式化
  // fieldMappingTime: [
  //   [
  //     'createTime',
  //     ['params[beginTime]', 'params[endTime]'],
  //     ['YYYY-MM-DD 00:00:00', 'YYYY-MM-DD 23:59:59'],
  //   ],
  // ],
};

// 列表中显示配置
const gridOptions: VxeGridProps = {
  checkboxConfig: {
    // 高亮
    highlight: true,
    // 翻页时保留选中状态
    reserve: true,
    // 点击行选中
    trigger: 'default',
  },
  columns,
  size: 'medium',
  height: 'auto',
  proxyConfig: {
    ajax: {
      query: async ({ page }, formValues = {}) => {
        const apiParams = mapFormToApi(formValues, page);
        apiParams.type = route.query.type || '';
        apiParams.directoryType = route.query.directoryType || '';
        console.log(apiParams);
        const response = await getCompetitorSalesSurveyList(apiParams);
        const items = mapApiToView(response?.rows || []);
        return {
          items,
          total: response?.total || 0,
        };
      },
    },
  },
  rowConfig: {
    keyField: 'id',
  },
  toolbarConfig: {
    custom: true,
    refresh: true,
    zoom: true,
  },
  id: 'competitive-sales-survey-index',
};
const [BasicTable] = useVbenVxeGrid({
  showSearchForm,
  formOptions,
  gridOptions,
});
const handleTaskClick = (taskId: number) => {
  router.push(`/schedule/detail/${taskId}`);
};
</script>
<template>
  <Page :auto-content-height="true">
    <BasicTable>
      <template #toolbar-tools>
        <el-button @click="() => (showSearchForm = !showSearchForm)">
          展开/折叠
        </el-button>
      </template>
      <template #action="{ row }">
        <el-button
          size="small"
          type="primary"
          plain
          @click="() => handleTaskClick(row.taskId)"
        >
          查看
        </el-button>
      </template>
    </BasicTable>
  </Page>
</template>
<style scoped lang="scss">
:deep(.el-tooltip__trigger:focus) {
  outline: none;
}
</style>
