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

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

import { h, ref } from 'vue';

import { Page, useVbenModal } from '@vben/common-ui';
import { MdiCheckboxMultipleMarked, MdiDetail, MdiEdit } from '@vben/icons';

import { ElTag } from 'element-plus';

import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { getCoupon2ListApi } from '#/api/coupon/coupon2';
import { $t } from '#/locales';

import AuditForm from './audit-form.vue';
import Coupon1Form from './form.vue';

const formOptions: VbenFormProps = {
  // 默认展开
  collapsed: true,
  // 控制表单是否显示折叠按钮
  showCollapseButton: true,
  // 按下回车时是否提交表单
  submitOnEnter: true,
  schema: [
    {
      component: 'Input',
      fieldName: 'couponmc',
      label: '优惠券名称',
      componentProps: {
        placeholder: $t('ui.placeholder.input'),
        allowClear: true,
      },
    },
  ],
  wrapperClass: 'grid-cols-1 md:grid-cols-3 lg:grid-cols-5',
};

const gridOptions: VxeGridProps<any> = {
  toolbarConfig: {
    custom: true,
    export: true,
    // import: true,
    refresh: true,
    zoom: true,
  },
  height: 'auto',
  exportConfig: {},
  pagerConfig: {},
  rowConfig: {
    isHover: true,
  },
  stripe: true,

  proxyConfig: {
    response: {
      result: 'Data',
      total: 'Total',
    },
    ajax: {
      query: async ({ page }, formValues) => {
        return await getCoupon2ListApi({
          pageindex: page.currentPage,
          rows: page.pageSize,
          ...formValues,
        });
      },
    },
  },

  columns: [
    { title: '优惠券ID', field: 'coupon2sid', sortable: true },
    { title: '优惠券代码', field: 'coupon2code', sortable: true },
    { title: '申请时间', field: 'coupon2adddatetime', sortable: true },
    { title: '审核时间', field: 'coupon2reviewdatetime', sortable: true },
    { title: '申请人ID', field: 'coupon2userid', sortable: true },
    {
      title: '优惠券状态',
      field: 'coupon2sype',
      sortable: true,
      slots: {
        default: ({ row }) => {
          const type = row.coupon2sype as 0 | 1 | 2;
          const typeMap = {
            0: { text: '申请中', type: 'info' },
            1: { text: '审核通过', type: 'success' },
            2: { text: '审核失败', type: 'danger' },
          } as const;
          return h(
            ElTag,
            { type: typeMap[type]?.type },
            () => typeMap[type]?.text,
          );
        },
      },
    },
    {
      title: '使用状态',
      field: 'coupon2isused',
      sortable: true,
      slots: {
        default: ({ row }) => {
          const type = row.coupon2isused as 0 | 1;
          const typeMap = {
            0: { text: '未使用', type: 'info' },
            1: { text: '已使用', type: 'success' },
          } as const;
          return h(
            ElTag,
            { type: typeMap[type]?.type },
            () => typeMap[type]?.text,
          );
        },
      },
    },
    { title: '可用产品', field: 'coupon2productids', sortable: true },
    { title: '关联主券ID', field: 'coupon2coupon1id', sortable: true },
    {
      title: '操作',
      field: 'action',
      fixed: 'right',
      slots: { default: 'action' },
      width: 160,
    },
  ],
};

const gridEvents: VxeGridListeners<any> = {
  cellDblclick: ({ row }) => {
    handleDetail(row);
  },
};

const [Grid, gridApi] = useVbenVxeGrid({
  gridEvents,
  gridOptions,
  formOptions,
});

const [Modal, modalApi] = useVbenModal({
  fullscreenButton: false,
  closeOnClickModal: false,
  closeOnPressEscape: false,
  connectedComponent: Coupon1Form,
});

// Add ref for audit-form.vue
const auditFormRef = ref<any>(null);

/* 编辑 */
function handleEdit(row: any) {
  modalApi.setState({ showCancelButton: true });
  modalApi.setData({ formType: 'edit', row }).open();
}

/* 详情 */
function handleDetail(row: any) {
  modalApi.setState({ showCancelButton: false });
  modalApi.setData({ formType: 'detail', row }).open();
}

/* 审核 */
function handleAudit(row: any) {
  auditFormRef.value?.openAuditModal(row);
}

function handleFinish() {
  gridApi.reload();
}
</script>

<template>
  <Page auto-content-height>
    <Grid table-title="优惠券列表">
      <!-- <template #toolbar-tools>
        <el-button type="primary" @click="handleCreate"> 新增 </el-button>
      </template> -->
      <template #action="{ row }">
        <el-tooltip
          class="box-item"
          effect="dark"
          content="详情"
          placement="top"
        >
          <el-button
            round
            @click="() => handleDetail(row)"
            :icon="MdiDetail"
            class="!p-2"
          />
        </el-tooltip>
        <el-tooltip
          class="box-item"
          effect="dark"
          content="编辑"
          placement="top"
        >
          <el-button
            round
            @click="() => handleEdit(row)"
            :icon="MdiEdit"
            class="!p-2"
          />
        </el-tooltip>
        <el-tooltip
          v-if="row.coupon2sype === 0"
          class="box-item"
          effect="dark"
          content="审核"
          placement="top"
        >
          <el-button
            round
            @click="() => handleAudit(row)"
            :icon="MdiCheckboxMultipleMarked"
            class="!p-2"
          />
        </el-tooltip>
      </template>
    </Grid>
    <Modal @finish="handleFinish" />
    <AuditForm ref="auditFormRef" @finish="handleFinish" />
  </Page>
</template>
