<script setup lang="ts">

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

import { Space } from 'ant-design-vue';

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

import {
  feedbacksExport,
  feedbacksList,
} from '#/api/property/customerService/feedbacks';
import type { FeedbacksForm } from '#/api/property/customerService/feedbacks/model';
import { commonDownloadExcel } from '#/utils/file/download';
import feedbacksModal from './feedbacks-modal.vue';
import feedbacksDetail from './feedbacks-detail.vue';
import workOrdersDetail from '../../businessManagement/workOrders/work-orders-detail.vue'
import { columns, querySchema } from './data';

const formOptions: VbenFormProps = {
  commonConfig: {
    labelWidth: 80,
    componentProps: {
      allowClear: true,
    },
  },
  schema: querySchema(),
  wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4',
};

const gridOptions: VxeGridProps = {
  checkboxConfig: {
    // 高亮
    highlight: true,
    // 翻页时保留选中状态
    reserve: true,
    // 点击行选中
    // trigger: 'row',
  },
  columns,
  height: 'auto',
  keepSource: true,
  pagerConfig: {},
  proxyConfig: {
    ajax: {
      query: async ({ page }, formValues = {}) => {
        return await feedbacksList({
          pageNum: page.currentPage,
          pageSize: page.pageSize,
          ...formValues,
        });
      },
    },
  },
  rowConfig: {
    keyField: 'id',
  },
  // 表格全局唯一表示 保存列配置需要用到
  id: 'property-feedbacks-index'
};

const [BasicTable, tableApi] = useVbenVxeGrid({
  formOptions,
  gridOptions,
});

const [FeedbacksModal, modalApi] = useVbenModal({
  connectedComponent: feedbacksModal,
});

const [WorkOrdersDetail, orderDetailApi] = useVbenModal({
  connectedComponent: workOrdersDetail,
});

const [FeedbacksDetail, detailApi] = useVbenModal({
  connectedComponent: feedbacksDetail,
});



async function handleEdit(row: Required<FeedbacksForm>) {
  modalApi.setData({ id: row.id });
  modalApi.open();
}

async function handleInfo(row: Required<FeedbacksForm>) {
  detailApi.setData({ id: row.id });
  detailApi.open();
}

async function handleOrderInfo(row: Required<FeedbacksForm>) {
  orderDetailApi.setData({ id: row.orderId });
  orderDetailApi.open();
}




function handleDownloadExcel() {
  commonDownloadExcel(feedbacksExport, '意见反馈数据', tableApi.formApi.form.values, {
    fieldMappingTime: formOptions.fieldMappingTime,
  });
}
</script>

<template>
  <Page :auto-content-height="true">
    <BasicTable table-title="意见反馈列表">
      <template #toolbar-tools>
        <Space>
          <a-button
            v-access:code="['system:feedbacks:export']"
            @click="handleDownloadExcel"
          >
            {{ $t('pages.common.export') }}
          </a-button>
        </Space>
      </template>
      <template #action="{ row }">
        <Space>
          <ghost-button
            v-access:code="['system:feedbacks:info']"
            @click.stop="handleInfo(row)"
          >
            {{ $t('pages.common.info') }}
          </ghost-button>
          <ghost-button
            v-access:code="['system:feedbacks:edit']"
            @click.stop="handleEdit(row)"
          >
            {{ $t('pages.common.edit') }}
          </ghost-button>
          <ghost-button
            v-access:code="['system:feedbacks:info']"
            @click.stop="handleOrderInfo(row)"
            :disabled="row.isWorkOrder=='1'"
          >
            工单详情
          </ghost-button>
        </Space>
      </template>
    </BasicTable>
    <FeedbacksModal @reload="tableApi.query()" />
    <FeedbacksDetail></FeedbacksDetail>
    <WorkOrdersDetail></WorkOrdersDetail>
  </Page>
</template>
