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

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

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

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

import { PRICE_STATUS_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: EnergyStationPriceApi.Price) {
  formModalApi.setData(row).open();
}

/** 删除价格 */
async function handleDelete(row: EnergyStationPriceApi.Price) {
  const hideLoading = message.loading({
    content: $t('ui.actionMessage.deleting', [row.stationName]),
    duration: 0,
  });
  try {
    await deleteStationPrice(row.id!);
    message.success($t('ui.actionMessage.deleteSuccess', [row.stationName]));
    handleRefresh();
  } finally {
    hideLoading();
  }
}

const [Grid, gridApi] = useVbenVxeGrid({
  formOptions: {
    collapsed: true,
    schema: useGridFormSchema(),
  },
  gridOptions: {
    columns: useGridColumns(),
    height: 'auto',
    keepSource: true,
    proxyConfig: {
      ajax: {
        query: async ({ page }, formValues) => {
          return await getStationPricePage({
            pageNo: page.currentPage,
            pageSize: page.pageSize,
            ...formValues,
          });
        },
      },
    },
    rowConfig: {
      keyField: 'id',
      isHover: true,
    },
    toolbarConfig: {
      refresh: true,
      search: true,
    },
  } as VxeTableGridOptions<EnergyStationPriceApi.Price>,
});
</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-price:create'],
              onClick: handleCreate,
            },
          ]"
        />
      </template>
      <template #costPrice="{ row }">
        <span class="font-bold text-orange-600">
          {{ row.costPrice != null ? row.costPrice.toFixed(2) : '—' }}
        </span>
      </template>
      <template #customerPrice="{ row }">
        <span class="font-bold text-blue-600">
          {{ row.customerPrice != null ? row.customerPrice.toFixed(2) : '—' }}
        </span>
      </template>
      <template #expiryDate="{ row }">
        {{ row.expiryDate ?? '—' }}
      </template>
      <template #status="{ row }">
        <Tag
          :color="
            PRICE_STATUS_OPTIONS.find((o) => o.value === row.status)?.color ??
            'default'
          "
        >
          {{
            PRICE_STATUS_OPTIONS.find((o) => o.value === row.status)?.label ??
            '—'
          }}
        </Tag>
      </template>
      <template #actions="{ row }">
        <TableAction
          :actions="[
            {
              label: $t('common.edit'),
              type: 'link',
              icon: ACTION_ICON.EDIT,
              auth: ['energy:station-price:update'],
              onClick: handleEdit.bind(null, row),
            },
            {
              label: $t('common.delete'),
              type: 'link',
              danger: true,
              icon: ACTION_ICON.DELETE,
              auth: ['energy:station-price:delete'],
              popConfirm: {
                title: $t('ui.actionMessage.deleteConfirm', [row.stationName]),
                confirm: handleDelete.bind(null, row),
              },
            },
          ]"
        />
      </template>
    </Grid>
  </Page>
</template>
