<template>
  <BasicTable
    ref="tableRef"
    @register="registerTable"
    rowKey="id"
    :pagination="{
      pageSize: 10,
      defaultPageSize: 10,
      showSizeChanger: false,
    }"
  >
    <template #form-custom> custom-slot </template>
    <template #action="{ record }">
      <TableAction :actions="createActions(record)" stopButtonPropagation />
    </template>
  </BasicTable>
</template>
<script lang="ts">
  import { defineComponent, ref, unref } from 'vue';
  import { useModal } from '/@/components/Modal';
  import { columns } from './data';
  import { getAttachmentList } from '/@/api/sys/general';
  import moment from 'moment';
  import {
    BasicTable,
    useTable,
    TableAction,
    ActionItem,
    EditRecordRow,
    TableActionType,
  } from '/@/components/Table';

  export default defineComponent({
    name: 'Attachment',
    components: { BasicTable, TableAction },
    emits: ['select'],
    setup(_, { emit }) {
      const tableRef = ref<Nullable<TableActionType>>(null);
      const [registerTable] = useTable({
        columns: columns,
        clickToRowSelect: false, // 点击行不勾选
        api: getAttachmentList,
        useSearchForm: true,
        showTableSetting: true,
        tableSetting: {
          redo: false,
          size: false,
        },
        beforeFetch: beforeFetch,
        afterFetch: afterFetch,
        actionColumn: {
          width: 160,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: undefined,
        },
        maxHeight: 190,
        showIndexColumn: false,
        bordered: true,
      });
      const [{ openModal: openPopup }] = useModal();
      function getTableAction() {
        // 获取组件
        const tableAction = unref(tableRef);
        if (!tableAction) {
          throw new Error('tableAction is null');
        }
        return tableAction;
      }

      function beforeFetch(params) {
        for (let k in params) {
          if (k !== 'page' && k !== 'pageSize' && k !== 'field' && k !== 'order') {
            if (params[k] === '') {
              delete params[k];
            } else {
              if (!params.filter) {
                params.filter = {};
              }
              if (params.birthday) {
                params.birthday = moment(params.birthday).format('YYYY-MM-DD');
              } else {
                delete params.birthday;
              }
              params.filter[k] = params[k];
              delete params[k];
            }
          }
        }
        params.filter = JSON.stringify(params.filter);
        params.offset = params.page;
        params.limit = params.pageSize;
        delete params.page;
        delete params.pageSize;
      }
      function afterFetch(result) {
        console.log(`result`, result);
        // tableData.result = result;
      }

      function handleAdd() {
        // 开启弹窗
        openPopup(true, { family: [] });
      }
      function handleSelect(record: EditRecordRow) {
        emit('select', record);
      }

      function createActions(record: EditRecordRow): ActionItem[] {
        return [
          {
            label: '选择',
            icon: 'ant-design:edit-outlined',
            color: 'warning',
            onClick: handleSelect.bind(null, record),
          },
        ];
      }
      return {
        tableRef,
        registerTable,
        handleAdd,
        handleSelect,
        createActions,
        getTableAction,
      };
    },
  });
</script>
<style>
  .ant-calendar-picker {
    width: 100%;
  }

  /* @media (max-width: 639px) {
    .sys-container .vben-basic-table-header__toolbar > * {
      padding: 6px !important;
      margin-right: 3px;
      font-size: 12px !important;
    }

    .sys-container .vben-basic-table .ant-table-wrapper {
      padding: 3px;
    }
  } */
</style>
