<script lang="ts" setup>
import type {
  OnActionClickParams,
  VxeTableGridOptions,
} from '#/adapter/vxe-table';
import type { ComplaintApi } from '#/api/complaint';

import { ref } from 'vue';

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

import { useVbenVxeGrid } from '#/adapter/vxe-table';
import {
  getComplaintDetail,
  getComplaintList,
  updateComplaintRemark,
  updateComplaintStatus,
} from '#/api/complaint';

import { useColumns, useGridFormSchema } from './data';
import DetailModal from './modules/detail-modal.vue';
import StatusModal from './modules/status-modal.vue';
import RemarkModal from './modules/remark-modal.vue';

const [Grid, gridApi] = useVbenVxeGrid({
  formOptions: {
    fieldMappingTime: [
      ['create_time', ['create_time_start', 'create_time_end']],
      ['handle_time', ['handle_time_start', 'handle_time_end']],
    ],
    schema: useGridFormSchema(),
    submitOnChange: true,
  },
  gridOptions: {
    columns: useColumns(onActionClick),
    height: 'auto',
    keepSource: true,
    proxyConfig: {
      ajax: {
        query: async ({ page }, formValues) => {
          return await getComplaintList({
            page: page.currentPage,
            pageSize: page.pageSize,
            ...formValues,
          });
        },
      },
    },
    rowConfig: {
      keyField: 'id',
    },
    toolbarConfig: {
      custom: true,
      export: true,
      refresh: { code: 'query' },
      search: true,
      zoom: true,
    },
  } as VxeTableGridOptions<ComplaintApi.Complaint>,
});

const detailData = ref<ComplaintApi.ComplaintDetail | null>(null);

const [DetailDrawer, detailDrawerApi] = useVbenDrawer({
  destroyOnClose: true,
});

const [StatusModalInstance, statusModalApi] = useVbenModal({
  connectedComponent: StatusModal,
  destroyOnClose: true,
});

const [RemarkModalInstance, remarkModalApi] = useVbenModal({
  connectedComponent: RemarkModal,
  destroyOnClose: true,
});

function onActionClick(e: OnActionClickParams<ComplaintApi.Complaint>) {
  switch (e.code) {
    case 'detail': {
      onViewDetail(e.row);
      break;
    }
    case 'update_status': {
      onUpdateStatus(e.row);
      break;
    }
    case 'update_remark': {
      onUpdateRemark(e.row);
      break;
    }
  }
}

async function onViewDetail(row: ComplaintApi.Complaint) {
  const detail = await getComplaintDetail(row.id);
  detailData.value = detail;
  detailDrawerApi.open();
}

function onUpdateStatus(row: ComplaintApi.Complaint) {
  statusModalApi.setData(row).open();
}

function onUpdateRemark(row: ComplaintApi.Complaint) {
  remarkModalApi.setData(row).open();
}

function onRefresh() {
  gridApi.query();
}

function onStatusSuccess() {
  onRefresh();
}

function onRemarkSuccess() {
  onRefresh();
}
</script>

<template>
  <Page auto-content-height>
    <DetailDrawer>
      <DetailModal :data="detailData || undefined" />
    </DetailDrawer>
    <StatusModalInstance @success="onStatusSuccess" />
    <RemarkModalInstance @success="onRemarkSuccess" />
    <Grid table-title="投诉列表" />
  </Page>
</template>
