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

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

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

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

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' },
    { title: '优惠券代码', field: 'coupon2code' },
    { title: '申请时间', field: 'coupon2adddatetime' },
    { title: '审核时间', field: 'coupon2reviewdatetime' },
    { title: '申请人ID', field: 'coupon2userid' },
    { title: '优惠券状态', field: 'coupon2sype' },
    { title: '使用状态', field: 'coupon2isused' },
    { title: '可用产品', field: 'coupon2productids' },
    { title: '关联主券ID', field: 'coupon2coupon1id' },
    {
      title: '操作',
      field: 'action',
      fixed: 'right',
      slots: { default: 'action' },
      width: 140,
    },
  ],
};

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

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

/* 创建 */
function handleCreate() {
  modalApi.setData({ formType: 'create' }).open();
}

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

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

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-button
          round
          @click="() => handleDetail(row)"
          :icon="MdiDetail"
          class="!p-2"
        />
        <el-button
          round
          @click="() => handleEdit(row)"
          :icon="MdiEdit"
          class="!p-2"
        />
      </template>
    </Grid>
    <Modal @finish="handleFinish" />
  </Page>
</template>
