<template>
  <a-button type="primary" @click="handleAdd"> 添加 </a-button>
  <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>
  <Popup @register="addRegister" @saveData="saveData" />
</template>
<script lang="ts">
  import { defineComponent, ref, unref } from 'vue';
  import Popup from './popup.vue';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useModal } from '/@/components/Modal';
  import { getFormConfig, columns } from './data';
  import { getDuesList, addDues } from '/@/api/sys/money';
  import moment from 'moment';
  import {
    BasicTable,
    useTable,
    TableAction,
    ActionItem,
    EditRecordRow,
    TableActionType,
  } from '/@/components/Table';

  export default defineComponent({
    name: 'Dues',
    components: { BasicTable, TableAction, Popup },
    emits: ['select'],
    setup(_, { emit }) {
      const { createMessage } = useMessage();
      const { success /*, error*/ } = createMessage;
      const tableRef = ref<Nullable<TableActionType>>(null);
      const [registerTable] = useTable({
        columns: columns,
        clickToRowSelect: false, // 点击行不勾选
        api: getDuesList,
        useSearchForm: true,
        showTableSetting: true,
        tableSetting: {
          redo: false,
          size: false,
        },
        beforeFetch: beforeFetch,
        afterFetch: afterFetch,
        formConfig: getFormConfig(),
        actionColumn: {
          width: 160,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: undefined,
        },
        maxHeight: 190,
        showIndexColumn: false,
        bordered: true,
      });
      const [addRegister, { 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);
      }

      async function saveData(params: any) {
        const data = params.data;
        const closeModel = params.closeModal;
        console.log(`data`, data);
        await addDues(data).then((res) => {
          console.log(res);
          getTableAction().reload();
          closeModel();
          success('创建成功!');
        });
      }

      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,
        addRegister,
        saveData,
      };
    },
  });
</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>
