<script lang="ts" setup>
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { EnergyStationConfigApi } from '#/api/energy/station-config';

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

import { Tag } from 'ant-design-vue';

import { ACTION_ICON, TableAction, useVbenVxeGrid } from '#/adapter/vxe-table';
import { getStationConfigPage } from '#/api/energy/station-config';
import { $t } from '#/locales';

import {
  COOPERATION_TYPE_OPTIONS,
  useGridColumns,
  useGridFormSchema,
} from './data';
import FormModal from './modules/form-modal.vue';

const [Form, formModalApi] = useVbenModal({
  connectedComponent: FormModal,
  destroyOnClose: true,
});

/** 刷新表格 */
function handleRefresh() {
  gridApi.query();
}

/** 创建站点配置 */
function handleCreate() {
  formModalApi.setData(null).open();
}

/** 编辑站点配置 */
function handleEdit(row: EnergyStationConfigApi.Config) {
  formModalApi.setData(row).open();
}

const [Grid, gridApi] = useVbenVxeGrid({
  formOptions: {
    collapsed: true,
    schema: useGridFormSchema(),
  },
  gridOptions: {
    columns: useGridColumns(),
    height: 'auto',
    keepSource: true,
    proxyConfig: {
      ajax: {
        query: async ({ page }, formValues) => {
          return await getStationConfigPage({
            pageNo: page.currentPage,
            pageSize: page.pageSize,
            ...formValues,
          });
        },
      },
    },
    rowConfig: {
      keyField: 'id',
      isHover: true,
    },
    toolbarConfig: {
      refresh: true,
      search: true,
    },
  } as VxeTableGridOptions<EnergyStationConfigApi.Config>,
});
</script>

<template>
  <Page auto-content-height>
    <Form @success="handleRefresh" />
    <Grid table-title="站点配置列表">
      <template #toolbar-tools>
        <TableAction
          :actions="[
            {
              label: $t('ui.actionTitle.create', ['站点配置']),
              type: 'primary',
              icon: ACTION_ICON.ADD,
              auth: ['energy:station-config:create'],
              onClick: handleCreate,
            },
          ]"
        />
      </template>
      <template #cooperationType="{ row }">
        <Tag
          v-if="row.cooperationType != null"
          :color="
            COOPERATION_TYPE_OPTIONS.find(
              (o) => o.value === row.cooperationType,
            )?.value === 1
              ? 'blue'
              : 'purple'
          "
        >
          {{
            COOPERATION_TYPE_OPTIONS.find(
              (o) => o.value === row.cooperationType,
            )?.label ?? '—'
          }}
        </Tag>
        <span v-else>—</span>
      </template>
      <template #autoDeduct="{ row }">
        <Tag :color="row.autoDeduct ? 'orange' : 'cyan'">
          {{ row.autoDeduct ? '审核即扣款' : '出账单后扣款' }}
        </Tag>
      </template>
      <template #autoMatch="{ row }">
        <Tag :color="row.autoMatch ? 'green' : 'default'">
          {{ row.autoMatch ? '开启' : '关闭' }}
        </Tag>
      </template>
      <template #actions="{ row }">
        <TableAction
          :actions="[
            {
              label: $t('common.edit'),
              type: 'link',
              icon: ACTION_ICON.EDIT,
              auth: ['energy:station-config:update'],
              onClick: handleEdit.bind(null, row),
            },
          ]"
        />
      </template>
    </Grid>
  </Page>
</template>
