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

import OrdersForm from './form.vue';

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

  columns: [
    { title: '订单号', field: 'ordersnumber' },
    { title: '订单关联用户id', field: 'ordersuserid' },
    { title: '订单关联产品id', field: 'ordersproductid' },
    { title: '产品名称', field: 'ordersproductname' },
    { title: '产品sn', field: 'ordersproductsn' },
    { title: '订单总金额', field: 'orderstotalprice' },
    { title: '订单优惠后金额', field: 'ordersdiscountprice' },
    { title: '优惠金额', field: 'ordersyhprice' },
    { title: '优惠卷id', field: 'orderscouponid' },
    { title: '订单状态', field: 'ordersorderstatus' },
    { title: '下单时间', field: 'ordersorderdate' },
    {
      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: OrdersForm,
});

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