<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 { getSubsidyApplicationsListApi } from '#/api/subsidyapplications';
import { $t } from '#/locales';

import SubsidyApplicationsForm from './form.vue';

const formOptions: VbenFormProps = {
  // 默认展开
  collapsed: true,
  // 控制表单是否显示折叠按钮
  showCollapseButton: true,
  // 按下回车时是否提交表单
  submitOnEnter: true,
  schema: [
    {
      component: 'Input',
      fieldName: 'subsidyapplicationsmerchantid',
      label: '渠道商ID',
      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 getSubsidyApplicationsListApi({
          pageindex: page.currentPage,
          rows: page.pageSize,
          ...formValues,
        });
      },
    },
  },

  columns: [
    { title: '补贴申请ID', field: 'subsidyapplicationsid', sortable: true },
    {
      title: '渠道商ID',
      field: 'subsidyapplicationsmerchantid',
      sortable: true,
    },
    {
      title: '补贴金额',
      field: 'subsidyapplicationssubsidyamount',
      sortable: true,
    },
    {
      title: '审批状态',
      field: 'subsidyapplicationsapprovalstatus',
      sortable: true,
    },
    {
      title: '申请日期',
      field: 'subsidyapplicationsapplydate',
      sortable: true,
    },
    {
      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: SubsidyApplicationsForm,
});

/* 创建 */
// 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>
