<template>
  <div class="p-2">
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <template #tableTitle>
        <a-button type="primary" @click="handleAdd" preIcon="ant-design:plus-outlined" v-if="mainId != ''">新增</a-button>
        <a-button type="primary" preIcon="ant-design:export-outlined" @click="onExportXls" v-if="mainId != ''">导出</a-button>
        <j-upload-button type="primary" preIcon="ant-design:import-outlined" @click="onImportXls" v-if="mainId != ''">导入</j-upload-button>
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleDelete">
                <Icon icon="ant-design:delete-outlined"></Icon>
                删除
              </a-menu-item>
            </a-menu>
          </template>
          <a-button>批量操作<Icon icon="mdi:chevron-down"></Icon></a-button>
        </a-dropdown>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)" />
      </template>
      <template v-slot:bodyCell="{ column, record, index, text }"> </template>
    </BasicTable>
    <BgXiSpeakFeedbackModal ref="registerModal" @success="handleSuccess" />
  </div>
</template>

<script lang="ts" setup>
  import { ref, reactive, unref, inject, watch } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useListPage } from '/@/hooks/system/useListPage';
  import BgXiSpeakFeedbackModal from './components/BgXiSpeakFeedbackModal.vue';
  import { bgXiSpeakFeedbackColumns } from './BgXiSpeak.data';
  import { bgXiSpeakFeedbackList, bgXiSpeakFeedbackDelete, bgXiSpeakFeedbackDeleteBatch, bgXiSpeakFeedbackExportXlsUrl, bgXiSpeakFeedbackImportUrl } from './BgXiSpeak.api';
  import { isEmpty } from '/@/utils/is';
  import { useMessage } from '/@/hooks/web/useMessage';

  const mainId = inject('mainId') || '';
  const $message = useMessage();
  const queryParam = {};
  const { tableContext, onImportXls, onExportXls } = useListPage({
    tableProps: {
      api: bgXiSpeakFeedbackList,
      columns: bgXiSpeakFeedbackColumns,
      canResize: false,
      useSearchForm: false,
      actionColumn: {
        width: 180,
        fixed: 'right',
      },
      beforeFetch: (params) => {
        return Object.assign(params, queryParam);
      },
    },
    exportConfig: {
      name: '习总书记重要讲话反馈表',
      url: bgXiSpeakFeedbackExportXlsUrl,
      params: {
        mainId: mainId,
      },
    },
    importConfig: {
      url: () => {
        return bgXiSpeakFeedbackImportUrl + '/' + unref(mainId);
      },
    },
  });

  const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext;
  const registerModal = ref();
  const formRef = ref();
  const labelCol = reactive({
    xs: 24,
    sm: 4,
    xl: 6,
    xxl: 4,
  });
  const wrapperCol = reactive({
    xs: 24,
    sm: 20,
  });

  function handleAdd() {
    if (isEmpty(unref(mainId))) {
      $message.createMessage.warning('请选择一个主表信息');
      return;
    }
    registerModal.value.disableSubmit = false;
    registerModal.value.add();
  }

  async function handleEdit(record) {
    registerModal.value.disableSubmit = false;
    registerModal.value.edit(record);
  }

  function handleDetail(record) {
    registerModal.value.disableSubmit = true;
    registerModal.value.edit(record);
  }

  async function handleDelete(record) {
    await bgXiSpeakFeedbackDelete({ id: record.id }, handleSuccess);
  }

  async function batchHandleDelete() {
    await bgXiSpeakFeedbackDeleteBatch({ ids: selectedRowKeys.value }, handleSuccess);
  }

  function handleSuccess() {
    selectedRowKeys.value = [];
    reload();
  }

  function getTableAction(record) {
    return [
      {
        label: '编辑',
        onClick: handleEdit.bind(null, record),
      },
    ];
  }

  function getDropDownAction(record) {
    return [
      {
        label: '详情',
        onClick: handleDetail.bind(null, record),
      },
      {
        label: '删除',
        popConfirm: {
          title: '是否确认删除',
          confirm: handleDelete.bind(null, record),
        },
      },
    ];
  }

  function searchReset() {
    formRef.value.resetFields();
    selectedRowKeys.value = [];
    reload();
  }

  watch(mainId, () => {
    queryParam['mainId'] = unref(mainId);
    reload();
  });
</script>

<style lang="less" scoped>
  .jeecg-basic-table-form-container {
    padding: 0;
    .table-page-search-submitButtons {
      display: block;
      margin-bottom: 24px;
      white-space: nowrap;
    }
    .query-group-cust {
      min-width: 100px !important;
    }
    .query-group-split-cust {
      width: 30px;
      display: inline-block;
      text-align: center;
    }
  }
</style>
